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 }
1149 
1150 TEST_F(FormatTest, FormatsLabels) {
1151   verifyFormat("void f() {\n"
1152                "  some_code();\n"
1153                "test_label:\n"
1154                "  some_other_code();\n"
1155                "  {\n"
1156                "    some_more_code();\n"
1157                "  another_label:\n"
1158                "    some_more_code();\n"
1159                "  }\n"
1160                "}");
1161   verifyFormat("{\n"
1162                "  some_code();\n"
1163                "test_label:\n"
1164                "  some_other_code();\n"
1165                "}");
1166   verifyFormat("{\n"
1167                "  some_code();\n"
1168                "test_label:;\n"
1169                "  int i = 0;\n"
1170                "}");
1171 }
1172 
1173 //===----------------------------------------------------------------------===//
1174 // Tests for classes, namespaces, etc.
1175 //===----------------------------------------------------------------------===//
1176 
1177 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1178   verifyFormat("class A {};");
1179 }
1180 
1181 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1182   verifyFormat("class A {\n"
1183                "public:\n"
1184                "public: // comment\n"
1185                "protected:\n"
1186                "private:\n"
1187                "  void f() {}\n"
1188                "};");
1189   verifyGoogleFormat("class A {\n"
1190                      " public:\n"
1191                      " protected:\n"
1192                      " private:\n"
1193                      "  void f() {}\n"
1194                      "};");
1195   verifyFormat("class A {\n"
1196                "public slots:\n"
1197                "  void f1() {}\n"
1198                "public Q_SLOTS:\n"
1199                "  void f2() {}\n"
1200                "protected slots:\n"
1201                "  void f3() {}\n"
1202                "protected Q_SLOTS:\n"
1203                "  void f4() {}\n"
1204                "private slots:\n"
1205                "  void f5() {}\n"
1206                "private Q_SLOTS:\n"
1207                "  void f6() {}\n"
1208                "signals:\n"
1209                "  void g1();\n"
1210                "Q_SIGNALS:\n"
1211                "  void g2();\n"
1212                "};");
1213 
1214   // Don't interpret 'signals' the wrong way.
1215   verifyFormat("signals.set();");
1216   verifyFormat("for (Signals signals : f()) {\n}");
1217   verifyFormat("{\n"
1218                "  signals.set(); // This needs indentation.\n"
1219                "}");
1220   verifyFormat("void f() {\n"
1221                "label:\n"
1222                "  signals.baz();\n"
1223                "}");
1224 }
1225 
1226 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1227   EXPECT_EQ("class A {\n"
1228             "public:\n"
1229             "  void f();\n"
1230             "\n"
1231             "private:\n"
1232             "  void g() {}\n"
1233             "  // test\n"
1234             "protected:\n"
1235             "  int h;\n"
1236             "};",
1237             format("class A {\n"
1238                    "public:\n"
1239                    "void f();\n"
1240                    "private:\n"
1241                    "void g() {}\n"
1242                    "// test\n"
1243                    "protected:\n"
1244                    "int h;\n"
1245                    "};"));
1246   EXPECT_EQ("class A {\n"
1247             "protected:\n"
1248             "public:\n"
1249             "  void f();\n"
1250             "};",
1251             format("class A {\n"
1252                    "protected:\n"
1253                    "\n"
1254                    "public:\n"
1255                    "\n"
1256                    "  void f();\n"
1257                    "};"));
1258 
1259   // Even ensure proper spacing inside macros.
1260   EXPECT_EQ("#define B     \\\n"
1261             "  class A {   \\\n"
1262             "   protected: \\\n"
1263             "   public:    \\\n"
1264             "    void f(); \\\n"
1265             "  };",
1266             format("#define B     \\\n"
1267                    "  class A {   \\\n"
1268                    "   protected: \\\n"
1269                    "              \\\n"
1270                    "   public:    \\\n"
1271                    "              \\\n"
1272                    "    void f(); \\\n"
1273                    "  };",
1274                    getGoogleStyle()));
1275   // But don't remove empty lines after macros ending in access specifiers.
1276   EXPECT_EQ("#define A private:\n"
1277             "\n"
1278             "int i;",
1279             format("#define A         private:\n"
1280                    "\n"
1281                    "int              i;"));
1282 }
1283 
1284 TEST_F(FormatTest, FormatsClasses) {
1285   verifyFormat("class A : public B {};");
1286   verifyFormat("class A : public ::B {};");
1287 
1288   verifyFormat(
1289       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1290       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1291   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1292                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1293                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1294   verifyFormat(
1295       "class A : public B, public C, public D, public E, public F {};");
1296   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1297                "                     public C,\n"
1298                "                     public D,\n"
1299                "                     public E,\n"
1300                "                     public F,\n"
1301                "                     public G {};");
1302 
1303   verifyFormat("class\n"
1304                "    ReallyReallyLongClassName {\n"
1305                "  int i;\n"
1306                "};",
1307                getLLVMStyleWithColumns(32));
1308   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1309                "                           aaaaaaaaaaaaaaaa> {};");
1310   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1311                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1312                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1313   verifyFormat("template <class R, class C>\n"
1314                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1315                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1316   verifyFormat("class ::A::B {};");
1317 }
1318 
1319 TEST_F(FormatTest, BreakInheritanceStyle) {
1320   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1321   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1322           FormatStyle::BILS_BeforeComma;
1323   verifyFormat("class MyClass : public X {};",
1324                StyleWithInheritanceBreakBeforeComma);
1325   verifyFormat("class MyClass\n"
1326                "    : public X\n"
1327                "    , public Y {};",
1328                StyleWithInheritanceBreakBeforeComma);
1329   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1330                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1331                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1332                StyleWithInheritanceBreakBeforeComma);
1333   verifyFormat("struct aaaaaaaaaaaaa\n"
1334                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1335                "          aaaaaaaaaaaaaaaa> {};",
1336                StyleWithInheritanceBreakBeforeComma);
1337 
1338   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1339   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1340           FormatStyle::BILS_AfterColon;
1341   verifyFormat("class MyClass : public X {};",
1342                StyleWithInheritanceBreakAfterColon);
1343   verifyFormat("class MyClass : public X, public Y {};",
1344                StyleWithInheritanceBreakAfterColon);
1345   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1346                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1347                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1348                StyleWithInheritanceBreakAfterColon);
1349   verifyFormat("struct aaaaaaaaaaaaa :\n"
1350                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1351                "        aaaaaaaaaaaaaaaa> {};",
1352                StyleWithInheritanceBreakAfterColon);
1353 }
1354 
1355 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1356   verifyFormat("class A {\n} a, b;");
1357   verifyFormat("struct A {\n} a, b;");
1358   verifyFormat("union A {\n} a;");
1359 }
1360 
1361 TEST_F(FormatTest, FormatsEnum) {
1362   verifyFormat("enum {\n"
1363                "  Zero,\n"
1364                "  One = 1,\n"
1365                "  Two = One + 1,\n"
1366                "  Three = (One + Two),\n"
1367                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1368                "  Five = (One, Two, Three, Four, 5)\n"
1369                "};");
1370   verifyGoogleFormat("enum {\n"
1371                      "  Zero,\n"
1372                      "  One = 1,\n"
1373                      "  Two = One + 1,\n"
1374                      "  Three = (One + Two),\n"
1375                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1376                      "  Five = (One, Two, Three, Four, 5)\n"
1377                      "};");
1378   verifyFormat("enum Enum {};");
1379   verifyFormat("enum {};");
1380   verifyFormat("enum X E {} d;");
1381   verifyFormat("enum __attribute__((...)) E {} d;");
1382   verifyFormat("enum __declspec__((...)) E {} d;");
1383   verifyFormat("enum {\n"
1384                "  Bar = Foo<int, int>::value\n"
1385                "};",
1386                getLLVMStyleWithColumns(30));
1387 
1388   verifyFormat("enum ShortEnum { A, B, C };");
1389   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1390 
1391   EXPECT_EQ("enum KeepEmptyLines {\n"
1392             "  ONE,\n"
1393             "\n"
1394             "  TWO,\n"
1395             "\n"
1396             "  THREE\n"
1397             "}",
1398             format("enum KeepEmptyLines {\n"
1399                    "  ONE,\n"
1400                    "\n"
1401                    "  TWO,\n"
1402                    "\n"
1403                    "\n"
1404                    "  THREE\n"
1405                    "}"));
1406   verifyFormat("enum E { // comment\n"
1407                "  ONE,\n"
1408                "  TWO\n"
1409                "};\n"
1410                "int i;");
1411   // Not enums.
1412   verifyFormat("enum X f() {\n"
1413                "  a();\n"
1414                "  return 42;\n"
1415                "}");
1416   verifyFormat("enum X Type::f() {\n"
1417                "  a();\n"
1418                "  return 42;\n"
1419                "}");
1420   verifyFormat("enum ::X f() {\n"
1421                "  a();\n"
1422                "  return 42;\n"
1423                "}");
1424   verifyFormat("enum ns::X f() {\n"
1425                "  a();\n"
1426                "  return 42;\n"
1427                "}");
1428 }
1429 
1430 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1431   verifyFormat("enum Type {\n"
1432                "  One = 0; // These semicolons should be commas.\n"
1433                "  Two = 1;\n"
1434                "};");
1435   verifyFormat("namespace n {\n"
1436                "enum Type {\n"
1437                "  One,\n"
1438                "  Two, // missing };\n"
1439                "  int i;\n"
1440                "}\n"
1441                "void g() {}");
1442 }
1443 
1444 TEST_F(FormatTest, FormatsEnumStruct) {
1445   verifyFormat("enum struct {\n"
1446                "  Zero,\n"
1447                "  One = 1,\n"
1448                "  Two = One + 1,\n"
1449                "  Three = (One + Two),\n"
1450                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1451                "  Five = (One, Two, Three, Four, 5)\n"
1452                "};");
1453   verifyFormat("enum struct Enum {};");
1454   verifyFormat("enum struct {};");
1455   verifyFormat("enum struct X E {} d;");
1456   verifyFormat("enum struct __attribute__((...)) E {} d;");
1457   verifyFormat("enum struct __declspec__((...)) E {} d;");
1458   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1459 }
1460 
1461 TEST_F(FormatTest, FormatsEnumClass) {
1462   verifyFormat("enum class {\n"
1463                "  Zero,\n"
1464                "  One = 1,\n"
1465                "  Two = One + 1,\n"
1466                "  Three = (One + Two),\n"
1467                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1468                "  Five = (One, Two, Three, Four, 5)\n"
1469                "};");
1470   verifyFormat("enum class Enum {};");
1471   verifyFormat("enum class {};");
1472   verifyFormat("enum class X E {} d;");
1473   verifyFormat("enum class __attribute__((...)) E {} d;");
1474   verifyFormat("enum class __declspec__((...)) E {} d;");
1475   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1476 }
1477 
1478 TEST_F(FormatTest, FormatsEnumTypes) {
1479   verifyFormat("enum X : int {\n"
1480                "  A, // Force multiple lines.\n"
1481                "  B\n"
1482                "};");
1483   verifyFormat("enum X : int { A, B };");
1484   verifyFormat("enum X : std::uint32_t { A, B };");
1485 }
1486 
1487 TEST_F(FormatTest, FormatsTypedefEnum) {
1488   FormatStyle Style = getLLVMStyle();
1489   Style.ColumnLimit = 40;
1490   verifyFormat("typedef enum {} EmptyEnum;");
1491   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1492   verifyFormat("typedef enum {\n"
1493                "  ZERO = 0,\n"
1494                "  ONE = 1,\n"
1495                "  TWO = 2,\n"
1496                "  THREE = 3\n"
1497                "} LongEnum;",
1498                Style);
1499   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1500   Style.BraceWrapping.AfterEnum = true;
1501   verifyFormat("typedef enum {} EmptyEnum;");
1502   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1503   verifyFormat("typedef enum\n"
1504                "{\n"
1505                "  ZERO = 0,\n"
1506                "  ONE = 1,\n"
1507                "  TWO = 2,\n"
1508                "  THREE = 3\n"
1509                "} LongEnum;",
1510                Style);
1511 }
1512 
1513 TEST_F(FormatTest, FormatsNSEnums) {
1514   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1515   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1516                      "  // Information about someDecentlyLongValue.\n"
1517                      "  someDecentlyLongValue,\n"
1518                      "  // Information about anotherDecentlyLongValue.\n"
1519                      "  anotherDecentlyLongValue,\n"
1520                      "  // Information about aThirdDecentlyLongValue.\n"
1521                      "  aThirdDecentlyLongValue\n"
1522                      "};");
1523   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1524                      "  a = 1,\n"
1525                      "  b = 2,\n"
1526                      "  c = 3,\n"
1527                      "};");
1528   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1529                      "  a = 1,\n"
1530                      "  b = 2,\n"
1531                      "  c = 3,\n"
1532                      "};");
1533   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1534                      "  a = 1,\n"
1535                      "  b = 2,\n"
1536                      "  c = 3,\n"
1537                      "};");
1538 }
1539 
1540 TEST_F(FormatTest, FormatsBitfields) {
1541   verifyFormat("struct Bitfields {\n"
1542                "  unsigned sClass : 8;\n"
1543                "  unsigned ValueKind : 2;\n"
1544                "};");
1545   verifyFormat("struct A {\n"
1546                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1547                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1548                "};");
1549   verifyFormat("struct MyStruct {\n"
1550                "  uchar data;\n"
1551                "  uchar : 8;\n"
1552                "  uchar : 8;\n"
1553                "  uchar other;\n"
1554                "};");
1555 }
1556 
1557 TEST_F(FormatTest, FormatsNamespaces) {
1558   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1559   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1560 
1561   verifyFormat("namespace some_namespace {\n"
1562                "class A {};\n"
1563                "void f() { f(); }\n"
1564                "}",
1565                LLVMWithNoNamespaceFix);
1566   verifyFormat("namespace {\n"
1567                "class A {};\n"
1568                "void f() { f(); }\n"
1569                "}",
1570                LLVMWithNoNamespaceFix);
1571   verifyFormat("inline namespace X {\n"
1572                "class A {};\n"
1573                "void f() { f(); }\n"
1574                "}",
1575                LLVMWithNoNamespaceFix);
1576   verifyFormat("using namespace some_namespace;\n"
1577                "class A {};\n"
1578                "void f() { f(); }",
1579                LLVMWithNoNamespaceFix);
1580 
1581   // This code is more common than we thought; if we
1582   // layout this correctly the semicolon will go into
1583   // its own line, which is undesirable.
1584   verifyFormat("namespace {};",
1585                LLVMWithNoNamespaceFix);
1586   verifyFormat("namespace {\n"
1587                "class A {};\n"
1588                "};",
1589                LLVMWithNoNamespaceFix);
1590 
1591   verifyFormat("namespace {\n"
1592                "int SomeVariable = 0; // comment\n"
1593                "} // namespace",
1594                LLVMWithNoNamespaceFix);
1595   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1596             "#define HEADER_GUARD\n"
1597             "namespace my_namespace {\n"
1598             "int i;\n"
1599             "} // my_namespace\n"
1600             "#endif // HEADER_GUARD",
1601             format("#ifndef HEADER_GUARD\n"
1602                    " #define HEADER_GUARD\n"
1603                    "   namespace my_namespace {\n"
1604                    "int i;\n"
1605                    "}    // my_namespace\n"
1606                    "#endif    // HEADER_GUARD",
1607                    LLVMWithNoNamespaceFix));
1608 
1609   EXPECT_EQ("namespace A::B {\n"
1610             "class C {};\n"
1611             "}",
1612             format("namespace A::B {\n"
1613                    "class C {};\n"
1614                    "}",
1615                    LLVMWithNoNamespaceFix));
1616 
1617   FormatStyle Style = getLLVMStyle();
1618   Style.NamespaceIndentation = FormatStyle::NI_All;
1619   EXPECT_EQ("namespace out {\n"
1620             "  int i;\n"
1621             "  namespace in {\n"
1622             "    int i;\n"
1623             "  } // namespace in\n"
1624             "} // namespace out",
1625             format("namespace out {\n"
1626                    "int i;\n"
1627                    "namespace in {\n"
1628                    "int i;\n"
1629                    "} // namespace in\n"
1630                    "} // namespace out",
1631                    Style));
1632 
1633   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1634   EXPECT_EQ("namespace out {\n"
1635             "int i;\n"
1636             "namespace in {\n"
1637             "  int i;\n"
1638             "} // namespace in\n"
1639             "} // namespace out",
1640             format("namespace out {\n"
1641                    "int i;\n"
1642                    "namespace in {\n"
1643                    "int i;\n"
1644                    "} // namespace in\n"
1645                    "} // namespace out",
1646                    Style));
1647 }
1648 
1649 TEST_F(FormatTest, FormatsCompactNamespaces) {
1650   FormatStyle Style = getLLVMStyle();
1651   Style.CompactNamespaces = true;
1652 
1653   verifyFormat("namespace A { namespace B {\n"
1654 			   "}} // namespace A::B",
1655 			   Style);
1656 
1657   EXPECT_EQ("namespace out { namespace in {\n"
1658             "}} // namespace out::in",
1659             format("namespace out {\n"
1660                    "namespace in {\n"
1661                    "} // namespace in\n"
1662                    "} // namespace out",
1663                    Style));
1664 
1665   // Only namespaces which have both consecutive opening and end get compacted
1666   EXPECT_EQ("namespace out {\n"
1667             "namespace in1 {\n"
1668             "} // namespace in1\n"
1669             "namespace in2 {\n"
1670             "} // namespace in2\n"
1671             "} // namespace out",
1672             format("namespace out {\n"
1673                    "namespace in1 {\n"
1674                    "} // namespace in1\n"
1675                    "namespace in2 {\n"
1676                    "} // namespace in2\n"
1677                    "} // namespace out",
1678                    Style));
1679 
1680   EXPECT_EQ("namespace out {\n"
1681             "int i;\n"
1682             "namespace in {\n"
1683             "int j;\n"
1684             "} // namespace in\n"
1685             "int k;\n"
1686             "} // namespace out",
1687             format("namespace out { int i;\n"
1688                    "namespace in { int j; } // namespace in\n"
1689                    "int k; } // namespace out",
1690                    Style));
1691 
1692   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1693             "}}} // namespace A::B::C\n",
1694             format("namespace A { namespace B {\n"
1695                    "namespace C {\n"
1696                    "}} // namespace B::C\n"
1697                    "} // namespace A\n",
1698                    Style));
1699 
1700   Style.ColumnLimit = 40;
1701   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1702             "namespace bbbbbbbbbb {\n"
1703             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1704             format("namespace aaaaaaaaaa {\n"
1705                    "namespace bbbbbbbbbb {\n"
1706                    "} // namespace bbbbbbbbbb\n"
1707                    "} // namespace aaaaaaaaaa",
1708                    Style));
1709 
1710   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1711             "namespace cccccc {\n"
1712             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1713             format("namespace aaaaaa {\n"
1714                    "namespace bbbbbb {\n"
1715                    "namespace cccccc {\n"
1716                    "} // namespace cccccc\n"
1717                    "} // namespace bbbbbb\n"
1718                    "} // namespace aaaaaa",
1719                    Style));
1720   Style.ColumnLimit = 80;
1721 
1722   // Extra semicolon after 'inner' closing brace prevents merging
1723   EXPECT_EQ("namespace out { namespace in {\n"
1724             "}; } // namespace out::in",
1725             format("namespace out {\n"
1726                    "namespace in {\n"
1727                    "}; // namespace in\n"
1728                    "} // namespace out",
1729                    Style));
1730 
1731   // Extra semicolon after 'outer' closing brace is conserved
1732   EXPECT_EQ("namespace out { namespace in {\n"
1733             "}}; // namespace out::in",
1734             format("namespace out {\n"
1735                    "namespace in {\n"
1736                    "} // namespace in\n"
1737                    "}; // namespace out",
1738                    Style));
1739 
1740   Style.NamespaceIndentation = FormatStyle::NI_All;
1741   EXPECT_EQ("namespace out { namespace in {\n"
1742             "  int i;\n"
1743             "}} // namespace out::in",
1744             format("namespace out {\n"
1745                    "namespace in {\n"
1746                    "int i;\n"
1747                    "} // namespace in\n"
1748                    "} // namespace out",
1749                    Style));
1750   EXPECT_EQ("namespace out { namespace mid {\n"
1751             "  namespace in {\n"
1752             "    int j;\n"
1753             "  } // namespace in\n"
1754             "  int k;\n"
1755             "}} // namespace out::mid",
1756             format("namespace out { namespace mid {\n"
1757                    "namespace in { int j; } // namespace in\n"
1758                    "int k; }} // namespace out::mid",
1759                    Style));
1760 
1761   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1762   EXPECT_EQ("namespace out { namespace in {\n"
1763             "  int i;\n"
1764             "}} // namespace out::in",
1765             format("namespace out {\n"
1766                    "namespace in {\n"
1767                    "int i;\n"
1768                    "} // namespace in\n"
1769                    "} // namespace out",
1770                    Style));
1771   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1772             "  int i;\n"
1773             "}}} // namespace out::mid::in",
1774             format("namespace out {\n"
1775                    "namespace mid {\n"
1776                    "namespace in {\n"
1777                    "int i;\n"
1778                    "} // namespace in\n"
1779                    "} // namespace mid\n"
1780                    "} // namespace out",
1781                    Style));
1782 }
1783 
1784 TEST_F(FormatTest, FormatsExternC) {
1785   verifyFormat("extern \"C\" {\nint a;");
1786   verifyFormat("extern \"C\" {}");
1787   verifyFormat("extern \"C\" {\n"
1788                "int foo();\n"
1789                "}");
1790   verifyFormat("extern \"C\" int foo() {}");
1791   verifyFormat("extern \"C\" int foo();");
1792   verifyFormat("extern \"C\" int foo() {\n"
1793                "  int i = 42;\n"
1794                "  return i;\n"
1795                "}");
1796 
1797   FormatStyle Style = getLLVMStyle();
1798   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1799   Style.BraceWrapping.AfterFunction = true;
1800   verifyFormat("extern \"C\" int foo() {}", Style);
1801   verifyFormat("extern \"C\" int foo();", Style);
1802   verifyFormat("extern \"C\" int foo()\n"
1803                "{\n"
1804                "  int i = 42;\n"
1805                "  return i;\n"
1806                "}",
1807                Style);
1808 
1809   Style.BraceWrapping.AfterExternBlock = true;
1810   Style.BraceWrapping.SplitEmptyRecord = false;
1811   verifyFormat("extern \"C\"\n"
1812                "{}",
1813                Style);
1814   verifyFormat("extern \"C\"\n"
1815                "{\n"
1816                "  int foo();\n"
1817                "}",
1818                Style);
1819 }
1820 
1821 TEST_F(FormatTest, FormatsInlineASM) {
1822   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1823   verifyFormat("asm(\"nop\" ::: \"memory\");");
1824   verifyFormat(
1825       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1826       "    \"cpuid\\n\\t\"\n"
1827       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1828       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1829       "    : \"a\"(value));");
1830   EXPECT_EQ(
1831       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1832       "  __asm {\n"
1833       "        mov     edx,[that] // vtable in edx\n"
1834       "        mov     eax,methodIndex\n"
1835       "        call    [edx][eax*4] // stdcall\n"
1836       "  }\n"
1837       "}",
1838       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1839              "    __asm {\n"
1840              "        mov     edx,[that] // vtable in edx\n"
1841              "        mov     eax,methodIndex\n"
1842              "        call    [edx][eax*4] // stdcall\n"
1843              "    }\n"
1844              "}"));
1845   EXPECT_EQ("_asm {\n"
1846             "  xor eax, eax;\n"
1847             "  cpuid;\n"
1848             "}",
1849             format("_asm {\n"
1850                    "  xor eax, eax;\n"
1851                    "  cpuid;\n"
1852                    "}"));
1853   verifyFormat("void function() {\n"
1854                "  // comment\n"
1855                "  asm(\"\");\n"
1856                "}");
1857   EXPECT_EQ("__asm {\n"
1858             "}\n"
1859             "int i;",
1860             format("__asm   {\n"
1861                    "}\n"
1862                    "int   i;"));
1863 }
1864 
1865 TEST_F(FormatTest, FormatTryCatch) {
1866   verifyFormat("try {\n"
1867                "  throw a * b;\n"
1868                "} catch (int a) {\n"
1869                "  // Do nothing.\n"
1870                "} catch (...) {\n"
1871                "  exit(42);\n"
1872                "}");
1873 
1874   // Function-level try statements.
1875   verifyFormat("int f() try { return 4; } catch (...) {\n"
1876                "  return 5;\n"
1877                "}");
1878   verifyFormat("class A {\n"
1879                "  int a;\n"
1880                "  A() try : a(0) {\n"
1881                "  } catch (...) {\n"
1882                "    throw;\n"
1883                "  }\n"
1884                "};\n");
1885 
1886   // Incomplete try-catch blocks.
1887   verifyIncompleteFormat("try {} catch (");
1888 }
1889 
1890 TEST_F(FormatTest, FormatSEHTryCatch) {
1891   verifyFormat("__try {\n"
1892                "  int a = b * c;\n"
1893                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1894                "  // Do nothing.\n"
1895                "}");
1896 
1897   verifyFormat("__try {\n"
1898                "  int a = b * c;\n"
1899                "} __finally {\n"
1900                "  // Do nothing.\n"
1901                "}");
1902 
1903   verifyFormat("DEBUG({\n"
1904                "  __try {\n"
1905                "  } __finally {\n"
1906                "  }\n"
1907                "});\n");
1908 }
1909 
1910 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1911   verifyFormat("try {\n"
1912                "  f();\n"
1913                "} catch {\n"
1914                "  g();\n"
1915                "}");
1916   verifyFormat("try {\n"
1917                "  f();\n"
1918                "} catch (A a) MACRO(x) {\n"
1919                "  g();\n"
1920                "} catch (B b) MACRO(x) {\n"
1921                "  g();\n"
1922                "}");
1923 }
1924 
1925 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1926   FormatStyle Style = getLLVMStyle();
1927   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1928                           FormatStyle::BS_WebKit}) {
1929     Style.BreakBeforeBraces = BraceStyle;
1930     verifyFormat("try {\n"
1931                  "  // something\n"
1932                  "} catch (...) {\n"
1933                  "  // something\n"
1934                  "}",
1935                  Style);
1936   }
1937   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1938   verifyFormat("try {\n"
1939                "  // something\n"
1940                "}\n"
1941                "catch (...) {\n"
1942                "  // something\n"
1943                "}",
1944                Style);
1945   verifyFormat("__try {\n"
1946                "  // something\n"
1947                "}\n"
1948                "__finally {\n"
1949                "  // something\n"
1950                "}",
1951                Style);
1952   verifyFormat("@try {\n"
1953                "  // something\n"
1954                "}\n"
1955                "@finally {\n"
1956                "  // something\n"
1957                "}",
1958                Style);
1959   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1960   verifyFormat("try\n"
1961                "{\n"
1962                "  // something\n"
1963                "}\n"
1964                "catch (...)\n"
1965                "{\n"
1966                "  // something\n"
1967                "}",
1968                Style);
1969   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1970   verifyFormat("try\n"
1971                "  {\n"
1972                "    // something\n"
1973                "  }\n"
1974                "catch (...)\n"
1975                "  {\n"
1976                "    // something\n"
1977                "  }",
1978                Style);
1979   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1980   Style.BraceWrapping.BeforeCatch = true;
1981   verifyFormat("try {\n"
1982                "  // something\n"
1983                "}\n"
1984                "catch (...) {\n"
1985                "  // something\n"
1986                "}",
1987                Style);
1988 }
1989 
1990 TEST_F(FormatTest, StaticInitializers) {
1991   verifyFormat("static SomeClass SC = {1, 'a'};");
1992 
1993   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1994                "    100000000, "
1995                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1996 
1997   // Here, everything other than the "}" would fit on a line.
1998   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1999                "    10000000000000000000000000};");
2000   EXPECT_EQ("S s = {a,\n"
2001             "\n"
2002             "       b};",
2003             format("S s = {\n"
2004                    "  a,\n"
2005                    "\n"
2006                    "  b\n"
2007                    "};"));
2008 
2009   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2010   // line. However, the formatting looks a bit off and this probably doesn't
2011   // happen often in practice.
2012   verifyFormat("static int Variable[1] = {\n"
2013                "    {1000000000000000000000000000000000000}};",
2014                getLLVMStyleWithColumns(40));
2015 }
2016 
2017 TEST_F(FormatTest, DesignatedInitializers) {
2018   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2019   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2020                "                    .bbbbbbbbbb = 2,\n"
2021                "                    .cccccccccc = 3,\n"
2022                "                    .dddddddddd = 4,\n"
2023                "                    .eeeeeeeeee = 5};");
2024   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2025                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2026                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2027                "    .ccccccccccccccccccccccccccc = 3,\n"
2028                "    .ddddddddddddddddddddddddddd = 4,\n"
2029                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2030 
2031   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2032 
2033   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2034   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2035                "                    [2] = bbbbbbbbbb,\n"
2036                "                    [3] = cccccccccc,\n"
2037                "                    [4] = dddddddddd,\n"
2038                "                    [5] = eeeeeeeeee};");
2039   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2040                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2041                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2042                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2043                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2044                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2045 }
2046 
2047 TEST_F(FormatTest, NestedStaticInitializers) {
2048   verifyFormat("static A x = {{{}}};\n");
2049   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2050                "               {init1, init2, init3, init4}}};",
2051                getLLVMStyleWithColumns(50));
2052 
2053   verifyFormat("somes Status::global_reps[3] = {\n"
2054                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2055                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2056                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2057                getLLVMStyleWithColumns(60));
2058   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2059                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2060                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2061                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2062   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2063                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2064                "rect.fTop}};");
2065 
2066   verifyFormat(
2067       "SomeArrayOfSomeType a = {\n"
2068       "    {{1, 2, 3},\n"
2069       "     {1, 2, 3},\n"
2070       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2071       "      333333333333333333333333333333},\n"
2072       "     {1, 2, 3},\n"
2073       "     {1, 2, 3}}};");
2074   verifyFormat(
2075       "SomeArrayOfSomeType a = {\n"
2076       "    {{1, 2, 3}},\n"
2077       "    {{1, 2, 3}},\n"
2078       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2079       "      333333333333333333333333333333}},\n"
2080       "    {{1, 2, 3}},\n"
2081       "    {{1, 2, 3}}};");
2082 
2083   verifyFormat("struct {\n"
2084                "  unsigned bit;\n"
2085                "  const char *const name;\n"
2086                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2087                "                 {kOsWin, \"Windows\"},\n"
2088                "                 {kOsLinux, \"Linux\"},\n"
2089                "                 {kOsCrOS, \"Chrome OS\"}};");
2090   verifyFormat("struct {\n"
2091                "  unsigned bit;\n"
2092                "  const char *const name;\n"
2093                "} kBitsToOs[] = {\n"
2094                "    {kOsMac, \"Mac\"},\n"
2095                "    {kOsWin, \"Windows\"},\n"
2096                "    {kOsLinux, \"Linux\"},\n"
2097                "    {kOsCrOS, \"Chrome OS\"},\n"
2098                "};");
2099 }
2100 
2101 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2102   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2103                "                      \\\n"
2104                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2105 }
2106 
2107 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2108   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2109                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2110 
2111   // Do break defaulted and deleted functions.
2112   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2113                "    default;",
2114                getLLVMStyleWithColumns(40));
2115   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2116                "    delete;",
2117                getLLVMStyleWithColumns(40));
2118 }
2119 
2120 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2121   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2122                getLLVMStyleWithColumns(40));
2123   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2124                getLLVMStyleWithColumns(40));
2125   EXPECT_EQ("#define Q                              \\\n"
2126             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2127             "  \"aaaaaaaa.cpp\"",
2128             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2129                    getLLVMStyleWithColumns(40)));
2130 }
2131 
2132 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2133   EXPECT_EQ("# 123 \"A string literal\"",
2134             format("   #     123    \"A string literal\""));
2135 }
2136 
2137 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2138   EXPECT_EQ("#;", format("#;"));
2139   verifyFormat("#\n;\n;\n;");
2140 }
2141 
2142 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2143   EXPECT_EQ("#line 42 \"test\"\n",
2144             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2145   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2146                                     getLLVMStyleWithColumns(12)));
2147 }
2148 
2149 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2150   EXPECT_EQ("#line 42 \"test\"",
2151             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2152   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2153 }
2154 
2155 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2156   verifyFormat("#define A \\x20");
2157   verifyFormat("#define A \\ x20");
2158   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2159   verifyFormat("#define A ''");
2160   verifyFormat("#define A ''qqq");
2161   verifyFormat("#define A `qqq");
2162   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2163   EXPECT_EQ("const char *c = STRINGIFY(\n"
2164             "\\na : b);",
2165             format("const char * c = STRINGIFY(\n"
2166                    "\\na : b);"));
2167 
2168   verifyFormat("a\r\\");
2169   verifyFormat("a\v\\");
2170   verifyFormat("a\f\\");
2171 }
2172 
2173 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2174   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2175   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2176   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2177   // FIXME: We never break before the macro name.
2178   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2179 
2180   verifyFormat("#define A A\n#define A A");
2181   verifyFormat("#define A(X) A\n#define A A");
2182 
2183   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2184   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2185 }
2186 
2187 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2188   EXPECT_EQ("// somecomment\n"
2189             "#include \"a.h\"\n"
2190             "#define A(  \\\n"
2191             "    A, B)\n"
2192             "#include \"b.h\"\n"
2193             "// somecomment\n",
2194             format("  // somecomment\n"
2195                    "  #include \"a.h\"\n"
2196                    "#define A(A,\\\n"
2197                    "    B)\n"
2198                    "    #include \"b.h\"\n"
2199                    " // somecomment\n",
2200                    getLLVMStyleWithColumns(13)));
2201 }
2202 
2203 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2204 
2205 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2206   EXPECT_EQ("#define A    \\\n"
2207             "  c;         \\\n"
2208             "  e;\n"
2209             "f;",
2210             format("#define A c; e;\n"
2211                    "f;",
2212                    getLLVMStyleWithColumns(14)));
2213 }
2214 
2215 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2216 
2217 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2218   EXPECT_EQ("int x,\n"
2219             "#define A\n"
2220             "    y;",
2221             format("int x,\n#define A\ny;"));
2222 }
2223 
2224 TEST_F(FormatTest, HashInMacroDefinition) {
2225   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2226   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2227   verifyFormat("#define A  \\\n"
2228                "  {        \\\n"
2229                "    f(#c); \\\n"
2230                "  }",
2231                getLLVMStyleWithColumns(11));
2232 
2233   verifyFormat("#define A(X)         \\\n"
2234                "  void function##X()",
2235                getLLVMStyleWithColumns(22));
2236 
2237   verifyFormat("#define A(a, b, c)   \\\n"
2238                "  void a##b##c()",
2239                getLLVMStyleWithColumns(22));
2240 
2241   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2242 }
2243 
2244 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2245   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2246   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2247 }
2248 
2249 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2250   EXPECT_EQ("#define A b;", format("#define A \\\n"
2251                                    "          \\\n"
2252                                    "  b;",
2253                                    getLLVMStyleWithColumns(25)));
2254   EXPECT_EQ("#define A \\\n"
2255             "          \\\n"
2256             "  a;      \\\n"
2257             "  b;",
2258             format("#define A \\\n"
2259                    "          \\\n"
2260                    "  a;      \\\n"
2261                    "  b;",
2262                    getLLVMStyleWithColumns(11)));
2263   EXPECT_EQ("#define A \\\n"
2264             "  a;      \\\n"
2265             "          \\\n"
2266             "  b;",
2267             format("#define A \\\n"
2268                    "  a;      \\\n"
2269                    "          \\\n"
2270                    "  b;",
2271                    getLLVMStyleWithColumns(11)));
2272 }
2273 
2274 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2275   verifyIncompleteFormat("#define A :");
2276   verifyFormat("#define SOMECASES  \\\n"
2277                "  case 1:          \\\n"
2278                "  case 2\n",
2279                getLLVMStyleWithColumns(20));
2280   verifyFormat("#define MACRO(a) \\\n"
2281                "  if (a)         \\\n"
2282                "    f();         \\\n"
2283                "  else           \\\n"
2284                "    g()",
2285                getLLVMStyleWithColumns(18));
2286   verifyFormat("#define A template <typename T>");
2287   verifyIncompleteFormat("#define STR(x) #x\n"
2288                          "f(STR(this_is_a_string_literal{));");
2289   verifyFormat("#pragma omp threadprivate( \\\n"
2290                "    y)), // expected-warning",
2291                getLLVMStyleWithColumns(28));
2292   verifyFormat("#d, = };");
2293   verifyFormat("#if \"a");
2294   verifyIncompleteFormat("({\n"
2295                          "#define b     \\\n"
2296                          "  }           \\\n"
2297                          "  a\n"
2298                          "a",
2299                          getLLVMStyleWithColumns(15));
2300   verifyFormat("#define A     \\\n"
2301                "  {           \\\n"
2302                "    {\n"
2303                "#define B     \\\n"
2304                "  }           \\\n"
2305                "  }",
2306                getLLVMStyleWithColumns(15));
2307   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2308   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2309   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2310   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2311 }
2312 
2313 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2314   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2315   EXPECT_EQ("class A : public QObject {\n"
2316             "  Q_OBJECT\n"
2317             "\n"
2318             "  A() {}\n"
2319             "};",
2320             format("class A  :  public QObject {\n"
2321                    "     Q_OBJECT\n"
2322                    "\n"
2323                    "  A() {\n}\n"
2324                    "}  ;"));
2325   EXPECT_EQ("MACRO\n"
2326             "/*static*/ int i;",
2327             format("MACRO\n"
2328                    " /*static*/ int   i;"));
2329   EXPECT_EQ("SOME_MACRO\n"
2330             "namespace {\n"
2331             "void f();\n"
2332             "} // namespace",
2333             format("SOME_MACRO\n"
2334                    "  namespace    {\n"
2335                    "void   f(  );\n"
2336                    "} // namespace"));
2337   // Only if the identifier contains at least 5 characters.
2338   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2339   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2340   // Only if everything is upper case.
2341   EXPECT_EQ("class A : public QObject {\n"
2342             "  Q_Object A() {}\n"
2343             "};",
2344             format("class A  :  public QObject {\n"
2345                    "     Q_Object\n"
2346                    "  A() {\n}\n"
2347                    "}  ;"));
2348 
2349   // Only if the next line can actually start an unwrapped line.
2350   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2351             format("SOME_WEIRD_LOG_MACRO\n"
2352                    "<< SomeThing;"));
2353 
2354   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2355                "(n, buffers))\n",
2356                getChromiumStyle(FormatStyle::LK_Cpp));
2357 }
2358 
2359 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2360   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2361             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2362             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2363             "class X {};\n"
2364             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2365             "int *createScopDetectionPass() { return 0; }",
2366             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2367                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2368                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2369                    "  class X {};\n"
2370                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2371                    "  int *createScopDetectionPass() { return 0; }"));
2372   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2373   // braces, so that inner block is indented one level more.
2374   EXPECT_EQ("int q() {\n"
2375             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2376             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2377             "  IPC_END_MESSAGE_MAP()\n"
2378             "}",
2379             format("int q() {\n"
2380                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2381                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2382                    "  IPC_END_MESSAGE_MAP()\n"
2383                    "}"));
2384 
2385   // Same inside macros.
2386   EXPECT_EQ("#define LIST(L) \\\n"
2387             "  L(A)          \\\n"
2388             "  L(B)          \\\n"
2389             "  L(C)",
2390             format("#define LIST(L) \\\n"
2391                    "  L(A) \\\n"
2392                    "  L(B) \\\n"
2393                    "  L(C)",
2394                    getGoogleStyle()));
2395 
2396   // These must not be recognized as macros.
2397   EXPECT_EQ("int q() {\n"
2398             "  f(x);\n"
2399             "  f(x) {}\n"
2400             "  f(x)->g();\n"
2401             "  f(x)->*g();\n"
2402             "  f(x).g();\n"
2403             "  f(x) = x;\n"
2404             "  f(x) += x;\n"
2405             "  f(x) -= x;\n"
2406             "  f(x) *= x;\n"
2407             "  f(x) /= x;\n"
2408             "  f(x) %= x;\n"
2409             "  f(x) &= x;\n"
2410             "  f(x) |= x;\n"
2411             "  f(x) ^= x;\n"
2412             "  f(x) >>= x;\n"
2413             "  f(x) <<= x;\n"
2414             "  f(x)[y].z();\n"
2415             "  LOG(INFO) << x;\n"
2416             "  ifstream(x) >> x;\n"
2417             "}\n",
2418             format("int q() {\n"
2419                    "  f(x)\n;\n"
2420                    "  f(x)\n {}\n"
2421                    "  f(x)\n->g();\n"
2422                    "  f(x)\n->*g();\n"
2423                    "  f(x)\n.g();\n"
2424                    "  f(x)\n = x;\n"
2425                    "  f(x)\n += x;\n"
2426                    "  f(x)\n -= x;\n"
2427                    "  f(x)\n *= x;\n"
2428                    "  f(x)\n /= x;\n"
2429                    "  f(x)\n %= x;\n"
2430                    "  f(x)\n &= x;\n"
2431                    "  f(x)\n |= x;\n"
2432                    "  f(x)\n ^= x;\n"
2433                    "  f(x)\n >>= x;\n"
2434                    "  f(x)\n <<= x;\n"
2435                    "  f(x)\n[y].z();\n"
2436                    "  LOG(INFO)\n << x;\n"
2437                    "  ifstream(x)\n >> x;\n"
2438                    "}\n"));
2439   EXPECT_EQ("int q() {\n"
2440             "  F(x)\n"
2441             "  if (1) {\n"
2442             "  }\n"
2443             "  F(x)\n"
2444             "  while (1) {\n"
2445             "  }\n"
2446             "  F(x)\n"
2447             "  G(x);\n"
2448             "  F(x)\n"
2449             "  try {\n"
2450             "    Q();\n"
2451             "  } catch (...) {\n"
2452             "  }\n"
2453             "}\n",
2454             format("int q() {\n"
2455                    "F(x)\n"
2456                    "if (1) {}\n"
2457                    "F(x)\n"
2458                    "while (1) {}\n"
2459                    "F(x)\n"
2460                    "G(x);\n"
2461                    "F(x)\n"
2462                    "try { Q(); } catch (...) {}\n"
2463                    "}\n"));
2464   EXPECT_EQ("class A {\n"
2465             "  A() : t(0) {}\n"
2466             "  A(int i) noexcept() : {}\n"
2467             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2468             "  try : t(0) {\n"
2469             "  } catch (...) {\n"
2470             "  }\n"
2471             "};",
2472             format("class A {\n"
2473                    "  A()\n : t(0) {}\n"
2474                    "  A(int i)\n noexcept() : {}\n"
2475                    "  A(X x)\n"
2476                    "  try : t(0) {} catch (...) {}\n"
2477                    "};"));
2478   EXPECT_EQ("class SomeClass {\n"
2479             "public:\n"
2480             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2481             "};",
2482             format("class SomeClass {\n"
2483                    "public:\n"
2484                    "  SomeClass()\n"
2485                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2486                    "};"));
2487   EXPECT_EQ("class SomeClass {\n"
2488             "public:\n"
2489             "  SomeClass()\n"
2490             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2491             "};",
2492             format("class SomeClass {\n"
2493                    "public:\n"
2494                    "  SomeClass()\n"
2495                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2496                    "};",
2497                    getLLVMStyleWithColumns(40)));
2498 
2499   verifyFormat("MACRO(>)");
2500 }
2501 
2502 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2503   verifyFormat("#define A \\\n"
2504                "  f({     \\\n"
2505                "    g();  \\\n"
2506                "  });",
2507                getLLVMStyleWithColumns(11));
2508 }
2509 
2510 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2511   FormatStyle Style = getLLVMStyle();
2512   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2513   Style.ColumnLimit = 40;
2514   verifyFormat("#ifdef _WIN32\n"
2515                "#define A 0\n"
2516                "#ifdef VAR2\n"
2517                "#define B 1\n"
2518                "#include <someheader.h>\n"
2519                "#define MACRO                          \\\n"
2520                "  some_very_long_func_aaaaaaaaaa();\n"
2521                "#endif\n"
2522                "#else\n"
2523                "#define A 1\n"
2524                "#endif",
2525                Style);
2526   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2527   verifyFormat("#ifdef _WIN32\n"
2528                "#  define A 0\n"
2529                "#  ifdef VAR2\n"
2530                "#    define B 1\n"
2531                "#    include <someheader.h>\n"
2532                "#    define MACRO                      \\\n"
2533                "      some_very_long_func_aaaaaaaaaa();\n"
2534                "#  endif\n"
2535                "#else\n"
2536                "#  define A 1\n"
2537                "#endif",
2538                Style);
2539   verifyFormat("#if A\n"
2540                "#  define MACRO                        \\\n"
2541                "    void a(int x) {                    \\\n"
2542                "      b();                             \\\n"
2543                "      c();                             \\\n"
2544                "      d();                             \\\n"
2545                "      e();                             \\\n"
2546                "      f();                             \\\n"
2547                "    }\n"
2548                "#endif",
2549                Style);
2550   // Comments before include guard.
2551   verifyFormat("// file comment\n"
2552                "// file comment\n"
2553                "#ifndef HEADER_H\n"
2554                "#define HEADER_H\n"
2555                "code();\n"
2556                "#endif",
2557                Style);
2558   // Test with include guards.
2559   verifyFormat("#ifndef HEADER_H\n"
2560                "#define HEADER_H\n"
2561                "code();\n"
2562                "#endif",
2563                Style);
2564   // Include guards must have a #define with the same variable immediately
2565   // after #ifndef.
2566   verifyFormat("#ifndef NOT_GUARD\n"
2567                "#  define FOO\n"
2568                "code();\n"
2569                "#endif",
2570                Style);
2571 
2572   // Include guards must cover the entire file.
2573   verifyFormat("code();\n"
2574                "code();\n"
2575                "#ifndef NOT_GUARD\n"
2576                "#  define NOT_GUARD\n"
2577                "code();\n"
2578                "#endif",
2579                Style);
2580   verifyFormat("#ifndef NOT_GUARD\n"
2581                "#  define NOT_GUARD\n"
2582                "code();\n"
2583                "#endif\n"
2584                "code();",
2585                Style);
2586   // Test with trailing blank lines.
2587   verifyFormat("#ifndef HEADER_H\n"
2588                "#define HEADER_H\n"
2589                "code();\n"
2590                "#endif\n",
2591                Style);
2592   // Include guards don't have #else.
2593   verifyFormat("#ifndef NOT_GUARD\n"
2594                "#  define NOT_GUARD\n"
2595                "code();\n"
2596                "#else\n"
2597                "#endif",
2598                Style);
2599   verifyFormat("#ifndef NOT_GUARD\n"
2600                "#  define NOT_GUARD\n"
2601                "code();\n"
2602                "#elif FOO\n"
2603                "#endif",
2604                Style);
2605   // Non-identifier #define after potential include guard.
2606   verifyFormat("#ifndef FOO\n"
2607                "#  define 1\n"
2608                "#endif\n",
2609                Style);
2610   // #if closes past last non-preprocessor line.
2611   verifyFormat("#ifndef FOO\n"
2612                "#define FOO\n"
2613                "#if 1\n"
2614                "int i;\n"
2615                "#  define A 0\n"
2616                "#endif\n"
2617                "#endif\n",
2618                Style);
2619   // FIXME: This doesn't handle the case where there's code between the
2620   // #ifndef and #define but all other conditions hold. This is because when
2621   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2622   // previous code line yet, so we can't detect it.
2623   EXPECT_EQ("#ifndef NOT_GUARD\n"
2624             "code();\n"
2625             "#define NOT_GUARD\n"
2626             "code();\n"
2627             "#endif",
2628             format("#ifndef NOT_GUARD\n"
2629                    "code();\n"
2630                    "#  define NOT_GUARD\n"
2631                    "code();\n"
2632                    "#endif",
2633                    Style));
2634   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2635   // be outside an include guard. Examples are #pragma once and
2636   // #pragma GCC diagnostic, or anything else that does not change the meaning
2637   // of the file if it's included multiple times.
2638   EXPECT_EQ("#ifdef WIN32\n"
2639             "#  pragma once\n"
2640             "#endif\n"
2641             "#ifndef HEADER_H\n"
2642             "#  define HEADER_H\n"
2643             "code();\n"
2644             "#endif",
2645             format("#ifdef WIN32\n"
2646                    "#  pragma once\n"
2647                    "#endif\n"
2648                    "#ifndef HEADER_H\n"
2649                    "#define HEADER_H\n"
2650                    "code();\n"
2651                    "#endif",
2652                    Style));
2653   // FIXME: This does not detect when there is a single non-preprocessor line
2654   // in front of an include-guard-like structure where other conditions hold
2655   // because ScopedLineState hides the line.
2656   EXPECT_EQ("code();\n"
2657             "#ifndef HEADER_H\n"
2658             "#define HEADER_H\n"
2659             "code();\n"
2660             "#endif",
2661             format("code();\n"
2662                    "#ifndef HEADER_H\n"
2663                    "#  define HEADER_H\n"
2664                    "code();\n"
2665                    "#endif",
2666                    Style));
2667   // Keep comments aligned with #, otherwise indent comments normally. These
2668   // tests cannot use verifyFormat because messUp manipulates leading
2669   // whitespace.
2670   {
2671     const char *Expected = ""
2672                            "void f() {\n"
2673                            "#if 1\n"
2674                            "// Preprocessor aligned.\n"
2675                            "#  define A 0\n"
2676                            "  // Code. Separated by blank line.\n"
2677                            "\n"
2678                            "#  define B 0\n"
2679                            "  // Code. Not aligned with #\n"
2680                            "#  define C 0\n"
2681                            "#endif";
2682     const char *ToFormat = ""
2683                            "void f() {\n"
2684                            "#if 1\n"
2685                            "// Preprocessor aligned.\n"
2686                            "#  define A 0\n"
2687                            "// Code. Separated by blank line.\n"
2688                            "\n"
2689                            "#  define B 0\n"
2690                            "   // Code. Not aligned with #\n"
2691                            "#  define C 0\n"
2692                            "#endif";
2693     EXPECT_EQ(Expected, format(ToFormat, Style));
2694     EXPECT_EQ(Expected, format(Expected, Style));
2695   }
2696   // Keep block quotes aligned.
2697   {
2698     const char *Expected = ""
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     const char *ToFormat = ""
2710                            "void f() {\n"
2711                            "#if 1\n"
2712                            "/* Preprocessor aligned. */\n"
2713                            "#  define A 0\n"
2714                            "/* Code. Separated by blank line. */\n"
2715                            "\n"
2716                            "#  define B 0\n"
2717                            "   /* Code. Not aligned with # */\n"
2718                            "#  define C 0\n"
2719                            "#endif";
2720     EXPECT_EQ(Expected, format(ToFormat, Style));
2721     EXPECT_EQ(Expected, format(Expected, Style));
2722   }
2723   // Keep comments aligned with un-indented directives.
2724   {
2725     const char *Expected = ""
2726                            "void f() {\n"
2727                            "// Preprocessor aligned.\n"
2728                            "#define A 0\n"
2729                            "  // Code. Separated by blank line.\n"
2730                            "\n"
2731                            "#define B 0\n"
2732                            "  // Code. Not aligned with #\n"
2733                            "#define C 0\n";
2734     const char *ToFormat = ""
2735                            "void f() {\n"
2736                            "// Preprocessor aligned.\n"
2737                            "#define A 0\n"
2738                            "// Code. Separated by blank line.\n"
2739                            "\n"
2740                            "#define B 0\n"
2741                            "   // Code. Not aligned with #\n"
2742                            "#define C 0\n";
2743     EXPECT_EQ(Expected, format(ToFormat, Style));
2744     EXPECT_EQ(Expected, format(Expected, Style));
2745   }
2746   // Test with tabs.
2747   Style.UseTab = FormatStyle::UT_Always;
2748   Style.IndentWidth = 8;
2749   Style.TabWidth = 8;
2750   verifyFormat("#ifdef _WIN32\n"
2751                "#\tdefine A 0\n"
2752                "#\tifdef VAR2\n"
2753                "#\t\tdefine B 1\n"
2754                "#\t\tinclude <someheader.h>\n"
2755                "#\t\tdefine MACRO          \\\n"
2756                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2757                "#\tendif\n"
2758                "#else\n"
2759                "#\tdefine A 1\n"
2760                "#endif",
2761                Style);
2762 
2763   // Regression test: Multiline-macro inside include guards.
2764   verifyFormat("#ifndef HEADER_H\n"
2765                "#define HEADER_H\n"
2766                "#define A()        \\\n"
2767                "  int i;           \\\n"
2768                "  int j;\n"
2769                "#endif // HEADER_H",
2770                getLLVMStyleWithColumns(20));
2771 }
2772 
2773 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2774   verifyFormat("{\n  { a #c; }\n}");
2775 }
2776 
2777 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2778   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2779             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2780   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2781             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2782 }
2783 
2784 TEST_F(FormatTest, EscapedNewlines) {
2785   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2786   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2787             format("#define A \\\nint i;\\\n  int j;", Narrow));
2788   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2789   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2790   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2791   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2792 
2793   FormatStyle AlignLeft = getLLVMStyle();
2794   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2795   EXPECT_EQ("#define MACRO(x) \\\n"
2796             "private:         \\\n"
2797             "  int x(int a);\n",
2798             format("#define MACRO(x) \\\n"
2799                    "private:         \\\n"
2800                    "  int x(int a);\n",
2801                    AlignLeft));
2802 
2803   // CRLF line endings
2804   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2805             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2806   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2807   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2808   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2809   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2810   EXPECT_EQ("#define MACRO(x) \\\r\n"
2811             "private:         \\\r\n"
2812             "  int x(int a);\r\n",
2813             format("#define MACRO(x) \\\r\n"
2814                    "private:         \\\r\n"
2815                    "  int x(int a);\r\n",
2816                    AlignLeft));
2817 
2818   FormatStyle DontAlign = getLLVMStyle();
2819   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2820   DontAlign.MaxEmptyLinesToKeep = 3;
2821   // FIXME: can't use verifyFormat here because the newline before
2822   // "public:" is not inserted the first time it's reformatted
2823   EXPECT_EQ("#define A \\\n"
2824             "  class Foo { \\\n"
2825             "    void bar(); \\\n"
2826             "\\\n"
2827             "\\\n"
2828             "\\\n"
2829             "  public: \\\n"
2830             "    void baz(); \\\n"
2831             "  };",
2832             format("#define A \\\n"
2833                    "  class Foo { \\\n"
2834                    "    void bar(); \\\n"
2835                    "\\\n"
2836                    "\\\n"
2837                    "\\\n"
2838                    "  public: \\\n"
2839                    "    void baz(); \\\n"
2840                    "  };",
2841                    DontAlign));
2842 }
2843 
2844 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2845   verifyFormat("#define A \\\n"
2846                "  int v(  \\\n"
2847                "      a); \\\n"
2848                "  int i;",
2849                getLLVMStyleWithColumns(11));
2850 }
2851 
2852 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2853   EXPECT_EQ(
2854       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2855       "                      \\\n"
2856       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2857       "\n"
2858       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2859       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2860       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2861              "\\\n"
2862              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2863              "  \n"
2864              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2865              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2866 }
2867 
2868 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2869   EXPECT_EQ("int\n"
2870             "#define A\n"
2871             "    a;",
2872             format("int\n#define A\na;"));
2873   verifyFormat("functionCallTo(\n"
2874                "    someOtherFunction(\n"
2875                "        withSomeParameters, whichInSequence,\n"
2876                "        areLongerThanALine(andAnotherCall,\n"
2877                "#define A B\n"
2878                "                           withMoreParamters,\n"
2879                "                           whichStronglyInfluenceTheLayout),\n"
2880                "        andMoreParameters),\n"
2881                "    trailing);",
2882                getLLVMStyleWithColumns(69));
2883   verifyFormat("Foo::Foo()\n"
2884                "#ifdef BAR\n"
2885                "    : baz(0)\n"
2886                "#endif\n"
2887                "{\n"
2888                "}");
2889   verifyFormat("void f() {\n"
2890                "  if (true)\n"
2891                "#ifdef A\n"
2892                "    f(42);\n"
2893                "  x();\n"
2894                "#else\n"
2895                "    g();\n"
2896                "  x();\n"
2897                "#endif\n"
2898                "}");
2899   verifyFormat("void f(param1, param2,\n"
2900                "       param3,\n"
2901                "#ifdef A\n"
2902                "       param4(param5,\n"
2903                "#ifdef A1\n"
2904                "              param6,\n"
2905                "#ifdef A2\n"
2906                "              param7),\n"
2907                "#else\n"
2908                "              param8),\n"
2909                "       param9,\n"
2910                "#endif\n"
2911                "       param10,\n"
2912                "#endif\n"
2913                "       param11)\n"
2914                "#else\n"
2915                "       param12)\n"
2916                "#endif\n"
2917                "{\n"
2918                "  x();\n"
2919                "}",
2920                getLLVMStyleWithColumns(28));
2921   verifyFormat("#if 1\n"
2922                "int i;");
2923   verifyFormat("#if 1\n"
2924                "#endif\n"
2925                "#if 1\n"
2926                "#else\n"
2927                "#endif\n");
2928   verifyFormat("DEBUG({\n"
2929                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2930                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2931                "});\n"
2932                "#if a\n"
2933                "#else\n"
2934                "#endif");
2935 
2936   verifyIncompleteFormat("void f(\n"
2937                          "#if A\n"
2938                          ");\n"
2939                          "#else\n"
2940                          "#endif");
2941 }
2942 
2943 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2944   verifyFormat("#endif\n"
2945                "#if B");
2946 }
2947 
2948 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2949   FormatStyle SingleLine = getLLVMStyle();
2950   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2951   verifyFormat("#if 0\n"
2952                "#elif 1\n"
2953                "#endif\n"
2954                "void foo() {\n"
2955                "  if (test) foo2();\n"
2956                "}",
2957                SingleLine);
2958 }
2959 
2960 TEST_F(FormatTest, LayoutBlockInsideParens) {
2961   verifyFormat("functionCall({ int i; });");
2962   verifyFormat("functionCall({\n"
2963                "  int i;\n"
2964                "  int j;\n"
2965                "});");
2966   verifyFormat("functionCall(\n"
2967                "    {\n"
2968                "      int i;\n"
2969                "      int j;\n"
2970                "    },\n"
2971                "    aaaa, bbbb, cccc);");
2972   verifyFormat("functionA(functionB({\n"
2973                "            int i;\n"
2974                "            int j;\n"
2975                "          }),\n"
2976                "          aaaa, bbbb, cccc);");
2977   verifyFormat("functionCall(\n"
2978                "    {\n"
2979                "      int i;\n"
2980                "      int j;\n"
2981                "    },\n"
2982                "    aaaa, bbbb, // comment\n"
2983                "    cccc);");
2984   verifyFormat("functionA(functionB({\n"
2985                "            int i;\n"
2986                "            int j;\n"
2987                "          }),\n"
2988                "          aaaa, bbbb, // comment\n"
2989                "          cccc);");
2990   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2991   verifyFormat("functionCall(aaaa, bbbb, {\n"
2992                "  int i;\n"
2993                "  int j;\n"
2994                "});");
2995   verifyFormat(
2996       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2997       "    {\n"
2998       "      int i; // break\n"
2999       "    },\n"
3000       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3001       "                                     ccccccccccccccccc));");
3002   verifyFormat("DEBUG({\n"
3003                "  if (a)\n"
3004                "    f();\n"
3005                "});");
3006 }
3007 
3008 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3009   EXPECT_EQ("SOME_MACRO { int i; }\n"
3010             "int i;",
3011             format("  SOME_MACRO  {int i;}  int i;"));
3012 }
3013 
3014 TEST_F(FormatTest, LayoutNestedBlocks) {
3015   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3016                "  struct s {\n"
3017                "    int i;\n"
3018                "  };\n"
3019                "  s kBitsToOs[] = {{10}};\n"
3020                "  for (int i = 0; i < 10; ++i)\n"
3021                "    return;\n"
3022                "}");
3023   verifyFormat("call(parameter, {\n"
3024                "  something();\n"
3025                "  // Comment using all columns.\n"
3026                "  somethingelse();\n"
3027                "});",
3028                getLLVMStyleWithColumns(40));
3029   verifyFormat("DEBUG( //\n"
3030                "    { f(); }, a);");
3031   verifyFormat("DEBUG( //\n"
3032                "    {\n"
3033                "      f(); //\n"
3034                "    },\n"
3035                "    a);");
3036 
3037   EXPECT_EQ("call(parameter, {\n"
3038             "  something();\n"
3039             "  // Comment too\n"
3040             "  // looooooooooong.\n"
3041             "  somethingElse();\n"
3042             "});",
3043             format("call(parameter, {\n"
3044                    "  something();\n"
3045                    "  // Comment too looooooooooong.\n"
3046                    "  somethingElse();\n"
3047                    "});",
3048                    getLLVMStyleWithColumns(29)));
3049   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3050   EXPECT_EQ("DEBUG({ // comment\n"
3051             "  int i;\n"
3052             "});",
3053             format("DEBUG({ // comment\n"
3054                    "int  i;\n"
3055                    "});"));
3056   EXPECT_EQ("DEBUG({\n"
3057             "  int i;\n"
3058             "\n"
3059             "  // comment\n"
3060             "  int j;\n"
3061             "});",
3062             format("DEBUG({\n"
3063                    "  int  i;\n"
3064                    "\n"
3065                    "  // comment\n"
3066                    "  int  j;\n"
3067                    "});"));
3068 
3069   verifyFormat("DEBUG({\n"
3070                "  if (a)\n"
3071                "    return;\n"
3072                "});");
3073   verifyGoogleFormat("DEBUG({\n"
3074                      "  if (a) return;\n"
3075                      "});");
3076   FormatStyle Style = getGoogleStyle();
3077   Style.ColumnLimit = 45;
3078   verifyFormat("Debug(aaaaa,\n"
3079                "      {\n"
3080                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3081                "      },\n"
3082                "      a);",
3083                Style);
3084 
3085   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3086 
3087   verifyNoCrash("^{v^{a}}");
3088 }
3089 
3090 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3091   EXPECT_EQ("#define MACRO()                     \\\n"
3092             "  Debug(aaa, /* force line break */ \\\n"
3093             "        {                           \\\n"
3094             "          int i;                    \\\n"
3095             "          int j;                    \\\n"
3096             "        })",
3097             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3098                    "          {  int   i;  int  j;   })",
3099                    getGoogleStyle()));
3100 
3101   EXPECT_EQ("#define A                                       \\\n"
3102             "  [] {                                          \\\n"
3103             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3104             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3105             "  }",
3106             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3107                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3108                    getGoogleStyle()));
3109 }
3110 
3111 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3112   EXPECT_EQ("{}", format("{}"));
3113   verifyFormat("enum E {};");
3114   verifyFormat("enum E {}");
3115 }
3116 
3117 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3118   FormatStyle Style = getLLVMStyle();
3119   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3120   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3121   verifyFormat("FOO_BEGIN\n"
3122                "  FOO_ENTRY\n"
3123                "FOO_END", Style);
3124   verifyFormat("FOO_BEGIN\n"
3125                "  NESTED_FOO_BEGIN\n"
3126                "    NESTED_FOO_ENTRY\n"
3127                "  NESTED_FOO_END\n"
3128                "FOO_END", Style);
3129   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3130                "  int x;\n"
3131                "  x = 1;\n"
3132                "FOO_END(Baz)", Style);
3133 }
3134 
3135 //===----------------------------------------------------------------------===//
3136 // Line break tests.
3137 //===----------------------------------------------------------------------===//
3138 
3139 TEST_F(FormatTest, PreventConfusingIndents) {
3140   verifyFormat(
3141       "void f() {\n"
3142       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3143       "                         parameter, parameter, parameter)),\n"
3144       "                     SecondLongCall(parameter));\n"
3145       "}");
3146   verifyFormat(
3147       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3148       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3149       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3150       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3151   verifyFormat(
3152       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3153       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3154       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3155       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3156   verifyFormat(
3157       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3158       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3159       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3160       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3161   verifyFormat("int a = bbbb && ccc &&\n"
3162                "        fffff(\n"
3163                "#define A Just forcing a new line\n"
3164                "            ddd);");
3165 }
3166 
3167 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3168   verifyFormat(
3169       "bool aaaaaaa =\n"
3170       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3171       "    bbbbbbbb();");
3172   verifyFormat(
3173       "bool aaaaaaa =\n"
3174       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3175       "    bbbbbbbb();");
3176 
3177   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3178                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3179                "    ccccccccc == ddddddddddd;");
3180   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3181                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3182                "    ccccccccc == ddddddddddd;");
3183   verifyFormat(
3184       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3185       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3186       "    ccccccccc == ddddddddddd;");
3187 
3188   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3189                "                 aaaaaa) &&\n"
3190                "         bbbbbb && cccccc;");
3191   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3192                "                 aaaaaa) >>\n"
3193                "         bbbbbb;");
3194   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3195                "    SourceMgr.getSpellingColumnNumber(\n"
3196                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3197                "    1);");
3198 
3199   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3200                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3201                "    cccccc) {\n}");
3202   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3203                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3204                "              cccccc) {\n}");
3205   verifyFormat("b = a &&\n"
3206                "    // Comment\n"
3207                "    b.c && d;");
3208 
3209   // If the LHS of a comparison is not a binary expression itself, the
3210   // additional linebreak confuses many people.
3211   verifyFormat(
3212       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3213       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3214       "}");
3215   verifyFormat(
3216       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3217       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3218       "}");
3219   verifyFormat(
3220       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3221       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3222       "}");
3223   verifyFormat(
3224       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3225       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3226       "}");
3227   // Even explicit parentheses stress the precedence enough to make the
3228   // additional break unnecessary.
3229   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3230                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3231                "}");
3232   // This cases is borderline, but with the indentation it is still readable.
3233   verifyFormat(
3234       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3235       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3236       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3237       "}",
3238       getLLVMStyleWithColumns(75));
3239 
3240   // If the LHS is a binary expression, we should still use the additional break
3241   // as otherwise the formatting hides the operator precedence.
3242   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3243                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3244                "    5) {\n"
3245                "}");
3246   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3247                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3248                "    5) {\n"
3249                "}");
3250 
3251   FormatStyle OnePerLine = getLLVMStyle();
3252   OnePerLine.BinPackParameters = false;
3253   verifyFormat(
3254       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3255       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3256       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3257       OnePerLine);
3258 
3259   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3260                "                .aaa(aaaaaaaaaaaaa) *\n"
3261                "            aaaaaaa +\n"
3262                "        aaaaaaa;",
3263                getLLVMStyleWithColumns(40));
3264 }
3265 
3266 TEST_F(FormatTest, ExpressionIndentation) {
3267   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3268                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3269                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3270                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3271                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3272                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3273                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3274                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3275                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3276   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3277                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3278                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3279                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3280   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3281                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3282                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3283                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3284   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3285                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3286                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3287                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3288   verifyFormat("if () {\n"
3289                "} else if (aaaaa && bbbbb > // break\n"
3290                "                        ccccc) {\n"
3291                "}");
3292   verifyFormat("if () {\n"
3293                "} else if (aaaaa &&\n"
3294                "           bbbbb > // break\n"
3295                "               ccccc &&\n"
3296                "           ddddd) {\n"
3297                "}");
3298 
3299   // Presence of a trailing comment used to change indentation of b.
3300   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3301                "       b;\n"
3302                "return aaaaaaaaaaaaaaaaaaa +\n"
3303                "       b; //",
3304                getLLVMStyleWithColumns(30));
3305 }
3306 
3307 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3308   // Not sure what the best system is here. Like this, the LHS can be found
3309   // immediately above an operator (everything with the same or a higher
3310   // indent). The RHS is aligned right of the operator and so compasses
3311   // everything until something with the same indent as the operator is found.
3312   // FIXME: Is this a good system?
3313   FormatStyle Style = getLLVMStyle();
3314   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3315   verifyFormat(
3316       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3317       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3318       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3319       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3320       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3321       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3322       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3323       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3324       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3325       Style);
3326   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3327                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3328                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3329                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3330                Style);
3331   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3332                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3333                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3334                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3335                Style);
3336   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3337                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3338                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3339                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3340                Style);
3341   verifyFormat("if () {\n"
3342                "} else if (aaaaa\n"
3343                "           && bbbbb // break\n"
3344                "                  > ccccc) {\n"
3345                "}",
3346                Style);
3347   verifyFormat("return (a)\n"
3348                "       // comment\n"
3349                "       + b;",
3350                Style);
3351   verifyFormat(
3352       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3353       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3354       "             + cc;",
3355       Style);
3356 
3357   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3358                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3359                Style);
3360 
3361   // Forced by comments.
3362   verifyFormat(
3363       "unsigned ContentSize =\n"
3364       "    sizeof(int16_t)   // DWARF ARange version number\n"
3365       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3366       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3367       "    + sizeof(int8_t); // Segment Size (in bytes)");
3368 
3369   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3370                "       == boost::fusion::at_c<1>(iiii).second;",
3371                Style);
3372 
3373   Style.ColumnLimit = 60;
3374   verifyFormat("zzzzzzzzzz\n"
3375                "    = bbbbbbbbbbbbbbbbb\n"
3376                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3377                Style);
3378 }
3379 
3380 TEST_F(FormatTest, EnforcedOperatorWraps) {
3381   // Here we'd like to wrap after the || operators, but a comment is forcing an
3382   // earlier wrap.
3383   verifyFormat("bool x = aaaaa //\n"
3384                "         || bbbbb\n"
3385                "         //\n"
3386                "         || cccc;");
3387 }
3388 
3389 TEST_F(FormatTest, NoOperandAlignment) {
3390   FormatStyle Style = getLLVMStyle();
3391   Style.AlignOperands = false;
3392   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3393                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3394                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3395                Style);
3396   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3397   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3398                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3399                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3400                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3401                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3402                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3403                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3404                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3405                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3406                Style);
3407 
3408   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3409                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3410                "    + cc;",
3411                Style);
3412   verifyFormat("int a = aa\n"
3413                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3414                "        * cccccccccccccccccccccccccccccccccccc;\n",
3415                Style);
3416 
3417   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3418   verifyFormat("return (a > b\n"
3419                "    // comment1\n"
3420                "    // comment2\n"
3421                "    || c);",
3422                Style);
3423 }
3424 
3425 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3426   FormatStyle Style = getLLVMStyle();
3427   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3428   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3429                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3430                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3431                Style);
3432 }
3433 
3434 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3435   FormatStyle Style = getLLVMStyle();
3436   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3437   Style.BinPackArguments = false;
3438   Style.ColumnLimit = 40;
3439   verifyFormat("void test() {\n"
3440                "  someFunction(\n"
3441                "      this + argument + is + quite\n"
3442                "      + long + so + it + gets + wrapped\n"
3443                "      + but + remains + bin - packed);\n"
3444                "}",
3445                Style);
3446   verifyFormat("void test() {\n"
3447                "  someFunction(arg1,\n"
3448                "               this + argument + is\n"
3449                "                   + quite + long + so\n"
3450                "                   + it + gets + wrapped\n"
3451                "                   + but + remains + bin\n"
3452                "                   - packed,\n"
3453                "               arg3);\n"
3454                "}",
3455                Style);
3456   verifyFormat("void test() {\n"
3457                "  someFunction(\n"
3458                "      arg1,\n"
3459                "      this + argument + has\n"
3460                "          + anotherFunc(nested,\n"
3461                "                        calls + whose\n"
3462                "                            + arguments\n"
3463                "                            + are + also\n"
3464                "                            + wrapped,\n"
3465                "                        in + addition)\n"
3466                "          + to + being + bin - packed,\n"
3467                "      arg3);\n"
3468                "}",
3469                Style);
3470 
3471   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3472   verifyFormat("void test() {\n"
3473                "  someFunction(\n"
3474                "      arg1,\n"
3475                "      this + argument + has +\n"
3476                "          anotherFunc(nested,\n"
3477                "                      calls + whose +\n"
3478                "                          arguments +\n"
3479                "                          are + also +\n"
3480                "                          wrapped,\n"
3481                "                      in + addition) +\n"
3482                "          to + being + bin - packed,\n"
3483                "      arg3);\n"
3484                "}",
3485                Style);
3486 }
3487 
3488 TEST_F(FormatTest, ConstructorInitializers) {
3489   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3490   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3491                getLLVMStyleWithColumns(45));
3492   verifyFormat("Constructor()\n"
3493                "    : Inttializer(FitsOnTheLine) {}",
3494                getLLVMStyleWithColumns(44));
3495   verifyFormat("Constructor()\n"
3496                "    : Inttializer(FitsOnTheLine) {}",
3497                getLLVMStyleWithColumns(43));
3498 
3499   verifyFormat("template <typename T>\n"
3500                "Constructor() : Initializer(FitsOnTheLine) {}",
3501                getLLVMStyleWithColumns(45));
3502 
3503   verifyFormat(
3504       "SomeClass::Constructor()\n"
3505       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3506 
3507   verifyFormat(
3508       "SomeClass::Constructor()\n"
3509       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3510       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3511   verifyFormat(
3512       "SomeClass::Constructor()\n"
3513       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3514       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3515   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3516                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3517                "    : aaaaaaaaaa(aaaaaa) {}");
3518 
3519   verifyFormat("Constructor()\n"
3520                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3521                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3522                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3523                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3524 
3525   verifyFormat("Constructor()\n"
3526                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3527                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3528 
3529   verifyFormat("Constructor(int Parameter = 0)\n"
3530                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3531                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3532   verifyFormat("Constructor()\n"
3533                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3534                "}",
3535                getLLVMStyleWithColumns(60));
3536   verifyFormat("Constructor()\n"
3537                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3538                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3539 
3540   // Here a line could be saved by splitting the second initializer onto two
3541   // lines, but that is not desirable.
3542   verifyFormat("Constructor()\n"
3543                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3544                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3545                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3546 
3547   FormatStyle OnePerLine = getLLVMStyle();
3548   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3549   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3550   verifyFormat("SomeClass::Constructor()\n"
3551                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3552                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3553                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3554                OnePerLine);
3555   verifyFormat("SomeClass::Constructor()\n"
3556                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3557                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3558                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3559                OnePerLine);
3560   verifyFormat("MyClass::MyClass(int var)\n"
3561                "    : some_var_(var),            // 4 space indent\n"
3562                "      some_other_var_(var + 1) { // lined up\n"
3563                "}",
3564                OnePerLine);
3565   verifyFormat("Constructor()\n"
3566                "    : aaaaa(aaaaaa),\n"
3567                "      aaaaa(aaaaaa),\n"
3568                "      aaaaa(aaaaaa),\n"
3569                "      aaaaa(aaaaaa),\n"
3570                "      aaaaa(aaaaaa) {}",
3571                OnePerLine);
3572   verifyFormat("Constructor()\n"
3573                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3574                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3575                OnePerLine);
3576   OnePerLine.BinPackParameters = false;
3577   verifyFormat(
3578       "Constructor()\n"
3579       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3580       "          aaaaaaaaaaa().aaa(),\n"
3581       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3582       OnePerLine);
3583   OnePerLine.ColumnLimit = 60;
3584   verifyFormat("Constructor()\n"
3585                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3586                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3587                OnePerLine);
3588 
3589   EXPECT_EQ("Constructor()\n"
3590             "    : // Comment forcing unwanted break.\n"
3591             "      aaaa(aaaa) {}",
3592             format("Constructor() :\n"
3593                    "    // Comment forcing unwanted break.\n"
3594                    "    aaaa(aaaa) {}"));
3595 }
3596 
3597 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3598   FormatStyle Style = getLLVMStyle();
3599   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3600 
3601   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3602   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3603                getStyleWithColumns(Style, 45));
3604   verifyFormat("Constructor() :\n"
3605                "    Initializer(FitsOnTheLine) {}",
3606                getStyleWithColumns(Style, 44));
3607   verifyFormat("Constructor() :\n"
3608                "    Initializer(FitsOnTheLine) {}",
3609                getStyleWithColumns(Style, 43));
3610 
3611   verifyFormat("template <typename T>\n"
3612                "Constructor() : Initializer(FitsOnTheLine) {}",
3613                getStyleWithColumns(Style, 50));
3614 
3615   verifyFormat(
3616       "SomeClass::Constructor() :\n"
3617       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3618 	  Style);
3619 
3620   verifyFormat(
3621       "SomeClass::Constructor() :\n"
3622       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3623       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3624 	  Style);
3625   verifyFormat(
3626       "SomeClass::Constructor() :\n"
3627       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3628       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3629 	  Style);
3630   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3631                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3632                "    aaaaaaaaaa(aaaaaa) {}",
3633 			   Style);
3634 
3635   verifyFormat("Constructor() :\n"
3636                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3637                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3638                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3639                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3640 			   Style);
3641 
3642   verifyFormat("Constructor() :\n"
3643                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3644                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3645 			   Style);
3646 
3647   verifyFormat("Constructor(int Parameter = 0) :\n"
3648                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3649                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3650 			   Style);
3651   verifyFormat("Constructor() :\n"
3652                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3653                "}",
3654                getStyleWithColumns(Style, 60));
3655   verifyFormat("Constructor() :\n"
3656                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3657                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3658 			   Style);
3659 
3660   // Here a line could be saved by splitting the second initializer onto two
3661   // lines, but that is not desirable.
3662   verifyFormat("Constructor() :\n"
3663                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3664                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3665                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3666 			   Style);
3667 
3668   FormatStyle OnePerLine = Style;
3669   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3670   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3671   verifyFormat("SomeClass::Constructor() :\n"
3672                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3673                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3674                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3675                OnePerLine);
3676   verifyFormat("SomeClass::Constructor() :\n"
3677                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3678                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3679                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3680                OnePerLine);
3681   verifyFormat("MyClass::MyClass(int var) :\n"
3682                "    some_var_(var),            // 4 space indent\n"
3683                "    some_other_var_(var + 1) { // lined up\n"
3684                "}",
3685                OnePerLine);
3686   verifyFormat("Constructor() :\n"
3687                "    aaaaa(aaaaaa),\n"
3688                "    aaaaa(aaaaaa),\n"
3689                "    aaaaa(aaaaaa),\n"
3690                "    aaaaa(aaaaaa),\n"
3691                "    aaaaa(aaaaaa) {}",
3692                OnePerLine);
3693   verifyFormat("Constructor() :\n"
3694                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3695                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3696                OnePerLine);
3697   OnePerLine.BinPackParameters = false;
3698   verifyFormat(
3699       "Constructor() :\n"
3700       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3701       "        aaaaaaaaaaa().aaa(),\n"
3702       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3703       OnePerLine);
3704   OnePerLine.ColumnLimit = 60;
3705   verifyFormat("Constructor() :\n"
3706                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3707                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3708                OnePerLine);
3709 
3710   EXPECT_EQ("Constructor() :\n"
3711             "    // Comment forcing unwanted break.\n"
3712             "    aaaa(aaaa) {}",
3713             format("Constructor() :\n"
3714                    "    // Comment forcing unwanted break.\n"
3715                    "    aaaa(aaaa) {}",
3716 				   Style));
3717 
3718   Style.ColumnLimit = 0;
3719   verifyFormat("SomeClass::Constructor() :\n"
3720                "    a(a) {}",
3721                Style);
3722   verifyFormat("SomeClass::Constructor() noexcept :\n"
3723                "    a(a) {}",
3724                Style);
3725   verifyFormat("SomeClass::Constructor() :\n"
3726 			   "    a(a), b(b), c(c) {}",
3727                Style);
3728   verifyFormat("SomeClass::Constructor() :\n"
3729                "    a(a) {\n"
3730                "  foo();\n"
3731                "  bar();\n"
3732                "}",
3733                Style);
3734 
3735   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3736   verifyFormat("SomeClass::Constructor() :\n"
3737 			   "    a(a), b(b), c(c) {\n"
3738 			   "}",
3739                Style);
3740   verifyFormat("SomeClass::Constructor() :\n"
3741                "    a(a) {\n"
3742 			   "}",
3743                Style);
3744 
3745   Style.ColumnLimit = 80;
3746   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3747   Style.ConstructorInitializerIndentWidth = 2;
3748   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3749                Style);
3750   verifyFormat("SomeClass::Constructor() :\n"
3751                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3752                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3753                Style);
3754 
3755   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
3756   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
3757   verifyFormat("class SomeClass\n"
3758                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3759                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3760                Style);
3761   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
3762   verifyFormat("class SomeClass\n"
3763                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3764                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3765                Style);
3766   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
3767   verifyFormat("class SomeClass :\n"
3768                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3769                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3770                Style);
3771 }
3772 
3773 #ifndef EXPENSIVE_CHECKS
3774 // Expensive checks enables libstdc++ checking which includes validating the
3775 // state of ranges used in std::priority_queue - this blows out the
3776 // runtime/scalability of the function and makes this test unacceptably slow.
3777 TEST_F(FormatTest, MemoizationTests) {
3778   // This breaks if the memoization lookup does not take \c Indent and
3779   // \c LastSpace into account.
3780   verifyFormat(
3781       "extern CFRunLoopTimerRef\n"
3782       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3783       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3784       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3785       "                     CFRunLoopTimerContext *context) {}");
3786 
3787   // Deep nesting somewhat works around our memoization.
3788   verifyFormat(
3789       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3790       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3791       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3792       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3793       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3794       getLLVMStyleWithColumns(65));
3795   verifyFormat(
3796       "aaaaa(\n"
3797       "    aaaaa,\n"
3798       "    aaaaa(\n"
3799       "        aaaaa,\n"
3800       "        aaaaa(\n"
3801       "            aaaaa,\n"
3802       "            aaaaa(\n"
3803       "                aaaaa,\n"
3804       "                aaaaa(\n"
3805       "                    aaaaa,\n"
3806       "                    aaaaa(\n"
3807       "                        aaaaa,\n"
3808       "                        aaaaa(\n"
3809       "                            aaaaa,\n"
3810       "                            aaaaa(\n"
3811       "                                aaaaa,\n"
3812       "                                aaaaa(\n"
3813       "                                    aaaaa,\n"
3814       "                                    aaaaa(\n"
3815       "                                        aaaaa,\n"
3816       "                                        aaaaa(\n"
3817       "                                            aaaaa,\n"
3818       "                                            aaaaa(\n"
3819       "                                                aaaaa,\n"
3820       "                                                aaaaa))))))))))));",
3821       getLLVMStyleWithColumns(65));
3822   verifyFormat(
3823       "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"
3824       "                                  a),\n"
3825       "                                a),\n"
3826       "                              a),\n"
3827       "                            a),\n"
3828       "                          a),\n"
3829       "                        a),\n"
3830       "                      a),\n"
3831       "                    a),\n"
3832       "                  a),\n"
3833       "                a),\n"
3834       "              a),\n"
3835       "            a),\n"
3836       "          a),\n"
3837       "        a),\n"
3838       "      a),\n"
3839       "    a),\n"
3840       "  a)",
3841       getLLVMStyleWithColumns(65));
3842 
3843   // This test takes VERY long when memoization is broken.
3844   FormatStyle OnePerLine = getLLVMStyle();
3845   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3846   OnePerLine.BinPackParameters = false;
3847   std::string input = "Constructor()\n"
3848                       "    : aaaa(a,\n";
3849   for (unsigned i = 0, e = 80; i != e; ++i) {
3850     input += "           a,\n";
3851   }
3852   input += "           a) {}";
3853   verifyFormat(input, OnePerLine);
3854 }
3855 #endif
3856 
3857 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3858   verifyFormat(
3859       "void f() {\n"
3860       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3861       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3862       "    f();\n"
3863       "}");
3864   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3865                "    Intervals[i - 1].getRange().getLast()) {\n}");
3866 }
3867 
3868 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3869   // Principially, we break function declarations in a certain order:
3870   // 1) break amongst arguments.
3871   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3872                "                              Cccccccccccccc cccccccccccccc);");
3873   verifyFormat("template <class TemplateIt>\n"
3874                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3875                "                            TemplateIt *stop) {}");
3876 
3877   // 2) break after return type.
3878   verifyFormat(
3879       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3880       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3881       getGoogleStyle());
3882 
3883   // 3) break after (.
3884   verifyFormat(
3885       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3886       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3887       getGoogleStyle());
3888 
3889   // 4) break before after nested name specifiers.
3890   verifyFormat(
3891       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3892       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3893       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3894       getGoogleStyle());
3895 
3896   // However, there are exceptions, if a sufficient amount of lines can be
3897   // saved.
3898   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3899   // more adjusting.
3900   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3901                "                                  Cccccccccccccc cccccccccc,\n"
3902                "                                  Cccccccccccccc cccccccccc,\n"
3903                "                                  Cccccccccccccc cccccccccc,\n"
3904                "                                  Cccccccccccccc cccccccccc);");
3905   verifyFormat(
3906       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3907       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3908       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3909       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3910       getGoogleStyle());
3911   verifyFormat(
3912       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3913       "                                          Cccccccccccccc cccccccccc,\n"
3914       "                                          Cccccccccccccc cccccccccc,\n"
3915       "                                          Cccccccccccccc cccccccccc,\n"
3916       "                                          Cccccccccccccc cccccccccc,\n"
3917       "                                          Cccccccccccccc cccccccccc,\n"
3918       "                                          Cccccccccccccc cccccccccc);");
3919   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3920                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3921                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3922                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3923                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3924 
3925   // Break after multi-line parameters.
3926   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3927                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3928                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3929                "    bbbb bbbb);");
3930   verifyFormat("void SomeLoooooooooooongFunction(\n"
3931                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3932                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3933                "    int bbbbbbbbbbbbb);");
3934 
3935   // Treat overloaded operators like other functions.
3936   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3937                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3938   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3939                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3940   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3941                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3942   verifyGoogleFormat(
3943       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3944       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3945   verifyGoogleFormat(
3946       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3947       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3948   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3949                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3950   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3951                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3952   verifyGoogleFormat(
3953       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3954       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3955       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3956   verifyGoogleFormat(
3957       "template <typename T>\n"
3958       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3959       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3960       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3961 
3962   FormatStyle Style = getLLVMStyle();
3963   Style.PointerAlignment = FormatStyle::PAS_Left;
3964   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3965                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3966                Style);
3967   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3968                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3969                Style);
3970 }
3971 
3972 TEST_F(FormatTest, TrailingReturnType) {
3973   verifyFormat("auto foo() -> int;\n");
3974   verifyFormat("struct S {\n"
3975                "  auto bar() const -> int;\n"
3976                "};");
3977   verifyFormat("template <size_t Order, typename T>\n"
3978                "auto load_img(const std::string &filename)\n"
3979                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3980   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3981                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3982   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3983   verifyFormat("template <typename T>\n"
3984                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3985                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3986 
3987   // Not trailing return types.
3988   verifyFormat("void f() { auto a = b->c(); }");
3989 }
3990 
3991 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3992   // Avoid breaking before trailing 'const' or other trailing annotations, if
3993   // they are not function-like.
3994   FormatStyle Style = getGoogleStyle();
3995   Style.ColumnLimit = 47;
3996   verifyFormat("void someLongFunction(\n"
3997                "    int someLoooooooooooooongParameter) const {\n}",
3998                getLLVMStyleWithColumns(47));
3999   verifyFormat("LoooooongReturnType\n"
4000                "someLoooooooongFunction() const {}",
4001                getLLVMStyleWithColumns(47));
4002   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4003                "    const {}",
4004                Style);
4005   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4006                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4007   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4008                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4009   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4010                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4011   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4012                "                   aaaaaaaaaaa aaaaa) const override;");
4013   verifyGoogleFormat(
4014       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4015       "    const override;");
4016 
4017   // Even if the first parameter has to be wrapped.
4018   verifyFormat("void someLongFunction(\n"
4019                "    int someLongParameter) const {}",
4020                getLLVMStyleWithColumns(46));
4021   verifyFormat("void someLongFunction(\n"
4022                "    int someLongParameter) const {}",
4023                Style);
4024   verifyFormat("void someLongFunction(\n"
4025                "    int someLongParameter) override {}",
4026                Style);
4027   verifyFormat("void someLongFunction(\n"
4028                "    int someLongParameter) OVERRIDE {}",
4029                Style);
4030   verifyFormat("void someLongFunction(\n"
4031                "    int someLongParameter) final {}",
4032                Style);
4033   verifyFormat("void someLongFunction(\n"
4034                "    int someLongParameter) FINAL {}",
4035                Style);
4036   verifyFormat("void someLongFunction(\n"
4037                "    int parameter) const override {}",
4038                Style);
4039 
4040   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4041   verifyFormat("void someLongFunction(\n"
4042                "    int someLongParameter) const\n"
4043                "{\n"
4044                "}",
4045                Style);
4046 
4047   // Unless these are unknown annotations.
4048   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4049                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4050                "    LONG_AND_UGLY_ANNOTATION;");
4051 
4052   // Breaking before function-like trailing annotations is fine to keep them
4053   // close to their arguments.
4054   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4055                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4056   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4057                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4058   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4059                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4060   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4061                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4062   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4063 
4064   verifyFormat(
4065       "void aaaaaaaaaaaaaaaaaa()\n"
4066       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4067       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4068   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4069                "    __attribute__((unused));");
4070   verifyGoogleFormat(
4071       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4072       "    GUARDED_BY(aaaaaaaaaaaa);");
4073   verifyGoogleFormat(
4074       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4075       "    GUARDED_BY(aaaaaaaaaaaa);");
4076   verifyGoogleFormat(
4077       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4078       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4079   verifyGoogleFormat(
4080       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4081       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4082 }
4083 
4084 TEST_F(FormatTest, FunctionAnnotations) {
4085   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4086                "int OldFunction(const string &parameter) {}");
4087   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4088                "string OldFunction(const string &parameter) {}");
4089   verifyFormat("template <typename T>\n"
4090                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4091                "string OldFunction(const string &parameter) {}");
4092 
4093   // Not function annotations.
4094   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4095                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4096   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4097                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4098   verifyFormat("MACRO(abc).function() // wrap\n"
4099                "    << abc;");
4100   verifyFormat("MACRO(abc)->function() // wrap\n"
4101                "    << abc;");
4102   verifyFormat("MACRO(abc)::function() // wrap\n"
4103                "    << abc;");
4104 }
4105 
4106 TEST_F(FormatTest, BreaksDesireably) {
4107   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4108                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4109                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4110   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4111                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4112                "}");
4113 
4114   verifyFormat(
4115       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4116       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4117 
4118   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4119                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4120                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4121 
4122   verifyFormat(
4123       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4124       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4125       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4126       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4127       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4128 
4129   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4130                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4131 
4132   verifyFormat(
4133       "void f() {\n"
4134       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4135       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4136       "}");
4137   verifyFormat(
4138       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4139       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4140   verifyFormat(
4141       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4142       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4143   verifyFormat(
4144       "aaaaaa(aaa,\n"
4145       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4146       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4147       "       aaaa);");
4148   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4149                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4150                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4151 
4152   // Indent consistently independent of call expression and unary operator.
4153   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4154                "    dddddddddddddddddddddddddddddd));");
4155   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4156                "    dddddddddddddddddddddddddddddd));");
4157   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4158                "    dddddddddddddddddddddddddddddd));");
4159 
4160   // This test case breaks on an incorrect memoization, i.e. an optimization not
4161   // taking into account the StopAt value.
4162   verifyFormat(
4163       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4164       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4165       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4166       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4167 
4168   verifyFormat("{\n  {\n    {\n"
4169                "      Annotation.SpaceRequiredBefore =\n"
4170                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4171                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4172                "    }\n  }\n}");
4173 
4174   // Break on an outer level if there was a break on an inner level.
4175   EXPECT_EQ("f(g(h(a, // comment\n"
4176             "      b, c),\n"
4177             "    d, e),\n"
4178             "  x, y);",
4179             format("f(g(h(a, // comment\n"
4180                    "    b, c), d, e), x, y);"));
4181 
4182   // Prefer breaking similar line breaks.
4183   verifyFormat(
4184       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4185       "                             NSTrackingMouseEnteredAndExited |\n"
4186       "                             NSTrackingActiveAlways;");
4187 }
4188 
4189 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4190   FormatStyle NoBinPacking = getGoogleStyle();
4191   NoBinPacking.BinPackParameters = false;
4192   NoBinPacking.BinPackArguments = true;
4193   verifyFormat("void f() {\n"
4194                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4195                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4196                "}",
4197                NoBinPacking);
4198   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4199                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4200                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4201                NoBinPacking);
4202 
4203   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4204   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4205                "                        vector<int> bbbbbbbbbbbbbbb);",
4206                NoBinPacking);
4207   // FIXME: This behavior difference is probably not wanted. However, currently
4208   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4209   // template arguments from BreakBeforeParameter being set because of the
4210   // one-per-line formatting.
4211   verifyFormat(
4212       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4213       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4214       NoBinPacking);
4215   verifyFormat(
4216       "void fffffffffff(\n"
4217       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4218       "        aaaaaaaaaa);");
4219 }
4220 
4221 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4222   FormatStyle NoBinPacking = getGoogleStyle();
4223   NoBinPacking.BinPackParameters = false;
4224   NoBinPacking.BinPackArguments = false;
4225   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4226                "  aaaaaaaaaaaaaaaaaaaa,\n"
4227                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4228                NoBinPacking);
4229   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4230                "        aaaaaaaaaaaaa,\n"
4231                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4232                NoBinPacking);
4233   verifyFormat(
4234       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4235       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4236       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4237       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4238       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4239       NoBinPacking);
4240   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4241                "    .aaaaaaaaaaaaaaaaaa();",
4242                NoBinPacking);
4243   verifyFormat("void f() {\n"
4244                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4245                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4246                "}",
4247                NoBinPacking);
4248 
4249   verifyFormat(
4250       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4251       "             aaaaaaaaaaaa,\n"
4252       "             aaaaaaaaaaaa);",
4253       NoBinPacking);
4254   verifyFormat(
4255       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4256       "                               ddddddddddddddddddddddddddddd),\n"
4257       "             test);",
4258       NoBinPacking);
4259 
4260   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4261                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4262                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4263                "    aaaaaaaaaaaaaaaaaa;",
4264                NoBinPacking);
4265   verifyFormat("a(\"a\"\n"
4266                "  \"a\",\n"
4267                "  a);");
4268 
4269   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4270   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4271                "                aaaaaaaaa,\n"
4272                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4273                NoBinPacking);
4274   verifyFormat(
4275       "void f() {\n"
4276       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4277       "      .aaaaaaa();\n"
4278       "}",
4279       NoBinPacking);
4280   verifyFormat(
4281       "template <class SomeType, class SomeOtherType>\n"
4282       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4283       NoBinPacking);
4284 }
4285 
4286 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4287   FormatStyle Style = getLLVMStyleWithColumns(15);
4288   Style.ExperimentalAutoDetectBinPacking = true;
4289   EXPECT_EQ("aaa(aaaa,\n"
4290             "    aaaa,\n"
4291             "    aaaa);\n"
4292             "aaa(aaaa,\n"
4293             "    aaaa,\n"
4294             "    aaaa);",
4295             format("aaa(aaaa,\n" // one-per-line
4296                    "  aaaa,\n"
4297                    "    aaaa  );\n"
4298                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4299                    Style));
4300   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4301             "    aaaa);\n"
4302             "aaa(aaaa, aaaa,\n"
4303             "    aaaa);",
4304             format("aaa(aaaa,  aaaa,\n" // bin-packed
4305                    "    aaaa  );\n"
4306                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4307                    Style));
4308 }
4309 
4310 TEST_F(FormatTest, FormatsBuilderPattern) {
4311   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4312                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4313                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4314                "    .StartsWith(\".init\", ORDER_INIT)\n"
4315                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4316                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4317                "    .Default(ORDER_TEXT);\n");
4318 
4319   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4320                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4321   verifyFormat(
4322       "aaaaaaa->aaaaaaa\n"
4323       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4324       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4325       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4326   verifyFormat(
4327       "aaaaaaa->aaaaaaa\n"
4328       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4329       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4330   verifyFormat(
4331       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4332       "    aaaaaaaaaaaaaa);");
4333   verifyFormat(
4334       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4335       "    aaaaaa->aaaaaaaaaaaa()\n"
4336       "        ->aaaaaaaaaaaaaaaa(\n"
4337       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4338       "        ->aaaaaaaaaaaaaaaaa();");
4339   verifyGoogleFormat(
4340       "void f() {\n"
4341       "  someo->Add((new util::filetools::Handler(dir))\n"
4342       "                 ->OnEvent1(NewPermanentCallback(\n"
4343       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4344       "                 ->OnEvent2(NewPermanentCallback(\n"
4345       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4346       "                 ->OnEvent3(NewPermanentCallback(\n"
4347       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4348       "                 ->OnEvent5(NewPermanentCallback(\n"
4349       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4350       "                 ->OnEvent6(NewPermanentCallback(\n"
4351       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4352       "}");
4353 
4354   verifyFormat(
4355       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4356   verifyFormat("aaaaaaaaaaaaaaa()\n"
4357                "    .aaaaaaaaaaaaaaa()\n"
4358                "    .aaaaaaaaaaaaaaa()\n"
4359                "    .aaaaaaaaaaaaaaa()\n"
4360                "    .aaaaaaaaaaaaaaa();");
4361   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4362                "    .aaaaaaaaaaaaaaa()\n"
4363                "    .aaaaaaaaaaaaaaa()\n"
4364                "    .aaaaaaaaaaaaaaa();");
4365   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4366                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4367                "    .aaaaaaaaaaaaaaa();");
4368   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4369                "    ->aaaaaaaaaaaaaae(0)\n"
4370                "    ->aaaaaaaaaaaaaaa();");
4371 
4372   // Don't linewrap after very short segments.
4373   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4374                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4375                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4376   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4377                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4378                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4379   verifyFormat("aaa()\n"
4380                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4381                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4382                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4383 
4384   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4385                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4386                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4388                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4389                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4390 
4391   // Prefer not to break after empty parentheses.
4392   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4393                "    First->LastNewlineOffset);");
4394 
4395   // Prefer not to create "hanging" indents.
4396   verifyFormat(
4397       "return !soooooooooooooome_map\n"
4398       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4399       "            .second;");
4400   verifyFormat(
4401       "return aaaaaaaaaaaaaaaa\n"
4402       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4403       "    .aaaa(aaaaaaaaaaaaaa);");
4404   // No hanging indent here.
4405   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4406                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4407   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4408                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4409   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4410                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4411                getLLVMStyleWithColumns(60));
4412   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4413                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4414                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4415                getLLVMStyleWithColumns(59));
4416   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4417                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4418                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4419 }
4420 
4421 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4422   verifyFormat(
4423       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4424       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4425   verifyFormat(
4426       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4427       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4428 
4429   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4430                "    ccccccccccccccccccccccccc) {\n}");
4431   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4432                "    ccccccccccccccccccccccccc) {\n}");
4433 
4434   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4435                "    ccccccccccccccccccccccccc) {\n}");
4436   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4437                "    ccccccccccccccccccccccccc) {\n}");
4438 
4439   verifyFormat(
4440       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4441       "    ccccccccccccccccccccccccc) {\n}");
4442   verifyFormat(
4443       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4444       "    ccccccccccccccccccccccccc) {\n}");
4445 
4446   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4447                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4448                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4449                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4450   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4451                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4452                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4453                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4454 
4455   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4456                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4457                "    aaaaaaaaaaaaaaa != aa) {\n}");
4458   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4459                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4460                "    aaaaaaaaaaaaaaa != aa) {\n}");
4461 }
4462 
4463 TEST_F(FormatTest, BreaksAfterAssignments) {
4464   verifyFormat(
4465       "unsigned Cost =\n"
4466       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4467       "                        SI->getPointerAddressSpaceee());\n");
4468   verifyFormat(
4469       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4470       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4471 
4472   verifyFormat(
4473       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4474       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4475   verifyFormat("unsigned OriginalStartColumn =\n"
4476                "    SourceMgr.getSpellingColumnNumber(\n"
4477                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4478                "    1;");
4479 }
4480 
4481 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4482   FormatStyle Style = getLLVMStyle();
4483   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4484                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4485                Style);
4486 
4487   Style.PenaltyBreakAssignment = 20;
4488   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4489                "                                 cccccccccccccccccccccccccc;",
4490                Style);
4491 }
4492 
4493 TEST_F(FormatTest, AlignsAfterAssignments) {
4494   verifyFormat(
4495       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4496       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4497   verifyFormat(
4498       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4499       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4500   verifyFormat(
4501       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4502       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4503   verifyFormat(
4504       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4505       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4506   verifyFormat(
4507       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4508       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4509       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4510 }
4511 
4512 TEST_F(FormatTest, AlignsAfterReturn) {
4513   verifyFormat(
4514       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4515       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4516   verifyFormat(
4517       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4518       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4519   verifyFormat(
4520       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4521       "       aaaaaaaaaaaaaaaaaaaaaa();");
4522   verifyFormat(
4523       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4524       "        aaaaaaaaaaaaaaaaaaaaaa());");
4525   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4526                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4527   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4528                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4529                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4530   verifyFormat("return\n"
4531                "    // true if code is one of a or b.\n"
4532                "    code == a || code == b;");
4533 }
4534 
4535 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4536   verifyFormat(
4537       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4538       "                                                aaaaaaaaa aaaaaaa) {}");
4539   verifyFormat(
4540       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4541       "                                               aaaaaaaaaaa aaaaaaaaa);");
4542   verifyFormat(
4543       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4544       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4545   FormatStyle Style = getLLVMStyle();
4546   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4547   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4548                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4549                Style);
4550   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4551                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4552                Style);
4553   verifyFormat("SomeLongVariableName->someFunction(\n"
4554                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4555                Style);
4556   verifyFormat(
4557       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4558       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4559       Style);
4560   verifyFormat(
4561       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4562       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4563       Style);
4564   verifyFormat(
4565       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4566       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4567       Style);
4568 
4569   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4570                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4571                "        b));",
4572                Style);
4573 
4574   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4575   Style.BinPackArguments = false;
4576   Style.BinPackParameters = false;
4577   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4578                "    aaaaaaaaaaa aaaaaaaa,\n"
4579                "    aaaaaaaaa aaaaaaa,\n"
4580                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4581                Style);
4582   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4583                "    aaaaaaaaaaa aaaaaaaaa,\n"
4584                "    aaaaaaaaaaa aaaaaaaaa,\n"
4585                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4586                Style);
4587   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4588                "    aaaaaaaaaaaaaaa,\n"
4589                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4590                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4591                Style);
4592   verifyFormat(
4593       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4594       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4595       Style);
4596   verifyFormat(
4597       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4598       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4599       Style);
4600   verifyFormat(
4601       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4602       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4603       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4604       "    aaaaaaaaaaaaaaaa);",
4605       Style);
4606   verifyFormat(
4607       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4608       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4609       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4610       "    aaaaaaaaaaaaaaaa);",
4611       Style);
4612 }
4613 
4614 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4615   FormatStyle Style = getLLVMStyleWithColumns(40);
4616   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4617                "          bbbbbbbbbbbbbbbbbbbbbb);",
4618                Style);
4619   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4620   Style.AlignOperands = false;
4621   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4622                "          bbbbbbbbbbbbbbbbbbbbbb);",
4623                Style);
4624   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4625   Style.AlignOperands = true;
4626   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4627                "          bbbbbbbbbbbbbbbbbbbbbb);",
4628                Style);
4629   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4630   Style.AlignOperands = false;
4631   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4632                "    bbbbbbbbbbbbbbbbbbbbbb);",
4633                Style);
4634 }
4635 
4636 TEST_F(FormatTest, BreaksConditionalExpressions) {
4637   verifyFormat(
4638       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4639       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4640       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4641   verifyFormat(
4642       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4643       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4644       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4645   verifyFormat(
4646       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4647       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4648   verifyFormat(
4649       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4650       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4651       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4652   verifyFormat(
4653       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4654       "                                                    : aaaaaaaaaaaaa);");
4655   verifyFormat(
4656       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4657       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4658       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4659       "                   aaaaaaaaaaaaa);");
4660   verifyFormat(
4661       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4662       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4663       "                   aaaaaaaaaaaaa);");
4664   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4665                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4666                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4667                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4668                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4669   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4670                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4671                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4672                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4673                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4674                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4675                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4676   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4677                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4678                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4679                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4680                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4681   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4682                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4683                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4684   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4685                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4686                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4687                "        : aaaaaaaaaaaaaaaa;");
4688   verifyFormat(
4689       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4690       "    ? aaaaaaaaaaaaaaa\n"
4691       "    : aaaaaaaaaaaaaaa;");
4692   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4693                "          aaaaaaaaa\n"
4694                "      ? b\n"
4695                "      : c);");
4696   verifyFormat("return aaaa == bbbb\n"
4697                "           // comment\n"
4698                "           ? aaaa\n"
4699                "           : bbbb;");
4700   verifyFormat("unsigned Indent =\n"
4701                "    format(TheLine.First,\n"
4702                "           IndentForLevel[TheLine.Level] >= 0\n"
4703                "               ? IndentForLevel[TheLine.Level]\n"
4704                "               : TheLine * 2,\n"
4705                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4706                getLLVMStyleWithColumns(60));
4707   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4708                "                  ? aaaaaaaaaaaaaaa\n"
4709                "                  : bbbbbbbbbbbbbbb //\n"
4710                "                        ? ccccccccccccccc\n"
4711                "                        : ddddddddddddddd;");
4712   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4713                "                  ? aaaaaaaaaaaaaaa\n"
4714                "                  : (bbbbbbbbbbbbbbb //\n"
4715                "                         ? ccccccccccccccc\n"
4716                "                         : ddddddddddddddd);");
4717   verifyFormat(
4718       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4719       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4720       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4721       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4722       "                                      : aaaaaaaaaa;");
4723   verifyFormat(
4724       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4725       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4726       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4727 
4728   FormatStyle NoBinPacking = getLLVMStyle();
4729   NoBinPacking.BinPackArguments = false;
4730   verifyFormat(
4731       "void f() {\n"
4732       "  g(aaa,\n"
4733       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4734       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4735       "        ? aaaaaaaaaaaaaaa\n"
4736       "        : aaaaaaaaaaaaaaa);\n"
4737       "}",
4738       NoBinPacking);
4739   verifyFormat(
4740       "void f() {\n"
4741       "  g(aaa,\n"
4742       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4743       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4744       "        ?: aaaaaaaaaaaaaaa);\n"
4745       "}",
4746       NoBinPacking);
4747 
4748   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4749                "             // comment.\n"
4750                "             ccccccccccccccccccccccccccccccccccccccc\n"
4751                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4752                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4753 
4754   // Assignments in conditional expressions. Apparently not uncommon :-(.
4755   verifyFormat("return a != b\n"
4756                "           // comment\n"
4757                "           ? a = b\n"
4758                "           : a = b;");
4759   verifyFormat("return a != b\n"
4760                "           // comment\n"
4761                "           ? a = a != b\n"
4762                "                     // comment\n"
4763                "                     ? a = b\n"
4764                "                     : a\n"
4765                "           : a;\n");
4766   verifyFormat("return a != b\n"
4767                "           // comment\n"
4768                "           ? a\n"
4769                "           : a = a != b\n"
4770                "                     // comment\n"
4771                "                     ? a = b\n"
4772                "                     : a;");
4773 }
4774 
4775 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4776   FormatStyle Style = getLLVMStyle();
4777   Style.BreakBeforeTernaryOperators = false;
4778   Style.ColumnLimit = 70;
4779   verifyFormat(
4780       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4781       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4782       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4783       Style);
4784   verifyFormat(
4785       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4786       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4787       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4788       Style);
4789   verifyFormat(
4790       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4791       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4792       Style);
4793   verifyFormat(
4794       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4795       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4796       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4797       Style);
4798   verifyFormat(
4799       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4800       "                                                      aaaaaaaaaaaaa);",
4801       Style);
4802   verifyFormat(
4803       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4804       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4805       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4806       "                   aaaaaaaaaaaaa);",
4807       Style);
4808   verifyFormat(
4809       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4810       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4811       "                   aaaaaaaaaaaaa);",
4812       Style);
4813   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4814                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4815                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4816                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4817                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4818                Style);
4819   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4820                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4821                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4822                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4823                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4824                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4825                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4826                Style);
4827   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4828                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4829                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4830                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4831                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4832                Style);
4833   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4834                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4835                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4836                Style);
4837   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4838                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4839                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4840                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4841                Style);
4842   verifyFormat(
4843       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4844       "    aaaaaaaaaaaaaaa :\n"
4845       "    aaaaaaaaaaaaaaa;",
4846       Style);
4847   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4848                "          aaaaaaaaa ?\n"
4849                "      b :\n"
4850                "      c);",
4851                Style);
4852   verifyFormat("unsigned Indent =\n"
4853                "    format(TheLine.First,\n"
4854                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4855                "               IndentForLevel[TheLine.Level] :\n"
4856                "               TheLine * 2,\n"
4857                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4858                Style);
4859   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4860                "                  aaaaaaaaaaaaaaa :\n"
4861                "                  bbbbbbbbbbbbbbb ? //\n"
4862                "                      ccccccccccccccc :\n"
4863                "                      ddddddddddddddd;",
4864                Style);
4865   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4866                "                  aaaaaaaaaaaaaaa :\n"
4867                "                  (bbbbbbbbbbbbbbb ? //\n"
4868                "                       ccccccccccccccc :\n"
4869                "                       ddddddddddddddd);",
4870                Style);
4871   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4872                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4873                "            ccccccccccccccccccccccccccc;",
4874                Style);
4875   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4876                "           aaaaa :\n"
4877                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4878                Style);
4879 }
4880 
4881 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4882   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4883                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4884   verifyFormat("bool a = true, b = false;");
4885 
4886   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4887                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4888                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4889                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4890   verifyFormat(
4891       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4892       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4893       "     d = e && f;");
4894   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4895                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4896   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4897                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4898   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4899                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4900 
4901   FormatStyle Style = getGoogleStyle();
4902   Style.PointerAlignment = FormatStyle::PAS_Left;
4903   Style.DerivePointerAlignment = false;
4904   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4905                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4906                "    *b = bbbbbbbbbbbbbbbbbbb;",
4907                Style);
4908   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4909                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4910                Style);
4911   verifyFormat("vector<int*> a, b;", Style);
4912   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4913 }
4914 
4915 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4916   verifyFormat("arr[foo ? bar : baz];");
4917   verifyFormat("f()[foo ? bar : baz];");
4918   verifyFormat("(a + b)[foo ? bar : baz];");
4919   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4920 }
4921 
4922 TEST_F(FormatTest, AlignsStringLiterals) {
4923   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4924                "                                      \"short literal\");");
4925   verifyFormat(
4926       "looooooooooooooooooooooooongFunction(\n"
4927       "    \"short literal\"\n"
4928       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4929   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4930                "             \" string literals\",\n"
4931                "             and, other, parameters);");
4932   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4933             "      \"5678\";",
4934             format("fun + \"1243\" /* comment */\n"
4935                    "    \"5678\";",
4936                    getLLVMStyleWithColumns(28)));
4937   EXPECT_EQ(
4938       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4939       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4940       "         \"aaaaaaaaaaaaaaaa\";",
4941       format("aaaaaa ="
4942              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4943              "aaaaaaaaaaaaaaaaaaaaa\" "
4944              "\"aaaaaaaaaaaaaaaa\";"));
4945   verifyFormat("a = a + \"a\"\n"
4946                "        \"a\"\n"
4947                "        \"a\";");
4948   verifyFormat("f(\"a\", \"b\"\n"
4949                "       \"c\");");
4950 
4951   verifyFormat(
4952       "#define LL_FORMAT \"ll\"\n"
4953       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4954       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4955 
4956   verifyFormat("#define A(X)          \\\n"
4957                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4958                "  \"ccccc\"",
4959                getLLVMStyleWithColumns(23));
4960   verifyFormat("#define A \"def\"\n"
4961                "f(\"abc\" A \"ghi\"\n"
4962                "  \"jkl\");");
4963 
4964   verifyFormat("f(L\"a\"\n"
4965                "  L\"b\");");
4966   verifyFormat("#define A(X)            \\\n"
4967                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4968                "  L\"ccccc\"",
4969                getLLVMStyleWithColumns(25));
4970 
4971   verifyFormat("f(@\"a\"\n"
4972                "  @\"b\");");
4973   verifyFormat("NSString s = @\"a\"\n"
4974                "             @\"b\"\n"
4975                "             @\"c\";");
4976   verifyFormat("NSString s = @\"a\"\n"
4977                "              \"b\"\n"
4978                "              \"c\";");
4979 }
4980 
4981 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4982   FormatStyle Style = getLLVMStyle();
4983   // No declarations or definitions should be moved to own line.
4984   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4985   verifyFormat("class A {\n"
4986                "  int f() { return 1; }\n"
4987                "  int g();\n"
4988                "};\n"
4989                "int f() { return 1; }\n"
4990                "int g();\n",
4991                Style);
4992 
4993   // All declarations and definitions should have the return type moved to its
4994   // own
4995   // line.
4996   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4997   verifyFormat("class E {\n"
4998                "  int\n"
4999                "  f() {\n"
5000                "    return 1;\n"
5001                "  }\n"
5002                "  int\n"
5003                "  g();\n"
5004                "};\n"
5005                "int\n"
5006                "f() {\n"
5007                "  return 1;\n"
5008                "}\n"
5009                "int\n"
5010                "g();\n",
5011                Style);
5012 
5013   // Top-level definitions, and no kinds of declarations should have the
5014   // return type moved to its own line.
5015   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5016   verifyFormat("class B {\n"
5017                "  int f() { return 1; }\n"
5018                "  int g();\n"
5019                "};\n"
5020                "int\n"
5021                "f() {\n"
5022                "  return 1;\n"
5023                "}\n"
5024                "int g();\n",
5025                Style);
5026 
5027   // Top-level definitions and declarations should have the return type moved
5028   // to its own line.
5029   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5030   verifyFormat("class C {\n"
5031                "  int f() { return 1; }\n"
5032                "  int g();\n"
5033                "};\n"
5034                "int\n"
5035                "f() {\n"
5036                "  return 1;\n"
5037                "}\n"
5038                "int\n"
5039                "g();\n",
5040                Style);
5041 
5042   // All definitions should have the return type moved to its own line, but no
5043   // kinds of declarations.
5044   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5045   verifyFormat("class D {\n"
5046                "  int\n"
5047                "  f() {\n"
5048                "    return 1;\n"
5049                "  }\n"
5050                "  int g();\n"
5051                "};\n"
5052                "int\n"
5053                "f() {\n"
5054                "  return 1;\n"
5055                "}\n"
5056                "int g();\n",
5057                Style);
5058   verifyFormat("const char *\n"
5059                "f(void) {\n" // Break here.
5060                "  return \"\";\n"
5061                "}\n"
5062                "const char *bar(void);\n", // No break here.
5063                Style);
5064   verifyFormat("template <class T>\n"
5065                "T *\n"
5066                "f(T &c) {\n" // Break here.
5067                "  return NULL;\n"
5068                "}\n"
5069                "template <class T> T *f(T &c);\n", // No break here.
5070                Style);
5071   verifyFormat("class C {\n"
5072                "  int\n"
5073                "  operator+() {\n"
5074                "    return 1;\n"
5075                "  }\n"
5076                "  int\n"
5077                "  operator()() {\n"
5078                "    return 1;\n"
5079                "  }\n"
5080                "};\n",
5081                Style);
5082   verifyFormat("void\n"
5083                "A::operator()() {}\n"
5084                "void\n"
5085                "A::operator>>() {}\n"
5086                "void\n"
5087                "A::operator+() {}\n",
5088                Style);
5089   verifyFormat("void *operator new(std::size_t s);", // No break here.
5090                Style);
5091   verifyFormat("void *\n"
5092                "operator new(std::size_t s) {}",
5093                Style);
5094   verifyFormat("void *\n"
5095                "operator delete[](void *ptr) {}",
5096                Style);
5097   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5098   verifyFormat("const char *\n"
5099                "f(void)\n" // Break here.
5100                "{\n"
5101                "  return \"\";\n"
5102                "}\n"
5103                "const char *bar(void);\n", // No break here.
5104                Style);
5105   verifyFormat("template <class T>\n"
5106                "T *\n"     // Problem here: no line break
5107                "f(T &c)\n" // Break here.
5108                "{\n"
5109                "  return NULL;\n"
5110                "}\n"
5111                "template <class T> T *f(T &c);\n", // No break here.
5112                Style);
5113 }
5114 
5115 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5116   FormatStyle NoBreak = getLLVMStyle();
5117   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5118   FormatStyle Break = getLLVMStyle();
5119   Break.AlwaysBreakBeforeMultilineStrings = true;
5120   verifyFormat("aaaa = \"bbbb\"\n"
5121                "       \"cccc\";",
5122                NoBreak);
5123   verifyFormat("aaaa =\n"
5124                "    \"bbbb\"\n"
5125                "    \"cccc\";",
5126                Break);
5127   verifyFormat("aaaa(\"bbbb\"\n"
5128                "     \"cccc\");",
5129                NoBreak);
5130   verifyFormat("aaaa(\n"
5131                "    \"bbbb\"\n"
5132                "    \"cccc\");",
5133                Break);
5134   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5135                "          \"cccc\");",
5136                NoBreak);
5137   verifyFormat("aaaa(qqq,\n"
5138                "     \"bbbb\"\n"
5139                "     \"cccc\");",
5140                Break);
5141   verifyFormat("aaaa(qqq,\n"
5142                "     L\"bbbb\"\n"
5143                "     L\"cccc\");",
5144                Break);
5145   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5146                "                      \"bbbb\"));",
5147                Break);
5148   verifyFormat("string s = someFunction(\n"
5149                "    \"abc\"\n"
5150                "    \"abc\");",
5151                Break);
5152 
5153   // As we break before unary operators, breaking right after them is bad.
5154   verifyFormat("string foo = abc ? \"x\"\n"
5155                "                   \"blah blah blah blah blah blah\"\n"
5156                "                 : \"y\";",
5157                Break);
5158 
5159   // Don't break if there is no column gain.
5160   verifyFormat("f(\"aaaa\"\n"
5161                "  \"bbbb\");",
5162                Break);
5163 
5164   // Treat literals with escaped newlines like multi-line string literals.
5165   EXPECT_EQ("x = \"a\\\n"
5166             "b\\\n"
5167             "c\";",
5168             format("x = \"a\\\n"
5169                    "b\\\n"
5170                    "c\";",
5171                    NoBreak));
5172   EXPECT_EQ("xxxx =\n"
5173             "    \"a\\\n"
5174             "b\\\n"
5175             "c\";",
5176             format("xxxx = \"a\\\n"
5177                    "b\\\n"
5178                    "c\";",
5179                    Break));
5180 
5181   EXPECT_EQ("NSString *const kString =\n"
5182             "    @\"aaaa\"\n"
5183             "    @\"bbbb\";",
5184             format("NSString *const kString = @\"aaaa\"\n"
5185                    "@\"bbbb\";",
5186                    Break));
5187 
5188   Break.ColumnLimit = 0;
5189   verifyFormat("const char *hello = \"hello llvm\";", Break);
5190 }
5191 
5192 TEST_F(FormatTest, AlignsPipes) {
5193   verifyFormat(
5194       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5195       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5196       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5197   verifyFormat(
5198       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5199       "                     << aaaaaaaaaaaaaaaaaaaa;");
5200   verifyFormat(
5201       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5202       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5203   verifyFormat(
5204       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5205       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5206   verifyFormat(
5207       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5208       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5209       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5210   verifyFormat(
5211       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5212       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5213       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5214   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5215                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5216                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5217                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5218   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5219                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5220   verifyFormat(
5221       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5222       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5223   verifyFormat(
5224       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5225       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5226 
5227   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5228                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5229   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5230                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5231                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5232                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5233   verifyFormat("LOG_IF(aaa == //\n"
5234                "       bbb)\n"
5235                "    << a << b;");
5236 
5237   // But sometimes, breaking before the first "<<" is desirable.
5238   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5239                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5240   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5241                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5242                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5243   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5244                "    << BEF << IsTemplate << Description << E->getType();");
5245   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5246                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5247                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5248   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5249                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5250                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5251                "    << aaa;");
5252 
5253   verifyFormat(
5254       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5255       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5256 
5257   // Incomplete string literal.
5258   EXPECT_EQ("llvm::errs() << \"\n"
5259             "             << a;",
5260             format("llvm::errs() << \"\n<<a;"));
5261 
5262   verifyFormat("void f() {\n"
5263                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5264                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5265                "}");
5266 
5267   // Handle 'endl'.
5268   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5269                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5270   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5271 
5272   // Handle '\n'.
5273   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5274                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5275   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5276                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5277   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5278                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5279   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5280 }
5281 
5282 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5283   verifyFormat("return out << \"somepacket = {\\n\"\n"
5284                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5285                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5286                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5287                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5288                "           << \"}\";");
5289 
5290   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5291                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5292                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5293   verifyFormat(
5294       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5295       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5296       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5297       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5298       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5299   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5300                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5301   verifyFormat(
5302       "void f() {\n"
5303       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5304       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5305       "}");
5306 
5307   // Breaking before the first "<<" is generally not desirable.
5308   verifyFormat(
5309       "llvm::errs()\n"
5310       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5311       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5312       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5313       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5314       getLLVMStyleWithColumns(70));
5315   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5316                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5317                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5318                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5319                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5320                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5321                getLLVMStyleWithColumns(70));
5322 
5323   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5324                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5325                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5326   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5327                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5328                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5329   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5330                "           (aaaa + aaaa);",
5331                getLLVMStyleWithColumns(40));
5332   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5333                "                  (aaaaaaa + aaaaa));",
5334                getLLVMStyleWithColumns(40));
5335   verifyFormat(
5336       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5337       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5338       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5339 }
5340 
5341 TEST_F(FormatTest, UnderstandsEquals) {
5342   verifyFormat(
5343       "aaaaaaaaaaaaaaaaa =\n"
5344       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5345   verifyFormat(
5346       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5347       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5348   verifyFormat(
5349       "if (a) {\n"
5350       "  f();\n"
5351       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5352       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5353       "}");
5354 
5355   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5356                "        100000000 + 10000000) {\n}");
5357 }
5358 
5359 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5360   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5361                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5362 
5363   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5364                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5365 
5366   verifyFormat(
5367       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5368       "                                                          Parameter2);");
5369 
5370   verifyFormat(
5371       "ShortObject->shortFunction(\n"
5372       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5373       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5374 
5375   verifyFormat("loooooooooooooongFunction(\n"
5376                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5377 
5378   verifyFormat(
5379       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5380       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5381 
5382   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5383                "    .WillRepeatedly(Return(SomeValue));");
5384   verifyFormat("void f() {\n"
5385                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5386                "      .Times(2)\n"
5387                "      .WillRepeatedly(Return(SomeValue));\n"
5388                "}");
5389   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5390                "    ccccccccccccccccccccccc);");
5391   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5392                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5393                "          .aaaaa(aaaaa),\n"
5394                "      aaaaaaaaaaaaaaaaaaaaa);");
5395   verifyFormat("void f() {\n"
5396                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5397                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5398                "}");
5399   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5400                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5401                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5402                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5403                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5404   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5405                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5406                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5407                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5408                "}");
5409 
5410   // Here, it is not necessary to wrap at "." or "->".
5411   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5412                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5413   verifyFormat(
5414       "aaaaaaaaaaa->aaaaaaaaa(\n"
5415       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5416       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5417 
5418   verifyFormat(
5419       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5420       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5421   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5422                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5423   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5424                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5425 
5426   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5427                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5428                "    .a();");
5429 
5430   FormatStyle NoBinPacking = getLLVMStyle();
5431   NoBinPacking.BinPackParameters = false;
5432   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5433                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5434                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5435                "                         aaaaaaaaaaaaaaaaaaa,\n"
5436                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5437                NoBinPacking);
5438 
5439   // If there is a subsequent call, change to hanging indentation.
5440   verifyFormat(
5441       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5442       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5443       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5444   verifyFormat(
5445       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5446       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5447   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5448                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5449                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5450   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5451                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5452                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5453 }
5454 
5455 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5456   verifyFormat("template <typename T>\n"
5457                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5458   verifyFormat("template <typename T>\n"
5459                "// T should be one of {A, B}.\n"
5460                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5461   verifyFormat(
5462       "template <typename T>\n"
5463       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5464   verifyFormat("template <typename T>\n"
5465                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5466                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5467   verifyFormat(
5468       "template <typename T>\n"
5469       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5470       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5471   verifyFormat(
5472       "template <typename T>\n"
5473       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5474       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5475       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5476   verifyFormat("template <typename T>\n"
5477                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5478                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5479   verifyFormat(
5480       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5481       "          typename T4 = char>\n"
5482       "void f();");
5483   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5484                "          template <typename> class cccccccccccccccccccccc,\n"
5485                "          typename ddddddddddddd>\n"
5486                "class C {};");
5487   verifyFormat(
5488       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5489       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5490 
5491   verifyFormat("void f() {\n"
5492                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5493                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5494                "}");
5495 
5496   verifyFormat("template <typename T> class C {};");
5497   verifyFormat("template <typename T> void f();");
5498   verifyFormat("template <typename T> void f() {}");
5499   verifyFormat(
5500       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5501       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5502       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5503       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5504       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5505       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5506       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5507       getLLVMStyleWithColumns(72));
5508   EXPECT_EQ("static_cast<A< //\n"
5509             "    B> *>(\n"
5510             "\n"
5511             ");",
5512             format("static_cast<A<//\n"
5513                    "    B>*>(\n"
5514                    "\n"
5515                    "    );"));
5516   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5517                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5518 
5519   FormatStyle AlwaysBreak = getLLVMStyle();
5520   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
5521   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5522   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5523   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5524   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5525                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5526                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5527   verifyFormat("template <template <typename> class Fooooooo,\n"
5528                "          template <typename> class Baaaaaaar>\n"
5529                "struct C {};",
5530                AlwaysBreak);
5531   verifyFormat("template <typename T> // T can be A, B or C.\n"
5532                "struct C {};",
5533                AlwaysBreak);
5534   verifyFormat("template <enum E> class A {\n"
5535                "public:\n"
5536                "  E *f();\n"
5537                "};");
5538 
5539   FormatStyle NeverBreak = getLLVMStyle();
5540   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
5541   verifyFormat("template <typename T> class C {};", NeverBreak);
5542   verifyFormat("template <typename T> void f();", NeverBreak);
5543   verifyFormat("template <typename T> void f() {}", NeverBreak);
5544   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5545                NeverBreak);
5546   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5547                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5548                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
5549                NeverBreak);
5550   verifyFormat("template <template <typename> class Fooooooo,\n"
5551                "          template <typename> class Baaaaaaar>\n"
5552                "struct C {};",
5553                NeverBreak);
5554   verifyFormat("template <typename T> // T can be A, B or C.\n"
5555                "struct C {};",
5556                NeverBreak);
5557   verifyFormat("template <enum E> class A {\n"
5558                "public:\n"
5559                "  E *f();\n"
5560                "};", NeverBreak);
5561   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
5562   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5563                NeverBreak);
5564 }
5565 
5566 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
5567   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
5568   Style.ColumnLimit = 60;
5569   EXPECT_EQ("// Baseline - no comments.\n"
5570             "template <\n"
5571             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5572             "void f() {}",
5573             format("// Baseline - no comments.\n"
5574                    "template <\n"
5575                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5576                    "void f() {}",
5577                    Style));
5578 
5579   EXPECT_EQ("template <\n"
5580             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5581             "void f() {}",
5582             format("template <\n"
5583                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5584                    "void f() {}",
5585                    Style));
5586 
5587   EXPECT_EQ(
5588       "template <\n"
5589       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
5590       "void f() {}",
5591       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
5592              "void f() {}",
5593              Style));
5594 
5595   EXPECT_EQ(
5596       "template <\n"
5597       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5598       "                                               // multiline\n"
5599       "void f() {}",
5600       format("template <\n"
5601              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5602              "                                              // multiline\n"
5603              "void f() {}",
5604              Style));
5605 
5606   EXPECT_EQ(
5607       "template <typename aaaaaaaaaa<\n"
5608       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
5609       "void f() {}",
5610       format(
5611           "template <\n"
5612           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
5613           "void f() {}",
5614           Style));
5615 }
5616 
5617 TEST_F(FormatTest, WrapsTemplateParameters) {
5618   FormatStyle Style = getLLVMStyle();
5619   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5620   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5621   verifyFormat(
5622       "template <typename... a> struct q {};\n"
5623       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5624       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5625       "    y;",
5626       Style);
5627   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5628   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5629   verifyFormat(
5630       "template <typename... a> struct r {};\n"
5631       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5632       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5633       "    y;",
5634       Style);
5635   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5636   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5637   verifyFormat(
5638       "template <typename... a> struct s {};\n"
5639       "extern s<\n"
5640       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5641       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5642       "    y;",
5643       Style);
5644   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5645   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5646   verifyFormat(
5647       "template <typename... a> struct t {};\n"
5648       "extern t<\n"
5649       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5650       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5651       "    y;",
5652       Style);
5653 }
5654 
5655 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5656   verifyFormat(
5657       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5658       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5659   verifyFormat(
5660       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5661       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5662       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5663 
5664   // FIXME: Should we have the extra indent after the second break?
5665   verifyFormat(
5666       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5667       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5668       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5669 
5670   verifyFormat(
5671       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5672       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5673 
5674   // Breaking at nested name specifiers is generally not desirable.
5675   verifyFormat(
5676       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5677       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5678 
5679   verifyFormat(
5680       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5681       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5682       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5683       "                   aaaaaaaaaaaaaaaaaaaaa);",
5684       getLLVMStyleWithColumns(74));
5685 
5686   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5687                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5688                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5689 }
5690 
5691 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5692   verifyFormat("A<int> a;");
5693   verifyFormat("A<A<A<int>>> a;");
5694   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5695   verifyFormat("bool x = a < 1 || 2 > a;");
5696   verifyFormat("bool x = 5 < f<int>();");
5697   verifyFormat("bool x = f<int>() > 5;");
5698   verifyFormat("bool x = 5 < a<int>::x;");
5699   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5700   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5701 
5702   verifyGoogleFormat("A<A<int>> a;");
5703   verifyGoogleFormat("A<A<A<int>>> a;");
5704   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5705   verifyGoogleFormat("A<A<int> > a;");
5706   verifyGoogleFormat("A<A<A<int> > > a;");
5707   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5708   verifyGoogleFormat("A<::A<int>> a;");
5709   verifyGoogleFormat("A<::A> a;");
5710   verifyGoogleFormat("A< ::A> a;");
5711   verifyGoogleFormat("A< ::A<int> > a;");
5712   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5713   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5714   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5715   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5716   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5717             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5718 
5719   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5720 
5721   verifyFormat("test >> a >> b;");
5722   verifyFormat("test << a >> b;");
5723 
5724   verifyFormat("f<int>();");
5725   verifyFormat("template <typename T> void f() {}");
5726   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5727   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5728                "sizeof(char)>::type>;");
5729   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5730   verifyFormat("f(a.operator()<A>());");
5731   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5732                "      .template operator()<A>());",
5733                getLLVMStyleWithColumns(35));
5734 
5735   // Not template parameters.
5736   verifyFormat("return a < b && c > d;");
5737   verifyFormat("void f() {\n"
5738                "  while (a < b && c > d) {\n"
5739                "  }\n"
5740                "}");
5741   verifyFormat("template <typename... Types>\n"
5742                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5743 
5744   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5745                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5746                getLLVMStyleWithColumns(60));
5747   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5748   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5749   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5750 }
5751 
5752 TEST_F(FormatTest, BitshiftOperatorWidth) {
5753   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5754             "                   bar */",
5755             format("int    a=1<<2;  /* foo\n"
5756                    "                   bar */"));
5757 
5758   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5759             "                     bar */",
5760             format("int  b  =256>>1 ;  /* foo\n"
5761                    "                      bar */"));
5762 }
5763 
5764 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5765   verifyFormat("COMPARE(a, ==, b);");
5766   verifyFormat("auto s = sizeof...(Ts) - 1;");
5767 }
5768 
5769 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5770   verifyFormat("int A::*x;");
5771   verifyFormat("int (S::*func)(void *);");
5772   verifyFormat("void f() { int (S::*func)(void *); }");
5773   verifyFormat("typedef bool *(Class::*Member)() const;");
5774   verifyFormat("void f() {\n"
5775                "  (a->*f)();\n"
5776                "  a->*x;\n"
5777                "  (a.*f)();\n"
5778                "  ((*a).*f)();\n"
5779                "  a.*x;\n"
5780                "}");
5781   verifyFormat("void f() {\n"
5782                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5783                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5784                "}");
5785   verifyFormat(
5786       "(aaaaaaaaaa->*bbbbbbb)(\n"
5787       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5788   FormatStyle Style = getLLVMStyle();
5789   Style.PointerAlignment = FormatStyle::PAS_Left;
5790   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5791 }
5792 
5793 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5794   verifyFormat("int a = -2;");
5795   verifyFormat("f(-1, -2, -3);");
5796   verifyFormat("a[-1] = 5;");
5797   verifyFormat("int a = 5 + -2;");
5798   verifyFormat("if (i == -1) {\n}");
5799   verifyFormat("if (i != -1) {\n}");
5800   verifyFormat("if (i > -1) {\n}");
5801   verifyFormat("if (i < -1) {\n}");
5802   verifyFormat("++(a->f());");
5803   verifyFormat("--(a->f());");
5804   verifyFormat("(a->f())++;");
5805   verifyFormat("a[42]++;");
5806   verifyFormat("if (!(a->f())) {\n}");
5807   verifyFormat("if (!+i) {\n}");
5808   verifyFormat("~&a;");
5809 
5810   verifyFormat("a-- > b;");
5811   verifyFormat("b ? -a : c;");
5812   verifyFormat("n * sizeof char16;");
5813   verifyFormat("n * alignof char16;", getGoogleStyle());
5814   verifyFormat("sizeof(char);");
5815   verifyFormat("alignof(char);", getGoogleStyle());
5816 
5817   verifyFormat("return -1;");
5818   verifyFormat("switch (a) {\n"
5819                "case -1:\n"
5820                "  break;\n"
5821                "}");
5822   verifyFormat("#define X -1");
5823   verifyFormat("#define X -kConstant");
5824 
5825   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5826   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5827 
5828   verifyFormat("int a = /* confusing comment */ -1;");
5829   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5830   verifyFormat("int a = i /* confusing comment */++;");
5831 }
5832 
5833 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5834   verifyFormat("if (!aaaaaaaaaa( // break\n"
5835                "        aaaaa)) {\n"
5836                "}");
5837   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5838                "    aaaaa));");
5839   verifyFormat("*aaa = aaaaaaa( // break\n"
5840                "    bbbbbb);");
5841 }
5842 
5843 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5844   verifyFormat("bool operator<();");
5845   verifyFormat("bool operator>();");
5846   verifyFormat("bool operator=();");
5847   verifyFormat("bool operator==();");
5848   verifyFormat("bool operator!=();");
5849   verifyFormat("int operator+();");
5850   verifyFormat("int operator++();");
5851   verifyFormat("int operator++(int) volatile noexcept;");
5852   verifyFormat("bool operator,();");
5853   verifyFormat("bool operator();");
5854   verifyFormat("bool operator()();");
5855   verifyFormat("bool operator[]();");
5856   verifyFormat("operator bool();");
5857   verifyFormat("operator int();");
5858   verifyFormat("operator void *();");
5859   verifyFormat("operator SomeType<int>();");
5860   verifyFormat("operator SomeType<int, int>();");
5861   verifyFormat("operator SomeType<SomeType<int>>();");
5862   verifyFormat("void *operator new(std::size_t size);");
5863   verifyFormat("void *operator new[](std::size_t size);");
5864   verifyFormat("void operator delete(void *ptr);");
5865   verifyFormat("void operator delete[](void *ptr);");
5866   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5867                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5868   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5869                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5870 
5871   verifyFormat(
5872       "ostream &operator<<(ostream &OutputStream,\n"
5873       "                    SomeReallyLongType WithSomeReallyLongValue);");
5874   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5875                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5876                "  return left.group < right.group;\n"
5877                "}");
5878   verifyFormat("SomeType &operator=(const SomeType &S);");
5879   verifyFormat("f.template operator()<int>();");
5880 
5881   verifyGoogleFormat("operator void*();");
5882   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5883   verifyGoogleFormat("operator ::A();");
5884 
5885   verifyFormat("using A::operator+;");
5886   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5887                "int i;");
5888 }
5889 
5890 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5891   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5892   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5893   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5894   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5895   verifyFormat("Deleted &operator=(const Deleted &) &;");
5896   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5897   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5898   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5899   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5900   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5901   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5902   verifyFormat("void Fn(T const &) const &;");
5903   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5904   verifyFormat("template <typename T>\n"
5905                "void F(T) && = delete;",
5906                getGoogleStyle());
5907 
5908   FormatStyle AlignLeft = getLLVMStyle();
5909   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5910   verifyFormat("void A::b() && {}", AlignLeft);
5911   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5912   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5913                AlignLeft);
5914   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5915   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5916   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5917   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5918   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5919   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5920   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5921   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5922 
5923   FormatStyle Spaces = getLLVMStyle();
5924   Spaces.SpacesInCStyleCastParentheses = true;
5925   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5926   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5927   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5928   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5929 
5930   Spaces.SpacesInCStyleCastParentheses = false;
5931   Spaces.SpacesInParentheses = true;
5932   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5933   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5934   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5935   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5936 }
5937 
5938 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5939   verifyFormat("void f() {\n"
5940                "  A *a = new A;\n"
5941                "  A *a = new (placement) A;\n"
5942                "  delete a;\n"
5943                "  delete (A *)a;\n"
5944                "}");
5945   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5946                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5947   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5948                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5949                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5950   verifyFormat("delete[] h->p;");
5951 }
5952 
5953 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5954   verifyFormat("int *f(int *a) {}");
5955   verifyFormat("int main(int argc, char **argv) {}");
5956   verifyFormat("Test::Test(int b) : a(b * b) {}");
5957   verifyIndependentOfContext("f(a, *a);");
5958   verifyFormat("void g() { f(*a); }");
5959   verifyIndependentOfContext("int a = b * 10;");
5960   verifyIndependentOfContext("int a = 10 * b;");
5961   verifyIndependentOfContext("int a = b * c;");
5962   verifyIndependentOfContext("int a += b * c;");
5963   verifyIndependentOfContext("int a -= b * c;");
5964   verifyIndependentOfContext("int a *= b * c;");
5965   verifyIndependentOfContext("int a /= b * c;");
5966   verifyIndependentOfContext("int a = *b;");
5967   verifyIndependentOfContext("int a = *b * c;");
5968   verifyIndependentOfContext("int a = b * *c;");
5969   verifyIndependentOfContext("int a = b * (10);");
5970   verifyIndependentOfContext("S << b * (10);");
5971   verifyIndependentOfContext("return 10 * b;");
5972   verifyIndependentOfContext("return *b * *c;");
5973   verifyIndependentOfContext("return a & ~b;");
5974   verifyIndependentOfContext("f(b ? *c : *d);");
5975   verifyIndependentOfContext("int a = b ? *c : *d;");
5976   verifyIndependentOfContext("*b = a;");
5977   verifyIndependentOfContext("a * ~b;");
5978   verifyIndependentOfContext("a * !b;");
5979   verifyIndependentOfContext("a * +b;");
5980   verifyIndependentOfContext("a * -b;");
5981   verifyIndependentOfContext("a * ++b;");
5982   verifyIndependentOfContext("a * --b;");
5983   verifyIndependentOfContext("a[4] * b;");
5984   verifyIndependentOfContext("a[a * a] = 1;");
5985   verifyIndependentOfContext("f() * b;");
5986   verifyIndependentOfContext("a * [self dostuff];");
5987   verifyIndependentOfContext("int x = a * (a + b);");
5988   verifyIndependentOfContext("(a *)(a + b);");
5989   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5990   verifyIndependentOfContext("int *pa = (int *)&a;");
5991   verifyIndependentOfContext("return sizeof(int **);");
5992   verifyIndependentOfContext("return sizeof(int ******);");
5993   verifyIndependentOfContext("return (int **&)a;");
5994   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5995   verifyFormat("void f(Type (*parameter)[10]) {}");
5996   verifyFormat("void f(Type (&parameter)[10]) {}");
5997   verifyGoogleFormat("return sizeof(int**);");
5998   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5999   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6000   verifyFormat("auto a = [](int **&, int ***) {};");
6001   verifyFormat("auto PointerBinding = [](const char *S) {};");
6002   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6003   verifyFormat("[](const decltype(*a) &value) {}");
6004   verifyFormat("decltype(a * b) F();");
6005   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6006   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6007   verifyIndependentOfContext("typedef void (*f)(int *a);");
6008   verifyIndependentOfContext("int i{a * b};");
6009   verifyIndependentOfContext("aaa && aaa->f();");
6010   verifyIndependentOfContext("int x = ~*p;");
6011   verifyFormat("Constructor() : a(a), area(width * height) {}");
6012   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6013   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6014   verifyFormat("void f() { f(a, c * d); }");
6015   verifyFormat("void f() { f(new a(), c * d); }");
6016   verifyFormat("void f(const MyOverride &override);");
6017   verifyFormat("void f(const MyFinal &final);");
6018   verifyIndependentOfContext("bool a = f() && override.f();");
6019   verifyIndependentOfContext("bool a = f() && final.f();");
6020 
6021   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6022 
6023   verifyIndependentOfContext("A<int *> a;");
6024   verifyIndependentOfContext("A<int **> a;");
6025   verifyIndependentOfContext("A<int *, int *> a;");
6026   verifyIndependentOfContext("A<int *[]> a;");
6027   verifyIndependentOfContext(
6028       "const char *const p = reinterpret_cast<const char *const>(q);");
6029   verifyIndependentOfContext("A<int **, int **> a;");
6030   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6031   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6032   verifyFormat("for (; a && b;) {\n}");
6033   verifyFormat("bool foo = true && [] { return false; }();");
6034 
6035   verifyFormat(
6036       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6037       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6038 
6039   verifyGoogleFormat("int const* a = &b;");
6040   verifyGoogleFormat("**outparam = 1;");
6041   verifyGoogleFormat("*outparam = a * b;");
6042   verifyGoogleFormat("int main(int argc, char** argv) {}");
6043   verifyGoogleFormat("A<int*> a;");
6044   verifyGoogleFormat("A<int**> a;");
6045   verifyGoogleFormat("A<int*, int*> a;");
6046   verifyGoogleFormat("A<int**, int**> a;");
6047   verifyGoogleFormat("f(b ? *c : *d);");
6048   verifyGoogleFormat("int a = b ? *c : *d;");
6049   verifyGoogleFormat("Type* t = **x;");
6050   verifyGoogleFormat("Type* t = *++*x;");
6051   verifyGoogleFormat("*++*x;");
6052   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6053   verifyGoogleFormat("Type* t = x++ * y;");
6054   verifyGoogleFormat(
6055       "const char* const p = reinterpret_cast<const char* const>(q);");
6056   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6057   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6058   verifyGoogleFormat("template <typename T>\n"
6059                      "void f(int i = 0, SomeType** temps = NULL);");
6060 
6061   FormatStyle Left = getLLVMStyle();
6062   Left.PointerAlignment = FormatStyle::PAS_Left;
6063   verifyFormat("x = *a(x) = *a(y);", Left);
6064   verifyFormat("for (;; *a = b) {\n}", Left);
6065   verifyFormat("return *this += 1;", Left);
6066   verifyFormat("throw *x;", Left);
6067   verifyFormat("delete *x;", Left);
6068   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6069   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6070   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6071 
6072   verifyIndependentOfContext("a = *(x + y);");
6073   verifyIndependentOfContext("a = &(x + y);");
6074   verifyIndependentOfContext("*(x + y).call();");
6075   verifyIndependentOfContext("&(x + y)->call();");
6076   verifyFormat("void f() { &(*I).first; }");
6077 
6078   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6079   verifyFormat(
6080       "int *MyValues = {\n"
6081       "    *A, // Operator detection might be confused by the '{'\n"
6082       "    *BB // Operator detection might be confused by previous comment\n"
6083       "};");
6084 
6085   verifyIndependentOfContext("if (int *a = &b)");
6086   verifyIndependentOfContext("if (int &a = *b)");
6087   verifyIndependentOfContext("if (a & b[i])");
6088   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6089   verifyIndependentOfContext("if (*b[i])");
6090   verifyIndependentOfContext("if (int *a = (&b))");
6091   verifyIndependentOfContext("while (int *a = &b)");
6092   verifyIndependentOfContext("size = sizeof *a;");
6093   verifyIndependentOfContext("if (a && (b = c))");
6094   verifyFormat("void f() {\n"
6095                "  for (const int &v : Values) {\n"
6096                "  }\n"
6097                "}");
6098   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6099   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6100   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6101 
6102   verifyFormat("#define A (!a * b)");
6103   verifyFormat("#define MACRO     \\\n"
6104                "  int *i = a * b; \\\n"
6105                "  void f(a *b);",
6106                getLLVMStyleWithColumns(19));
6107 
6108   verifyIndependentOfContext("A = new SomeType *[Length];");
6109   verifyIndependentOfContext("A = new SomeType *[Length]();");
6110   verifyIndependentOfContext("T **t = new T *;");
6111   verifyIndependentOfContext("T **t = new T *();");
6112   verifyGoogleFormat("A = new SomeType*[Length]();");
6113   verifyGoogleFormat("A = new SomeType*[Length];");
6114   verifyGoogleFormat("T** t = new T*;");
6115   verifyGoogleFormat("T** t = new T*();");
6116 
6117   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6118   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6119   verifyFormat("template <bool a, bool b> "
6120                "typename t::if<x && y>::type f() {}");
6121   verifyFormat("template <int *y> f() {}");
6122   verifyFormat("vector<int *> v;");
6123   verifyFormat("vector<int *const> v;");
6124   verifyFormat("vector<int *const **const *> v;");
6125   verifyFormat("vector<int *volatile> v;");
6126   verifyFormat("vector<a * b> v;");
6127   verifyFormat("foo<b && false>();");
6128   verifyFormat("foo<b & 1>();");
6129   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6130   verifyFormat(
6131       "template <class T, class = typename std::enable_if<\n"
6132       "                       std::is_integral<T>::value &&\n"
6133       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6134       "void F();",
6135       getLLVMStyleWithColumns(70));
6136   verifyFormat(
6137       "template <class T,\n"
6138       "          class = typename std::enable_if<\n"
6139       "              std::is_integral<T>::value &&\n"
6140       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6141       "          class U>\n"
6142       "void F();",
6143       getLLVMStyleWithColumns(70));
6144   verifyFormat(
6145       "template <class T,\n"
6146       "          class = typename ::std::enable_if<\n"
6147       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6148       "void F();",
6149       getGoogleStyleWithColumns(68));
6150 
6151   verifyIndependentOfContext("MACRO(int *i);");
6152   verifyIndependentOfContext("MACRO(auto *a);");
6153   verifyIndependentOfContext("MACRO(const A *a);");
6154   verifyIndependentOfContext("MACRO(A *const a);");
6155   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6156   verifyFormat("void f() { f(float{1}, a * a); }");
6157   // FIXME: Is there a way to make this work?
6158   // verifyIndependentOfContext("MACRO(A *a);");
6159 
6160   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6161   verifyFormat("return options != nullptr && operator==(*options);");
6162 
6163   EXPECT_EQ("#define OP(x)                                    \\\n"
6164             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6165             "    return s << a.DebugString();                 \\\n"
6166             "  }",
6167             format("#define OP(x) \\\n"
6168                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6169                    "    return s << a.DebugString(); \\\n"
6170                    "  }",
6171                    getLLVMStyleWithColumns(50)));
6172 
6173   // FIXME: We cannot handle this case yet; we might be able to figure out that
6174   // foo<x> d > v; doesn't make sense.
6175   verifyFormat("foo<a<b && c> d> v;");
6176 
6177   FormatStyle PointerMiddle = getLLVMStyle();
6178   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6179   verifyFormat("delete *x;", PointerMiddle);
6180   verifyFormat("int * x;", PointerMiddle);
6181   verifyFormat("int *[] x;", PointerMiddle);
6182   verifyFormat("template <int * y> f() {}", PointerMiddle);
6183   verifyFormat("int * f(int * a) {}", PointerMiddle);
6184   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6185   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6186   verifyFormat("A<int *> a;", PointerMiddle);
6187   verifyFormat("A<int **> a;", PointerMiddle);
6188   verifyFormat("A<int *, int *> a;", PointerMiddle);
6189   verifyFormat("A<int *[]> a;", PointerMiddle);
6190   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6191   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6192   verifyFormat("T ** t = new T *;", PointerMiddle);
6193 
6194   // Member function reference qualifiers aren't binary operators.
6195   verifyFormat("string // break\n"
6196                "operator()() & {}");
6197   verifyFormat("string // break\n"
6198                "operator()() && {}");
6199   verifyGoogleFormat("template <typename T>\n"
6200                      "auto x() & -> int {}");
6201 }
6202 
6203 TEST_F(FormatTest, UnderstandsAttributes) {
6204   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6205   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6206                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6207   FormatStyle AfterType = getLLVMStyle();
6208   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6209   verifyFormat("__attribute__((nodebug)) void\n"
6210                "foo() {}\n",
6211                AfterType);
6212 }
6213 
6214 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6215   verifyFormat("SomeType s [[unused]] (InitValue);");
6216   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6217   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6218   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6219   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6220   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6221                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6222 
6223   // Make sure we do not mistake attributes for array subscripts.
6224   verifyFormat("int a() {}\n"
6225                "[[unused]] int b() {}\n");
6226 
6227   // On the other hand, we still need to correctly find array subscripts.
6228   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6229 
6230   // Make sure we do not parse attributes as lambda introducers.
6231   FormatStyle MultiLineFunctions = getLLVMStyle();
6232   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6233   verifyFormat("[[unused]] int b() {\n"
6234                "  return 42;\n"
6235                "}\n",
6236                MultiLineFunctions);
6237 }
6238 
6239 TEST_F(FormatTest, UnderstandsEllipsis) {
6240   verifyFormat("int printf(const char *fmt, ...);");
6241   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6242   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6243 
6244   FormatStyle PointersLeft = getLLVMStyle();
6245   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6246   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6247 }
6248 
6249 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6250   EXPECT_EQ("int *a;\n"
6251             "int *a;\n"
6252             "int *a;",
6253             format("int *a;\n"
6254                    "int* a;\n"
6255                    "int *a;",
6256                    getGoogleStyle()));
6257   EXPECT_EQ("int* a;\n"
6258             "int* a;\n"
6259             "int* a;",
6260             format("int* a;\n"
6261                    "int* a;\n"
6262                    "int *a;",
6263                    getGoogleStyle()));
6264   EXPECT_EQ("int *a;\n"
6265             "int *a;\n"
6266             "int *a;",
6267             format("int *a;\n"
6268                    "int * a;\n"
6269                    "int *  a;",
6270                    getGoogleStyle()));
6271   EXPECT_EQ("auto x = [] {\n"
6272             "  int *a;\n"
6273             "  int *a;\n"
6274             "  int *a;\n"
6275             "};",
6276             format("auto x=[]{int *a;\n"
6277                    "int * a;\n"
6278                    "int *  a;};",
6279                    getGoogleStyle()));
6280 }
6281 
6282 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6283   verifyFormat("int f(int &&a) {}");
6284   verifyFormat("int f(int a, char &&b) {}");
6285   verifyFormat("void f() { int &&a = b; }");
6286   verifyGoogleFormat("int f(int a, char&& b) {}");
6287   verifyGoogleFormat("void f() { int&& a = b; }");
6288 
6289   verifyIndependentOfContext("A<int &&> a;");
6290   verifyIndependentOfContext("A<int &&, int &&> a;");
6291   verifyGoogleFormat("A<int&&> a;");
6292   verifyGoogleFormat("A<int&&, int&&> a;");
6293 
6294   // Not rvalue references:
6295   verifyFormat("template <bool B, bool C> class A {\n"
6296                "  static_assert(B && C, \"Something is wrong\");\n"
6297                "};");
6298   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6299   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6300   verifyFormat("#define A(a, b) (a && b)");
6301 }
6302 
6303 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6304   verifyFormat("void f() {\n"
6305                "  x[aaaaaaaaa -\n"
6306                "    b] = 23;\n"
6307                "}",
6308                getLLVMStyleWithColumns(15));
6309 }
6310 
6311 TEST_F(FormatTest, FormatsCasts) {
6312   verifyFormat("Type *A = static_cast<Type *>(P);");
6313   verifyFormat("Type *A = (Type *)P;");
6314   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6315   verifyFormat("int a = (int)(2.0f);");
6316   verifyFormat("int a = (int)2.0f;");
6317   verifyFormat("x[(int32)y];");
6318   verifyFormat("x = (int32)y;");
6319   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6320   verifyFormat("int a = (int)*b;");
6321   verifyFormat("int a = (int)2.0f;");
6322   verifyFormat("int a = (int)~0;");
6323   verifyFormat("int a = (int)++a;");
6324   verifyFormat("int a = (int)sizeof(int);");
6325   verifyFormat("int a = (int)+2;");
6326   verifyFormat("my_int a = (my_int)2.0f;");
6327   verifyFormat("my_int a = (my_int)sizeof(int);");
6328   verifyFormat("return (my_int)aaa;");
6329   verifyFormat("#define x ((int)-1)");
6330   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6331   verifyFormat("#define p(q) ((int *)&q)");
6332   verifyFormat("fn(a)(b) + 1;");
6333 
6334   verifyFormat("void f() { my_int a = (my_int)*b; }");
6335   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6336   verifyFormat("my_int a = (my_int)~0;");
6337   verifyFormat("my_int a = (my_int)++a;");
6338   verifyFormat("my_int a = (my_int)-2;");
6339   verifyFormat("my_int a = (my_int)1;");
6340   verifyFormat("my_int a = (my_int *)1;");
6341   verifyFormat("my_int a = (const my_int)-1;");
6342   verifyFormat("my_int a = (const my_int *)-1;");
6343   verifyFormat("my_int a = (my_int)(my_int)-1;");
6344   verifyFormat("my_int a = (ns::my_int)-2;");
6345   verifyFormat("case (my_int)ONE:");
6346   verifyFormat("auto x = (X)this;");
6347 
6348   // FIXME: single value wrapped with paren will be treated as cast.
6349   verifyFormat("void f(int i = (kValue)*kMask) {}");
6350 
6351   verifyFormat("{ (void)F; }");
6352 
6353   // Don't break after a cast's
6354   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6355                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6356                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6357 
6358   // These are not casts.
6359   verifyFormat("void f(int *) {}");
6360   verifyFormat("f(foo)->b;");
6361   verifyFormat("f(foo).b;");
6362   verifyFormat("f(foo)(b);");
6363   verifyFormat("f(foo)[b];");
6364   verifyFormat("[](foo) { return 4; }(bar);");
6365   verifyFormat("(*funptr)(foo)[4];");
6366   verifyFormat("funptrs[4](foo)[4];");
6367   verifyFormat("void f(int *);");
6368   verifyFormat("void f(int *) = 0;");
6369   verifyFormat("void f(SmallVector<int>) {}");
6370   verifyFormat("void f(SmallVector<int>);");
6371   verifyFormat("void f(SmallVector<int>) = 0;");
6372   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6373   verifyFormat("int a = sizeof(int) * b;");
6374   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6375   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6376   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6377   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6378 
6379   // These are not casts, but at some point were confused with casts.
6380   verifyFormat("virtual void foo(int *) override;");
6381   verifyFormat("virtual void foo(char &) const;");
6382   verifyFormat("virtual void foo(int *a, char *) const;");
6383   verifyFormat("int a = sizeof(int *) + b;");
6384   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6385   verifyFormat("bool b = f(g<int>) && c;");
6386   verifyFormat("typedef void (*f)(int i) func;");
6387 
6388   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6389                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6390   // FIXME: The indentation here is not ideal.
6391   verifyFormat(
6392       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6393       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6394       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6395 }
6396 
6397 TEST_F(FormatTest, FormatsFunctionTypes) {
6398   verifyFormat("A<bool()> a;");
6399   verifyFormat("A<SomeType()> a;");
6400   verifyFormat("A<void (*)(int, std::string)> a;");
6401   verifyFormat("A<void *(int)>;");
6402   verifyFormat("void *(*a)(int *, SomeType *);");
6403   verifyFormat("int (*func)(void *);");
6404   verifyFormat("void f() { int (*func)(void *); }");
6405   verifyFormat("template <class CallbackClass>\n"
6406                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6407 
6408   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6409   verifyGoogleFormat("void* (*a)(int);");
6410   verifyGoogleFormat(
6411       "template <class CallbackClass>\n"
6412       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6413 
6414   // Other constructs can look somewhat like function types:
6415   verifyFormat("A<sizeof(*x)> a;");
6416   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6417   verifyFormat("some_var = function(*some_pointer_var)[0];");
6418   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6419   verifyFormat("int x = f(&h)();");
6420   verifyFormat("returnsFunction(&param1, &param2)(param);");
6421   verifyFormat("std::function<\n"
6422                "    LooooooooooongTemplatedType<\n"
6423                "        SomeType>*(\n"
6424                "        LooooooooooooooooongType type)>\n"
6425                "    function;",
6426                getGoogleStyleWithColumns(40));
6427 }
6428 
6429 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6430   verifyFormat("A (*foo_)[6];");
6431   verifyFormat("vector<int> (*foo_)[6];");
6432 }
6433 
6434 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6435   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6436                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6437   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6438                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6439   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6440                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6441 
6442   // Different ways of ()-initializiation.
6443   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6444                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6445   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6446                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6447   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6448                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6449   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6450                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6451 
6452   // Lambdas should not confuse the variable declaration heuristic.
6453   verifyFormat("LooooooooooooooooongType\n"
6454                "    variable(nullptr, [](A *a) {});",
6455                getLLVMStyleWithColumns(40));
6456 }
6457 
6458 TEST_F(FormatTest, BreaksLongDeclarations) {
6459   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6460                "    AnotherNameForTheLongType;");
6461   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6462                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6463   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6464                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6465   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6466                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6467   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6468                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6469   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6470                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6471   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6472                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6473   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6474                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6475   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6476                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6477   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6478                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6479   FormatStyle Indented = getLLVMStyle();
6480   Indented.IndentWrappedFunctionNames = true;
6481   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6482                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6483                Indented);
6484   verifyFormat(
6485       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6486       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6487       Indented);
6488   verifyFormat(
6489       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6490       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6491       Indented);
6492   verifyFormat(
6493       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6494       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6495       Indented);
6496 
6497   // FIXME: Without the comment, this breaks after "(".
6498   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6499                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6500                getGoogleStyle());
6501 
6502   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6503                "                  int LoooooooooooooooooooongParam2) {}");
6504   verifyFormat(
6505       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6506       "                                   SourceLocation L, IdentifierIn *II,\n"
6507       "                                   Type *T) {}");
6508   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6509                "ReallyReaaallyLongFunctionName(\n"
6510                "    const std::string &SomeParameter,\n"
6511                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6512                "        &ReallyReallyLongParameterName,\n"
6513                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6514                "        &AnotherLongParameterName) {}");
6515   verifyFormat("template <typename A>\n"
6516                "SomeLoooooooooooooooooooooongType<\n"
6517                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6518                "Function() {}");
6519 
6520   verifyGoogleFormat(
6521       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6522       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6523   verifyGoogleFormat(
6524       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6525       "                                   SourceLocation L) {}");
6526   verifyGoogleFormat(
6527       "some_namespace::LongReturnType\n"
6528       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6529       "    int first_long_parameter, int second_parameter) {}");
6530 
6531   verifyGoogleFormat("template <typename T>\n"
6532                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6533                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6534   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6535                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6536 
6537   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6538                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6539                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6540   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6541                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6542                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6543   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6544                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6545                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6546                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6547 
6548   verifyFormat("template <typename T> // Templates on own line.\n"
6549                "static int            // Some comment.\n"
6550                "MyFunction(int a);",
6551                getLLVMStyle());
6552 }
6553 
6554 TEST_F(FormatTest, FormatsArrays) {
6555   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6556                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6557   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6558                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6559   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6560                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6561   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6562                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6563   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6564                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6565   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6566                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6567                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6568   verifyFormat(
6569       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6570       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6571       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6572   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6573                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6574 
6575   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6576                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6577   verifyFormat(
6578       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6579       "                                  .aaaaaaa[0]\n"
6580       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6581   verifyFormat("a[::b::c];");
6582 
6583   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6584 
6585   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6586   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6587 }
6588 
6589 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6590   verifyFormat("(a)->b();");
6591   verifyFormat("--a;");
6592 }
6593 
6594 TEST_F(FormatTest, HandlesIncludeDirectives) {
6595   verifyFormat("#include <string>\n"
6596                "#include <a/b/c.h>\n"
6597                "#include \"a/b/string\"\n"
6598                "#include \"string.h\"\n"
6599                "#include \"string.h\"\n"
6600                "#include <a-a>\n"
6601                "#include < path with space >\n"
6602                "#include_next <test.h>"
6603                "#include \"abc.h\" // this is included for ABC\n"
6604                "#include \"some long include\" // with a comment\n"
6605                "#include \"some very long include path\"\n"
6606                "#include <some/very/long/include/path>\n",
6607                getLLVMStyleWithColumns(35));
6608   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6609   EXPECT_EQ("#include <a>", format("#include<a>"));
6610 
6611   verifyFormat("#import <string>");
6612   verifyFormat("#import <a/b/c.h>");
6613   verifyFormat("#import \"a/b/string\"");
6614   verifyFormat("#import \"string.h\"");
6615   verifyFormat("#import \"string.h\"");
6616   verifyFormat("#if __has_include(<strstream>)\n"
6617                "#include <strstream>\n"
6618                "#endif");
6619 
6620   verifyFormat("#define MY_IMPORT <a/b>");
6621 
6622   verifyFormat("#if __has_include(<a/b>)");
6623   verifyFormat("#if __has_include_next(<a/b>)");
6624   verifyFormat("#define F __has_include(<a/b>)");
6625   verifyFormat("#define F __has_include_next(<a/b>)");
6626 
6627   // Protocol buffer definition or missing "#".
6628   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6629                getLLVMStyleWithColumns(30));
6630 
6631   FormatStyle Style = getLLVMStyle();
6632   Style.AlwaysBreakBeforeMultilineStrings = true;
6633   Style.ColumnLimit = 0;
6634   verifyFormat("#import \"abc.h\"", Style);
6635 
6636   // But 'import' might also be a regular C++ namespace.
6637   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6638                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6639 }
6640 
6641 //===----------------------------------------------------------------------===//
6642 // Error recovery tests.
6643 //===----------------------------------------------------------------------===//
6644 
6645 TEST_F(FormatTest, IncompleteParameterLists) {
6646   FormatStyle NoBinPacking = getLLVMStyle();
6647   NoBinPacking.BinPackParameters = false;
6648   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6649                "                        double *min_x,\n"
6650                "                        double *max_x,\n"
6651                "                        double *min_y,\n"
6652                "                        double *max_y,\n"
6653                "                        double *min_z,\n"
6654                "                        double *max_z, ) {}",
6655                NoBinPacking);
6656 }
6657 
6658 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6659   verifyFormat("void f() { return; }\n42");
6660   verifyFormat("void f() {\n"
6661                "  if (0)\n"
6662                "    return;\n"
6663                "}\n"
6664                "42");
6665   verifyFormat("void f() { return }\n42");
6666   verifyFormat("void f() {\n"
6667                "  if (0)\n"
6668                "    return\n"
6669                "}\n"
6670                "42");
6671 }
6672 
6673 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6674   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6675   EXPECT_EQ("void f() {\n"
6676             "  if (a)\n"
6677             "    return\n"
6678             "}",
6679             format("void  f  (  )  {  if  ( a )  return  }"));
6680   EXPECT_EQ("namespace N {\n"
6681             "void f()\n"
6682             "}",
6683             format("namespace  N  {  void f()  }"));
6684   EXPECT_EQ("namespace N {\n"
6685             "void f() {}\n"
6686             "void g()\n"
6687             "} // namespace N",
6688             format("namespace N  { void f( ) { } void g( ) }"));
6689 }
6690 
6691 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6692   verifyFormat("int aaaaaaaa =\n"
6693                "    // Overlylongcomment\n"
6694                "    b;",
6695                getLLVMStyleWithColumns(20));
6696   verifyFormat("function(\n"
6697                "    ShortArgument,\n"
6698                "    LoooooooooooongArgument);\n",
6699                getLLVMStyleWithColumns(20));
6700 }
6701 
6702 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6703   verifyFormat("public:");
6704   verifyFormat("class A {\n"
6705                "public\n"
6706                "  void f() {}\n"
6707                "};");
6708   verifyFormat("public\n"
6709                "int qwerty;");
6710   verifyFormat("public\n"
6711                "B {}");
6712   verifyFormat("public\n"
6713                "{}");
6714   verifyFormat("public\n"
6715                "B { int x; }");
6716 }
6717 
6718 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6719   verifyFormat("{");
6720   verifyFormat("#})");
6721   verifyNoCrash("(/**/[:!] ?[).");
6722 }
6723 
6724 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
6725   // Found by oss-fuzz:
6726   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
6727   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6728   Style.ColumnLimit = 60;
6729   verifyNoCrash(
6730       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
6731       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
6732       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
6733       Style);
6734 }
6735 
6736 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6737   verifyFormat("do {\n}");
6738   verifyFormat("do {\n}\n"
6739                "f();");
6740   verifyFormat("do {\n}\n"
6741                "wheeee(fun);");
6742   verifyFormat("do {\n"
6743                "  f();\n"
6744                "}");
6745 }
6746 
6747 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6748   verifyFormat("if {\n  foo;\n  foo();\n}");
6749   verifyFormat("switch {\n  foo;\n  foo();\n}");
6750   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6751   verifyFormat("while {\n  foo;\n  foo();\n}");
6752   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6753 }
6754 
6755 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6756   verifyIncompleteFormat("namespace {\n"
6757                          "class Foo { Foo (\n"
6758                          "};\n"
6759                          "} // namespace");
6760 }
6761 
6762 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6763   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6764   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6765   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6766   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6767 
6768   EXPECT_EQ("{\n"
6769             "  {\n"
6770             "    breakme(\n"
6771             "        qwe);\n"
6772             "  }\n",
6773             format("{\n"
6774                    "    {\n"
6775                    " breakme(qwe);\n"
6776                    "}\n",
6777                    getLLVMStyleWithColumns(10)));
6778 }
6779 
6780 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6781   verifyFormat("int x = {\n"
6782                "    avariable,\n"
6783                "    b(alongervariable)};",
6784                getLLVMStyleWithColumns(25));
6785 }
6786 
6787 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6788   verifyFormat("return (a)(b){1, 2, 3};");
6789 }
6790 
6791 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6792   verifyFormat("vector<int> x{1, 2, 3, 4};");
6793   verifyFormat("vector<int> x{\n"
6794                "    1,\n"
6795                "    2,\n"
6796                "    3,\n"
6797                "    4,\n"
6798                "};");
6799   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6800   verifyFormat("f({1, 2});");
6801   verifyFormat("auto v = Foo{-1};");
6802   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6803   verifyFormat("Class::Class : member{1, 2, 3} {}");
6804   verifyFormat("new vector<int>{1, 2, 3};");
6805   verifyFormat("new int[3]{1, 2, 3};");
6806   verifyFormat("new int{1};");
6807   verifyFormat("return {arg1, arg2};");
6808   verifyFormat("return {arg1, SomeType{parameter}};");
6809   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6810   verifyFormat("new T{arg1, arg2};");
6811   verifyFormat("f(MyMap[{composite, key}]);");
6812   verifyFormat("class Class {\n"
6813                "  T member = {arg1, arg2};\n"
6814                "};");
6815   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6816   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6817   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6818   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6819   verifyFormat("int a = std::is_integral<int>{} + 0;");
6820 
6821   verifyFormat("int foo(int i) { return fo1{}(i); }");
6822   verifyFormat("int foo(int i) { return fo1{}(i); }");
6823   verifyFormat("auto i = decltype(x){};");
6824   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6825   verifyFormat("Node n{1, Node{1000}, //\n"
6826                "       2};");
6827   verifyFormat("Aaaa aaaaaaa{\n"
6828                "    {\n"
6829                "        aaaa,\n"
6830                "    },\n"
6831                "};");
6832   verifyFormat("class C : public D {\n"
6833                "  SomeClass SC{2};\n"
6834                "};");
6835   verifyFormat("class C : public A {\n"
6836                "  class D : public B {\n"
6837                "    void f() { int i{2}; }\n"
6838                "  };\n"
6839                "};");
6840   verifyFormat("#define A {a, a},");
6841 
6842   // Avoid breaking between equal sign and opening brace
6843   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
6844   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
6845   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
6846                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
6847                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
6848                "     {\"ccccccccccccccccccccc\", 2}};",
6849                AvoidBreakingFirstArgument);
6850 
6851   // Binpacking only if there is no trailing comma
6852   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6853                "                      cccccccccc, dddddddddd};",
6854 			   getLLVMStyleWithColumns(50));
6855   verifyFormat("const Aaaaaa aaaaa = {\n"
6856                "    aaaaaaaaaaa,\n"
6857                "    bbbbbbbbbbb,\n"
6858                "    ccccccccccc,\n"
6859                "    ddddddddddd,\n"
6860                "};", getLLVMStyleWithColumns(50));
6861 
6862   // Cases where distinguising braced lists and blocks is hard.
6863   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6864   verifyFormat("void f() {\n"
6865                "  return; // comment\n"
6866                "}\n"
6867                "SomeType t;");
6868   verifyFormat("void f() {\n"
6869                "  if (a) {\n"
6870                "    f();\n"
6871                "  }\n"
6872                "}\n"
6873                "SomeType t;");
6874 
6875   // In combination with BinPackArguments = false.
6876   FormatStyle NoBinPacking = getLLVMStyle();
6877   NoBinPacking.BinPackArguments = false;
6878   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6879                "                      bbbbb,\n"
6880                "                      ccccc,\n"
6881                "                      ddddd,\n"
6882                "                      eeeee,\n"
6883                "                      ffffff,\n"
6884                "                      ggggg,\n"
6885                "                      hhhhhh,\n"
6886                "                      iiiiii,\n"
6887                "                      jjjjjj,\n"
6888                "                      kkkkkk};",
6889                NoBinPacking);
6890   verifyFormat("const Aaaaaa aaaaa = {\n"
6891                "    aaaaa,\n"
6892                "    bbbbb,\n"
6893                "    ccccc,\n"
6894                "    ddddd,\n"
6895                "    eeeee,\n"
6896                "    ffffff,\n"
6897                "    ggggg,\n"
6898                "    hhhhhh,\n"
6899                "    iiiiii,\n"
6900                "    jjjjjj,\n"
6901                "    kkkkkk,\n"
6902                "};",
6903                NoBinPacking);
6904   verifyFormat(
6905       "const Aaaaaa aaaaa = {\n"
6906       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6907       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6908       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6909       "};",
6910       NoBinPacking);
6911 
6912   // FIXME: The alignment of these trailing comments might be bad. Then again,
6913   // this might be utterly useless in real code.
6914   verifyFormat("Constructor::Constructor()\n"
6915                "    : some_value{         //\n"
6916                "                 aaaaaaa, //\n"
6917                "                 bbbbbbb} {}");
6918 
6919   // In braced lists, the first comment is always assumed to belong to the
6920   // first element. Thus, it can be moved to the next or previous line as
6921   // appropriate.
6922   EXPECT_EQ("function({// First element:\n"
6923             "          1,\n"
6924             "          // Second element:\n"
6925             "          2});",
6926             format("function({\n"
6927                    "    // First element:\n"
6928                    "    1,\n"
6929                    "    // Second element:\n"
6930                    "    2});"));
6931   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6932             "    // First element:\n"
6933             "    1,\n"
6934             "    // Second element:\n"
6935             "    2};",
6936             format("std::vector<int> MyNumbers{// First element:\n"
6937                    "                           1,\n"
6938                    "                           // Second element:\n"
6939                    "                           2};",
6940                    getLLVMStyleWithColumns(30)));
6941   // A trailing comma should still lead to an enforced line break and no
6942   // binpacking.
6943   EXPECT_EQ("vector<int> SomeVector = {\n"
6944             "    // aaa\n"
6945             "    1,\n"
6946             "    2,\n"
6947             "};",
6948             format("vector<int> SomeVector = { // aaa\n"
6949                    "    1, 2, };"));
6950 
6951   FormatStyle ExtraSpaces = getLLVMStyle();
6952   ExtraSpaces.Cpp11BracedListStyle = false;
6953   ExtraSpaces.ColumnLimit = 75;
6954   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6955   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6956   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6957   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6958   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6959   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6960   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6961   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6962   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6963   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6964   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6965   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6966   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6967   verifyFormat("class Class {\n"
6968                "  T member = { arg1, arg2 };\n"
6969                "};",
6970                ExtraSpaces);
6971   verifyFormat(
6972       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6973       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6974       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6975       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6976       ExtraSpaces);
6977   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6978   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6979                ExtraSpaces);
6980   verifyFormat(
6981       "someFunction(OtherParam,\n"
6982       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6983       "                         param1, param2,\n"
6984       "                         // comment 2\n"
6985       "                         param3, param4 });",
6986       ExtraSpaces);
6987   verifyFormat(
6988       "std::this_thread::sleep_for(\n"
6989       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6990       ExtraSpaces);
6991   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6992                "    aaaaaaa,\n"
6993                "    aaaaaaaaaa,\n"
6994                "    aaaaa,\n"
6995                "    aaaaaaaaaaaaaaa,\n"
6996                "    aaa,\n"
6997                "    aaaaaaaaaa,\n"
6998                "    a,\n"
6999                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7000                "    aaaaaaaaaaaa,\n"
7001                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7002                "    aaaaaaa,\n"
7003                "    a};");
7004   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7005   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7006   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7007 
7008   // Avoid breaking between initializer/equal sign and opening brace
7009   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7010   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7011                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7012                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7013                "  { \"ccccccccccccccccccccc\", 2 }\n"
7014                "};",
7015                ExtraSpaces);
7016   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7017                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7018                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7019                "  { \"ccccccccccccccccccccc\", 2 }\n"
7020                "};",
7021                ExtraSpaces);
7022 
7023   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7024   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7025   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7026   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7027 }
7028 
7029 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7030   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7031                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7032                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7033                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7034                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7035                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7036   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7037                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7038                "                 1, 22, 333, 4444, 55555, //\n"
7039                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7040                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7041   verifyFormat(
7042       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7043       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7044       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7045       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7046       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7047       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7048       "                 7777777};");
7049   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7050                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7051                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7052   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7053                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7054                "    // Separating comment.\n"
7055                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7056   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7057                "    // Leading comment\n"
7058                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7059                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7060   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7061                "                 1, 1, 1, 1};",
7062                getLLVMStyleWithColumns(39));
7063   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7064                "                 1, 1, 1, 1};",
7065                getLLVMStyleWithColumns(38));
7066   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7067                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7068                getLLVMStyleWithColumns(43));
7069   verifyFormat(
7070       "static unsigned SomeValues[10][3] = {\n"
7071       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7072       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7073   verifyFormat("static auto fields = new vector<string>{\n"
7074                "    \"aaaaaaaaaaaaa\",\n"
7075                "    \"aaaaaaaaaaaaa\",\n"
7076                "    \"aaaaaaaaaaaa\",\n"
7077                "    \"aaaaaaaaaaaaaa\",\n"
7078                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7079                "    \"aaaaaaaaaaaa\",\n"
7080                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7081                "};");
7082   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7083   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7084                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7085                "                 3, cccccccccccccccccccccc};",
7086                getLLVMStyleWithColumns(60));
7087 
7088   // Trailing commas.
7089   verifyFormat("vector<int> x = {\n"
7090                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7091                "};",
7092                getLLVMStyleWithColumns(39));
7093   verifyFormat("vector<int> x = {\n"
7094                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7095                "};",
7096                getLLVMStyleWithColumns(39));
7097   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7098                "                 1, 1, 1, 1,\n"
7099                "                 /**/ /**/};",
7100                getLLVMStyleWithColumns(39));
7101 
7102   // Trailing comment in the first line.
7103   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7104                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7105                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7106                "    11111111,   22222222,   333333333,   44444444};");
7107   // Trailing comment in the last line.
7108   verifyFormat("int aaaaa[] = {\n"
7109                "    1, 2, 3, // comment\n"
7110                "    4, 5, 6  // comment\n"
7111                "};");
7112 
7113   // With nested lists, we should either format one item per line or all nested
7114   // lists one on line.
7115   // FIXME: For some nested lists, we can do better.
7116   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7117                "        {aaaaaaaaaaaaaaaaaaa},\n"
7118                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7119                "        {aaaaaaaaaaaaaaaaa}};",
7120                getLLVMStyleWithColumns(60));
7121   verifyFormat(
7122       "SomeStruct my_struct_array = {\n"
7123       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7124       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7125       "    {aaa, aaa},\n"
7126       "    {aaa, aaa},\n"
7127       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7128       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7129       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7130 
7131   // No column layout should be used here.
7132   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7133                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7134 
7135   verifyNoCrash("a<,");
7136 
7137   // No braced initializer here.
7138   verifyFormat("void f() {\n"
7139                "  struct Dummy {};\n"
7140                "  f(v);\n"
7141                "}");
7142 
7143   // Long lists should be formatted in columns even if they are nested.
7144   verifyFormat(
7145       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7146       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7147       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7148       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7149       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7150       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7151 
7152   // Allow "single-column" layout even if that violates the column limit. There
7153   // isn't going to be a better way.
7154   verifyFormat("std::vector<int> a = {\n"
7155                "    aaaaaaaa,\n"
7156                "    aaaaaaaa,\n"
7157                "    aaaaaaaa,\n"
7158                "    aaaaaaaa,\n"
7159                "    aaaaaaaaaa,\n"
7160                "    aaaaaaaa,\n"
7161                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7162                getLLVMStyleWithColumns(30));
7163   verifyFormat("vector<int> aaaa = {\n"
7164                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7165                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7166                "    aaaaaa.aaaaaaa,\n"
7167                "    aaaaaa.aaaaaaa,\n"
7168                "    aaaaaa.aaaaaaa,\n"
7169                "    aaaaaa.aaaaaaa,\n"
7170                "};");
7171 
7172   // Don't create hanging lists.
7173   verifyFormat("someFunction(Param, {List1, List2,\n"
7174                "                     List3});",
7175                getLLVMStyleWithColumns(35));
7176   verifyFormat("someFunction(Param, Param,\n"
7177                "             {List1, List2,\n"
7178                "              List3});",
7179                getLLVMStyleWithColumns(35));
7180   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7181                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7182 }
7183 
7184 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7185   FormatStyle DoNotMerge = getLLVMStyle();
7186   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7187 
7188   verifyFormat("void f() { return 42; }");
7189   verifyFormat("void f() {\n"
7190                "  return 42;\n"
7191                "}",
7192                DoNotMerge);
7193   verifyFormat("void f() {\n"
7194                "  // Comment\n"
7195                "}");
7196   verifyFormat("{\n"
7197                "#error {\n"
7198                "  int a;\n"
7199                "}");
7200   verifyFormat("{\n"
7201                "  int a;\n"
7202                "#error {\n"
7203                "}");
7204   verifyFormat("void f() {} // comment");
7205   verifyFormat("void f() { int a; } // comment");
7206   verifyFormat("void f() {\n"
7207                "} // comment",
7208                DoNotMerge);
7209   verifyFormat("void f() {\n"
7210                "  int a;\n"
7211                "} // comment",
7212                DoNotMerge);
7213   verifyFormat("void f() {\n"
7214                "} // comment",
7215                getLLVMStyleWithColumns(15));
7216 
7217   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7218   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7219 
7220   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7221   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7222   verifyFormat("class C {\n"
7223                "  C()\n"
7224                "      : iiiiiiii(nullptr),\n"
7225                "        kkkkkkk(nullptr),\n"
7226                "        mmmmmmm(nullptr),\n"
7227                "        nnnnnnn(nullptr) {}\n"
7228                "};",
7229                getGoogleStyle());
7230 
7231   FormatStyle NoColumnLimit = getLLVMStyle();
7232   NoColumnLimit.ColumnLimit = 0;
7233   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7234   EXPECT_EQ("class C {\n"
7235             "  A() : b(0) {}\n"
7236             "};",
7237             format("class C{A():b(0){}};", NoColumnLimit));
7238   EXPECT_EQ("A()\n"
7239             "    : b(0) {\n"
7240             "}",
7241             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7242 
7243   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7244   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7245       FormatStyle::SFS_None;
7246   EXPECT_EQ("A()\n"
7247             "    : b(0) {\n"
7248             "}",
7249             format("A():b(0){}", DoNotMergeNoColumnLimit));
7250   EXPECT_EQ("A()\n"
7251             "    : b(0) {\n"
7252             "}",
7253             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7254 
7255   verifyFormat("#define A          \\\n"
7256                "  void f() {       \\\n"
7257                "    int i;         \\\n"
7258                "  }",
7259                getLLVMStyleWithColumns(20));
7260   verifyFormat("#define A           \\\n"
7261                "  void f() { int i; }",
7262                getLLVMStyleWithColumns(21));
7263   verifyFormat("#define A            \\\n"
7264                "  void f() {         \\\n"
7265                "    int i;           \\\n"
7266                "  }                  \\\n"
7267                "  int j;",
7268                getLLVMStyleWithColumns(22));
7269   verifyFormat("#define A             \\\n"
7270                "  void f() { int i; } \\\n"
7271                "  int j;",
7272                getLLVMStyleWithColumns(23));
7273 }
7274 
7275 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7276   FormatStyle MergeEmptyOnly = getLLVMStyle();
7277   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7278   verifyFormat("class C {\n"
7279                "  int f() {}\n"
7280                "};",
7281                MergeEmptyOnly);
7282   verifyFormat("class C {\n"
7283                "  int f() {\n"
7284                "    return 42;\n"
7285                "  }\n"
7286                "};",
7287                MergeEmptyOnly);
7288   verifyFormat("int f() {}", MergeEmptyOnly);
7289   verifyFormat("int f() {\n"
7290                "  return 42;\n"
7291                "}",
7292                MergeEmptyOnly);
7293 
7294   // Also verify behavior when BraceWrapping.AfterFunction = true
7295   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7296   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7297   verifyFormat("int f() {}", MergeEmptyOnly);
7298   verifyFormat("class C {\n"
7299                "  int f() {}\n"
7300                "};",
7301                MergeEmptyOnly);
7302 }
7303 
7304 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7305   FormatStyle MergeInlineOnly = getLLVMStyle();
7306   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7307   verifyFormat("class C {\n"
7308                "  int f() { return 42; }\n"
7309                "};",
7310                MergeInlineOnly);
7311   verifyFormat("int f() {\n"
7312                "  return 42;\n"
7313                "}",
7314                MergeInlineOnly);
7315 
7316   // SFS_Inline implies SFS_Empty
7317   verifyFormat("class C {\n"
7318                "  int f() {}\n"
7319                "};",
7320                MergeInlineOnly);
7321   verifyFormat("int f() {}", MergeInlineOnly);
7322 
7323   // Also verify behavior when BraceWrapping.AfterFunction = true
7324   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7325   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7326   verifyFormat("class C {\n"
7327                "  int f() { return 42; }\n"
7328                "};",
7329                MergeInlineOnly);
7330   verifyFormat("int f()\n"
7331                "{\n"
7332                "  return 42;\n"
7333                "}",
7334                MergeInlineOnly);
7335 
7336   // SFS_Inline implies SFS_Empty
7337   verifyFormat("int f() {}", MergeInlineOnly);
7338   verifyFormat("class C {\n"
7339                "  int f() {}\n"
7340                "};",
7341                MergeInlineOnly);
7342 }
7343 
7344 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7345   FormatStyle MergeInlineOnly = getLLVMStyle();
7346   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7347       FormatStyle::SFS_InlineOnly;
7348   verifyFormat("class C {\n"
7349                "  int f() { return 42; }\n"
7350                "};",
7351                MergeInlineOnly);
7352   verifyFormat("int f() {\n"
7353                "  return 42;\n"
7354                "}",
7355                MergeInlineOnly);
7356 
7357   // SFS_InlineOnly does not imply SFS_Empty
7358   verifyFormat("class C {\n"
7359                "  int f() {}\n"
7360                "};",
7361                MergeInlineOnly);
7362   verifyFormat("int f() {\n"
7363                "}",
7364                MergeInlineOnly);
7365 
7366   // Also verify behavior when BraceWrapping.AfterFunction = true
7367   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7368   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7369   verifyFormat("class C {\n"
7370                "  int f() { return 42; }\n"
7371                "};",
7372                MergeInlineOnly);
7373   verifyFormat("int f()\n"
7374                "{\n"
7375                "  return 42;\n"
7376                "}",
7377                MergeInlineOnly);
7378 
7379   // SFS_InlineOnly does not imply SFS_Empty
7380   verifyFormat("int f()\n"
7381                "{\n"
7382                "}",
7383                MergeInlineOnly);
7384   verifyFormat("class C {\n"
7385                "  int f() {}\n"
7386                "};",
7387                MergeInlineOnly);
7388 }
7389 
7390 TEST_F(FormatTest, SplitEmptyFunction) {
7391   FormatStyle Style = getLLVMStyle();
7392   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7393   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7394   Style.BraceWrapping.AfterFunction = true;
7395   Style.BraceWrapping.SplitEmptyFunction = false;
7396   Style.ColumnLimit = 40;
7397 
7398   verifyFormat("int f()\n"
7399                "{}",
7400                Style);
7401   verifyFormat("int f()\n"
7402                "{\n"
7403                "  return 42;\n"
7404                "}",
7405                Style);
7406   verifyFormat("int f()\n"
7407                "{\n"
7408                "  // some comment\n"
7409                "}",
7410                Style);
7411 
7412   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7413   verifyFormat("int f() {}", Style);
7414   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7415                "{}",
7416                Style);
7417   verifyFormat("int f()\n"
7418                "{\n"
7419                "  return 0;\n"
7420                "}",
7421                Style);
7422 
7423   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7424   verifyFormat("class Foo {\n"
7425                "  int f() {}\n"
7426                "};\n",
7427                Style);
7428   verifyFormat("class Foo {\n"
7429                "  int f() { return 0; }\n"
7430                "};\n",
7431                Style);
7432   verifyFormat("class Foo {\n"
7433                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7434                "  {}\n"
7435                "};\n",
7436                Style);
7437   verifyFormat("class Foo {\n"
7438                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7439                "  {\n"
7440                "    return 0;\n"
7441                "  }\n"
7442                "};\n",
7443                Style);
7444 
7445   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7446   verifyFormat("int f() {}", Style);
7447   verifyFormat("int f() { return 0; }", Style);
7448   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7449                "{}",
7450                Style);
7451   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7452                "{\n"
7453                "  return 0;\n"
7454                "}",
7455                Style);
7456 }
7457 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7458   FormatStyle Style = getLLVMStyle();
7459   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7460   verifyFormat("#ifdef A\n"
7461                "int f() {}\n"
7462                "#else\n"
7463                "int g() {}\n"
7464                "#endif",
7465                Style);
7466 }
7467 
7468 TEST_F(FormatTest, SplitEmptyClass) {
7469   FormatStyle Style = getLLVMStyle();
7470   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7471   Style.BraceWrapping.AfterClass = true;
7472   Style.BraceWrapping.SplitEmptyRecord = false;
7473 
7474   verifyFormat("class Foo\n"
7475                "{};",
7476                Style);
7477   verifyFormat("/* something */ class Foo\n"
7478                "{};",
7479                Style);
7480   verifyFormat("template <typename X> class Foo\n"
7481                "{};",
7482                Style);
7483   verifyFormat("class Foo\n"
7484                "{\n"
7485                "  Foo();\n"
7486                "};",
7487                Style);
7488   verifyFormat("typedef class Foo\n"
7489                "{\n"
7490                "} Foo_t;",
7491                Style);
7492 }
7493 
7494 TEST_F(FormatTest, SplitEmptyStruct) {
7495   FormatStyle Style = getLLVMStyle();
7496   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7497   Style.BraceWrapping.AfterStruct = true;
7498   Style.BraceWrapping.SplitEmptyRecord = false;
7499 
7500   verifyFormat("struct Foo\n"
7501                "{};",
7502                Style);
7503   verifyFormat("/* something */ struct Foo\n"
7504                "{};",
7505                Style);
7506   verifyFormat("template <typename X> struct Foo\n"
7507                "{};",
7508                Style);
7509   verifyFormat("struct Foo\n"
7510                "{\n"
7511                "  Foo();\n"
7512                "};",
7513                Style);
7514   verifyFormat("typedef struct Foo\n"
7515                "{\n"
7516                "} Foo_t;",
7517                Style);
7518   //typedef struct Bar {} Bar_t;
7519 }
7520 
7521 TEST_F(FormatTest, SplitEmptyUnion) {
7522   FormatStyle Style = getLLVMStyle();
7523   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7524   Style.BraceWrapping.AfterUnion = true;
7525   Style.BraceWrapping.SplitEmptyRecord = false;
7526 
7527   verifyFormat("union Foo\n"
7528                "{};",
7529                Style);
7530   verifyFormat("/* something */ union Foo\n"
7531                "{};",
7532                Style);
7533   verifyFormat("union Foo\n"
7534                "{\n"
7535                "  A,\n"
7536                "};",
7537                Style);
7538   verifyFormat("typedef union Foo\n"
7539                "{\n"
7540                "} Foo_t;",
7541                Style);
7542 }
7543 
7544 TEST_F(FormatTest, SplitEmptyNamespace) {
7545   FormatStyle Style = getLLVMStyle();
7546   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7547   Style.BraceWrapping.AfterNamespace = true;
7548   Style.BraceWrapping.SplitEmptyNamespace = false;
7549 
7550   verifyFormat("namespace Foo\n"
7551                "{};",
7552                Style);
7553   verifyFormat("/* something */ namespace Foo\n"
7554                "{};",
7555                Style);
7556   verifyFormat("inline namespace Foo\n"
7557                "{};",
7558                Style);
7559   verifyFormat("namespace Foo\n"
7560                "{\n"
7561                "void Bar();\n"
7562                "};",
7563                Style);
7564 }
7565 
7566 TEST_F(FormatTest, NeverMergeShortRecords) {
7567   FormatStyle Style = getLLVMStyle();
7568 
7569   verifyFormat("class Foo {\n"
7570                "  Foo();\n"
7571                "};",
7572                Style);
7573   verifyFormat("typedef class Foo {\n"
7574                "  Foo();\n"
7575                "} Foo_t;",
7576                Style);
7577   verifyFormat("struct Foo {\n"
7578                "  Foo();\n"
7579                "};",
7580                Style);
7581   verifyFormat("typedef struct Foo {\n"
7582                "  Foo();\n"
7583                "} Foo_t;",
7584                Style);
7585   verifyFormat("union Foo {\n"
7586                "  A,\n"
7587                "};",
7588                Style);
7589   verifyFormat("typedef union Foo {\n"
7590                "  A,\n"
7591                "} Foo_t;",
7592                Style);
7593   verifyFormat("namespace Foo {\n"
7594                "void Bar();\n"
7595                "};",
7596                Style);
7597 
7598   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7599   Style.BraceWrapping.AfterClass = true;
7600   Style.BraceWrapping.AfterStruct = true;
7601   Style.BraceWrapping.AfterUnion = true;
7602   Style.BraceWrapping.AfterNamespace = true;
7603   verifyFormat("class Foo\n"
7604                "{\n"
7605                "  Foo();\n"
7606                "};",
7607                Style);
7608   verifyFormat("typedef class Foo\n"
7609                "{\n"
7610                "  Foo();\n"
7611                "} Foo_t;",
7612                Style);
7613   verifyFormat("struct Foo\n"
7614                "{\n"
7615                "  Foo();\n"
7616                "};",
7617                Style);
7618   verifyFormat("typedef struct Foo\n"
7619                "{\n"
7620                "  Foo();\n"
7621                "} Foo_t;",
7622                Style);
7623   verifyFormat("union Foo\n"
7624                "{\n"
7625                "  A,\n"
7626                "};",
7627                Style);
7628   verifyFormat("typedef union Foo\n"
7629                "{\n"
7630                "  A,\n"
7631                "} Foo_t;",
7632                Style);
7633   verifyFormat("namespace Foo\n"
7634                "{\n"
7635                "void Bar();\n"
7636                "};",
7637                Style);
7638 }
7639 
7640 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7641   // Elaborate type variable declarations.
7642   verifyFormat("struct foo a = {bar};\nint n;");
7643   verifyFormat("class foo a = {bar};\nint n;");
7644   verifyFormat("union foo a = {bar};\nint n;");
7645 
7646   // Elaborate types inside function definitions.
7647   verifyFormat("struct foo f() {}\nint n;");
7648   verifyFormat("class foo f() {}\nint n;");
7649   verifyFormat("union foo f() {}\nint n;");
7650 
7651   // Templates.
7652   verifyFormat("template <class X> void f() {}\nint n;");
7653   verifyFormat("template <struct X> void f() {}\nint n;");
7654   verifyFormat("template <union X> void f() {}\nint n;");
7655 
7656   // Actual definitions...
7657   verifyFormat("struct {\n} n;");
7658   verifyFormat(
7659       "template <template <class T, class Y>, class Z> class X {\n} n;");
7660   verifyFormat("union Z {\n  int n;\n} x;");
7661   verifyFormat("class MACRO Z {\n} n;");
7662   verifyFormat("class MACRO(X) Z {\n} n;");
7663   verifyFormat("class __attribute__(X) Z {\n} n;");
7664   verifyFormat("class __declspec(X) Z {\n} n;");
7665   verifyFormat("class A##B##C {\n} n;");
7666   verifyFormat("class alignas(16) Z {\n} n;");
7667   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7668   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7669 
7670   // Redefinition from nested context:
7671   verifyFormat("class A::B::C {\n} n;");
7672 
7673   // Template definitions.
7674   verifyFormat(
7675       "template <typename F>\n"
7676       "Matcher(const Matcher<F> &Other,\n"
7677       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7678       "                             !is_same<F, T>::value>::type * = 0)\n"
7679       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7680 
7681   // FIXME: This is still incorrectly handled at the formatter side.
7682   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7683   verifyFormat("int i = SomeFunction(a<b, a> b);");
7684 
7685   // FIXME:
7686   // This now gets parsed incorrectly as class definition.
7687   // verifyFormat("class A<int> f() {\n}\nint n;");
7688 
7689   // Elaborate types where incorrectly parsing the structural element would
7690   // break the indent.
7691   verifyFormat("if (true)\n"
7692                "  class X x;\n"
7693                "else\n"
7694                "  f();\n");
7695 
7696   // This is simply incomplete. Formatting is not important, but must not crash.
7697   verifyFormat("class A:");
7698 }
7699 
7700 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7701   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7702             format("#error Leave     all         white!!!!! space* alone!\n"));
7703   EXPECT_EQ(
7704       "#warning Leave     all         white!!!!! space* alone!\n",
7705       format("#warning Leave     all         white!!!!! space* alone!\n"));
7706   EXPECT_EQ("#error 1", format("  #  error   1"));
7707   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7708 }
7709 
7710 TEST_F(FormatTest, FormatHashIfExpressions) {
7711   verifyFormat("#if AAAA && BBBB");
7712   verifyFormat("#if (AAAA && BBBB)");
7713   verifyFormat("#elif (AAAA && BBBB)");
7714   // FIXME: Come up with a better indentation for #elif.
7715   verifyFormat(
7716       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7717       "    defined(BBBBBBBB)\n"
7718       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7719       "    defined(BBBBBBBB)\n"
7720       "#endif",
7721       getLLVMStyleWithColumns(65));
7722 }
7723 
7724 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7725   FormatStyle AllowsMergedIf = getGoogleStyle();
7726   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7727   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7728   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7729   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7730   EXPECT_EQ("if (true) return 42;",
7731             format("if (true)\nreturn 42;", AllowsMergedIf));
7732   FormatStyle ShortMergedIf = AllowsMergedIf;
7733   ShortMergedIf.ColumnLimit = 25;
7734   verifyFormat("#define A \\\n"
7735                "  if (true) return 42;",
7736                ShortMergedIf);
7737   verifyFormat("#define A \\\n"
7738                "  f();    \\\n"
7739                "  if (true)\n"
7740                "#define B",
7741                ShortMergedIf);
7742   verifyFormat("#define A \\\n"
7743                "  f();    \\\n"
7744                "  if (true)\n"
7745                "g();",
7746                ShortMergedIf);
7747   verifyFormat("{\n"
7748                "#ifdef A\n"
7749                "  // Comment\n"
7750                "  if (true) continue;\n"
7751                "#endif\n"
7752                "  // Comment\n"
7753                "  if (true) continue;\n"
7754                "}",
7755                ShortMergedIf);
7756   ShortMergedIf.ColumnLimit = 33;
7757   verifyFormat("#define A \\\n"
7758                "  if constexpr (true) return 42;",
7759                ShortMergedIf);
7760   ShortMergedIf.ColumnLimit = 29;
7761   verifyFormat("#define A                   \\\n"
7762                "  if (aaaaaaaaaa) return 1; \\\n"
7763                "  return 2;",
7764                ShortMergedIf);
7765   ShortMergedIf.ColumnLimit = 28;
7766   verifyFormat("#define A         \\\n"
7767                "  if (aaaaaaaaaa) \\\n"
7768                "    return 1;     \\\n"
7769                "  return 2;",
7770                ShortMergedIf);
7771   verifyFormat("#define A                \\\n"
7772                "  if constexpr (aaaaaaa) \\\n"
7773                "    return 1;            \\\n"
7774                "  return 2;",
7775                ShortMergedIf);
7776 }
7777 
7778 TEST_F(FormatTest, FormatStarDependingOnContext) {
7779   verifyFormat("void f(int *a);");
7780   verifyFormat("void f() { f(fint * b); }");
7781   verifyFormat("class A {\n  void f(int *a);\n};");
7782   verifyFormat("class A {\n  int *a;\n};");
7783   verifyFormat("namespace a {\n"
7784                "namespace b {\n"
7785                "class A {\n"
7786                "  void f() {}\n"
7787                "  int *a;\n"
7788                "};\n"
7789                "} // namespace b\n"
7790                "} // namespace a");
7791 }
7792 
7793 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7794   verifyFormat("while");
7795   verifyFormat("operator");
7796 }
7797 
7798 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7799   // This code would be painfully slow to format if we didn't skip it.
7800   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
7801                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7802                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7803                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7804                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7805                    "A(1, 1)\n"
7806                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7807                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7808                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7809                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7810                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7811                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7812                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7813                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7814                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7815                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7816   // Deeply nested part is untouched, rest is formatted.
7817   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7818             format(std::string("int    i;\n") + Code + "int    j;\n",
7819                    getLLVMStyle(), SC_ExpectIncomplete));
7820 }
7821 
7822 //===----------------------------------------------------------------------===//
7823 // Objective-C tests.
7824 //===----------------------------------------------------------------------===//
7825 
7826 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7827   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7828   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7829             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7830   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7831   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7832   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7833             format("-(NSInteger)Method3:(id)anObject;"));
7834   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7835             format("-(NSInteger)Method4:(id)anObject;"));
7836   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7837             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7838   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7839             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7840   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7841             "forAllCells:(BOOL)flag;",
7842             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7843                    "forAllCells:(BOOL)flag;"));
7844 
7845   // Very long objectiveC method declaration.
7846   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7847                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7848   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7849                "                    inRange:(NSRange)range\n"
7850                "                   outRange:(NSRange)out_range\n"
7851                "                  outRange1:(NSRange)out_range1\n"
7852                "                  outRange2:(NSRange)out_range2\n"
7853                "                  outRange3:(NSRange)out_range3\n"
7854                "                  outRange4:(NSRange)out_range4\n"
7855                "                  outRange5:(NSRange)out_range5\n"
7856                "                  outRange6:(NSRange)out_range6\n"
7857                "                  outRange7:(NSRange)out_range7\n"
7858                "                  outRange8:(NSRange)out_range8\n"
7859                "                  outRange9:(NSRange)out_range9;");
7860 
7861   // When the function name has to be wrapped.
7862   FormatStyle Style = getLLVMStyle();
7863   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
7864   // and always indents instead.
7865   Style.IndentWrappedFunctionNames = false;
7866   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7867                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7868                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7869                "}",
7870                Style);
7871   Style.IndentWrappedFunctionNames = true;
7872   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7873                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
7874                "               anotherName:(NSString)dddddddddddddd {\n"
7875                "}",
7876                Style);
7877 
7878   verifyFormat("- (int)sum:(vector<int>)numbers;");
7879   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7880   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7881   // protocol lists (but not for template classes):
7882   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7883 
7884   verifyFormat("- (int (*)())foo:(int (*)())f;");
7885   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7886 
7887   // If there's no return type (very rare in practice!), LLVM and Google style
7888   // agree.
7889   verifyFormat("- foo;");
7890   verifyFormat("- foo:(int)f;");
7891   verifyGoogleFormat("- foo:(int)foo;");
7892 }
7893 
7894 
7895 TEST_F(FormatTest, BreaksStringLiterals) {
7896   EXPECT_EQ("\"some text \"\n"
7897             "\"other\";",
7898             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7899   EXPECT_EQ("\"some text \"\n"
7900             "\"other\";",
7901             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7902   EXPECT_EQ(
7903       "#define A  \\\n"
7904       "  \"some \"  \\\n"
7905       "  \"text \"  \\\n"
7906       "  \"other\";",
7907       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7908   EXPECT_EQ(
7909       "#define A  \\\n"
7910       "  \"so \"    \\\n"
7911       "  \"text \"  \\\n"
7912       "  \"other\";",
7913       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7914 
7915   EXPECT_EQ("\"some text\"",
7916             format("\"some text\"", getLLVMStyleWithColumns(1)));
7917   EXPECT_EQ("\"some text\"",
7918             format("\"some text\"", getLLVMStyleWithColumns(11)));
7919   EXPECT_EQ("\"some \"\n"
7920             "\"text\"",
7921             format("\"some text\"", getLLVMStyleWithColumns(10)));
7922   EXPECT_EQ("\"some \"\n"
7923             "\"text\"",
7924             format("\"some text\"", getLLVMStyleWithColumns(7)));
7925   EXPECT_EQ("\"some\"\n"
7926             "\" tex\"\n"
7927             "\"t\"",
7928             format("\"some text\"", getLLVMStyleWithColumns(6)));
7929   EXPECT_EQ("\"some\"\n"
7930             "\" tex\"\n"
7931             "\" and\"",
7932             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7933   EXPECT_EQ("\"some\"\n"
7934             "\"/tex\"\n"
7935             "\"/and\"",
7936             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7937 
7938   EXPECT_EQ("variable =\n"
7939             "    \"long string \"\n"
7940             "    \"literal\";",
7941             format("variable = \"long string literal\";",
7942                    getLLVMStyleWithColumns(20)));
7943 
7944   EXPECT_EQ("variable = f(\n"
7945             "    \"long string \"\n"
7946             "    \"literal\",\n"
7947             "    short,\n"
7948             "    loooooooooooooooooooong);",
7949             format("variable = f(\"long string literal\", short, "
7950                    "loooooooooooooooooooong);",
7951                    getLLVMStyleWithColumns(20)));
7952 
7953   EXPECT_EQ(
7954       "f(g(\"long string \"\n"
7955       "    \"literal\"),\n"
7956       "  b);",
7957       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7958   EXPECT_EQ("f(g(\"long string \"\n"
7959             "    \"literal\",\n"
7960             "    a),\n"
7961             "  b);",
7962             format("f(g(\"long string literal\", a), b);",
7963                    getLLVMStyleWithColumns(20)));
7964   EXPECT_EQ(
7965       "f(\"one two\".split(\n"
7966       "    variable));",
7967       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7968   EXPECT_EQ("f(\"one two three four five six \"\n"
7969             "  \"seven\".split(\n"
7970             "      really_looooong_variable));",
7971             format("f(\"one two three four five six seven\"."
7972                    "split(really_looooong_variable));",
7973                    getLLVMStyleWithColumns(33)));
7974 
7975   EXPECT_EQ("f(\"some \"\n"
7976             "  \"text\",\n"
7977             "  other);",
7978             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7979 
7980   // Only break as a last resort.
7981   verifyFormat(
7982       "aaaaaaaaaaaaaaaaaaaa(\n"
7983       "    aaaaaaaaaaaaaaaaaaaa,\n"
7984       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7985 
7986   EXPECT_EQ("\"splitmea\"\n"
7987             "\"trandomp\"\n"
7988             "\"oint\"",
7989             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7990 
7991   EXPECT_EQ("\"split/\"\n"
7992             "\"pathat/\"\n"
7993             "\"slashes\"",
7994             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7995 
7996   EXPECT_EQ("\"split/\"\n"
7997             "\"pathat/\"\n"
7998             "\"slashes\"",
7999             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8000   EXPECT_EQ("\"split at \"\n"
8001             "\"spaces/at/\"\n"
8002             "\"slashes.at.any$\"\n"
8003             "\"non-alphanumeric%\"\n"
8004             "\"1111111111characte\"\n"
8005             "\"rs\"",
8006             format("\"split at "
8007                    "spaces/at/"
8008                    "slashes.at."
8009                    "any$non-"
8010                    "alphanumeric%"
8011                    "1111111111characte"
8012                    "rs\"",
8013                    getLLVMStyleWithColumns(20)));
8014 
8015   // Verify that splitting the strings understands
8016   // Style::AlwaysBreakBeforeMultilineStrings.
8017   EXPECT_EQ(
8018       "aaaaaaaaaaaa(\n"
8019       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8020       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8021       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8022              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8023              "aaaaaaaaaaaaaaaaaaaaaa\");",
8024              getGoogleStyle()));
8025   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8026             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8027             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8028                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8029                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8030                    getGoogleStyle()));
8031   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8032             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8033             format("llvm::outs() << "
8034                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8035                    "aaaaaaaaaaaaaaaaaaa\";"));
8036   EXPECT_EQ("ffff(\n"
8037             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8038             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8039             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8040                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8041                    getGoogleStyle()));
8042 
8043   FormatStyle Style = getLLVMStyleWithColumns(12);
8044   Style.BreakStringLiterals = false;
8045   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8046 
8047   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8048   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8049   EXPECT_EQ("#define A \\\n"
8050             "  \"some \" \\\n"
8051             "  \"text \" \\\n"
8052             "  \"other\";",
8053             format("#define A \"some text other\";", AlignLeft));
8054 }
8055 
8056 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8057   EXPECT_EQ("C a = \"some more \"\n"
8058             "      \"text\";",
8059             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8060 }
8061 
8062 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8063   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8064   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8065   EXPECT_EQ("int i = a(b());",
8066             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8067 }
8068 
8069 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8070   EXPECT_EQ(
8071       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8072       "(\n"
8073       "    \"x\t\");",
8074       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8075              "aaaaaaa("
8076              "\"x\t\");"));
8077 }
8078 
8079 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8080   EXPECT_EQ(
8081       "u8\"utf8 string \"\n"
8082       "u8\"literal\";",
8083       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8084   EXPECT_EQ(
8085       "u\"utf16 string \"\n"
8086       "u\"literal\";",
8087       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8088   EXPECT_EQ(
8089       "U\"utf32 string \"\n"
8090       "U\"literal\";",
8091       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8092   EXPECT_EQ("L\"wide string \"\n"
8093             "L\"literal\";",
8094             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8095   EXPECT_EQ("@\"NSString \"\n"
8096             "@\"literal\";",
8097             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8098   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8099 
8100   // This input makes clang-format try to split the incomplete unicode escape
8101   // sequence, which used to lead to a crasher.
8102   verifyNoCrash(
8103       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8104       getLLVMStyleWithColumns(60));
8105 }
8106 
8107 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8108   FormatStyle Style = getGoogleStyleWithColumns(15);
8109   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8110   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8111   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8112   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8113   EXPECT_EQ("u8R\"x(raw literal)x\";",
8114             format("u8R\"x(raw literal)x\";", Style));
8115 }
8116 
8117 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8118   FormatStyle Style = getLLVMStyleWithColumns(20);
8119   EXPECT_EQ(
8120       "_T(\"aaaaaaaaaaaaaa\")\n"
8121       "_T(\"aaaaaaaaaaaaaa\")\n"
8122       "_T(\"aaaaaaaaaaaa\")",
8123       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8124   EXPECT_EQ("f(x,\n"
8125             "  _T(\"aaaaaaaaaaaa\")\n"
8126             "  _T(\"aaa\"),\n"
8127             "  z);",
8128             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8129 
8130   // FIXME: Handle embedded spaces in one iteration.
8131   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8132   //            "_T(\"aaaaaaaaaaaaa\")\n"
8133   //            "_T(\"aaaaaaaaaaaaa\")\n"
8134   //            "_T(\"a\")",
8135   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8136   //                   getLLVMStyleWithColumns(20)));
8137   EXPECT_EQ(
8138       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8139       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8140   EXPECT_EQ("f(\n"
8141             "#if !TEST\n"
8142             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8143             "#endif\n"
8144             ");",
8145             format("f(\n"
8146                    "#if !TEST\n"
8147                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8148                    "#endif\n"
8149                    ");"));
8150   EXPECT_EQ("f(\n"
8151             "\n"
8152             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8153             format("f(\n"
8154                    "\n"
8155                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8156 }
8157 
8158 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8159   // In a function call with two operands, the second can be broken with no line
8160   // break before it.
8161   EXPECT_EQ("func(a, \"long long \"\n"
8162             "        \"long long\");",
8163             format("func(a, \"long long long long\");",
8164                    getLLVMStyleWithColumns(24)));
8165   // In a function call with three operands, the second must be broken with a
8166   // line break before it.
8167   EXPECT_EQ("func(a,\n"
8168             "     \"long long long \"\n"
8169             "     \"long\",\n"
8170             "     c);",
8171             format("func(a, \"long long long long\", c);",
8172                    getLLVMStyleWithColumns(24)));
8173   // In a function call with three operands, the third must be broken with a
8174   // line break before it.
8175   EXPECT_EQ("func(a, b,\n"
8176             "     \"long long long \"\n"
8177             "     \"long\");",
8178             format("func(a, b, \"long long long long\");",
8179                    getLLVMStyleWithColumns(24)));
8180   // In a function call with three operands, both the second and the third must
8181   // be broken with a line break before them.
8182   EXPECT_EQ("func(a,\n"
8183             "     \"long long long \"\n"
8184             "     \"long\",\n"
8185             "     \"long long long \"\n"
8186             "     \"long\");",
8187             format("func(a, \"long long long long\", \"long long long long\");",
8188                    getLLVMStyleWithColumns(24)));
8189   // In a chain of << with two operands, the second can be broken with no line
8190   // break before it.
8191   EXPECT_EQ("a << \"line line \"\n"
8192             "     \"line\";",
8193             format("a << \"line line line\";",
8194                    getLLVMStyleWithColumns(20)));
8195   // In a chain of << with three operands, the second can be broken with no line
8196   // break before it.
8197   EXPECT_EQ("abcde << \"line \"\n"
8198             "         \"line line\"\n"
8199             "      << c;",
8200             format("abcde << \"line line line\" << c;",
8201                    getLLVMStyleWithColumns(20)));
8202   // In a chain of << with three operands, the third must be broken with a line
8203   // break before it.
8204   EXPECT_EQ("a << b\n"
8205             "  << \"line line \"\n"
8206             "     \"line\";",
8207             format("a << b << \"line line line\";",
8208                    getLLVMStyleWithColumns(20)));
8209   // In a chain of << with three operands, the second can be broken with no line
8210   // break before it and the third must be broken with a line break before it.
8211   EXPECT_EQ("abcd << \"line line \"\n"
8212             "        \"line\"\n"
8213             "     << \"line line \"\n"
8214             "        \"line\";",
8215             format("abcd << \"line line line\" << \"line line line\";",
8216                    getLLVMStyleWithColumns(20)));
8217   // In a chain of binary operators with two operands, the second can be broken
8218   // with no line break before it.
8219   EXPECT_EQ("abcd + \"line line \"\n"
8220             "       \"line line\";",
8221             format("abcd + \"line line line line\";",
8222                    getLLVMStyleWithColumns(20)));
8223   // In a chain of binary operators with three operands, the second must be
8224   // broken with a line break before it.
8225   EXPECT_EQ("abcd +\n"
8226             "    \"line line \"\n"
8227             "    \"line line\" +\n"
8228             "    e;",
8229             format("abcd + \"line line line line\" + e;",
8230                    getLLVMStyleWithColumns(20)));
8231   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8232   // the first must be broken with a line break before it.
8233   FormatStyle Style = getLLVMStyleWithColumns(25);
8234   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8235   EXPECT_EQ("someFunction(\n"
8236             "    \"long long long \"\n"
8237             "    \"long\",\n"
8238             "    a);",
8239             format("someFunction(\"long long long long\", a);", Style));
8240 }
8241 
8242 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8243   EXPECT_EQ(
8244       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8245       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8246       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8247       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8248              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8249              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8250 }
8251 
8252 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8253   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8254             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8255   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8256             "multiline raw string literal xxxxxxxxxxxxxx\n"
8257             ")x\",\n"
8258             "              a),\n"
8259             "            b);",
8260             format("fffffffffff(g(R\"x(\n"
8261                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8262                    ")x\", a), b);",
8263                    getGoogleStyleWithColumns(20)));
8264   EXPECT_EQ("fffffffffff(\n"
8265             "    g(R\"x(qqq\n"
8266             "multiline raw string literal xxxxxxxxxxxxxx\n"
8267             ")x\",\n"
8268             "      a),\n"
8269             "    b);",
8270             format("fffffffffff(g(R\"x(qqq\n"
8271                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8272                    ")x\", a), b);",
8273                    getGoogleStyleWithColumns(20)));
8274 
8275   EXPECT_EQ("fffffffffff(R\"x(\n"
8276             "multiline raw string literal xxxxxxxxxxxxxx\n"
8277             ")x\");",
8278             format("fffffffffff(R\"x(\n"
8279                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8280                    ")x\");",
8281                    getGoogleStyleWithColumns(20)));
8282   EXPECT_EQ("fffffffffff(R\"x(\n"
8283             "multiline raw string literal xxxxxxxxxxxxxx\n"
8284             ")x\" + bbbbbb);",
8285             format("fffffffffff(R\"x(\n"
8286                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8287                    ")x\" +   bbbbbb);",
8288                    getGoogleStyleWithColumns(20)));
8289   EXPECT_EQ("fffffffffff(\n"
8290             "    R\"x(\n"
8291             "multiline raw string literal xxxxxxxxxxxxxx\n"
8292             ")x\" +\n"
8293             "    bbbbbb);",
8294             format("fffffffffff(\n"
8295                    " R\"x(\n"
8296                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8297                    ")x\" + bbbbbb);",
8298                    getGoogleStyleWithColumns(20)));
8299   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8300             format("fffffffffff(\n"
8301                    " R\"(single line raw string)\" + bbbbbb);"));
8302 }
8303 
8304 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8305   verifyFormat("string a = \"unterminated;");
8306   EXPECT_EQ("function(\"unterminated,\n"
8307             "         OtherParameter);",
8308             format("function(  \"unterminated,\n"
8309                    "    OtherParameter);"));
8310 }
8311 
8312 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8313   FormatStyle Style = getLLVMStyle();
8314   Style.Standard = FormatStyle::LS_Cpp03;
8315   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8316             format("#define x(_a) printf(\"foo\"_a);", Style));
8317 }
8318 
8319 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8320 
8321 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8322   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8323             "             \"ddeeefff\");",
8324             format("someFunction(\"aaabbbcccdddeeefff\");",
8325                    getLLVMStyleWithColumns(25)));
8326   EXPECT_EQ("someFunction1234567890(\n"
8327             "    \"aaabbbcccdddeeefff\");",
8328             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8329                    getLLVMStyleWithColumns(26)));
8330   EXPECT_EQ("someFunction1234567890(\n"
8331             "    \"aaabbbcccdddeeeff\"\n"
8332             "    \"f\");",
8333             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8334                    getLLVMStyleWithColumns(25)));
8335   EXPECT_EQ("someFunction1234567890(\n"
8336             "    \"aaabbbcccdddeeeff\"\n"
8337             "    \"f\");",
8338             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8339                    getLLVMStyleWithColumns(24)));
8340   EXPECT_EQ("someFunction(\n"
8341             "    \"aaabbbcc ddde \"\n"
8342             "    \"efff\");",
8343             format("someFunction(\"aaabbbcc ddde efff\");",
8344                    getLLVMStyleWithColumns(25)));
8345   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8346             "             \"ddeeefff\");",
8347             format("someFunction(\"aaabbbccc ddeeefff\");",
8348                    getLLVMStyleWithColumns(25)));
8349   EXPECT_EQ("someFunction1234567890(\n"
8350             "    \"aaabb \"\n"
8351             "    \"cccdddeeefff\");",
8352             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8353                    getLLVMStyleWithColumns(25)));
8354   EXPECT_EQ("#define A          \\\n"
8355             "  string s =       \\\n"
8356             "      \"123456789\"  \\\n"
8357             "      \"0\";         \\\n"
8358             "  int i;",
8359             format("#define A string s = \"1234567890\"; int i;",
8360                    getLLVMStyleWithColumns(20)));
8361   EXPECT_EQ("someFunction(\n"
8362             "    \"aaabbbcc \"\n"
8363             "    \"dddeeefff\");",
8364             format("someFunction(\"aaabbbcc dddeeefff\");",
8365                    getLLVMStyleWithColumns(25)));
8366 }
8367 
8368 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8369   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8370   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8371   EXPECT_EQ("\"test\"\n"
8372             "\"\\n\"",
8373             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8374   EXPECT_EQ("\"tes\\\\\"\n"
8375             "\"n\"",
8376             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8377   EXPECT_EQ("\"\\\\\\\\\"\n"
8378             "\"\\n\"",
8379             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8380   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8381   EXPECT_EQ("\"\\uff01\"\n"
8382             "\"test\"",
8383             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8384   EXPECT_EQ("\"\\Uff01ff02\"",
8385             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8386   EXPECT_EQ("\"\\x000000000001\"\n"
8387             "\"next\"",
8388             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8389   EXPECT_EQ("\"\\x000000000001next\"",
8390             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8391   EXPECT_EQ("\"\\x000000000001\"",
8392             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8393   EXPECT_EQ("\"test\"\n"
8394             "\"\\000000\"\n"
8395             "\"000001\"",
8396             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8397   EXPECT_EQ("\"test\\000\"\n"
8398             "\"00000000\"\n"
8399             "\"1\"",
8400             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8401 }
8402 
8403 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8404   verifyFormat("void f() {\n"
8405                "  return g() {}\n"
8406                "  void h() {}");
8407   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8408                "g();\n"
8409                "}");
8410 }
8411 
8412 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8413   verifyFormat(
8414       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8415 }
8416 
8417 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8418   verifyFormat("class X {\n"
8419                "  void f() {\n"
8420                "  }\n"
8421                "};",
8422                getLLVMStyleWithColumns(12));
8423 }
8424 
8425 TEST_F(FormatTest, ConfigurableIndentWidth) {
8426   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8427   EightIndent.IndentWidth = 8;
8428   EightIndent.ContinuationIndentWidth = 8;
8429   verifyFormat("void f() {\n"
8430                "        someFunction();\n"
8431                "        if (true) {\n"
8432                "                f();\n"
8433                "        }\n"
8434                "}",
8435                EightIndent);
8436   verifyFormat("class X {\n"
8437                "        void f() {\n"
8438                "        }\n"
8439                "};",
8440                EightIndent);
8441   verifyFormat("int x[] = {\n"
8442                "        call(),\n"
8443                "        call()};",
8444                EightIndent);
8445 }
8446 
8447 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8448   verifyFormat("double\n"
8449                "f();",
8450                getLLVMStyleWithColumns(8));
8451 }
8452 
8453 TEST_F(FormatTest, ConfigurableUseOfTab) {
8454   FormatStyle Tab = getLLVMStyleWithColumns(42);
8455   Tab.IndentWidth = 8;
8456   Tab.UseTab = FormatStyle::UT_Always;
8457   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8458 
8459   EXPECT_EQ("if (aaaaaaaa && // q\n"
8460             "    bb)\t\t// w\n"
8461             "\t;",
8462             format("if (aaaaaaaa &&// q\n"
8463                    "bb)// w\n"
8464                    ";",
8465                    Tab));
8466   EXPECT_EQ("if (aaa && bbb) // w\n"
8467             "\t;",
8468             format("if(aaa&&bbb)// w\n"
8469                    ";",
8470                    Tab));
8471 
8472   verifyFormat("class X {\n"
8473                "\tvoid f() {\n"
8474                "\t\tsomeFunction(parameter1,\n"
8475                "\t\t\t     parameter2);\n"
8476                "\t}\n"
8477                "};",
8478                Tab);
8479   verifyFormat("#define A                        \\\n"
8480                "\tvoid f() {               \\\n"
8481                "\t\tsomeFunction(    \\\n"
8482                "\t\t    parameter1,  \\\n"
8483                "\t\t    parameter2); \\\n"
8484                "\t}",
8485                Tab);
8486 
8487   Tab.TabWidth = 4;
8488   Tab.IndentWidth = 8;
8489   verifyFormat("class TabWidth4Indent8 {\n"
8490                "\t\tvoid f() {\n"
8491                "\t\t\t\tsomeFunction(parameter1,\n"
8492                "\t\t\t\t\t\t\t parameter2);\n"
8493                "\t\t}\n"
8494                "};",
8495                Tab);
8496 
8497   Tab.TabWidth = 4;
8498   Tab.IndentWidth = 4;
8499   verifyFormat("class TabWidth4Indent4 {\n"
8500                "\tvoid f() {\n"
8501                "\t\tsomeFunction(parameter1,\n"
8502                "\t\t\t\t\t parameter2);\n"
8503                "\t}\n"
8504                "};",
8505                Tab);
8506 
8507   Tab.TabWidth = 8;
8508   Tab.IndentWidth = 4;
8509   verifyFormat("class TabWidth8Indent4 {\n"
8510                "    void f() {\n"
8511                "\tsomeFunction(parameter1,\n"
8512                "\t\t     parameter2);\n"
8513                "    }\n"
8514                "};",
8515                Tab);
8516 
8517   Tab.TabWidth = 8;
8518   Tab.IndentWidth = 8;
8519   EXPECT_EQ("/*\n"
8520             "\t      a\t\tcomment\n"
8521             "\t      in multiple lines\n"
8522             "       */",
8523             format("   /*\t \t \n"
8524                    " \t \t a\t\tcomment\t \t\n"
8525                    " \t \t in multiple lines\t\n"
8526                    " \t  */",
8527                    Tab));
8528 
8529   Tab.UseTab = FormatStyle::UT_ForIndentation;
8530   verifyFormat("{\n"
8531                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8532                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8533                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8534                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8535                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8536                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8537                "};",
8538                Tab);
8539   verifyFormat("enum AA {\n"
8540                "\ta1, // Force multiple lines\n"
8541                "\ta2,\n"
8542                "\ta3\n"
8543                "};",
8544                Tab);
8545   EXPECT_EQ("if (aaaaaaaa && // q\n"
8546             "    bb)         // w\n"
8547             "\t;",
8548             format("if (aaaaaaaa &&// q\n"
8549                    "bb)// w\n"
8550                    ";",
8551                    Tab));
8552   verifyFormat("class X {\n"
8553                "\tvoid f() {\n"
8554                "\t\tsomeFunction(parameter1,\n"
8555                "\t\t             parameter2);\n"
8556                "\t}\n"
8557                "};",
8558                Tab);
8559   verifyFormat("{\n"
8560                "\tQ(\n"
8561                "\t    {\n"
8562                "\t\t    int a;\n"
8563                "\t\t    someFunction(aaaaaaaa,\n"
8564                "\t\t                 bbbbbbb);\n"
8565                "\t    },\n"
8566                "\t    p);\n"
8567                "}",
8568                Tab);
8569   EXPECT_EQ("{\n"
8570             "\t/* aaaa\n"
8571             "\t   bbbb */\n"
8572             "}",
8573             format("{\n"
8574                    "/* aaaa\n"
8575                    "   bbbb */\n"
8576                    "}",
8577                    Tab));
8578   EXPECT_EQ("{\n"
8579             "\t/*\n"
8580             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8581             "\t  bbbbbbbbbbbbb\n"
8582             "\t*/\n"
8583             "}",
8584             format("{\n"
8585                    "/*\n"
8586                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8587                    "*/\n"
8588                    "}",
8589                    Tab));
8590   EXPECT_EQ("{\n"
8591             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8592             "\t// bbbbbbbbbbbbb\n"
8593             "}",
8594             format("{\n"
8595                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8596                    "}",
8597                    Tab));
8598   EXPECT_EQ("{\n"
8599             "\t/*\n"
8600             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8601             "\t  bbbbbbbbbbbbb\n"
8602             "\t*/\n"
8603             "}",
8604             format("{\n"
8605                    "\t/*\n"
8606                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8607                    "\t*/\n"
8608                    "}",
8609                    Tab));
8610   EXPECT_EQ("{\n"
8611             "\t/*\n"
8612             "\n"
8613             "\t*/\n"
8614             "}",
8615             format("{\n"
8616                    "\t/*\n"
8617                    "\n"
8618                    "\t*/\n"
8619                    "}",
8620                    Tab));
8621   EXPECT_EQ("{\n"
8622             "\t/*\n"
8623             " asdf\n"
8624             "\t*/\n"
8625             "}",
8626             format("{\n"
8627                    "\t/*\n"
8628                    " asdf\n"
8629                    "\t*/\n"
8630                    "}",
8631                    Tab));
8632 
8633   Tab.UseTab = FormatStyle::UT_Never;
8634   EXPECT_EQ("/*\n"
8635             "              a\t\tcomment\n"
8636             "              in multiple lines\n"
8637             "       */",
8638             format("   /*\t \t \n"
8639                    " \t \t a\t\tcomment\t \t\n"
8640                    " \t \t in multiple lines\t\n"
8641                    " \t  */",
8642                    Tab));
8643   EXPECT_EQ("/* some\n"
8644             "   comment */",
8645             format(" \t \t /* some\n"
8646                    " \t \t    comment */",
8647                    Tab));
8648   EXPECT_EQ("int a; /* some\n"
8649             "   comment */",
8650             format(" \t \t int a; /* some\n"
8651                    " \t \t    comment */",
8652                    Tab));
8653 
8654   EXPECT_EQ("int a; /* some\n"
8655             "comment */",
8656             format(" \t \t int\ta; /* some\n"
8657                    " \t \t    comment */",
8658                    Tab));
8659   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8660             "    comment */",
8661             format(" \t \t f(\"\t\t\"); /* some\n"
8662                    " \t \t    comment */",
8663                    Tab));
8664   EXPECT_EQ("{\n"
8665             "  /*\n"
8666             "   * Comment\n"
8667             "   */\n"
8668             "  int i;\n"
8669             "}",
8670             format("{\n"
8671                    "\t/*\n"
8672                    "\t * Comment\n"
8673                    "\t */\n"
8674                    "\t int i;\n"
8675                    "}"));
8676 
8677   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8678   Tab.TabWidth = 8;
8679   Tab.IndentWidth = 8;
8680   EXPECT_EQ("if (aaaaaaaa && // q\n"
8681             "    bb)         // w\n"
8682             "\t;",
8683             format("if (aaaaaaaa &&// q\n"
8684                    "bb)// w\n"
8685                    ";",
8686                    Tab));
8687   EXPECT_EQ("if (aaa && bbb) // w\n"
8688             "\t;",
8689             format("if(aaa&&bbb)// w\n"
8690                    ";",
8691                    Tab));
8692   verifyFormat("class X {\n"
8693                "\tvoid f() {\n"
8694                "\t\tsomeFunction(parameter1,\n"
8695                "\t\t\t     parameter2);\n"
8696                "\t}\n"
8697                "};",
8698                Tab);
8699   verifyFormat("#define A                        \\\n"
8700                "\tvoid f() {               \\\n"
8701                "\t\tsomeFunction(    \\\n"
8702                "\t\t    parameter1,  \\\n"
8703                "\t\t    parameter2); \\\n"
8704                "\t}",
8705                Tab);
8706   Tab.TabWidth = 4;
8707   Tab.IndentWidth = 8;
8708   verifyFormat("class TabWidth4Indent8 {\n"
8709                "\t\tvoid f() {\n"
8710                "\t\t\t\tsomeFunction(parameter1,\n"
8711                "\t\t\t\t\t\t\t parameter2);\n"
8712                "\t\t}\n"
8713                "};",
8714                Tab);
8715   Tab.TabWidth = 4;
8716   Tab.IndentWidth = 4;
8717   verifyFormat("class TabWidth4Indent4 {\n"
8718                "\tvoid f() {\n"
8719                "\t\tsomeFunction(parameter1,\n"
8720                "\t\t\t\t\t parameter2);\n"
8721                "\t}\n"
8722                "};",
8723                Tab);
8724   Tab.TabWidth = 8;
8725   Tab.IndentWidth = 4;
8726   verifyFormat("class TabWidth8Indent4 {\n"
8727                "    void f() {\n"
8728                "\tsomeFunction(parameter1,\n"
8729                "\t\t     parameter2);\n"
8730                "    }\n"
8731                "};",
8732                Tab);
8733   Tab.TabWidth = 8;
8734   Tab.IndentWidth = 8;
8735   EXPECT_EQ("/*\n"
8736             "\t      a\t\tcomment\n"
8737             "\t      in multiple lines\n"
8738             "       */",
8739             format("   /*\t \t \n"
8740                    " \t \t a\t\tcomment\t \t\n"
8741                    " \t \t in multiple lines\t\n"
8742                    " \t  */",
8743                    Tab));
8744   verifyFormat("{\n"
8745                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8746                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8747                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8748                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8749                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8750                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8751                "};",
8752                Tab);
8753   verifyFormat("enum AA {\n"
8754                "\ta1, // Force multiple lines\n"
8755                "\ta2,\n"
8756                "\ta3\n"
8757                "};",
8758                Tab);
8759   EXPECT_EQ("if (aaaaaaaa && // q\n"
8760             "    bb)         // w\n"
8761             "\t;",
8762             format("if (aaaaaaaa &&// q\n"
8763                    "bb)// w\n"
8764                    ";",
8765                    Tab));
8766   verifyFormat("class X {\n"
8767                "\tvoid f() {\n"
8768                "\t\tsomeFunction(parameter1,\n"
8769                "\t\t\t     parameter2);\n"
8770                "\t}\n"
8771                "};",
8772                Tab);
8773   verifyFormat("{\n"
8774                "\tQ(\n"
8775                "\t    {\n"
8776                "\t\t    int a;\n"
8777                "\t\t    someFunction(aaaaaaaa,\n"
8778                "\t\t\t\t bbbbbbb);\n"
8779                "\t    },\n"
8780                "\t    p);\n"
8781                "}",
8782                Tab);
8783   EXPECT_EQ("{\n"
8784             "\t/* aaaa\n"
8785             "\t   bbbb */\n"
8786             "}",
8787             format("{\n"
8788                    "/* aaaa\n"
8789                    "   bbbb */\n"
8790                    "}",
8791                    Tab));
8792   EXPECT_EQ("{\n"
8793             "\t/*\n"
8794             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8795             "\t  bbbbbbbbbbbbb\n"
8796             "\t*/\n"
8797             "}",
8798             format("{\n"
8799                    "/*\n"
8800                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8801                    "*/\n"
8802                    "}",
8803                    Tab));
8804   EXPECT_EQ("{\n"
8805             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8806             "\t// bbbbbbbbbbbbb\n"
8807             "}",
8808             format("{\n"
8809                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8810                    "}",
8811                    Tab));
8812   EXPECT_EQ("{\n"
8813             "\t/*\n"
8814             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8815             "\t  bbbbbbbbbbbbb\n"
8816             "\t*/\n"
8817             "}",
8818             format("{\n"
8819                    "\t/*\n"
8820                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8821                    "\t*/\n"
8822                    "}",
8823                    Tab));
8824   EXPECT_EQ("{\n"
8825             "\t/*\n"
8826             "\n"
8827             "\t*/\n"
8828             "}",
8829             format("{\n"
8830                    "\t/*\n"
8831                    "\n"
8832                    "\t*/\n"
8833                    "}",
8834                    Tab));
8835   EXPECT_EQ("{\n"
8836             "\t/*\n"
8837             " asdf\n"
8838             "\t*/\n"
8839             "}",
8840             format("{\n"
8841                    "\t/*\n"
8842                    " asdf\n"
8843                    "\t*/\n"
8844                    "}",
8845                    Tab));
8846   EXPECT_EQ("/*\n"
8847             "\t      a\t\tcomment\n"
8848             "\t      in multiple lines\n"
8849             "       */",
8850             format("   /*\t \t \n"
8851                    " \t \t a\t\tcomment\t \t\n"
8852                    " \t \t in multiple lines\t\n"
8853                    " \t  */",
8854                    Tab));
8855   EXPECT_EQ("/* some\n"
8856             "   comment */",
8857             format(" \t \t /* some\n"
8858                    " \t \t    comment */",
8859                    Tab));
8860   EXPECT_EQ("int a; /* some\n"
8861             "   comment */",
8862             format(" \t \t int a; /* some\n"
8863                    " \t \t    comment */",
8864                    Tab));
8865   EXPECT_EQ("int a; /* some\n"
8866             "comment */",
8867             format(" \t \t int\ta; /* some\n"
8868                    " \t \t    comment */",
8869                    Tab));
8870   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8871             "    comment */",
8872             format(" \t \t f(\"\t\t\"); /* some\n"
8873                    " \t \t    comment */",
8874                    Tab));
8875   EXPECT_EQ("{\n"
8876             "  /*\n"
8877             "   * Comment\n"
8878             "   */\n"
8879             "  int i;\n"
8880             "}",
8881             format("{\n"
8882                    "\t/*\n"
8883                    "\t * Comment\n"
8884                    "\t */\n"
8885                    "\t int i;\n"
8886                    "}"));
8887   Tab.AlignConsecutiveAssignments = true;
8888   Tab.AlignConsecutiveDeclarations = true;
8889   Tab.TabWidth = 4;
8890   Tab.IndentWidth = 4;
8891   verifyFormat("class Assign {\n"
8892                "\tvoid f() {\n"
8893                "\t\tint         x      = 123;\n"
8894                "\t\tint         random = 4;\n"
8895                "\t\tstd::string alphabet =\n"
8896                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8897                "\t}\n"
8898                "};",
8899                Tab);
8900 }
8901 
8902 TEST_F(FormatTest, CalculatesOriginalColumn) {
8903   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8904             "q\"; /* some\n"
8905             "       comment */",
8906             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8907                    "q\"; /* some\n"
8908                    "       comment */",
8909                    getLLVMStyle()));
8910   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8911             "/* some\n"
8912             "   comment */",
8913             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8914                    " /* some\n"
8915                    "    comment */",
8916                    getLLVMStyle()));
8917   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8918             "qqq\n"
8919             "/* some\n"
8920             "   comment */",
8921             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8922                    "qqq\n"
8923                    " /* some\n"
8924                    "    comment */",
8925                    getLLVMStyle()));
8926   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8927             "wwww; /* some\n"
8928             "         comment */",
8929             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8930                    "wwww; /* some\n"
8931                    "         comment */",
8932                    getLLVMStyle()));
8933 }
8934 
8935 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8936   FormatStyle NoSpace = getLLVMStyle();
8937   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8938 
8939   verifyFormat("while(true)\n"
8940                "  continue;",
8941                NoSpace);
8942   verifyFormat("for(;;)\n"
8943                "  continue;",
8944                NoSpace);
8945   verifyFormat("if(true)\n"
8946                "  f();\n"
8947                "else if(true)\n"
8948                "  f();",
8949                NoSpace);
8950   verifyFormat("do {\n"
8951                "  do_something();\n"
8952                "} while(something());",
8953                NoSpace);
8954   verifyFormat("switch(x) {\n"
8955                "default:\n"
8956                "  break;\n"
8957                "}",
8958                NoSpace);
8959   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8960   verifyFormat("size_t x = sizeof(x);", NoSpace);
8961   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8962   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8963   verifyFormat("alignas(128) char a[128];", NoSpace);
8964   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8965   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8966   verifyFormat("int f() throw(Deprecated);", NoSpace);
8967   verifyFormat("typedef void (*cb)(int);", NoSpace);
8968   verifyFormat("T A::operator()();", NoSpace);
8969   verifyFormat("X A::operator++(T);", NoSpace);
8970 
8971   FormatStyle Space = getLLVMStyle();
8972   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8973 
8974   verifyFormat("int f ();", Space);
8975   verifyFormat("void f (int a, T b) {\n"
8976                "  while (true)\n"
8977                "    continue;\n"
8978                "}",
8979                Space);
8980   verifyFormat("if (true)\n"
8981                "  f ();\n"
8982                "else if (true)\n"
8983                "  f ();",
8984                Space);
8985   verifyFormat("do {\n"
8986                "  do_something ();\n"
8987                "} while (something ());",
8988                Space);
8989   verifyFormat("switch (x) {\n"
8990                "default:\n"
8991                "  break;\n"
8992                "}",
8993                Space);
8994   verifyFormat("A::A () : a (1) {}", Space);
8995   verifyFormat("void f () __attribute__ ((asdf));", Space);
8996   verifyFormat("*(&a + 1);\n"
8997                "&((&a)[1]);\n"
8998                "a[(b + c) * d];\n"
8999                "(((a + 1) * 2) + 3) * 4;",
9000                Space);
9001   verifyFormat("#define A(x) x", Space);
9002   verifyFormat("#define A (x) x", Space);
9003   verifyFormat("#if defined(x)\n"
9004                "#endif",
9005                Space);
9006   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9007   verifyFormat("size_t x = sizeof (x);", Space);
9008   verifyFormat("auto f (int x) -> decltype (x);", Space);
9009   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9010   verifyFormat("alignas (128) char a[128];", Space);
9011   verifyFormat("size_t x = alignof (MyType);", Space);
9012   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9013   verifyFormat("int f () throw (Deprecated);", Space);
9014   verifyFormat("typedef void (*cb) (int);", Space);
9015   verifyFormat("T A::operator() ();", Space);
9016   verifyFormat("X A::operator++ (T);", Space);
9017 }
9018 
9019 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9020   FormatStyle Spaces = getLLVMStyle();
9021 
9022   Spaces.SpacesInParentheses = true;
9023   verifyFormat("do_something( ::globalVar );", Spaces);
9024   verifyFormat("call( x, y, z );", Spaces);
9025   verifyFormat("call();", Spaces);
9026   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9027   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9028                Spaces);
9029   verifyFormat("while ( (bool)1 )\n"
9030                "  continue;",
9031                Spaces);
9032   verifyFormat("for ( ;; )\n"
9033                "  continue;",
9034                Spaces);
9035   verifyFormat("if ( true )\n"
9036                "  f();\n"
9037                "else if ( true )\n"
9038                "  f();",
9039                Spaces);
9040   verifyFormat("do {\n"
9041                "  do_something( (int)i );\n"
9042                "} while ( something() );",
9043                Spaces);
9044   verifyFormat("switch ( x ) {\n"
9045                "default:\n"
9046                "  break;\n"
9047                "}",
9048                Spaces);
9049 
9050   Spaces.SpacesInParentheses = false;
9051   Spaces.SpacesInCStyleCastParentheses = true;
9052   verifyFormat("Type *A = ( Type * )P;", Spaces);
9053   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9054   verifyFormat("x = ( int32 )y;", Spaces);
9055   verifyFormat("int a = ( int )(2.0f);", Spaces);
9056   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9057   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9058   verifyFormat("#define x (( int )-1)", Spaces);
9059 
9060   // Run the first set of tests again with:
9061   Spaces.SpacesInParentheses = false;
9062   Spaces.SpaceInEmptyParentheses = true;
9063   Spaces.SpacesInCStyleCastParentheses = true;
9064   verifyFormat("call(x, y, z);", Spaces);
9065   verifyFormat("call( );", Spaces);
9066   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9067   verifyFormat("while (( bool )1)\n"
9068                "  continue;",
9069                Spaces);
9070   verifyFormat("for (;;)\n"
9071                "  continue;",
9072                Spaces);
9073   verifyFormat("if (true)\n"
9074                "  f( );\n"
9075                "else if (true)\n"
9076                "  f( );",
9077                Spaces);
9078   verifyFormat("do {\n"
9079                "  do_something(( int )i);\n"
9080                "} while (something( ));",
9081                Spaces);
9082   verifyFormat("switch (x) {\n"
9083                "default:\n"
9084                "  break;\n"
9085                "}",
9086                Spaces);
9087 
9088   // Run the first set of tests again with:
9089   Spaces.SpaceAfterCStyleCast = true;
9090   verifyFormat("call(x, y, z);", Spaces);
9091   verifyFormat("call( );", Spaces);
9092   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9093   verifyFormat("while (( bool ) 1)\n"
9094                "  continue;",
9095                Spaces);
9096   verifyFormat("for (;;)\n"
9097                "  continue;",
9098                Spaces);
9099   verifyFormat("if (true)\n"
9100                "  f( );\n"
9101                "else if (true)\n"
9102                "  f( );",
9103                Spaces);
9104   verifyFormat("do {\n"
9105                "  do_something(( int ) i);\n"
9106                "} while (something( ));",
9107                Spaces);
9108   verifyFormat("switch (x) {\n"
9109                "default:\n"
9110                "  break;\n"
9111                "}",
9112                Spaces);
9113 
9114   // Run subset of tests again with:
9115   Spaces.SpacesInCStyleCastParentheses = false;
9116   Spaces.SpaceAfterCStyleCast = true;
9117   verifyFormat("while ((bool) 1)\n"
9118                "  continue;",
9119                Spaces);
9120   verifyFormat("do {\n"
9121                "  do_something((int) i);\n"
9122                "} while (something( ));",
9123                Spaces);
9124 }
9125 
9126 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9127   verifyFormat("int a[5];");
9128   verifyFormat("a[3] += 42;");
9129 
9130   FormatStyle Spaces = getLLVMStyle();
9131   Spaces.SpacesInSquareBrackets = true;
9132   // Lambdas unchanged.
9133   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9134   verifyFormat("return [i, args...] {};", Spaces);
9135 
9136   // Not lambdas.
9137   verifyFormat("int a[ 5 ];", Spaces);
9138   verifyFormat("a[ 3 ] += 42;", Spaces);
9139   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9140   verifyFormat("double &operator[](int i) { return 0; }\n"
9141                "int i;",
9142                Spaces);
9143   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9144   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9145   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9146 }
9147 
9148 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9149   verifyFormat("int a = 5;");
9150   verifyFormat("a += 42;");
9151   verifyFormat("a or_eq 8;");
9152 
9153   FormatStyle Spaces = getLLVMStyle();
9154   Spaces.SpaceBeforeAssignmentOperators = false;
9155   verifyFormat("int a= 5;", Spaces);
9156   verifyFormat("a+= 42;", Spaces);
9157   verifyFormat("a or_eq 8;", Spaces);
9158 }
9159 
9160 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9161   verifyFormat("class Foo : public Bar {};");
9162   verifyFormat("Foo::Foo() : foo(1) {}");
9163   verifyFormat("for (auto a : b) {\n}");
9164   verifyFormat("int x = a ? b : c;");
9165   verifyFormat("{\n"
9166                "label0:\n"
9167                "  int x = 0;\n"
9168                "}");
9169   verifyFormat("switch (x) {\n"
9170                "case 1:\n"
9171                "default:\n"
9172                "}");
9173 
9174   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9175   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9176   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9177   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9178   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9179   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9180   verifyFormat("{\n"
9181                "label1:\n"
9182                "  int x = 0;\n"
9183                "}",
9184                CtorInitializerStyle);
9185   verifyFormat("switch (x) {\n"
9186                "case 1:\n"
9187                "default:\n"
9188                "}",
9189                CtorInitializerStyle);
9190   CtorInitializerStyle.BreakConstructorInitializers =
9191       FormatStyle::BCIS_AfterColon;
9192   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9193                "    aaaaaaaaaaaaaaaa(1),\n"
9194                "    bbbbbbbbbbbbbbbb(2) {}",
9195                CtorInitializerStyle);
9196   CtorInitializerStyle.BreakConstructorInitializers =
9197       FormatStyle::BCIS_BeforeComma;
9198   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9199                "    : aaaaaaaaaaaaaaaa(1)\n"
9200                "    , bbbbbbbbbbbbbbbb(2) {}",
9201                CtorInitializerStyle);
9202   CtorInitializerStyle.BreakConstructorInitializers =
9203       FormatStyle::BCIS_BeforeColon;
9204   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9205                "    : aaaaaaaaaaaaaaaa(1),\n"
9206                "      bbbbbbbbbbbbbbbb(2) {}",
9207                CtorInitializerStyle);
9208   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9209   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9210                ": aaaaaaaaaaaaaaaa(1),\n"
9211                "  bbbbbbbbbbbbbbbb(2) {}",
9212                CtorInitializerStyle);
9213 
9214   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9215   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9216   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9217   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9218   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9219   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9220   verifyFormat("{\n"
9221                "label2:\n"
9222                "  int x = 0;\n"
9223                "}",
9224                InheritanceStyle);
9225   verifyFormat("switch (x) {\n"
9226                "case 1:\n"
9227                "default:\n"
9228                "}",
9229                InheritanceStyle);
9230   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9231   verifyFormat("class Foooooooooooooooooooooo:\n"
9232                "    public aaaaaaaaaaaaaaaaaa,\n"
9233                "    public bbbbbbbbbbbbbbbbbb {\n"
9234                "}",
9235                InheritanceStyle);
9236   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9237   verifyFormat("class Foooooooooooooooooooooo\n"
9238                "    : public aaaaaaaaaaaaaaaaaa\n"
9239                "    , public bbbbbbbbbbbbbbbbbb {\n"
9240                "}",
9241                InheritanceStyle);
9242   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9243   verifyFormat("class Foooooooooooooooooooooo\n"
9244                "    : public aaaaaaaaaaaaaaaaaa,\n"
9245                "      public bbbbbbbbbbbbbbbbbb {\n"
9246                "}",
9247                InheritanceStyle);
9248   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9249   verifyFormat("class Foooooooooooooooooooooo\n"
9250                ": public aaaaaaaaaaaaaaaaaa,\n"
9251                "  public bbbbbbbbbbbbbbbbbb {}",
9252                InheritanceStyle);
9253 
9254   FormatStyle ForLoopStyle = getLLVMStyle();
9255   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9256   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9257   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9258   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9259   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9260   verifyFormat("{\n"
9261                "label2:\n"
9262                "  int x = 0;\n"
9263                "}",
9264                ForLoopStyle);
9265   verifyFormat("switch (x) {\n"
9266                "case 1:\n"
9267                "default:\n"
9268                "}",
9269                ForLoopStyle);
9270 
9271   FormatStyle NoSpaceStyle = getLLVMStyle();
9272   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9273   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9274   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9275   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9276   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9277   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9278   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9279   verifyFormat("{\n"
9280                "label3:\n"
9281                "  int x = 0;\n"
9282                "}",
9283                NoSpaceStyle);
9284   verifyFormat("switch (x) {\n"
9285                "case 1:\n"
9286                "default:\n"
9287                "}",
9288                NoSpaceStyle);
9289 }
9290 
9291 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9292   FormatStyle Alignment = getLLVMStyle();
9293   Alignment.AlignConsecutiveAssignments = false;
9294   verifyFormat("int a = 5;\n"
9295                "int oneTwoThree = 123;",
9296                Alignment);
9297   verifyFormat("int a = 5;\n"
9298                "int oneTwoThree = 123;",
9299                Alignment);
9300 
9301   Alignment.AlignConsecutiveAssignments = true;
9302   verifyFormat("int a           = 5;\n"
9303                "int oneTwoThree = 123;",
9304                Alignment);
9305   verifyFormat("int a           = method();\n"
9306                "int oneTwoThree = 133;",
9307                Alignment);
9308   verifyFormat("a &= 5;\n"
9309                "bcd *= 5;\n"
9310                "ghtyf += 5;\n"
9311                "dvfvdb -= 5;\n"
9312                "a /= 5;\n"
9313                "vdsvsv %= 5;\n"
9314                "sfdbddfbdfbb ^= 5;\n"
9315                "dvsdsv |= 5;\n"
9316                "int dsvvdvsdvvv = 123;",
9317                Alignment);
9318   verifyFormat("int i = 1, j = 10;\n"
9319                "something = 2000;",
9320                Alignment);
9321   verifyFormat("something = 2000;\n"
9322                "int i = 1, j = 10;\n",
9323                Alignment);
9324   verifyFormat("something = 2000;\n"
9325                "another   = 911;\n"
9326                "int i = 1, j = 10;\n"
9327                "oneMore = 1;\n"
9328                "i       = 2;",
9329                Alignment);
9330   verifyFormat("int a   = 5;\n"
9331                "int one = 1;\n"
9332                "method();\n"
9333                "int oneTwoThree = 123;\n"
9334                "int oneTwo      = 12;",
9335                Alignment);
9336   verifyFormat("int oneTwoThree = 123;\n"
9337                "int oneTwo      = 12;\n"
9338                "method();\n",
9339                Alignment);
9340   verifyFormat("int oneTwoThree = 123; // comment\n"
9341                "int oneTwo      = 12;  // comment",
9342                Alignment);
9343   EXPECT_EQ("int a = 5;\n"
9344             "\n"
9345             "int oneTwoThree = 123;",
9346             format("int a       = 5;\n"
9347                    "\n"
9348                    "int oneTwoThree= 123;",
9349                    Alignment));
9350   EXPECT_EQ("int a   = 5;\n"
9351             "int one = 1;\n"
9352             "\n"
9353             "int oneTwoThree = 123;",
9354             format("int a = 5;\n"
9355                    "int one = 1;\n"
9356                    "\n"
9357                    "int oneTwoThree = 123;",
9358                    Alignment));
9359   EXPECT_EQ("int a   = 5;\n"
9360             "int one = 1;\n"
9361             "\n"
9362             "int oneTwoThree = 123;\n"
9363             "int oneTwo      = 12;",
9364             format("int a = 5;\n"
9365                    "int one = 1;\n"
9366                    "\n"
9367                    "int oneTwoThree = 123;\n"
9368                    "int oneTwo = 12;",
9369                    Alignment));
9370   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9371   verifyFormat("#define A \\\n"
9372                "  int aaaa       = 12; \\\n"
9373                "  int b          = 23; \\\n"
9374                "  int ccc        = 234; \\\n"
9375                "  int dddddddddd = 2345;",
9376                Alignment);
9377   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9378   verifyFormat("#define A               \\\n"
9379                "  int aaaa       = 12;  \\\n"
9380                "  int b          = 23;  \\\n"
9381                "  int ccc        = 234; \\\n"
9382                "  int dddddddddd = 2345;",
9383                Alignment);
9384   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9385   verifyFormat("#define A                                                      "
9386                "                \\\n"
9387                "  int aaaa       = 12;                                         "
9388                "                \\\n"
9389                "  int b          = 23;                                         "
9390                "                \\\n"
9391                "  int ccc        = 234;                                        "
9392                "                \\\n"
9393                "  int dddddddddd = 2345;",
9394                Alignment);
9395   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9396                "k = 4, int l = 5,\n"
9397                "                  int m = 6) {\n"
9398                "  int j      = 10;\n"
9399                "  otherThing = 1;\n"
9400                "}",
9401                Alignment);
9402   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9403                "  int i   = 1;\n"
9404                "  int j   = 2;\n"
9405                "  int big = 10000;\n"
9406                "}",
9407                Alignment);
9408   verifyFormat("class C {\n"
9409                "public:\n"
9410                "  int i            = 1;\n"
9411                "  virtual void f() = 0;\n"
9412                "};",
9413                Alignment);
9414   verifyFormat("int i = 1;\n"
9415                "if (SomeType t = getSomething()) {\n"
9416                "}\n"
9417                "int j   = 2;\n"
9418                "int big = 10000;",
9419                Alignment);
9420   verifyFormat("int j = 7;\n"
9421                "for (int k = 0; k < N; ++k) {\n"
9422                "}\n"
9423                "int j   = 2;\n"
9424                "int big = 10000;\n"
9425                "}",
9426                Alignment);
9427   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9428   verifyFormat("int i = 1;\n"
9429                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9430                "    = someLooooooooooooooooongFunction();\n"
9431                "int j = 2;",
9432                Alignment);
9433   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9434   verifyFormat("int i = 1;\n"
9435                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9436                "    someLooooooooooooooooongFunction();\n"
9437                "int j = 2;",
9438                Alignment);
9439 
9440   verifyFormat("auto lambda = []() {\n"
9441                "  auto i = 0;\n"
9442                "  return 0;\n"
9443                "};\n"
9444                "int i  = 0;\n"
9445                "auto v = type{\n"
9446                "    i = 1,   //\n"
9447                "    (i = 2), //\n"
9448                "    i = 3    //\n"
9449                "};",
9450                Alignment);
9451 
9452   verifyFormat(
9453       "int i      = 1;\n"
9454       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9455       "                          loooooooooooooooooooooongParameterB);\n"
9456       "int j      = 2;",
9457       Alignment);
9458 
9459   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9460                "          typename B   = very_long_type_name_1,\n"
9461                "          typename T_2 = very_long_type_name_2>\n"
9462                "auto foo() {}\n",
9463                Alignment);
9464   verifyFormat("int a, b = 1;\n"
9465                "int c  = 2;\n"
9466                "int dd = 3;\n",
9467                Alignment);
9468   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9469                "float b[1][] = {{3.f}};\n",
9470                Alignment);
9471   verifyFormat("for (int i = 0; i < 1; i++)\n"
9472                "  int x = 1;\n",
9473                Alignment);
9474   verifyFormat("for (i = 0; i < 1; i++)\n"
9475                "  x = 1;\n"
9476                "y = 1;\n",
9477                Alignment);
9478 }
9479 
9480 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9481   FormatStyle Alignment = getLLVMStyle();
9482   Alignment.AlignConsecutiveDeclarations = false;
9483   verifyFormat("float const a = 5;\n"
9484                "int oneTwoThree = 123;",
9485                Alignment);
9486   verifyFormat("int a = 5;\n"
9487                "float const oneTwoThree = 123;",
9488                Alignment);
9489 
9490   Alignment.AlignConsecutiveDeclarations = true;
9491   verifyFormat("float const a = 5;\n"
9492                "int         oneTwoThree = 123;",
9493                Alignment);
9494   verifyFormat("int         a = method();\n"
9495                "float const oneTwoThree = 133;",
9496                Alignment);
9497   verifyFormat("int i = 1, j = 10;\n"
9498                "something = 2000;",
9499                Alignment);
9500   verifyFormat("something = 2000;\n"
9501                "int i = 1, j = 10;\n",
9502                Alignment);
9503   verifyFormat("float      something = 2000;\n"
9504                "double     another = 911;\n"
9505                "int        i = 1, j = 10;\n"
9506                "const int *oneMore = 1;\n"
9507                "unsigned   i = 2;",
9508                Alignment);
9509   verifyFormat("float a = 5;\n"
9510                "int   one = 1;\n"
9511                "method();\n"
9512                "const double       oneTwoThree = 123;\n"
9513                "const unsigned int oneTwo = 12;",
9514                Alignment);
9515   verifyFormat("int      oneTwoThree{0}; // comment\n"
9516                "unsigned oneTwo;         // comment",
9517                Alignment);
9518   EXPECT_EQ("float const a = 5;\n"
9519             "\n"
9520             "int oneTwoThree = 123;",
9521             format("float const   a = 5;\n"
9522                    "\n"
9523                    "int           oneTwoThree= 123;",
9524                    Alignment));
9525   EXPECT_EQ("float a = 5;\n"
9526             "int   one = 1;\n"
9527             "\n"
9528             "unsigned oneTwoThree = 123;",
9529             format("float    a = 5;\n"
9530                    "int      one = 1;\n"
9531                    "\n"
9532                    "unsigned oneTwoThree = 123;",
9533                    Alignment));
9534   EXPECT_EQ("float a = 5;\n"
9535             "int   one = 1;\n"
9536             "\n"
9537             "unsigned oneTwoThree = 123;\n"
9538             "int      oneTwo = 12;",
9539             format("float    a = 5;\n"
9540                    "int one = 1;\n"
9541                    "\n"
9542                    "unsigned oneTwoThree = 123;\n"
9543                    "int oneTwo = 12;",
9544                    Alignment));
9545   // Function prototype alignment
9546   verifyFormat("int    a();\n"
9547                "double b();",
9548                Alignment);
9549   verifyFormat("int    a(int x);\n"
9550                "double b();",
9551                Alignment);
9552   unsigned OldColumnLimit = Alignment.ColumnLimit;
9553   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9554   // otherwise the function parameters will be re-flowed onto a single line.
9555   Alignment.ColumnLimit = 0;
9556   EXPECT_EQ("int    a(int   x,\n"
9557             "         float y);\n"
9558             "double b(int    x,\n"
9559             "         double y);",
9560             format("int a(int x,\n"
9561                    " float y);\n"
9562                    "double b(int x,\n"
9563                    " double y);",
9564                    Alignment));
9565   // This ensures that function parameters of function declarations are
9566   // correctly indented when their owning functions are indented.
9567   // The failure case here is for 'double y' to not be indented enough.
9568   EXPECT_EQ("double a(int x);\n"
9569             "int    b(int    y,\n"
9570             "         double z);",
9571             format("double a(int x);\n"
9572                    "int b(int y,\n"
9573                    " double z);",
9574                    Alignment));
9575   // Set ColumnLimit low so that we induce wrapping immediately after
9576   // the function name and opening paren.
9577   Alignment.ColumnLimit = 13;
9578   verifyFormat("int function(\n"
9579                "    int  x,\n"
9580                "    bool y);",
9581                Alignment);
9582   Alignment.ColumnLimit = OldColumnLimit;
9583   // Ensure function pointers don't screw up recursive alignment
9584   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9585                "double b();",
9586                Alignment);
9587   Alignment.AlignConsecutiveAssignments = true;
9588   // Ensure recursive alignment is broken by function braces, so that the
9589   // "a = 1" does not align with subsequent assignments inside the function
9590   // body.
9591   verifyFormat("int func(int a = 1) {\n"
9592                "  int b  = 2;\n"
9593                "  int cc = 3;\n"
9594                "}",
9595                Alignment);
9596   verifyFormat("float      something = 2000;\n"
9597                "double     another   = 911;\n"
9598                "int        i = 1, j = 10;\n"
9599                "const int *oneMore = 1;\n"
9600                "unsigned   i       = 2;",
9601                Alignment);
9602   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9603                "unsigned oneTwo      = 0;   // comment",
9604                Alignment);
9605   // Make sure that scope is correctly tracked, in the absence of braces
9606   verifyFormat("for (int i = 0; i < n; i++)\n"
9607                "  j = i;\n"
9608                "double x = 1;\n",
9609                Alignment);
9610   verifyFormat("if (int i = 0)\n"
9611                "  j = i;\n"
9612                "double x = 1;\n",
9613                Alignment);
9614   // Ensure operator[] and operator() are comprehended
9615   verifyFormat("struct test {\n"
9616                "  long long int foo();\n"
9617                "  int           operator[](int a);\n"
9618                "  double        bar();\n"
9619                "};\n",
9620                Alignment);
9621   verifyFormat("struct test {\n"
9622                "  long long int foo();\n"
9623                "  int           operator()(int a);\n"
9624                "  double        bar();\n"
9625                "};\n",
9626                Alignment);
9627   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9628             "  int const i   = 1;\n"
9629             "  int *     j   = 2;\n"
9630             "  int       big = 10000;\n"
9631             "\n"
9632             "  unsigned oneTwoThree = 123;\n"
9633             "  int      oneTwo      = 12;\n"
9634             "  method();\n"
9635             "  float k  = 2;\n"
9636             "  int   ll = 10000;\n"
9637             "}",
9638             format("void SomeFunction(int parameter= 0) {\n"
9639                    " int const  i= 1;\n"
9640                    "  int *j=2;\n"
9641                    " int big  =  10000;\n"
9642                    "\n"
9643                    "unsigned oneTwoThree  =123;\n"
9644                    "int oneTwo = 12;\n"
9645                    "  method();\n"
9646                    "float k= 2;\n"
9647                    "int ll=10000;\n"
9648                    "}",
9649                    Alignment));
9650   Alignment.AlignConsecutiveAssignments = false;
9651   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9652   verifyFormat("#define A \\\n"
9653                "  int       aaaa = 12; \\\n"
9654                "  float     b = 23; \\\n"
9655                "  const int ccc = 234; \\\n"
9656                "  unsigned  dddddddddd = 2345;",
9657                Alignment);
9658   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9659   verifyFormat("#define A              \\\n"
9660                "  int       aaaa = 12; \\\n"
9661                "  float     b = 23;    \\\n"
9662                "  const int ccc = 234; \\\n"
9663                "  unsigned  dddddddddd = 2345;",
9664                Alignment);
9665   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9666   Alignment.ColumnLimit = 30;
9667   verifyFormat("#define A                    \\\n"
9668                "  int       aaaa = 12;       \\\n"
9669                "  float     b = 23;          \\\n"
9670                "  const int ccc = 234;       \\\n"
9671                "  int       dddddddddd = 2345;",
9672                Alignment);
9673   Alignment.ColumnLimit = 80;
9674   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9675                "k = 4, int l = 5,\n"
9676                "                  int m = 6) {\n"
9677                "  const int j = 10;\n"
9678                "  otherThing = 1;\n"
9679                "}",
9680                Alignment);
9681   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9682                "  int const i = 1;\n"
9683                "  int *     j = 2;\n"
9684                "  int       big = 10000;\n"
9685                "}",
9686                Alignment);
9687   verifyFormat("class C {\n"
9688                "public:\n"
9689                "  int          i = 1;\n"
9690                "  virtual void f() = 0;\n"
9691                "};",
9692                Alignment);
9693   verifyFormat("float i = 1;\n"
9694                "if (SomeType t = getSomething()) {\n"
9695                "}\n"
9696                "const unsigned j = 2;\n"
9697                "int            big = 10000;",
9698                Alignment);
9699   verifyFormat("float j = 7;\n"
9700                "for (int k = 0; k < N; ++k) {\n"
9701                "}\n"
9702                "unsigned j = 2;\n"
9703                "int      big = 10000;\n"
9704                "}",
9705                Alignment);
9706   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9707   verifyFormat("float              i = 1;\n"
9708                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9709                "    = someLooooooooooooooooongFunction();\n"
9710                "int j = 2;",
9711                Alignment);
9712   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9713   verifyFormat("int                i = 1;\n"
9714                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9715                "    someLooooooooooooooooongFunction();\n"
9716                "int j = 2;",
9717                Alignment);
9718 
9719   Alignment.AlignConsecutiveAssignments = true;
9720   verifyFormat("auto lambda = []() {\n"
9721                "  auto  ii = 0;\n"
9722                "  float j  = 0;\n"
9723                "  return 0;\n"
9724                "};\n"
9725                "int   i  = 0;\n"
9726                "float i2 = 0;\n"
9727                "auto  v  = type{\n"
9728                "    i = 1,   //\n"
9729                "    (i = 2), //\n"
9730                "    i = 3    //\n"
9731                "};",
9732                Alignment);
9733   Alignment.AlignConsecutiveAssignments = false;
9734 
9735   verifyFormat(
9736       "int      i = 1;\n"
9737       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9738       "                          loooooooooooooooooooooongParameterB);\n"
9739       "int      j = 2;",
9740       Alignment);
9741 
9742   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9743   // We expect declarations and assignments to align, as long as it doesn't
9744   // exceed the column limit, starting a new alignment sequence whenever it
9745   // happens.
9746   Alignment.AlignConsecutiveAssignments = true;
9747   Alignment.ColumnLimit = 30;
9748   verifyFormat("float    ii              = 1;\n"
9749                "unsigned j               = 2;\n"
9750                "int someVerylongVariable = 1;\n"
9751                "AnotherLongType  ll = 123456;\n"
9752                "VeryVeryLongType k  = 2;\n"
9753                "int              myvar = 1;",
9754                Alignment);
9755   Alignment.ColumnLimit = 80;
9756   Alignment.AlignConsecutiveAssignments = false;
9757 
9758   verifyFormat(
9759       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9760       "          typename LongType, typename B>\n"
9761       "auto foo() {}\n",
9762       Alignment);
9763   verifyFormat("float a, b = 1;\n"
9764                "int   c = 2;\n"
9765                "int   dd = 3;\n",
9766                Alignment);
9767   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9768                "float b[1][] = {{3.f}};\n",
9769                Alignment);
9770   Alignment.AlignConsecutiveAssignments = true;
9771   verifyFormat("float a, b = 1;\n"
9772                "int   c  = 2;\n"
9773                "int   dd = 3;\n",
9774                Alignment);
9775   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9776                "float b[1][] = {{3.f}};\n",
9777                Alignment);
9778   Alignment.AlignConsecutiveAssignments = false;
9779 
9780   Alignment.ColumnLimit = 30;
9781   Alignment.BinPackParameters = false;
9782   verifyFormat("void foo(float     a,\n"
9783                "         float     b,\n"
9784                "         int       c,\n"
9785                "         uint32_t *d) {\n"
9786                "  int *  e = 0;\n"
9787                "  float  f = 0;\n"
9788                "  double g = 0;\n"
9789                "}\n"
9790                "void bar(ino_t     a,\n"
9791                "         int       b,\n"
9792                "         uint32_t *c,\n"
9793                "         bool      d) {}\n",
9794                Alignment);
9795   Alignment.BinPackParameters = true;
9796   Alignment.ColumnLimit = 80;
9797 
9798   // Bug 33507
9799   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9800   verifyFormat(
9801       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9802       "  static const Version verVs2017;\n"
9803       "  return true;\n"
9804       "});\n",
9805       Alignment);
9806   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9807 
9808   // See llvm.org/PR35641
9809   Alignment.AlignConsecutiveDeclarations = true;
9810   verifyFormat("int func() { //\n"
9811                "  int      b;\n"
9812                "  unsigned c;\n"
9813                "}",
9814                Alignment);
9815 }
9816 
9817 TEST_F(FormatTest, LinuxBraceBreaking) {
9818   FormatStyle LinuxBraceStyle = getLLVMStyle();
9819   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9820   verifyFormat("namespace a\n"
9821                "{\n"
9822                "class A\n"
9823                "{\n"
9824                "  void f()\n"
9825                "  {\n"
9826                "    if (true) {\n"
9827                "      a();\n"
9828                "      b();\n"
9829                "    } else {\n"
9830                "      a();\n"
9831                "    }\n"
9832                "  }\n"
9833                "  void g() { return; }\n"
9834                "};\n"
9835                "struct B {\n"
9836                "  int x;\n"
9837                "};\n"
9838                "} // namespace a\n",
9839                LinuxBraceStyle);
9840   verifyFormat("enum X {\n"
9841                "  Y = 0,\n"
9842                "}\n",
9843                LinuxBraceStyle);
9844   verifyFormat("struct S {\n"
9845                "  int Type;\n"
9846                "  union {\n"
9847                "    int x;\n"
9848                "    double y;\n"
9849                "  } Value;\n"
9850                "  class C\n"
9851                "  {\n"
9852                "    MyFavoriteType Value;\n"
9853                "  } Class;\n"
9854                "}\n",
9855                LinuxBraceStyle);
9856 }
9857 
9858 TEST_F(FormatTest, MozillaBraceBreaking) {
9859   FormatStyle MozillaBraceStyle = getLLVMStyle();
9860   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9861   MozillaBraceStyle.FixNamespaceComments = false;
9862   verifyFormat("namespace a {\n"
9863                "class A\n"
9864                "{\n"
9865                "  void f()\n"
9866                "  {\n"
9867                "    if (true) {\n"
9868                "      a();\n"
9869                "      b();\n"
9870                "    }\n"
9871                "  }\n"
9872                "  void g() { return; }\n"
9873                "};\n"
9874                "enum E\n"
9875                "{\n"
9876                "  A,\n"
9877                "  // foo\n"
9878                "  B,\n"
9879                "  C\n"
9880                "};\n"
9881                "struct B\n"
9882                "{\n"
9883                "  int x;\n"
9884                "};\n"
9885                "}\n",
9886                MozillaBraceStyle);
9887   verifyFormat("struct S\n"
9888                "{\n"
9889                "  int Type;\n"
9890                "  union\n"
9891                "  {\n"
9892                "    int x;\n"
9893                "    double y;\n"
9894                "  } Value;\n"
9895                "  class C\n"
9896                "  {\n"
9897                "    MyFavoriteType Value;\n"
9898                "  } Class;\n"
9899                "}\n",
9900                MozillaBraceStyle);
9901 }
9902 
9903 TEST_F(FormatTest, StroustrupBraceBreaking) {
9904   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9905   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9906   verifyFormat("namespace a {\n"
9907                "class A {\n"
9908                "  void f()\n"
9909                "  {\n"
9910                "    if (true) {\n"
9911                "      a();\n"
9912                "      b();\n"
9913                "    }\n"
9914                "  }\n"
9915                "  void g() { return; }\n"
9916                "};\n"
9917                "struct B {\n"
9918                "  int x;\n"
9919                "};\n"
9920                "} // namespace a\n",
9921                StroustrupBraceStyle);
9922 
9923   verifyFormat("void foo()\n"
9924                "{\n"
9925                "  if (a) {\n"
9926                "    a();\n"
9927                "  }\n"
9928                "  else {\n"
9929                "    b();\n"
9930                "  }\n"
9931                "}\n",
9932                StroustrupBraceStyle);
9933 
9934   verifyFormat("#ifdef _DEBUG\n"
9935                "int foo(int i = 0)\n"
9936                "#else\n"
9937                "int foo(int i = 5)\n"
9938                "#endif\n"
9939                "{\n"
9940                "  return i;\n"
9941                "}",
9942                StroustrupBraceStyle);
9943 
9944   verifyFormat("void foo() {}\n"
9945                "void bar()\n"
9946                "#ifdef _DEBUG\n"
9947                "{\n"
9948                "  foo();\n"
9949                "}\n"
9950                "#else\n"
9951                "{\n"
9952                "}\n"
9953                "#endif",
9954                StroustrupBraceStyle);
9955 
9956   verifyFormat("void foobar() { int i = 5; }\n"
9957                "#ifdef _DEBUG\n"
9958                "void bar() {}\n"
9959                "#else\n"
9960                "void bar() { foobar(); }\n"
9961                "#endif",
9962                StroustrupBraceStyle);
9963 }
9964 
9965 TEST_F(FormatTest, AllmanBraceBreaking) {
9966   FormatStyle AllmanBraceStyle = getLLVMStyle();
9967   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9968 
9969   EXPECT_EQ("namespace a\n"
9970             "{\n"
9971             "void f();\n"
9972             "void g();\n"
9973             "} // namespace a\n",
9974             format("namespace a\n"
9975                    "{\n"
9976                    "void f();\n"
9977                    "void g();\n"
9978                    "}\n",
9979                    AllmanBraceStyle));
9980 
9981   verifyFormat("namespace a\n"
9982                "{\n"
9983                "class A\n"
9984                "{\n"
9985                "  void f()\n"
9986                "  {\n"
9987                "    if (true)\n"
9988                "    {\n"
9989                "      a();\n"
9990                "      b();\n"
9991                "    }\n"
9992                "  }\n"
9993                "  void g() { return; }\n"
9994                "};\n"
9995                "struct B\n"
9996                "{\n"
9997                "  int x;\n"
9998                "};\n"
9999                "} // namespace a",
10000                AllmanBraceStyle);
10001 
10002   verifyFormat("void f()\n"
10003                "{\n"
10004                "  if (true)\n"
10005                "  {\n"
10006                "    a();\n"
10007                "  }\n"
10008                "  else if (false)\n"
10009                "  {\n"
10010                "    b();\n"
10011                "  }\n"
10012                "  else\n"
10013                "  {\n"
10014                "    c();\n"
10015                "  }\n"
10016                "}\n",
10017                AllmanBraceStyle);
10018 
10019   verifyFormat("void f()\n"
10020                "{\n"
10021                "  for (int i = 0; i < 10; ++i)\n"
10022                "  {\n"
10023                "    a();\n"
10024                "  }\n"
10025                "  while (false)\n"
10026                "  {\n"
10027                "    b();\n"
10028                "  }\n"
10029                "  do\n"
10030                "  {\n"
10031                "    c();\n"
10032                "  } while (false)\n"
10033                "}\n",
10034                AllmanBraceStyle);
10035 
10036   verifyFormat("void f(int a)\n"
10037                "{\n"
10038                "  switch (a)\n"
10039                "  {\n"
10040                "  case 0:\n"
10041                "    break;\n"
10042                "  case 1:\n"
10043                "  {\n"
10044                "    break;\n"
10045                "  }\n"
10046                "  case 2:\n"
10047                "  {\n"
10048                "  }\n"
10049                "  break;\n"
10050                "  default:\n"
10051                "    break;\n"
10052                "  }\n"
10053                "}\n",
10054                AllmanBraceStyle);
10055 
10056   verifyFormat("enum X\n"
10057                "{\n"
10058                "  Y = 0,\n"
10059                "}\n",
10060                AllmanBraceStyle);
10061   verifyFormat("enum X\n"
10062                "{\n"
10063                "  Y = 0\n"
10064                "}\n",
10065                AllmanBraceStyle);
10066 
10067   verifyFormat("@interface BSApplicationController ()\n"
10068                "{\n"
10069                "@private\n"
10070                "  id _extraIvar;\n"
10071                "}\n"
10072                "@end\n",
10073                AllmanBraceStyle);
10074 
10075   verifyFormat("#ifdef _DEBUG\n"
10076                "int foo(int i = 0)\n"
10077                "#else\n"
10078                "int foo(int i = 5)\n"
10079                "#endif\n"
10080                "{\n"
10081                "  return i;\n"
10082                "}",
10083                AllmanBraceStyle);
10084 
10085   verifyFormat("void foo() {}\n"
10086                "void bar()\n"
10087                "#ifdef _DEBUG\n"
10088                "{\n"
10089                "  foo();\n"
10090                "}\n"
10091                "#else\n"
10092                "{\n"
10093                "}\n"
10094                "#endif",
10095                AllmanBraceStyle);
10096 
10097   verifyFormat("void foobar() { int i = 5; }\n"
10098                "#ifdef _DEBUG\n"
10099                "void bar() {}\n"
10100                "#else\n"
10101                "void bar() { foobar(); }\n"
10102                "#endif",
10103                AllmanBraceStyle);
10104 
10105   // This shouldn't affect ObjC blocks..
10106   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10107                "  // ...\n"
10108                "  int i;\n"
10109                "}];",
10110                AllmanBraceStyle);
10111   verifyFormat("void (^block)(void) = ^{\n"
10112                "  // ...\n"
10113                "  int i;\n"
10114                "};",
10115                AllmanBraceStyle);
10116   // .. or dict literals.
10117   verifyFormat("void f()\n"
10118                "{\n"
10119                "  // ...\n"
10120                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10121                "}",
10122                AllmanBraceStyle);
10123   verifyFormat("void f()\n"
10124                "{\n"
10125                "  // ...\n"
10126                "  [object someMethod:@{a : @\"b\"}];\n"
10127                "}",
10128                AllmanBraceStyle);
10129   verifyFormat("int f()\n"
10130                "{ // comment\n"
10131                "  return 42;\n"
10132                "}",
10133                AllmanBraceStyle);
10134 
10135   AllmanBraceStyle.ColumnLimit = 19;
10136   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10137   AllmanBraceStyle.ColumnLimit = 18;
10138   verifyFormat("void f()\n"
10139                "{\n"
10140                "  int i;\n"
10141                "}",
10142                AllmanBraceStyle);
10143   AllmanBraceStyle.ColumnLimit = 80;
10144 
10145   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10146   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
10147   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10148   verifyFormat("void f(bool b)\n"
10149                "{\n"
10150                "  if (b)\n"
10151                "  {\n"
10152                "    return;\n"
10153                "  }\n"
10154                "}\n",
10155                BreakBeforeBraceShortIfs);
10156   verifyFormat("void f(bool b)\n"
10157                "{\n"
10158                "  if constexpr (b)\n"
10159                "  {\n"
10160                "    return;\n"
10161                "  }\n"
10162                "}\n",
10163                BreakBeforeBraceShortIfs);
10164   verifyFormat("void f(bool b)\n"
10165                "{\n"
10166                "  if (b) return;\n"
10167                "}\n",
10168                BreakBeforeBraceShortIfs);
10169   verifyFormat("void f(bool b)\n"
10170                "{\n"
10171                "  if constexpr (b) return;\n"
10172                "}\n",
10173                BreakBeforeBraceShortIfs);
10174   verifyFormat("void f(bool b)\n"
10175                "{\n"
10176                "  while (b)\n"
10177                "  {\n"
10178                "    return;\n"
10179                "  }\n"
10180                "}\n",
10181                BreakBeforeBraceShortIfs);
10182 }
10183 
10184 TEST_F(FormatTest, GNUBraceBreaking) {
10185   FormatStyle GNUBraceStyle = getLLVMStyle();
10186   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10187   verifyFormat("namespace a\n"
10188                "{\n"
10189                "class A\n"
10190                "{\n"
10191                "  void f()\n"
10192                "  {\n"
10193                "    int a;\n"
10194                "    {\n"
10195                "      int b;\n"
10196                "    }\n"
10197                "    if (true)\n"
10198                "      {\n"
10199                "        a();\n"
10200                "        b();\n"
10201                "      }\n"
10202                "  }\n"
10203                "  void g() { return; }\n"
10204                "}\n"
10205                "} // namespace a",
10206                GNUBraceStyle);
10207 
10208   verifyFormat("void f()\n"
10209                "{\n"
10210                "  if (true)\n"
10211                "    {\n"
10212                "      a();\n"
10213                "    }\n"
10214                "  else if (false)\n"
10215                "    {\n"
10216                "      b();\n"
10217                "    }\n"
10218                "  else\n"
10219                "    {\n"
10220                "      c();\n"
10221                "    }\n"
10222                "}\n",
10223                GNUBraceStyle);
10224 
10225   verifyFormat("void f()\n"
10226                "{\n"
10227                "  for (int i = 0; i < 10; ++i)\n"
10228                "    {\n"
10229                "      a();\n"
10230                "    }\n"
10231                "  while (false)\n"
10232                "    {\n"
10233                "      b();\n"
10234                "    }\n"
10235                "  do\n"
10236                "    {\n"
10237                "      c();\n"
10238                "    }\n"
10239                "  while (false);\n"
10240                "}\n",
10241                GNUBraceStyle);
10242 
10243   verifyFormat("void f(int a)\n"
10244                "{\n"
10245                "  switch (a)\n"
10246                "    {\n"
10247                "    case 0:\n"
10248                "      break;\n"
10249                "    case 1:\n"
10250                "      {\n"
10251                "        break;\n"
10252                "      }\n"
10253                "    case 2:\n"
10254                "      {\n"
10255                "      }\n"
10256                "      break;\n"
10257                "    default:\n"
10258                "      break;\n"
10259                "    }\n"
10260                "}\n",
10261                GNUBraceStyle);
10262 
10263   verifyFormat("enum X\n"
10264                "{\n"
10265                "  Y = 0,\n"
10266                "}\n",
10267                GNUBraceStyle);
10268 
10269   verifyFormat("@interface BSApplicationController ()\n"
10270                "{\n"
10271                "@private\n"
10272                "  id _extraIvar;\n"
10273                "}\n"
10274                "@end\n",
10275                GNUBraceStyle);
10276 
10277   verifyFormat("#ifdef _DEBUG\n"
10278                "int foo(int i = 0)\n"
10279                "#else\n"
10280                "int foo(int i = 5)\n"
10281                "#endif\n"
10282                "{\n"
10283                "  return i;\n"
10284                "}",
10285                GNUBraceStyle);
10286 
10287   verifyFormat("void foo() {}\n"
10288                "void bar()\n"
10289                "#ifdef _DEBUG\n"
10290                "{\n"
10291                "  foo();\n"
10292                "}\n"
10293                "#else\n"
10294                "{\n"
10295                "}\n"
10296                "#endif",
10297                GNUBraceStyle);
10298 
10299   verifyFormat("void foobar() { int i = 5; }\n"
10300                "#ifdef _DEBUG\n"
10301                "void bar() {}\n"
10302                "#else\n"
10303                "void bar() { foobar(); }\n"
10304                "#endif",
10305                GNUBraceStyle);
10306 }
10307 
10308 TEST_F(FormatTest, WebKitBraceBreaking) {
10309   FormatStyle WebKitBraceStyle = getLLVMStyle();
10310   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10311   WebKitBraceStyle.FixNamespaceComments = false;
10312   verifyFormat("namespace a {\n"
10313                "class A {\n"
10314                "  void f()\n"
10315                "  {\n"
10316                "    if (true) {\n"
10317                "      a();\n"
10318                "      b();\n"
10319                "    }\n"
10320                "  }\n"
10321                "  void g() { return; }\n"
10322                "};\n"
10323                "enum E {\n"
10324                "  A,\n"
10325                "  // foo\n"
10326                "  B,\n"
10327                "  C\n"
10328                "};\n"
10329                "struct B {\n"
10330                "  int x;\n"
10331                "};\n"
10332                "}\n",
10333                WebKitBraceStyle);
10334   verifyFormat("struct S {\n"
10335                "  int Type;\n"
10336                "  union {\n"
10337                "    int x;\n"
10338                "    double y;\n"
10339                "  } Value;\n"
10340                "  class C {\n"
10341                "    MyFavoriteType Value;\n"
10342                "  } Class;\n"
10343                "};\n",
10344                WebKitBraceStyle);
10345 }
10346 
10347 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10348   verifyFormat("void f() {\n"
10349                "  try {\n"
10350                "  } catch (const Exception &e) {\n"
10351                "  }\n"
10352                "}\n",
10353                getLLVMStyle());
10354 }
10355 
10356 TEST_F(FormatTest, UnderstandsPragmas) {
10357   verifyFormat("#pragma omp reduction(| : var)");
10358   verifyFormat("#pragma omp reduction(+ : var)");
10359 
10360   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10361             "(including parentheses).",
10362             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10363                    "(including parentheses)."));
10364 }
10365 
10366 TEST_F(FormatTest, UnderstandPragmaOption) {
10367   verifyFormat("#pragma option -C -A");
10368 
10369   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10370 }
10371 
10372 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10373   FormatStyle Style = getLLVMStyle();
10374   Style.ColumnLimit = 20;
10375 
10376   verifyFormat("int a; // the\n"
10377                "       // comment", Style);
10378   EXPECT_EQ("int a; /* first line\n"
10379             "        * second\n"
10380             "        * line third\n"
10381             "        * line\n"
10382             "        */",
10383             format("int a; /* first line\n"
10384                    "        * second\n"
10385                    "        * line third\n"
10386                    "        * line\n"
10387                    "        */",
10388                    Style));
10389   EXPECT_EQ("int a; // first line\n"
10390             "       // second\n"
10391             "       // line third\n"
10392             "       // line",
10393             format("int a; // first line\n"
10394                    "       // second line\n"
10395                    "       // third line",
10396                    Style));
10397 
10398   Style.PenaltyExcessCharacter = 90;
10399   verifyFormat("int a; // the comment", Style);
10400   EXPECT_EQ("int a; // the comment\n"
10401             "       // aaa",
10402             format("int a; // the comment aaa", Style));
10403   EXPECT_EQ("int a; /* first line\n"
10404             "        * second line\n"
10405             "        * third line\n"
10406             "        */",
10407             format("int a; /* first line\n"
10408                    "        * second line\n"
10409                    "        * third line\n"
10410                    "        */",
10411                    Style));
10412   EXPECT_EQ("int a; // first line\n"
10413             "       // second line\n"
10414             "       // third line",
10415             format("int a; // first line\n"
10416                    "       // second line\n"
10417                    "       // third line",
10418                    Style));
10419   // FIXME: Investigate why this is not getting the same layout as the test
10420   // above.
10421   EXPECT_EQ("int a; /* first line\n"
10422             "        * second line\n"
10423             "        * third line\n"
10424             "        */",
10425             format("int a; /* first line second line third line"
10426                    "\n*/",
10427                    Style));
10428 
10429   EXPECT_EQ("// foo bar baz bazfoo\n"
10430             "// foo bar foo bar\n",
10431             format("// foo bar baz bazfoo\n"
10432                    "// foo bar foo           bar\n",
10433                    Style));
10434   EXPECT_EQ("// foo bar baz bazfoo\n"
10435             "// foo bar foo bar\n",
10436             format("// foo bar baz      bazfoo\n"
10437                    "// foo            bar foo bar\n",
10438                    Style));
10439 
10440   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10441   // next one.
10442   EXPECT_EQ("// foo bar baz bazfoo\n"
10443             "// bar foo bar\n",
10444             format("// foo bar baz      bazfoo bar\n"
10445                    "// foo            bar\n",
10446                    Style));
10447 
10448   EXPECT_EQ("// foo bar baz bazfoo\n"
10449             "// foo bar baz bazfoo\n"
10450             "// bar foo bar\n",
10451             format("// foo bar baz      bazfoo\n"
10452                    "// foo bar baz      bazfoo bar\n"
10453                    "// foo bar\n",
10454                    Style));
10455 
10456   EXPECT_EQ("// foo bar baz bazfoo\n"
10457             "// foo bar baz bazfoo\n"
10458             "// bar foo bar\n",
10459             format("// foo bar baz      bazfoo\n"
10460                    "// foo bar baz      bazfoo bar\n"
10461                    "// foo           bar\n",
10462                    Style));
10463 
10464   // Make sure we do not keep protruding characters if strict mode reflow is
10465   // cheaper than keeping protruding characters.
10466   Style.ColumnLimit = 21;
10467   EXPECT_EQ("// foo foo foo foo\n"
10468             "// foo foo foo foo\n"
10469             "// foo foo foo foo\n",
10470             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10471                            Style));
10472 
10473   EXPECT_EQ("int a = /* long block\n"
10474             "           comment */\n"
10475             "    42;",
10476             format("int a = /* long block comment */ 42;", Style));
10477 }
10478 
10479 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10480   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10481   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10482                                   << " differs from Style #0"
10483 
10484 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10485   SmallVector<FormatStyle, 3> Styles;
10486   Styles.resize(3);
10487 
10488   Styles[0] = getLLVMStyle();
10489   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10490   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10491   EXPECT_ALL_STYLES_EQUAL(Styles);
10492 
10493   Styles[0] = getGoogleStyle();
10494   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10495   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10496   EXPECT_ALL_STYLES_EQUAL(Styles);
10497 
10498   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10499   EXPECT_TRUE(
10500       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10501   EXPECT_TRUE(
10502       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10503   EXPECT_ALL_STYLES_EQUAL(Styles);
10504 
10505   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10506   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10507   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10508   EXPECT_ALL_STYLES_EQUAL(Styles);
10509 
10510   Styles[0] = getMozillaStyle();
10511   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10512   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10513   EXPECT_ALL_STYLES_EQUAL(Styles);
10514 
10515   Styles[0] = getWebKitStyle();
10516   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10517   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10518   EXPECT_ALL_STYLES_EQUAL(Styles);
10519 
10520   Styles[0] = getGNUStyle();
10521   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10522   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10523   EXPECT_ALL_STYLES_EQUAL(Styles);
10524 
10525   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10526 }
10527 
10528 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10529   SmallVector<FormatStyle, 8> Styles;
10530   Styles.resize(2);
10531 
10532   Styles[0] = getGoogleStyle();
10533   Styles[1] = getLLVMStyle();
10534   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10535   EXPECT_ALL_STYLES_EQUAL(Styles);
10536 
10537   Styles.resize(5);
10538   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10539   Styles[1] = getLLVMStyle();
10540   Styles[1].Language = FormatStyle::LK_JavaScript;
10541   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10542 
10543   Styles[2] = getLLVMStyle();
10544   Styles[2].Language = FormatStyle::LK_JavaScript;
10545   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10546                                   "BasedOnStyle: Google",
10547                                   &Styles[2])
10548                    .value());
10549 
10550   Styles[3] = getLLVMStyle();
10551   Styles[3].Language = FormatStyle::LK_JavaScript;
10552   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10553                                   "Language: JavaScript",
10554                                   &Styles[3])
10555                    .value());
10556 
10557   Styles[4] = getLLVMStyle();
10558   Styles[4].Language = FormatStyle::LK_JavaScript;
10559   EXPECT_EQ(0, parseConfiguration("---\n"
10560                                   "BasedOnStyle: LLVM\n"
10561                                   "IndentWidth: 123\n"
10562                                   "---\n"
10563                                   "BasedOnStyle: Google\n"
10564                                   "Language: JavaScript",
10565                                   &Styles[4])
10566                    .value());
10567   EXPECT_ALL_STYLES_EQUAL(Styles);
10568 }
10569 
10570 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10571   Style.FIELD = false;                                                         \
10572   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10573   EXPECT_TRUE(Style.FIELD);                                                    \
10574   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10575   EXPECT_FALSE(Style.FIELD);
10576 
10577 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10578 
10579 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10580   Style.STRUCT.FIELD = false;                                                  \
10581   EXPECT_EQ(0,                                                                 \
10582             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10583                 .value());                                                     \
10584   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10585   EXPECT_EQ(0,                                                                 \
10586             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10587                 .value());                                                     \
10588   EXPECT_FALSE(Style.STRUCT.FIELD);
10589 
10590 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10591   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10592 
10593 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10594   EXPECT_NE(VALUE, Style.FIELD);                                               \
10595   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10596   EXPECT_EQ(VALUE, Style.FIELD)
10597 
10598 TEST_F(FormatTest, ParsesConfigurationBools) {
10599   FormatStyle Style = {};
10600   Style.Language = FormatStyle::LK_Cpp;
10601   CHECK_PARSE_BOOL(AlignOperands);
10602   CHECK_PARSE_BOOL(AlignTrailingComments);
10603   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10604   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10605   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10606   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10607   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10608   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10609   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10610   CHECK_PARSE_BOOL(BinPackArguments);
10611   CHECK_PARSE_BOOL(BinPackParameters);
10612   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10613   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10614   CHECK_PARSE_BOOL(BreakStringLiterals);
10615   CHECK_PARSE_BOOL(CompactNamespaces);
10616   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10617   CHECK_PARSE_BOOL(DerivePointerAlignment);
10618   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10619   CHECK_PARSE_BOOL(DisableFormat);
10620   CHECK_PARSE_BOOL(IndentCaseLabels);
10621   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10622   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10623   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10624   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10625   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10626   CHECK_PARSE_BOOL(ReflowComments);
10627   CHECK_PARSE_BOOL(SortIncludes);
10628   CHECK_PARSE_BOOL(SortUsingDeclarations);
10629   CHECK_PARSE_BOOL(SpacesInParentheses);
10630   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10631   CHECK_PARSE_BOOL(SpacesInAngles);
10632   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10633   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10634   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10635   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10636   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10637   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10638   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
10639   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10640   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10641   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10642 
10643   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10644   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10645   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10646   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10647   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10648   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10649   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10650   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10651   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10652   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10653   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10654   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10655   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10656   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10657   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10658 }
10659 
10660 #undef CHECK_PARSE_BOOL
10661 
10662 TEST_F(FormatTest, ParsesConfiguration) {
10663   FormatStyle Style = {};
10664   Style.Language = FormatStyle::LK_Cpp;
10665   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10666   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10667               ConstructorInitializerIndentWidth, 1234u);
10668   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10669   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10670   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10671   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10672               PenaltyBreakAssignment, 1234u);
10673   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10674               PenaltyBreakBeforeFirstCallParameter, 1234u);
10675   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
10676               PenaltyBreakTemplateDeclaration, 1234u);
10677   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10678   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10679               PenaltyReturnTypeOnItsOwnLine, 1234u);
10680   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10681               SpacesBeforeTrailingComments, 1234u);
10682   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10683   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10684   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10685 
10686   Style.PointerAlignment = FormatStyle::PAS_Middle;
10687   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10688               FormatStyle::PAS_Left);
10689   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10690               FormatStyle::PAS_Right);
10691   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10692               FormatStyle::PAS_Middle);
10693   // For backward compatibility:
10694   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10695               FormatStyle::PAS_Left);
10696   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10697               FormatStyle::PAS_Right);
10698   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10699               FormatStyle::PAS_Middle);
10700 
10701   Style.Standard = FormatStyle::LS_Auto;
10702   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10703   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10704   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10705   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10706   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10707 
10708   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10709   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10710               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10711   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10712               FormatStyle::BOS_None);
10713   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10714               FormatStyle::BOS_All);
10715   // For backward compatibility:
10716   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10717               FormatStyle::BOS_None);
10718   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10719               FormatStyle::BOS_All);
10720 
10721   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10722   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10723               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10724   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10725               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10726   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10727               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10728   // For backward compatibility:
10729   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10730               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10731 
10732   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
10733   CHECK_PARSE("BreakInheritanceList: BeforeComma",
10734               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10735   CHECK_PARSE("BreakInheritanceList: AfterColon",
10736               BreakInheritanceList, FormatStyle::BILS_AfterColon);
10737   CHECK_PARSE("BreakInheritanceList: BeforeColon",
10738               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
10739   // For backward compatibility:
10740   CHECK_PARSE("BreakBeforeInheritanceComma: true",
10741               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10742 
10743   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10744   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10745               FormatStyle::BAS_Align);
10746   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10747               FormatStyle::BAS_DontAlign);
10748   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10749               FormatStyle::BAS_AlwaysBreak);
10750   // For backward compatibility:
10751   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10752               FormatStyle::BAS_DontAlign);
10753   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10754               FormatStyle::BAS_Align);
10755 
10756   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10757   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10758               FormatStyle::ENAS_DontAlign);
10759   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10760               FormatStyle::ENAS_Left);
10761   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10762               FormatStyle::ENAS_Right);
10763   // For backward compatibility:
10764   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10765               FormatStyle::ENAS_Left);
10766   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10767               FormatStyle::ENAS_Right);
10768 
10769   Style.UseTab = FormatStyle::UT_ForIndentation;
10770   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10771   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10772   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10773   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10774               FormatStyle::UT_ForContinuationAndIndentation);
10775   // For backward compatibility:
10776   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10777   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10778 
10779   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10780   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10781               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10782   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10783               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10784   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10785               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10786   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10787               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10788   // For backward compatibility:
10789   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10790               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10791   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10792               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10793 
10794   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10795   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10796               FormatStyle::SBPO_Never);
10797   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10798               FormatStyle::SBPO_Always);
10799   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10800               FormatStyle::SBPO_ControlStatements);
10801   // For backward compatibility:
10802   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10803               FormatStyle::SBPO_Never);
10804   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10805               FormatStyle::SBPO_ControlStatements);
10806 
10807   Style.ColumnLimit = 123;
10808   FormatStyle BaseStyle = getLLVMStyle();
10809   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10810   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10811 
10812   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10813   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10814               FormatStyle::BS_Attach);
10815   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10816               FormatStyle::BS_Linux);
10817   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10818               FormatStyle::BS_Mozilla);
10819   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10820               FormatStyle::BS_Stroustrup);
10821   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10822               FormatStyle::BS_Allman);
10823   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10824   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10825               FormatStyle::BS_WebKit);
10826   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10827               FormatStyle::BS_Custom);
10828 
10829   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10830   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10831               FormatStyle::RTBS_None);
10832   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10833               FormatStyle::RTBS_All);
10834   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10835               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10836   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10837               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10838   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10839               AlwaysBreakAfterReturnType,
10840               FormatStyle::RTBS_TopLevelDefinitions);
10841 
10842   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10843   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
10844               FormatStyle::BTDS_No);
10845   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
10846               FormatStyle::BTDS_MultiLine);
10847   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
10848               FormatStyle::BTDS_Yes);
10849   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
10850               FormatStyle::BTDS_MultiLine);
10851   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
10852               FormatStyle::BTDS_Yes);
10853 
10854   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10855   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10856               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10857   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10858               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10859   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10860               AlwaysBreakAfterDefinitionReturnType,
10861               FormatStyle::DRTBS_TopLevel);
10862 
10863   Style.NamespaceIndentation = FormatStyle::NI_All;
10864   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10865               FormatStyle::NI_None);
10866   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10867               FormatStyle::NI_Inner);
10868   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10869               FormatStyle::NI_All);
10870 
10871   // FIXME: This is required because parsing a configuration simply overwrites
10872   // the first N elements of the list instead of resetting it.
10873   Style.ForEachMacros.clear();
10874   std::vector<std::string> BoostForeach;
10875   BoostForeach.push_back("BOOST_FOREACH");
10876   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10877   std::vector<std::string> BoostAndQForeach;
10878   BoostAndQForeach.push_back("BOOST_FOREACH");
10879   BoostAndQForeach.push_back("Q_FOREACH");
10880   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10881               BoostAndQForeach);
10882 
10883   Style.IncludeStyle.IncludeCategories.clear();
10884   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
10885       {"abc/.*", 2}, {".*", 1}};
10886   CHECK_PARSE("IncludeCategories:\n"
10887               "  - Regex: abc/.*\n"
10888               "    Priority: 2\n"
10889               "  - Regex: .*\n"
10890               "    Priority: 1",
10891               IncludeStyle.IncludeCategories, ExpectedCategories);
10892   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
10893               "abc$");
10894 
10895   Style.RawStringFormats.clear();
10896   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10897       {
10898           FormatStyle::LK_TextProto,
10899           {"pb", "proto"},
10900           {"PARSE_TEXT_PROTO"},
10901           /*CanonicalDelimiter=*/"",
10902           "llvm",
10903       },
10904       {
10905           FormatStyle::LK_Cpp,
10906           {"cc", "cpp"},
10907           {"C_CODEBLOCK", "CPPEVAL"},
10908           /*CanonicalDelimiter=*/"cc",
10909           /*BasedOnStyle=*/"",
10910       },
10911   };
10912 
10913   CHECK_PARSE("RawStringFormats:\n"
10914               "  - Language: TextProto\n"
10915               "    Delimiters:\n"
10916               "      - 'pb'\n"
10917               "      - 'proto'\n"
10918               "    EnclosingFunctions:\n"
10919               "      - 'PARSE_TEXT_PROTO'\n"
10920               "    BasedOnStyle: llvm\n"
10921               "  - Language: Cpp\n"
10922               "    Delimiters:\n"
10923               "      - 'cc'\n"
10924               "      - 'cpp'\n"
10925               "    EnclosingFunctions:\n"
10926               "      - 'C_CODEBLOCK'\n"
10927               "      - 'CPPEVAL'\n"
10928               "    CanonicalDelimiter: 'cc'",
10929               RawStringFormats, ExpectedRawStringFormats);
10930 }
10931 
10932 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10933   FormatStyle Style = {};
10934   Style.Language = FormatStyle::LK_Cpp;
10935   CHECK_PARSE("Language: Cpp\n"
10936               "IndentWidth: 12",
10937               IndentWidth, 12u);
10938   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10939                                "IndentWidth: 34",
10940                                &Style),
10941             ParseError::Unsuitable);
10942   EXPECT_EQ(12u, Style.IndentWidth);
10943   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10944   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10945 
10946   Style.Language = FormatStyle::LK_JavaScript;
10947   CHECK_PARSE("Language: JavaScript\n"
10948               "IndentWidth: 12",
10949               IndentWidth, 12u);
10950   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10951   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10952                                "IndentWidth: 34",
10953                                &Style),
10954             ParseError::Unsuitable);
10955   EXPECT_EQ(23u, Style.IndentWidth);
10956   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10957   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10958 
10959   CHECK_PARSE("BasedOnStyle: LLVM\n"
10960               "IndentWidth: 67",
10961               IndentWidth, 67u);
10962 
10963   CHECK_PARSE("---\n"
10964               "Language: JavaScript\n"
10965               "IndentWidth: 12\n"
10966               "---\n"
10967               "Language: Cpp\n"
10968               "IndentWidth: 34\n"
10969               "...\n",
10970               IndentWidth, 12u);
10971 
10972   Style.Language = FormatStyle::LK_Cpp;
10973   CHECK_PARSE("---\n"
10974               "Language: JavaScript\n"
10975               "IndentWidth: 12\n"
10976               "---\n"
10977               "Language: Cpp\n"
10978               "IndentWidth: 34\n"
10979               "...\n",
10980               IndentWidth, 34u);
10981   CHECK_PARSE("---\n"
10982               "IndentWidth: 78\n"
10983               "---\n"
10984               "Language: JavaScript\n"
10985               "IndentWidth: 56\n"
10986               "...\n",
10987               IndentWidth, 78u);
10988 
10989   Style.ColumnLimit = 123;
10990   Style.IndentWidth = 234;
10991   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10992   Style.TabWidth = 345;
10993   EXPECT_FALSE(parseConfiguration("---\n"
10994                                   "IndentWidth: 456\n"
10995                                   "BreakBeforeBraces: Allman\n"
10996                                   "---\n"
10997                                   "Language: JavaScript\n"
10998                                   "IndentWidth: 111\n"
10999                                   "TabWidth: 111\n"
11000                                   "---\n"
11001                                   "Language: Cpp\n"
11002                                   "BreakBeforeBraces: Stroustrup\n"
11003                                   "TabWidth: 789\n"
11004                                   "...\n",
11005                                   &Style));
11006   EXPECT_EQ(123u, Style.ColumnLimit);
11007   EXPECT_EQ(456u, Style.IndentWidth);
11008   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11009   EXPECT_EQ(789u, Style.TabWidth);
11010 
11011   EXPECT_EQ(parseConfiguration("---\n"
11012                                "Language: JavaScript\n"
11013                                "IndentWidth: 56\n"
11014                                "---\n"
11015                                "IndentWidth: 78\n"
11016                                "...\n",
11017                                &Style),
11018             ParseError::Error);
11019   EXPECT_EQ(parseConfiguration("---\n"
11020                                "Language: JavaScript\n"
11021                                "IndentWidth: 56\n"
11022                                "---\n"
11023                                "Language: JavaScript\n"
11024                                "IndentWidth: 78\n"
11025                                "...\n",
11026                                &Style),
11027             ParseError::Error);
11028 
11029   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11030 }
11031 
11032 #undef CHECK_PARSE
11033 
11034 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11035   FormatStyle Style = {};
11036   Style.Language = FormatStyle::LK_JavaScript;
11037   Style.BreakBeforeTernaryOperators = true;
11038   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11039   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11040 
11041   Style.BreakBeforeTernaryOperators = true;
11042   EXPECT_EQ(0, parseConfiguration("---\n"
11043                                   "BasedOnStyle: Google\n"
11044                                   "---\n"
11045                                   "Language: JavaScript\n"
11046                                   "IndentWidth: 76\n"
11047                                   "...\n",
11048                                   &Style)
11049                    .value());
11050   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11051   EXPECT_EQ(76u, Style.IndentWidth);
11052   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11053 }
11054 
11055 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11056   FormatStyle Style = getLLVMStyle();
11057   std::string YAML = configurationAsText(Style);
11058   FormatStyle ParsedStyle = {};
11059   ParsedStyle.Language = FormatStyle::LK_Cpp;
11060   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11061   EXPECT_EQ(Style, ParsedStyle);
11062 }
11063 
11064 TEST_F(FormatTest, WorksFor8bitEncodings) {
11065   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11066             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11067             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11068             "\"\xef\xee\xf0\xf3...\"",
11069             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11070                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11071                    "\xef\xee\xf0\xf3...\"",
11072                    getLLVMStyleWithColumns(12)));
11073 }
11074 
11075 TEST_F(FormatTest, HandlesUTF8BOM) {
11076   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11077   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11078             format("\xef\xbb\xbf#include <iostream>"));
11079   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11080             format("\xef\xbb\xbf\n#include <iostream>"));
11081 }
11082 
11083 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11084 #if !defined(_MSC_VER)
11085 
11086 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11087   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11088                getLLVMStyleWithColumns(35));
11089   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11090                getLLVMStyleWithColumns(31));
11091   verifyFormat("// Однажды в студёную зимнюю пору...",
11092                getLLVMStyleWithColumns(36));
11093   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11094   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11095                getLLVMStyleWithColumns(39));
11096   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11097                getLLVMStyleWithColumns(35));
11098 }
11099 
11100 TEST_F(FormatTest, SplitsUTF8Strings) {
11101   // Non-printable characters' width is currently considered to be the length in
11102   // bytes in UTF8. The characters can be displayed in very different manner
11103   // (zero-width, single width with a substitution glyph, expanded to their code
11104   // (e.g. "<8d>"), so there's no single correct way to handle them.
11105   EXPECT_EQ("\"aaaaÄ\"\n"
11106             "\"\xc2\x8d\";",
11107             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11108   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11109             "\"\xc2\x8d\";",
11110             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11111   EXPECT_EQ("\"Однажды, в \"\n"
11112             "\"студёную \"\n"
11113             "\"зимнюю \"\n"
11114             "\"пору,\"",
11115             format("\"Однажды, в студёную зимнюю пору,\"",
11116                    getLLVMStyleWithColumns(13)));
11117   EXPECT_EQ(
11118       "\"一 二 三 \"\n"
11119       "\"四 五六 \"\n"
11120       "\"七 八 九 \"\n"
11121       "\"十\"",
11122       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11123   EXPECT_EQ("\"一\t\"\n"
11124             "\"二 \t\"\n"
11125             "\"三 四 \"\n"
11126             "\"五\t\"\n"
11127             "\"六 \t\"\n"
11128             "\"七 \"\n"
11129             "\"八九十\tqq\"",
11130             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11131                    getLLVMStyleWithColumns(11)));
11132 
11133   // UTF8 character in an escape sequence.
11134   EXPECT_EQ("\"aaaaaa\"\n"
11135             "\"\\\xC2\x8D\"",
11136             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11137 }
11138 
11139 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11140   EXPECT_EQ("const char *sssss =\n"
11141             "    \"一二三四五六七八\\\n"
11142             " 九 十\";",
11143             format("const char *sssss = \"一二三四五六七八\\\n"
11144                    " 九 十\";",
11145                    getLLVMStyleWithColumns(30)));
11146 }
11147 
11148 TEST_F(FormatTest, SplitsUTF8LineComments) {
11149   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11150             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11151   EXPECT_EQ("// Я из лесу\n"
11152             "// вышел; был\n"
11153             "// сильный\n"
11154             "// мороз.",
11155             format("// Я из лесу вышел; был сильный мороз.",
11156                    getLLVMStyleWithColumns(13)));
11157   EXPECT_EQ("// 一二三\n"
11158             "// 四五六七\n"
11159             "// 八  九\n"
11160             "// 十",
11161             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11162 }
11163 
11164 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11165   EXPECT_EQ("/* Гляжу,\n"
11166             " * поднимается\n"
11167             " * медленно в\n"
11168             " * гору\n"
11169             " * Лошадка,\n"
11170             " * везущая\n"
11171             " * хворосту\n"
11172             " * воз. */",
11173             format("/* Гляжу, поднимается медленно в гору\n"
11174                    " * Лошадка, везущая хворосту воз. */",
11175                    getLLVMStyleWithColumns(13)));
11176   EXPECT_EQ(
11177       "/* 一二三\n"
11178       " * 四五六七\n"
11179       " * 八  九\n"
11180       " * 十  */",
11181       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11182   EXPECT_EQ("/* �������� ��������\n"
11183             " * ��������\n"
11184             " * ������-�� */",
11185             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11186 }
11187 
11188 #endif // _MSC_VER
11189 
11190 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11191   FormatStyle Style = getLLVMStyle();
11192 
11193   Style.ConstructorInitializerIndentWidth = 4;
11194   verifyFormat(
11195       "SomeClass::Constructor()\n"
11196       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11197       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11198       Style);
11199 
11200   Style.ConstructorInitializerIndentWidth = 2;
11201   verifyFormat(
11202       "SomeClass::Constructor()\n"
11203       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11204       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11205       Style);
11206 
11207   Style.ConstructorInitializerIndentWidth = 0;
11208   verifyFormat(
11209       "SomeClass::Constructor()\n"
11210       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11211       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11212       Style);
11213   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11214   verifyFormat(
11215       "SomeLongTemplateVariableName<\n"
11216       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11217       Style);
11218   verifyFormat(
11219       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11220       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11221       Style);
11222 }
11223 
11224 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11225   FormatStyle Style = getLLVMStyle();
11226   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11227   Style.ConstructorInitializerIndentWidth = 4;
11228   verifyFormat("SomeClass::Constructor()\n"
11229                "    : a(a)\n"
11230                "    , b(b)\n"
11231                "    , c(c) {}",
11232                Style);
11233   verifyFormat("SomeClass::Constructor()\n"
11234                "    : a(a) {}",
11235                Style);
11236 
11237   Style.ColumnLimit = 0;
11238   verifyFormat("SomeClass::Constructor()\n"
11239                "    : a(a) {}",
11240                Style);
11241   verifyFormat("SomeClass::Constructor() noexcept\n"
11242                "    : a(a) {}",
11243                Style);
11244   verifyFormat("SomeClass::Constructor()\n"
11245                "    : a(a)\n"
11246                "    , b(b)\n"
11247                "    , c(c) {}",
11248                Style);
11249   verifyFormat("SomeClass::Constructor()\n"
11250                "    : a(a) {\n"
11251                "  foo();\n"
11252                "  bar();\n"
11253                "}",
11254                Style);
11255 
11256   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11257   verifyFormat("SomeClass::Constructor()\n"
11258                "    : a(a)\n"
11259                "    , b(b)\n"
11260                "    , c(c) {\n}",
11261                Style);
11262   verifyFormat("SomeClass::Constructor()\n"
11263                "    : a(a) {\n}",
11264                Style);
11265 
11266   Style.ColumnLimit = 80;
11267   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11268   Style.ConstructorInitializerIndentWidth = 2;
11269   verifyFormat("SomeClass::Constructor()\n"
11270                "  : a(a)\n"
11271                "  , b(b)\n"
11272                "  , c(c) {}",
11273                Style);
11274 
11275   Style.ConstructorInitializerIndentWidth = 0;
11276   verifyFormat("SomeClass::Constructor()\n"
11277                ": a(a)\n"
11278                ", b(b)\n"
11279                ", c(c) {}",
11280                Style);
11281 
11282   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11283   Style.ConstructorInitializerIndentWidth = 4;
11284   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11285   verifyFormat(
11286       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11287       Style);
11288   verifyFormat(
11289       "SomeClass::Constructor()\n"
11290       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11291       Style);
11292   Style.ConstructorInitializerIndentWidth = 4;
11293   Style.ColumnLimit = 60;
11294   verifyFormat("SomeClass::Constructor()\n"
11295                "    : aaaaaaaa(aaaaaaaa)\n"
11296                "    , aaaaaaaa(aaaaaaaa)\n"
11297                "    , aaaaaaaa(aaaaaaaa) {}",
11298                Style);
11299 }
11300 
11301 TEST_F(FormatTest, Destructors) {
11302   verifyFormat("void F(int &i) { i.~int(); }");
11303   verifyFormat("void F(int &i) { i->~int(); }");
11304 }
11305 
11306 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11307   FormatStyle Style = getWebKitStyle();
11308 
11309   // Don't indent in outer namespaces.
11310   verifyFormat("namespace outer {\n"
11311                "int i;\n"
11312                "namespace inner {\n"
11313                "    int i;\n"
11314                "} // namespace inner\n"
11315                "} // namespace outer\n"
11316                "namespace other_outer {\n"
11317                "int i;\n"
11318                "}",
11319                Style);
11320 
11321   // Don't indent case labels.
11322   verifyFormat("switch (variable) {\n"
11323                "case 1:\n"
11324                "case 2:\n"
11325                "    doSomething();\n"
11326                "    break;\n"
11327                "default:\n"
11328                "    ++variable;\n"
11329                "}",
11330                Style);
11331 
11332   // Wrap before binary operators.
11333   EXPECT_EQ("void f()\n"
11334             "{\n"
11335             "    if (aaaaaaaaaaaaaaaa\n"
11336             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11337             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11338             "        return;\n"
11339             "}",
11340             format("void f() {\n"
11341                    "if (aaaaaaaaaaaaaaaa\n"
11342                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11343                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11344                    "return;\n"
11345                    "}",
11346                    Style));
11347 
11348   // Allow functions on a single line.
11349   verifyFormat("void f() { return; }", Style);
11350 
11351   // Constructor initializers are formatted one per line with the "," on the
11352   // new line.
11353   verifyFormat("Constructor()\n"
11354                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11355                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11356                "          aaaaaaaaaaaaaa)\n"
11357                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11358                "{\n"
11359                "}",
11360                Style);
11361   verifyFormat("SomeClass::Constructor()\n"
11362                "    : a(a)\n"
11363                "{\n"
11364                "}",
11365                Style);
11366   EXPECT_EQ("SomeClass::Constructor()\n"
11367             "    : a(a)\n"
11368             "{\n"
11369             "}",
11370             format("SomeClass::Constructor():a(a){}", Style));
11371   verifyFormat("SomeClass::Constructor()\n"
11372                "    : a(a)\n"
11373                "    , b(b)\n"
11374                "    , c(c)\n"
11375                "{\n"
11376                "}",
11377                Style);
11378   verifyFormat("SomeClass::Constructor()\n"
11379                "    : a(a)\n"
11380                "{\n"
11381                "    foo();\n"
11382                "    bar();\n"
11383                "}",
11384                Style);
11385 
11386   // Access specifiers should be aligned left.
11387   verifyFormat("class C {\n"
11388                "public:\n"
11389                "    int i;\n"
11390                "};",
11391                Style);
11392 
11393   // Do not align comments.
11394   verifyFormat("int a; // Do not\n"
11395                "double b; // align comments.",
11396                Style);
11397 
11398   // Do not align operands.
11399   EXPECT_EQ("ASSERT(aaaa\n"
11400             "    || bbbb);",
11401             format("ASSERT ( aaaa\n||bbbb);", Style));
11402 
11403   // Accept input's line breaks.
11404   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11405             "    || bbbbbbbbbbbbbbb) {\n"
11406             "    i++;\n"
11407             "}",
11408             format("if (aaaaaaaaaaaaaaa\n"
11409                    "|| bbbbbbbbbbbbbbb) { i++; }",
11410                    Style));
11411   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11412             "    i++;\n"
11413             "}",
11414             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11415 
11416   // Don't automatically break all macro definitions (llvm.org/PR17842).
11417   verifyFormat("#define aNumber 10", Style);
11418   // However, generally keep the line breaks that the user authored.
11419   EXPECT_EQ("#define aNumber \\\n"
11420             "    10",
11421             format("#define aNumber \\\n"
11422                    " 10",
11423                    Style));
11424 
11425   // Keep empty and one-element array literals on a single line.
11426   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11427             "                                  copyItems:YES];",
11428             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11429                    "copyItems:YES];",
11430                    Style));
11431   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11432             "                                  copyItems:YES];",
11433             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11434                    "             copyItems:YES];",
11435                    Style));
11436   // FIXME: This does not seem right, there should be more indentation before
11437   // the array literal's entries. Nested blocks have the same problem.
11438   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11439             "    @\"a\",\n"
11440             "    @\"a\"\n"
11441             "]\n"
11442             "                                  copyItems:YES];",
11443             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11444                    "     @\"a\",\n"
11445                    "     @\"a\"\n"
11446                    "     ]\n"
11447                    "       copyItems:YES];",
11448                    Style));
11449   EXPECT_EQ(
11450       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11451       "                                  copyItems:YES];",
11452       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11453              "   copyItems:YES];",
11454              Style));
11455 
11456   verifyFormat("[self.a b:c c:d];", Style);
11457   EXPECT_EQ("[self.a b:c\n"
11458             "        c:d];",
11459             format("[self.a b:c\n"
11460                    "c:d];",
11461                    Style));
11462 }
11463 
11464 TEST_F(FormatTest, FormatsLambdas) {
11465   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11466   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11467   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11468   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11469   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11470   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11471   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11472   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11473   verifyFormat("int x = f(*+[] {});");
11474   verifyFormat("void f() {\n"
11475                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11476                "}\n");
11477   verifyFormat("void f() {\n"
11478                "  other(x.begin(), //\n"
11479                "        x.end(),   //\n"
11480                "        [&](int, int) { return 1; });\n"
11481                "}\n");
11482   verifyFormat("SomeFunction([]() { // A cool function...\n"
11483                "  return 43;\n"
11484                "});");
11485   EXPECT_EQ("SomeFunction([]() {\n"
11486             "#define A a\n"
11487             "  return 43;\n"
11488             "});",
11489             format("SomeFunction([](){\n"
11490                    "#define A a\n"
11491                    "return 43;\n"
11492                    "});"));
11493   verifyFormat("void f() {\n"
11494                "  SomeFunction([](decltype(x), A *a) {});\n"
11495                "}");
11496   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11497                "    [](const aaaaaaaaaa &a) { return a; });");
11498   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11499                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11500                "});");
11501   verifyFormat("Constructor()\n"
11502                "    : Field([] { // comment\n"
11503                "        int i;\n"
11504                "      }) {}");
11505   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11506                "  return some_parameter.size();\n"
11507                "};");
11508   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11509                "    [](const string &s) { return s; };");
11510   verifyFormat("int i = aaaaaa ? 1 //\n"
11511                "               : [] {\n"
11512                "                   return 2; //\n"
11513                "                 }();");
11514   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11515                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11516                "                  return x == 2; // force break\n"
11517                "                });");
11518   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11519                "    [=](int iiiiiiiiiiii) {\n"
11520                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11521                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11522                "    });",
11523                getLLVMStyleWithColumns(60));
11524   verifyFormat("SomeFunction({[&] {\n"
11525                "                // comment\n"
11526                "              },\n"
11527                "              [&] {\n"
11528                "                // comment\n"
11529                "              }});");
11530   verifyFormat("SomeFunction({[&] {\n"
11531                "  // comment\n"
11532                "}});");
11533   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
11534                "                             [&]() { return true; },\n"
11535                "                         aaaaa aaaaaaaaa);");
11536 
11537   // Lambdas with return types.
11538   verifyFormat("int c = []() -> int { return 2; }();\n");
11539   verifyFormat("int c = []() -> int * { return 2; }();\n");
11540   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11541   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11542   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11543   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11544   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11545   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11546   verifyFormat("[a, a]() -> a<1> {};");
11547   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11548                "                   int j) -> int {\n"
11549                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11550                "};");
11551   verifyFormat(
11552       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11553       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11554       "      return aaaaaaaaaaaaaaaaa;\n"
11555       "    });",
11556       getLLVMStyleWithColumns(70));
11557   verifyFormat("[]() //\n"
11558                "    -> int {\n"
11559                "  return 1; //\n"
11560                "};");
11561 
11562   // Multiple lambdas in the same parentheses change indentation rules.
11563   verifyFormat("SomeFunction(\n"
11564                "    []() {\n"
11565                "      int i = 42;\n"
11566                "      return i;\n"
11567                "    },\n"
11568                "    []() {\n"
11569                "      int j = 43;\n"
11570                "      return j;\n"
11571                "    });");
11572 
11573   // More complex introducers.
11574   verifyFormat("return [i, args...] {};");
11575 
11576   // Not lambdas.
11577   verifyFormat("constexpr char hello[]{\"hello\"};");
11578   verifyFormat("double &operator[](int i) { return 0; }\n"
11579                "int i;");
11580   verifyFormat("std::unique_ptr<int[]> foo() {}");
11581   verifyFormat("int i = a[a][a]->f();");
11582   verifyFormat("int i = (*b)[a]->f();");
11583 
11584   // Other corner cases.
11585   verifyFormat("void f() {\n"
11586                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11587                "  );\n"
11588                "}");
11589 
11590   // Lambdas created through weird macros.
11591   verifyFormat("void f() {\n"
11592                "  MACRO((const AA &a) { return 1; });\n"
11593                "  MACRO((AA &a) { return 1; });\n"
11594                "}");
11595 
11596   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11597                "      doo_dah();\n"
11598                "      doo_dah();\n"
11599                "    })) {\n"
11600                "}");
11601   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
11602                "                doo_dah();\n"
11603                "                doo_dah();\n"
11604                "              })) {\n"
11605                "}");
11606   verifyFormat("auto lambda = []() {\n"
11607                "  int a = 2\n"
11608                "#if A\n"
11609                "          + 2\n"
11610                "#endif\n"
11611                "      ;\n"
11612                "};");
11613 
11614   // Lambdas with complex multiline introducers.
11615   verifyFormat(
11616       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11617       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
11618       "        -> ::std::unordered_set<\n"
11619       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
11620       "      //\n"
11621       "    });");
11622 }
11623 
11624 TEST_F(FormatTest, EmptyLinesInLambdas) {
11625   verifyFormat("auto lambda = []() {\n"
11626                "  x(); //\n"
11627                "};",
11628                "auto lambda = []() {\n"
11629                "\n"
11630                "  x(); //\n"
11631                "\n"
11632                "};");
11633 }
11634 
11635 TEST_F(FormatTest, FormatsBlocks) {
11636   FormatStyle ShortBlocks = getLLVMStyle();
11637   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11638   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11639   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11640   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11641   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11642   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11643   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11644 
11645   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11646   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11647   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11648 
11649   verifyFormat("[operation setCompletionBlock:^{\n"
11650                "  [self onOperationDone];\n"
11651                "}];");
11652   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11653                "  [self onOperationDone];\n"
11654                "}]};");
11655   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11656                "  f();\n"
11657                "}];");
11658   verifyFormat("int a = [operation block:^int(int *i) {\n"
11659                "  return 1;\n"
11660                "}];");
11661   verifyFormat("[myObject doSomethingWith:arg1\n"
11662                "                      aaa:^int(int *a) {\n"
11663                "                        return 1;\n"
11664                "                      }\n"
11665                "                      bbb:f(a * bbbbbbbb)];");
11666 
11667   verifyFormat("[operation setCompletionBlock:^{\n"
11668                "  [self.delegate newDataAvailable];\n"
11669                "}];",
11670                getLLVMStyleWithColumns(60));
11671   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11672                "  NSString *path = [self sessionFilePath];\n"
11673                "  if (path) {\n"
11674                "    // ...\n"
11675                "  }\n"
11676                "});");
11677   verifyFormat("[[SessionService sharedService]\n"
11678                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11679                "      if (window) {\n"
11680                "        [self windowDidLoad:window];\n"
11681                "      } else {\n"
11682                "        [self errorLoadingWindow];\n"
11683                "      }\n"
11684                "    }];");
11685   verifyFormat("void (^largeBlock)(void) = ^{\n"
11686                "  // ...\n"
11687                "};\n",
11688                getLLVMStyleWithColumns(40));
11689   verifyFormat("[[SessionService sharedService]\n"
11690                "    loadWindowWithCompletionBlock: //\n"
11691                "        ^(SessionWindow *window) {\n"
11692                "          if (window) {\n"
11693                "            [self windowDidLoad:window];\n"
11694                "          } else {\n"
11695                "            [self errorLoadingWindow];\n"
11696                "          }\n"
11697                "        }];",
11698                getLLVMStyleWithColumns(60));
11699   verifyFormat("[myObject doSomethingWith:arg1\n"
11700                "    firstBlock:^(Foo *a) {\n"
11701                "      // ...\n"
11702                "      int i;\n"
11703                "    }\n"
11704                "    secondBlock:^(Bar *b) {\n"
11705                "      // ...\n"
11706                "      int i;\n"
11707                "    }\n"
11708                "    thirdBlock:^Foo(Bar *b) {\n"
11709                "      // ...\n"
11710                "      int i;\n"
11711                "    }];");
11712   verifyFormat("[myObject doSomethingWith:arg1\n"
11713                "               firstBlock:-1\n"
11714                "              secondBlock:^(Bar *b) {\n"
11715                "                // ...\n"
11716                "                int i;\n"
11717                "              }];");
11718 
11719   verifyFormat("f(^{\n"
11720                "  @autoreleasepool {\n"
11721                "    if (a) {\n"
11722                "      g();\n"
11723                "    }\n"
11724                "  }\n"
11725                "});");
11726   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11727   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11728                "};");
11729 
11730   FormatStyle FourIndent = getLLVMStyle();
11731   FourIndent.ObjCBlockIndentWidth = 4;
11732   verifyFormat("[operation setCompletionBlock:^{\n"
11733                "    [self onOperationDone];\n"
11734                "}];",
11735                FourIndent);
11736 }
11737 
11738 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11739   FormatStyle ZeroColumn = getLLVMStyle();
11740   ZeroColumn.ColumnLimit = 0;
11741 
11742   verifyFormat("[[SessionService sharedService] "
11743                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11744                "  if (window) {\n"
11745                "    [self windowDidLoad:window];\n"
11746                "  } else {\n"
11747                "    [self errorLoadingWindow];\n"
11748                "  }\n"
11749                "}];",
11750                ZeroColumn);
11751   EXPECT_EQ("[[SessionService sharedService]\n"
11752             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11753             "      if (window) {\n"
11754             "        [self windowDidLoad:window];\n"
11755             "      } else {\n"
11756             "        [self errorLoadingWindow];\n"
11757             "      }\n"
11758             "    }];",
11759             format("[[SessionService sharedService]\n"
11760                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11761                    "                if (window) {\n"
11762                    "    [self windowDidLoad:window];\n"
11763                    "  } else {\n"
11764                    "    [self errorLoadingWindow];\n"
11765                    "  }\n"
11766                    "}];",
11767                    ZeroColumn));
11768   verifyFormat("[myObject doSomethingWith:arg1\n"
11769                "    firstBlock:^(Foo *a) {\n"
11770                "      // ...\n"
11771                "      int i;\n"
11772                "    }\n"
11773                "    secondBlock:^(Bar *b) {\n"
11774                "      // ...\n"
11775                "      int i;\n"
11776                "    }\n"
11777                "    thirdBlock:^Foo(Bar *b) {\n"
11778                "      // ...\n"
11779                "      int i;\n"
11780                "    }];",
11781                ZeroColumn);
11782   verifyFormat("f(^{\n"
11783                "  @autoreleasepool {\n"
11784                "    if (a) {\n"
11785                "      g();\n"
11786                "    }\n"
11787                "  }\n"
11788                "});",
11789                ZeroColumn);
11790   verifyFormat("void (^largeBlock)(void) = ^{\n"
11791                "  // ...\n"
11792                "};",
11793                ZeroColumn);
11794 
11795   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11796   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11797             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11798   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11799   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11800             "  int i;\n"
11801             "};",
11802             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11803 }
11804 
11805 TEST_F(FormatTest, SupportsCRLF) {
11806   EXPECT_EQ("int a;\r\n"
11807             "int b;\r\n"
11808             "int c;\r\n",
11809             format("int a;\r\n"
11810                    "  int b;\r\n"
11811                    "    int c;\r\n",
11812                    getLLVMStyle()));
11813   EXPECT_EQ("int a;\r\n"
11814             "int b;\r\n"
11815             "int c;\r\n",
11816             format("int a;\r\n"
11817                    "  int b;\n"
11818                    "    int c;\r\n",
11819                    getLLVMStyle()));
11820   EXPECT_EQ("int a;\n"
11821             "int b;\n"
11822             "int c;\n",
11823             format("int a;\r\n"
11824                    "  int b;\n"
11825                    "    int c;\n",
11826                    getLLVMStyle()));
11827   EXPECT_EQ("\"aaaaaaa \"\r\n"
11828             "\"bbbbbbb\";\r\n",
11829             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11830   EXPECT_EQ("#define A \\\r\n"
11831             "  b;      \\\r\n"
11832             "  c;      \\\r\n"
11833             "  d;\r\n",
11834             format("#define A \\\r\n"
11835                    "  b; \\\r\n"
11836                    "  c; d; \r\n",
11837                    getGoogleStyle()));
11838 
11839   EXPECT_EQ("/*\r\n"
11840             "multi line block comments\r\n"
11841             "should not introduce\r\n"
11842             "an extra carriage return\r\n"
11843             "*/\r\n",
11844             format("/*\r\n"
11845                    "multi line block comments\r\n"
11846                    "should not introduce\r\n"
11847                    "an extra carriage return\r\n"
11848                    "*/\r\n"));
11849 }
11850 
11851 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11852   verifyFormat("MY_CLASS(C) {\n"
11853                "  int i;\n"
11854                "  int j;\n"
11855                "};");
11856 }
11857 
11858 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11859   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11860   TwoIndent.ContinuationIndentWidth = 2;
11861 
11862   EXPECT_EQ("int i =\n"
11863             "  longFunction(\n"
11864             "    arg);",
11865             format("int i = longFunction(arg);", TwoIndent));
11866 
11867   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11868   SixIndent.ContinuationIndentWidth = 6;
11869 
11870   EXPECT_EQ("int i =\n"
11871             "      longFunction(\n"
11872             "            arg);",
11873             format("int i = longFunction(arg);", SixIndent));
11874 }
11875 
11876 TEST_F(FormatTest, SpacesInAngles) {
11877   FormatStyle Spaces = getLLVMStyle();
11878   Spaces.SpacesInAngles = true;
11879 
11880   verifyFormat("static_cast< int >(arg);", Spaces);
11881   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11882   verifyFormat("f< int, float >();", Spaces);
11883   verifyFormat("template <> g() {}", Spaces);
11884   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11885   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11886   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11887                Spaces);
11888 
11889   Spaces.Standard = FormatStyle::LS_Cpp03;
11890   Spaces.SpacesInAngles = true;
11891   verifyFormat("A< A< int > >();", Spaces);
11892 
11893   Spaces.SpacesInAngles = false;
11894   verifyFormat("A<A<int> >();", Spaces);
11895 
11896   Spaces.Standard = FormatStyle::LS_Cpp11;
11897   Spaces.SpacesInAngles = true;
11898   verifyFormat("A< A< int > >();", Spaces);
11899 
11900   Spaces.SpacesInAngles = false;
11901   verifyFormat("A<A<int>>();", Spaces);
11902 }
11903 
11904 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11905   FormatStyle Style = getLLVMStyle();
11906   Style.SpaceAfterTemplateKeyword = false;
11907   verifyFormat("template<int> void foo();", Style);
11908 }
11909 
11910 TEST_F(FormatTest, TripleAngleBrackets) {
11911   verifyFormat("f<<<1, 1>>>();");
11912   verifyFormat("f<<<1, 1, 1, s>>>();");
11913   verifyFormat("f<<<a, b, c, d>>>();");
11914   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11915   verifyFormat("f<param><<<1, 1>>>();");
11916   verifyFormat("f<1><<<1, 1>>>();");
11917   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11918   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11919                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11920   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11921                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11922 }
11923 
11924 TEST_F(FormatTest, MergeLessLessAtEnd) {
11925   verifyFormat("<<");
11926   EXPECT_EQ("< < <", format("\\\n<<<"));
11927   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11928                "aaallvm::outs() <<");
11929   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11930                "aaaallvm::outs()\n    <<");
11931 }
11932 
11933 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11934   std::string code = "#if A\n"
11935                      "#if B\n"
11936                      "a.\n"
11937                      "#endif\n"
11938                      "    a = 1;\n"
11939                      "#else\n"
11940                      "#endif\n"
11941                      "#if C\n"
11942                      "#else\n"
11943                      "#endif\n";
11944   EXPECT_EQ(code, format(code));
11945 }
11946 
11947 TEST_F(FormatTest, HandleConflictMarkers) {
11948   // Git/SVN conflict markers.
11949   EXPECT_EQ("int a;\n"
11950             "void f() {\n"
11951             "  callme(some(parameter1,\n"
11952             "<<<<<<< text by the vcs\n"
11953             "              parameter2),\n"
11954             "||||||| text by the vcs\n"
11955             "              parameter2),\n"
11956             "         parameter3,\n"
11957             "======= text by the vcs\n"
11958             "              parameter2, parameter3),\n"
11959             ">>>>>>> text by the vcs\n"
11960             "         otherparameter);\n",
11961             format("int a;\n"
11962                    "void f() {\n"
11963                    "  callme(some(parameter1,\n"
11964                    "<<<<<<< text by the vcs\n"
11965                    "  parameter2),\n"
11966                    "||||||| text by the vcs\n"
11967                    "  parameter2),\n"
11968                    "  parameter3,\n"
11969                    "======= text by the vcs\n"
11970                    "  parameter2,\n"
11971                    "  parameter3),\n"
11972                    ">>>>>>> text by the vcs\n"
11973                    "  otherparameter);\n"));
11974 
11975   // Perforce markers.
11976   EXPECT_EQ("void f() {\n"
11977             "  function(\n"
11978             ">>>> text by the vcs\n"
11979             "      parameter,\n"
11980             "==== text by the vcs\n"
11981             "      parameter,\n"
11982             "==== text by the vcs\n"
11983             "      parameter,\n"
11984             "<<<< text by the vcs\n"
11985             "      parameter);\n",
11986             format("void f() {\n"
11987                    "  function(\n"
11988                    ">>>> text by the vcs\n"
11989                    "  parameter,\n"
11990                    "==== text by the vcs\n"
11991                    "  parameter,\n"
11992                    "==== text by the vcs\n"
11993                    "  parameter,\n"
11994                    "<<<< text by the vcs\n"
11995                    "  parameter);\n"));
11996 
11997   EXPECT_EQ("<<<<<<<\n"
11998             "|||||||\n"
11999             "=======\n"
12000             ">>>>>>>",
12001             format("<<<<<<<\n"
12002                    "|||||||\n"
12003                    "=======\n"
12004                    ">>>>>>>"));
12005 
12006   EXPECT_EQ("<<<<<<<\n"
12007             "|||||||\n"
12008             "int i;\n"
12009             "=======\n"
12010             ">>>>>>>",
12011             format("<<<<<<<\n"
12012                    "|||||||\n"
12013                    "int i;\n"
12014                    "=======\n"
12015                    ">>>>>>>"));
12016 
12017   // FIXME: Handle parsing of macros around conflict markers correctly:
12018   EXPECT_EQ("#define Macro \\\n"
12019             "<<<<<<<\n"
12020             "Something \\\n"
12021             "|||||||\n"
12022             "Else \\\n"
12023             "=======\n"
12024             "Other \\\n"
12025             ">>>>>>>\n"
12026             "    End int i;\n",
12027             format("#define Macro \\\n"
12028                    "<<<<<<<\n"
12029                    "  Something \\\n"
12030                    "|||||||\n"
12031                    "  Else \\\n"
12032                    "=======\n"
12033                    "  Other \\\n"
12034                    ">>>>>>>\n"
12035                    "  End\n"
12036                    "int i;\n"));
12037 }
12038 
12039 TEST_F(FormatTest, DisableRegions) {
12040   EXPECT_EQ("int i;\n"
12041             "// clang-format off\n"
12042             "  int j;\n"
12043             "// clang-format on\n"
12044             "int k;",
12045             format(" int  i;\n"
12046                    "   // clang-format off\n"
12047                    "  int j;\n"
12048                    " // clang-format on\n"
12049                    "   int   k;"));
12050   EXPECT_EQ("int i;\n"
12051             "/* clang-format off */\n"
12052             "  int j;\n"
12053             "/* clang-format on */\n"
12054             "int k;",
12055             format(" int  i;\n"
12056                    "   /* clang-format off */\n"
12057                    "  int j;\n"
12058                    " /* clang-format on */\n"
12059                    "   int   k;"));
12060 
12061   // Don't reflow comments within disabled regions.
12062   EXPECT_EQ(
12063       "// clang-format off\n"
12064       "// long long long long long long line\n"
12065       "/* clang-format on */\n"
12066       "/* long long long\n"
12067       " * long long long\n"
12068       " * line */\n"
12069       "int i;\n"
12070       "/* clang-format off */\n"
12071       "/* long long long long long long line */\n",
12072       format("// clang-format off\n"
12073              "// long long long long long long line\n"
12074              "/* clang-format on */\n"
12075              "/* long long long long long long line */\n"
12076              "int i;\n"
12077              "/* clang-format off */\n"
12078              "/* long long long long long long line */\n",
12079              getLLVMStyleWithColumns(20)));
12080 }
12081 
12082 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12083   format("? ) =");
12084   verifyNoCrash("#define a\\\n /**/}");
12085 }
12086 
12087 TEST_F(FormatTest, FormatsTableGenCode) {
12088   FormatStyle Style = getLLVMStyle();
12089   Style.Language = FormatStyle::LK_TableGen;
12090   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12091 }
12092 
12093 TEST_F(FormatTest, ArrayOfTemplates) {
12094   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12095             format("auto a = new unique_ptr<int > [ 10];"));
12096 
12097   FormatStyle Spaces = getLLVMStyle();
12098   Spaces.SpacesInSquareBrackets = true;
12099   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12100             format("auto a = new unique_ptr<int > [10];", Spaces));
12101 }
12102 
12103 TEST_F(FormatTest, ArrayAsTemplateType) {
12104   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12105             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12106 
12107   FormatStyle Spaces = getLLVMStyle();
12108   Spaces.SpacesInSquareBrackets = true;
12109   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12110             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12111 }
12112 
12113 TEST_F(FormatTest, NoSpaceAfterSuper) {
12114     verifyFormat("__super::FooBar();");
12115 }
12116 
12117 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12118   vfs::InMemoryFileSystem FS;
12119   auto Style1 = getStyle("file", "", "Google", "", &FS);
12120   ASSERT_TRUE((bool)Style1);
12121   ASSERT_EQ(*Style1, getGoogleStyle());
12122 }
12123 
12124 TEST(FormatStyle, GetStyleOfFile) {
12125   vfs::InMemoryFileSystem FS;
12126   // Test 1: format file in the same directory.
12127   ASSERT_TRUE(
12128       FS.addFile("/a/.clang-format", 0,
12129                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12130   ASSERT_TRUE(
12131       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12132   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12133   ASSERT_TRUE((bool)Style1);
12134   ASSERT_EQ(*Style1, getLLVMStyle());
12135 
12136   // Test 2.1: fallback to default.
12137   ASSERT_TRUE(
12138       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12139   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12140   ASSERT_TRUE((bool)Style2);
12141   ASSERT_EQ(*Style2, getMozillaStyle());
12142 
12143   // Test 2.2: no format on 'none' fallback style.
12144   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12145   ASSERT_TRUE((bool)Style2);
12146   ASSERT_EQ(*Style2, getNoStyle());
12147 
12148   // Test 2.3: format if config is found with no based style while fallback is
12149   // 'none'.
12150   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12151                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12152   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12153   ASSERT_TRUE((bool)Style2);
12154   ASSERT_EQ(*Style2, getLLVMStyle());
12155 
12156   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12157   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12158   ASSERT_TRUE((bool)Style2);
12159   ASSERT_EQ(*Style2, getLLVMStyle());
12160 
12161   // Test 3: format file in parent directory.
12162   ASSERT_TRUE(
12163       FS.addFile("/c/.clang-format", 0,
12164                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12165   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12166                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12167   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12168   ASSERT_TRUE((bool)Style3);
12169   ASSERT_EQ(*Style3, getGoogleStyle());
12170 
12171   // Test 4: error on invalid fallback style
12172   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12173   ASSERT_FALSE((bool)Style4);
12174   llvm::consumeError(Style4.takeError());
12175 
12176   // Test 5: error on invalid yaml on command line
12177   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12178   ASSERT_FALSE((bool)Style5);
12179   llvm::consumeError(Style5.takeError());
12180 
12181   // Test 6: error on invalid style
12182   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12183   ASSERT_FALSE((bool)Style6);
12184   llvm::consumeError(Style6.takeError());
12185 
12186   // Test 7: found config file, error on parsing it
12187   ASSERT_TRUE(
12188       FS.addFile("/d/.clang-format", 0,
12189                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
12190                                                   "InvalidKey: InvalidValue")));
12191   ASSERT_TRUE(
12192       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12193   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
12194   ASSERT_FALSE((bool)Style7);
12195   llvm::consumeError(Style7.takeError());
12196 }
12197 
12198 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
12199   // Column limit is 20.
12200   std::string Code = "Type *a =\n"
12201                      "    new Type();\n"
12202                      "g(iiiii, 0, jjjjj,\n"
12203                      "  0, kkkkk, 0, mm);\n"
12204                      "int  bad     = format   ;";
12205   std::string Expected = "auto a = new Type();\n"
12206                          "g(iiiii, nullptr,\n"
12207                          "  jjjjj, nullptr,\n"
12208                          "  kkkkk, nullptr,\n"
12209                          "  mm);\n"
12210                          "int  bad     = format   ;";
12211   FileID ID = Context.createInMemoryFile("format.cpp", Code);
12212   tooling::Replacements Replaces = toReplacements(
12213       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
12214                             "auto "),
12215        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12216                             "nullptr"),
12217        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12218                             "nullptr"),
12219        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12220                             "nullptr")});
12221 
12222   format::FormatStyle Style = format::getLLVMStyle();
12223   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12224   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12225   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12226       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12227   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12228   EXPECT_TRUE(static_cast<bool>(Result));
12229   EXPECT_EQ(Expected, *Result);
12230 }
12231 
12232 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12233   std::string Code = "#include \"a.h\"\n"
12234                      "#include \"c.h\"\n"
12235                      "\n"
12236                      "int main() {\n"
12237                      "  return 0;\n"
12238                      "}";
12239   std::string Expected = "#include \"a.h\"\n"
12240                          "#include \"b.h\"\n"
12241                          "#include \"c.h\"\n"
12242                          "\n"
12243                          "int main() {\n"
12244                          "  return 0;\n"
12245                          "}";
12246   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12247   tooling::Replacements Replaces = toReplacements(
12248       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12249                             "#include \"b.h\"\n")});
12250 
12251   format::FormatStyle Style = format::getLLVMStyle();
12252   Style.SortIncludes = true;
12253   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12254   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12255       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12256   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12257   EXPECT_TRUE(static_cast<bool>(Result));
12258   EXPECT_EQ(Expected, *Result);
12259 }
12260 
12261 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12262   EXPECT_EQ("using std::cin;\n"
12263             "using std::cout;",
12264             format("using std::cout;\n"
12265                    "using std::cin;", getGoogleStyle()));
12266 }
12267 
12268 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12269   format::FormatStyle Style = format::getLLVMStyle();
12270   Style.Standard = FormatStyle::LS_Cpp03;
12271   // cpp03 recognize this string as identifier u8 and literal character 'a'
12272   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12273 }
12274 
12275 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12276   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12277   // all modes, including C++11, C++14 and C++17
12278   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12279 }
12280 
12281 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12282   EXPECT_EQ("<!-- ;> -->",
12283             format("<!-- ;> -->", getGoogleStyle()));
12284   EXPECT_EQ(" <!-- >; -->",
12285             format(" <!-- >; -->", getGoogleStyle()));
12286 }
12287 
12288 TEST_F(FormatTest, StructuredBindings) {
12289   // Structured bindings is a C++17 feature.
12290   // all modes, including C++11, C++14 and C++17
12291   verifyFormat("auto [a, b] = f();");
12292   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12293   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12294   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12295   EXPECT_EQ("auto const volatile [a, b] = f();",
12296             format("auto  const   volatile[a, b] = f();"));
12297   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12298   EXPECT_EQ("auto &[a, b, c] = f();",
12299             format("auto   &[  a  ,  b,c   ] = f();"));
12300   EXPECT_EQ("auto &&[a, b, c] = f();",
12301             format("auto   &&[  a  ,  b,c   ] = f();"));
12302   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12303   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12304             format("auto  const  volatile  &&[a, b] = f();"));
12305   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12306   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12307   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12308             format("const  auto   volatile  &&[a, b] = f();"));
12309   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12310             format("volatile  const  auto   &&[a, b] = f();"));
12311   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12312 
12313   // Make sure we don't mistake structured bindings for lambdas.
12314   FormatStyle PointerMiddle = getLLVMStyle();
12315   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12316   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12317   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12318   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12319   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12320   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12321   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12322   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12323   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12324   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12325   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12326   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12327   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12328 
12329   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12330             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12331   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12332             format("for (const auto   &   [a, b] : some_range) {\n}"));
12333   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12334             format("for (const auto[a, b] : some_range) {\n}"));
12335   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12336   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12337   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12338   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12339   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12340   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12341   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12342   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12343 
12344   format::FormatStyle Spaces = format::getLLVMStyle();
12345   Spaces.SpacesInSquareBrackets = true;
12346   verifyFormat("auto [ a, b ] = f();", Spaces);
12347   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12348   verifyFormat("auto &[ a, b ] = f();", Spaces);
12349   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12350   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12351 }
12352 
12353 TEST_F(FormatTest, FileAndCode) {
12354   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12355   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12356   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12357   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12358   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12359   EXPECT_EQ(
12360       FormatStyle::LK_ObjC,
12361       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12362   EXPECT_EQ(FormatStyle::LK_ObjC,
12363             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12364   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12365   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12366   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12367   EXPECT_EQ(FormatStyle::LK_ObjC,
12368             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12369   EXPECT_EQ(
12370       FormatStyle::LK_ObjC,
12371       guessLanguage("foo.h",
12372                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12373   EXPECT_EQ(
12374       FormatStyle::LK_Cpp,
12375       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12376 }
12377 
12378 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12379   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12380   EXPECT_EQ(FormatStyle::LK_ObjC,
12381             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12382   EXPECT_EQ(FormatStyle::LK_Cpp,
12383             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12384   EXPECT_EQ(
12385       FormatStyle::LK_Cpp,
12386       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12387   EXPECT_EQ(FormatStyle::LK_ObjC,
12388             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12389   EXPECT_EQ(FormatStyle::LK_Cpp,
12390             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12391   EXPECT_EQ(FormatStyle::LK_ObjC,
12392             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12393   EXPECT_EQ(FormatStyle::LK_Cpp,
12394             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12395   EXPECT_EQ(FormatStyle::LK_Cpp,
12396             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12397   EXPECT_EQ(FormatStyle::LK_ObjC,
12398             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12399   EXPECT_EQ(FormatStyle::LK_Cpp,
12400             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12401   EXPECT_EQ(
12402       FormatStyle::LK_Cpp,
12403       guessLanguage("foo.h",
12404                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12405   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12406 }
12407 
12408 TEST_F(FormatTest, GuessLanguageWithCaret) {
12409   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12410   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12411   EXPECT_EQ(FormatStyle::LK_ObjC,
12412             guessLanguage("foo.h", "int(^)(char, float);"));
12413   EXPECT_EQ(FormatStyle::LK_ObjC,
12414             guessLanguage("foo.h", "int(^foo)(char, float);"));
12415   EXPECT_EQ(FormatStyle::LK_ObjC,
12416             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12417   EXPECT_EQ(FormatStyle::LK_ObjC,
12418             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12419   EXPECT_EQ(
12420       FormatStyle::LK_ObjC,
12421       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12422 }
12423 
12424 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12425   EXPECT_EQ(FormatStyle::LK_Cpp,
12426             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12427   EXPECT_EQ(FormatStyle::LK_ObjC,
12428             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12429   EXPECT_EQ(
12430       FormatStyle::LK_Cpp,
12431       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12432   EXPECT_EQ(
12433       FormatStyle::LK_ObjC,
12434       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12435 }
12436 
12437 } // end namespace
12438 } // end namespace format
12439 } // end namespace clang
12440