1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68 
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72 
73   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Expected.str(), format(Expected, Style))
76         << "Expected code is not stable";
77     EXPECT_EQ(Expected.str(), format(Code, Style));
78     if (Style.Language == FormatStyle::LK_Cpp) {
79       // Objective-C++ is a superset of C++, so everything checked for C++
80       // needs to be checked for Objective-C++ as well.
81       FormatStyle ObjCStyle = Style;
82       ObjCStyle.Language = FormatStyle::LK_ObjC;
83       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
84     }
85   }
86 
87   void verifyFormat(llvm::StringRef Code,
88                     const FormatStyle &Style = getLLVMStyle()) {
89     verifyFormat(Code, test::messUp(Code), Style);
90   }
91 
92   void verifyIncompleteFormat(llvm::StringRef Code,
93                               const FormatStyle &Style = getLLVMStyle()) {
94     EXPECT_EQ(Code.str(),
95               format(test::messUp(Code), Style, SC_ExpectIncomplete));
96   }
97 
98   void verifyGoogleFormat(llvm::StringRef Code) {
99     verifyFormat(Code, getGoogleStyle());
100   }
101 
102   void verifyIndependentOfContext(llvm::StringRef text) {
103     verifyFormat(text);
104     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
105   }
106 
107   /// \brief Verify that clang-format does not crash on the given input.
108   void verifyNoCrash(llvm::StringRef Code,
109                      const FormatStyle &Style = getLLVMStyle()) {
110     format(Code, Style, SC_DoNotCheck);
111   }
112 
113   int ReplacementCount;
114 };
115 
116 TEST_F(FormatTest, MessUp) {
117   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
118   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
119   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
120   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
121   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
122 }
123 
124 //===----------------------------------------------------------------------===//
125 // Basic function tests.
126 //===----------------------------------------------------------------------===//
127 
128 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
129   EXPECT_EQ(";", format(";"));
130 }
131 
132 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
133   EXPECT_EQ("int i;", format("  int i;"));
134   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
135   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
136   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
137 }
138 
139 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
140   EXPECT_EQ("int i;", format("int\ni;"));
141 }
142 
143 TEST_F(FormatTest, FormatsNestedBlockStatements) {
144   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
145 }
146 
147 TEST_F(FormatTest, FormatsNestedCall) {
148   verifyFormat("Method(f1, f2(f3));");
149   verifyFormat("Method(f1(f2, f3()));");
150   verifyFormat("Method(f1(f2, (f3())));");
151 }
152 
153 TEST_F(FormatTest, NestedNameSpecifiers) {
154   verifyFormat("vector<::Type> v;");
155   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
156   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
157   verifyFormat("bool a = 2 < ::SomeFunction();");
158   verifyFormat("ALWAYS_INLINE ::std::string getName();");
159   verifyFormat("some::string getName();");
160 }
161 
162 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
163   EXPECT_EQ("if (a) {\n"
164             "  f();\n"
165             "}",
166             format("if(a){f();}"));
167   EXPECT_EQ(4, ReplacementCount);
168   EXPECT_EQ("if (a) {\n"
169             "  f();\n"
170             "}",
171             format("if (a) {\n"
172                    "  f();\n"
173                    "}"));
174   EXPECT_EQ(0, ReplacementCount);
175   EXPECT_EQ("/*\r\n"
176             "\r\n"
177             "*/\r\n",
178             format("/*\r\n"
179                    "\r\n"
180                    "*/\r\n"));
181   EXPECT_EQ(0, ReplacementCount);
182 }
183 
184 TEST_F(FormatTest, RemovesEmptyLines) {
185   EXPECT_EQ("class C {\n"
186             "  int i;\n"
187             "};",
188             format("class C {\n"
189                    " int i;\n"
190                    "\n"
191                    "};"));
192 
193   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
194   EXPECT_EQ("namespace N {\n"
195             "\n"
196             "int i;\n"
197             "}",
198             format("namespace N {\n"
199                    "\n"
200                    "int    i;\n"
201                    "}",
202                    getGoogleStyle()));
203   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
204             "\n"
205             "int i;\n"
206             "}",
207             format("extern /**/ \"C\" /**/ {\n"
208                    "\n"
209                    "int    i;\n"
210                    "}",
211                    getGoogleStyle()));
212 
213   // ...but do keep inlining and removing empty lines for non-block extern "C"
214   // functions.
215   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
216   EXPECT_EQ("extern \"C\" int f() {\n"
217             "  int i = 42;\n"
218             "  return i;\n"
219             "}",
220             format("extern \"C\" int f() {\n"
221                    "\n"
222                    "  int i = 42;\n"
223                    "  return i;\n"
224                    "}",
225                    getGoogleStyle()));
226 
227   // Remove empty lines at the beginning and end of blocks.
228   EXPECT_EQ("void f() {\n"
229             "\n"
230             "  if (a) {\n"
231             "\n"
232             "    f();\n"
233             "  }\n"
234             "}",
235             format("void f() {\n"
236                    "\n"
237                    "  if (a) {\n"
238                    "\n"
239                    "    f();\n"
240                    "\n"
241                    "  }\n"
242                    "\n"
243                    "}",
244                    getLLVMStyle()));
245   EXPECT_EQ("void f() {\n"
246             "  if (a) {\n"
247             "    f();\n"
248             "  }\n"
249             "}",
250             format("void f() {\n"
251                    "\n"
252                    "  if (a) {\n"
253                    "\n"
254                    "    f();\n"
255                    "\n"
256                    "  }\n"
257                    "\n"
258                    "}",
259                    getGoogleStyle()));
260 
261   // Don't remove empty lines in more complex control statements.
262   EXPECT_EQ("void f() {\n"
263             "  if (a) {\n"
264             "    f();\n"
265             "\n"
266             "  } else if (b) {\n"
267             "    f();\n"
268             "  }\n"
269             "}",
270             format("void f() {\n"
271                    "  if (a) {\n"
272                    "    f();\n"
273                    "\n"
274                    "  } else if (b) {\n"
275                    "    f();\n"
276                    "\n"
277                    "  }\n"
278                    "\n"
279                    "}"));
280 
281   // 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, BreakBeforeInheritanceComma) {
1320   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1321   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1322 
1323   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1324   verifyFormat("class MyClass\n"
1325                "    : public X\n"
1326                "    , public Y {};",
1327                StyleWithInheritanceBreak);
1328 }
1329 
1330 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1331   verifyFormat("class A {\n} a, b;");
1332   verifyFormat("struct A {\n} a, b;");
1333   verifyFormat("union A {\n} a;");
1334 }
1335 
1336 TEST_F(FormatTest, FormatsEnum) {
1337   verifyFormat("enum {\n"
1338                "  Zero,\n"
1339                "  One = 1,\n"
1340                "  Two = One + 1,\n"
1341                "  Three = (One + Two),\n"
1342                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1343                "  Five = (One, Two, Three, Four, 5)\n"
1344                "};");
1345   verifyGoogleFormat("enum {\n"
1346                      "  Zero,\n"
1347                      "  One = 1,\n"
1348                      "  Two = One + 1,\n"
1349                      "  Three = (One + Two),\n"
1350                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1351                      "  Five = (One, Two, Three, Four, 5)\n"
1352                      "};");
1353   verifyFormat("enum Enum {};");
1354   verifyFormat("enum {};");
1355   verifyFormat("enum X E {} d;");
1356   verifyFormat("enum __attribute__((...)) E {} d;");
1357   verifyFormat("enum __declspec__((...)) E {} d;");
1358   verifyFormat("enum {\n"
1359                "  Bar = Foo<int, int>::value\n"
1360                "};",
1361                getLLVMStyleWithColumns(30));
1362 
1363   verifyFormat("enum ShortEnum { A, B, C };");
1364   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1365 
1366   EXPECT_EQ("enum KeepEmptyLines {\n"
1367             "  ONE,\n"
1368             "\n"
1369             "  TWO,\n"
1370             "\n"
1371             "  THREE\n"
1372             "}",
1373             format("enum KeepEmptyLines {\n"
1374                    "  ONE,\n"
1375                    "\n"
1376                    "  TWO,\n"
1377                    "\n"
1378                    "\n"
1379                    "  THREE\n"
1380                    "}"));
1381   verifyFormat("enum E { // comment\n"
1382                "  ONE,\n"
1383                "  TWO\n"
1384                "};\n"
1385                "int i;");
1386   // Not enums.
1387   verifyFormat("enum X f() {\n"
1388                "  a();\n"
1389                "  return 42;\n"
1390                "}");
1391   verifyFormat("enum X Type::f() {\n"
1392                "  a();\n"
1393                "  return 42;\n"
1394                "}");
1395   verifyFormat("enum ::X f() {\n"
1396                "  a();\n"
1397                "  return 42;\n"
1398                "}");
1399   verifyFormat("enum ns::X f() {\n"
1400                "  a();\n"
1401                "  return 42;\n"
1402                "}");
1403 }
1404 
1405 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1406   verifyFormat("enum Type {\n"
1407                "  One = 0; // These semicolons should be commas.\n"
1408                "  Two = 1;\n"
1409                "};");
1410   verifyFormat("namespace n {\n"
1411                "enum Type {\n"
1412                "  One,\n"
1413                "  Two, // missing };\n"
1414                "  int i;\n"
1415                "}\n"
1416                "void g() {}");
1417 }
1418 
1419 TEST_F(FormatTest, FormatsEnumStruct) {
1420   verifyFormat("enum struct {\n"
1421                "  Zero,\n"
1422                "  One = 1,\n"
1423                "  Two = One + 1,\n"
1424                "  Three = (One + Two),\n"
1425                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1426                "  Five = (One, Two, Three, Four, 5)\n"
1427                "};");
1428   verifyFormat("enum struct Enum {};");
1429   verifyFormat("enum struct {};");
1430   verifyFormat("enum struct X E {} d;");
1431   verifyFormat("enum struct __attribute__((...)) E {} d;");
1432   verifyFormat("enum struct __declspec__((...)) E {} d;");
1433   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1434 }
1435 
1436 TEST_F(FormatTest, FormatsEnumClass) {
1437   verifyFormat("enum class {\n"
1438                "  Zero,\n"
1439                "  One = 1,\n"
1440                "  Two = One + 1,\n"
1441                "  Three = (One + Two),\n"
1442                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1443                "  Five = (One, Two, Three, Four, 5)\n"
1444                "};");
1445   verifyFormat("enum class Enum {};");
1446   verifyFormat("enum class {};");
1447   verifyFormat("enum class X E {} d;");
1448   verifyFormat("enum class __attribute__((...)) E {} d;");
1449   verifyFormat("enum class __declspec__((...)) E {} d;");
1450   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1451 }
1452 
1453 TEST_F(FormatTest, FormatsEnumTypes) {
1454   verifyFormat("enum X : int {\n"
1455                "  A, // Force multiple lines.\n"
1456                "  B\n"
1457                "};");
1458   verifyFormat("enum X : int { A, B };");
1459   verifyFormat("enum X : std::uint32_t { A, B };");
1460 }
1461 
1462 TEST_F(FormatTest, FormatsTypedefEnum) {
1463   FormatStyle Style = getLLVMStyle();
1464   Style.ColumnLimit = 40;
1465   verifyFormat("typedef enum {} EmptyEnum;");
1466   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1467   verifyFormat("typedef enum {\n"
1468                "  ZERO = 0,\n"
1469                "  ONE = 1,\n"
1470                "  TWO = 2,\n"
1471                "  THREE = 3\n"
1472                "} LongEnum;",
1473                Style);
1474   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1475   Style.BraceWrapping.AfterEnum = true;
1476   verifyFormat("typedef enum {} EmptyEnum;");
1477   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1478   verifyFormat("typedef enum\n"
1479                "{\n"
1480                "  ZERO = 0,\n"
1481                "  ONE = 1,\n"
1482                "  TWO = 2,\n"
1483                "  THREE = 3\n"
1484                "} LongEnum;",
1485                Style);
1486 }
1487 
1488 TEST_F(FormatTest, FormatsNSEnums) {
1489   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1490   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1491                      "  // Information about someDecentlyLongValue.\n"
1492                      "  someDecentlyLongValue,\n"
1493                      "  // Information about anotherDecentlyLongValue.\n"
1494                      "  anotherDecentlyLongValue,\n"
1495                      "  // Information about aThirdDecentlyLongValue.\n"
1496                      "  aThirdDecentlyLongValue\n"
1497                      "};");
1498   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1499                      "  a = 1,\n"
1500                      "  b = 2,\n"
1501                      "  c = 3,\n"
1502                      "};");
1503   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1504                      "  a = 1,\n"
1505                      "  b = 2,\n"
1506                      "  c = 3,\n"
1507                      "};");
1508   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1509                      "  a = 1,\n"
1510                      "  b = 2,\n"
1511                      "  c = 3,\n"
1512                      "};");
1513 }
1514 
1515 TEST_F(FormatTest, FormatsBitfields) {
1516   verifyFormat("struct Bitfields {\n"
1517                "  unsigned sClass : 8;\n"
1518                "  unsigned ValueKind : 2;\n"
1519                "};");
1520   verifyFormat("struct A {\n"
1521                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1522                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1523                "};");
1524   verifyFormat("struct MyStruct {\n"
1525                "  uchar data;\n"
1526                "  uchar : 8;\n"
1527                "  uchar : 8;\n"
1528                "  uchar other;\n"
1529                "};");
1530 }
1531 
1532 TEST_F(FormatTest, FormatsNamespaces) {
1533   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1534   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1535 
1536   verifyFormat("namespace some_namespace {\n"
1537                "class A {};\n"
1538                "void f() { f(); }\n"
1539                "}",
1540                LLVMWithNoNamespaceFix);
1541   verifyFormat("namespace {\n"
1542                "class A {};\n"
1543                "void f() { f(); }\n"
1544                "}",
1545                LLVMWithNoNamespaceFix);
1546   verifyFormat("inline namespace X {\n"
1547                "class A {};\n"
1548                "void f() { f(); }\n"
1549                "}",
1550                LLVMWithNoNamespaceFix);
1551   verifyFormat("using namespace some_namespace;\n"
1552                "class A {};\n"
1553                "void f() { f(); }",
1554                LLVMWithNoNamespaceFix);
1555 
1556   // This code is more common than we thought; if we
1557   // layout this correctly the semicolon will go into
1558   // its own line, which is undesirable.
1559   verifyFormat("namespace {};",
1560                LLVMWithNoNamespaceFix);
1561   verifyFormat("namespace {\n"
1562                "class A {};\n"
1563                "};",
1564                LLVMWithNoNamespaceFix);
1565 
1566   verifyFormat("namespace {\n"
1567                "int SomeVariable = 0; // comment\n"
1568                "} // namespace",
1569                LLVMWithNoNamespaceFix);
1570   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1571             "#define HEADER_GUARD\n"
1572             "namespace my_namespace {\n"
1573             "int i;\n"
1574             "} // my_namespace\n"
1575             "#endif // HEADER_GUARD",
1576             format("#ifndef HEADER_GUARD\n"
1577                    " #define HEADER_GUARD\n"
1578                    "   namespace my_namespace {\n"
1579                    "int i;\n"
1580                    "}    // my_namespace\n"
1581                    "#endif    // HEADER_GUARD",
1582                    LLVMWithNoNamespaceFix));
1583 
1584   EXPECT_EQ("namespace A::B {\n"
1585             "class C {};\n"
1586             "}",
1587             format("namespace A::B {\n"
1588                    "class C {};\n"
1589                    "}",
1590                    LLVMWithNoNamespaceFix));
1591 
1592   FormatStyle Style = getLLVMStyle();
1593   Style.NamespaceIndentation = FormatStyle::NI_All;
1594   EXPECT_EQ("namespace out {\n"
1595             "  int i;\n"
1596             "  namespace in {\n"
1597             "    int i;\n"
1598             "  } // namespace in\n"
1599             "} // namespace out",
1600             format("namespace out {\n"
1601                    "int i;\n"
1602                    "namespace in {\n"
1603                    "int i;\n"
1604                    "} // namespace in\n"
1605                    "} // namespace out",
1606                    Style));
1607 
1608   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1609   EXPECT_EQ("namespace out {\n"
1610             "int i;\n"
1611             "namespace in {\n"
1612             "  int i;\n"
1613             "} // namespace in\n"
1614             "} // namespace out",
1615             format("namespace out {\n"
1616                    "int i;\n"
1617                    "namespace in {\n"
1618                    "int i;\n"
1619                    "} // namespace in\n"
1620                    "} // namespace out",
1621                    Style));
1622 }
1623 
1624 TEST_F(FormatTest, FormatsCompactNamespaces) {
1625   FormatStyle Style = getLLVMStyle();
1626   Style.CompactNamespaces = true;
1627 
1628   verifyFormat("namespace A { namespace B {\n"
1629 			   "}} // namespace A::B",
1630 			   Style);
1631 
1632   EXPECT_EQ("namespace out { namespace in {\n"
1633             "}} // namespace out::in",
1634             format("namespace out {\n"
1635                    "namespace in {\n"
1636                    "} // namespace in\n"
1637                    "} // namespace out",
1638                    Style));
1639 
1640   // Only namespaces which have both consecutive opening and end get compacted
1641   EXPECT_EQ("namespace out {\n"
1642             "namespace in1 {\n"
1643             "} // namespace in1\n"
1644             "namespace in2 {\n"
1645             "} // namespace in2\n"
1646             "} // namespace out",
1647             format("namespace out {\n"
1648                    "namespace in1 {\n"
1649                    "} // namespace in1\n"
1650                    "namespace in2 {\n"
1651                    "} // namespace in2\n"
1652                    "} // namespace out",
1653                    Style));
1654 
1655   EXPECT_EQ("namespace out {\n"
1656             "int i;\n"
1657             "namespace in {\n"
1658             "int j;\n"
1659             "} // namespace in\n"
1660             "int k;\n"
1661             "} // namespace out",
1662             format("namespace out { int i;\n"
1663                    "namespace in { int j; } // namespace in\n"
1664                    "int k; } // namespace out",
1665                    Style));
1666 
1667   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1668             "}}} // namespace A::B::C\n",
1669             format("namespace A { namespace B {\n"
1670                    "namespace C {\n"
1671                    "}} // namespace B::C\n"
1672                    "} // namespace A\n",
1673                    Style));
1674 
1675   Style.ColumnLimit = 40;
1676   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1677             "namespace bbbbbbbbbb {\n"
1678             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1679             format("namespace aaaaaaaaaa {\n"
1680                    "namespace bbbbbbbbbb {\n"
1681                    "} // namespace bbbbbbbbbb\n"
1682                    "} // namespace aaaaaaaaaa",
1683                    Style));
1684 
1685   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1686             "namespace cccccc {\n"
1687             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1688             format("namespace aaaaaa {\n"
1689                    "namespace bbbbbb {\n"
1690                    "namespace cccccc {\n"
1691                    "} // namespace cccccc\n"
1692                    "} // namespace bbbbbb\n"
1693                    "} // namespace aaaaaa",
1694                    Style));
1695   Style.ColumnLimit = 80;
1696 
1697   // Extra semicolon after 'inner' closing brace prevents merging
1698   EXPECT_EQ("namespace out { namespace in {\n"
1699             "}; } // namespace out::in",
1700             format("namespace out {\n"
1701                    "namespace in {\n"
1702                    "}; // namespace in\n"
1703                    "} // namespace out",
1704                    Style));
1705 
1706   // Extra semicolon after 'outer' closing brace is conserved
1707   EXPECT_EQ("namespace out { namespace in {\n"
1708             "}}; // namespace out::in",
1709             format("namespace out {\n"
1710                    "namespace in {\n"
1711                    "} // namespace in\n"
1712                    "}; // namespace out",
1713                    Style));
1714 
1715   Style.NamespaceIndentation = FormatStyle::NI_All;
1716   EXPECT_EQ("namespace out { namespace in {\n"
1717             "  int i;\n"
1718             "}} // namespace out::in",
1719             format("namespace out {\n"
1720                    "namespace in {\n"
1721                    "int i;\n"
1722                    "} // namespace in\n"
1723                    "} // namespace out",
1724                    Style));
1725   EXPECT_EQ("namespace out { namespace mid {\n"
1726             "  namespace in {\n"
1727             "    int j;\n"
1728             "  } // namespace in\n"
1729             "  int k;\n"
1730             "}} // namespace out::mid",
1731             format("namespace out { namespace mid {\n"
1732                    "namespace in { int j; } // namespace in\n"
1733                    "int k; }} // namespace out::mid",
1734                    Style));
1735 
1736   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1737   EXPECT_EQ("namespace out { namespace in {\n"
1738             "  int i;\n"
1739             "}} // namespace out::in",
1740             format("namespace out {\n"
1741                    "namespace in {\n"
1742                    "int i;\n"
1743                    "} // namespace in\n"
1744                    "} // namespace out",
1745                    Style));
1746   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1747             "  int i;\n"
1748             "}}} // namespace out::mid::in",
1749             format("namespace out {\n"
1750                    "namespace mid {\n"
1751                    "namespace in {\n"
1752                    "int i;\n"
1753                    "} // namespace in\n"
1754                    "} // namespace mid\n"
1755                    "} // namespace out",
1756                    Style));
1757 }
1758 
1759 TEST_F(FormatTest, FormatsExternC) {
1760   verifyFormat("extern \"C\" {\nint a;");
1761   verifyFormat("extern \"C\" {}");
1762   verifyFormat("extern \"C\" {\n"
1763                "int foo();\n"
1764                "}");
1765   verifyFormat("extern \"C\" int foo() {}");
1766   verifyFormat("extern \"C\" int foo();");
1767   verifyFormat("extern \"C\" int foo() {\n"
1768                "  int i = 42;\n"
1769                "  return i;\n"
1770                "}");
1771 
1772   FormatStyle Style = getLLVMStyle();
1773   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1774   Style.BraceWrapping.AfterFunction = true;
1775   verifyFormat("extern \"C\" int foo() {}", Style);
1776   verifyFormat("extern \"C\" int foo();", Style);
1777   verifyFormat("extern \"C\" int foo()\n"
1778                "{\n"
1779                "  int i = 42;\n"
1780                "  return i;\n"
1781                "}",
1782                Style);
1783 
1784   Style.BraceWrapping.AfterExternBlock = true;
1785   Style.BraceWrapping.SplitEmptyRecord = false;
1786   verifyFormat("extern \"C\"\n"
1787                "{}",
1788                Style);
1789   verifyFormat("extern \"C\"\n"
1790                "{\n"
1791                "  int foo();\n"
1792                "}",
1793                Style);
1794 }
1795 
1796 TEST_F(FormatTest, FormatsInlineASM) {
1797   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1798   verifyFormat("asm(\"nop\" ::: \"memory\");");
1799   verifyFormat(
1800       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1801       "    \"cpuid\\n\\t\"\n"
1802       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1803       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1804       "    : \"a\"(value));");
1805   EXPECT_EQ(
1806       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1807       "  __asm {\n"
1808       "        mov     edx,[that] // vtable in edx\n"
1809       "        mov     eax,methodIndex\n"
1810       "        call    [edx][eax*4] // stdcall\n"
1811       "  }\n"
1812       "}",
1813       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1814              "    __asm {\n"
1815              "        mov     edx,[that] // vtable in edx\n"
1816              "        mov     eax,methodIndex\n"
1817              "        call    [edx][eax*4] // stdcall\n"
1818              "    }\n"
1819              "}"));
1820   EXPECT_EQ("_asm {\n"
1821             "  xor eax, eax;\n"
1822             "  cpuid;\n"
1823             "}",
1824             format("_asm {\n"
1825                    "  xor eax, eax;\n"
1826                    "  cpuid;\n"
1827                    "}"));
1828   verifyFormat("void function() {\n"
1829                "  // comment\n"
1830                "  asm(\"\");\n"
1831                "}");
1832   EXPECT_EQ("__asm {\n"
1833             "}\n"
1834             "int i;",
1835             format("__asm   {\n"
1836                    "}\n"
1837                    "int   i;"));
1838 }
1839 
1840 TEST_F(FormatTest, FormatTryCatch) {
1841   verifyFormat("try {\n"
1842                "  throw a * b;\n"
1843                "} catch (int a) {\n"
1844                "  // Do nothing.\n"
1845                "} catch (...) {\n"
1846                "  exit(42);\n"
1847                "}");
1848 
1849   // Function-level try statements.
1850   verifyFormat("int f() try { return 4; } catch (...) {\n"
1851                "  return 5;\n"
1852                "}");
1853   verifyFormat("class A {\n"
1854                "  int a;\n"
1855                "  A() try : a(0) {\n"
1856                "  } catch (...) {\n"
1857                "    throw;\n"
1858                "  }\n"
1859                "};\n");
1860 
1861   // Incomplete try-catch blocks.
1862   verifyIncompleteFormat("try {} catch (");
1863 }
1864 
1865 TEST_F(FormatTest, FormatSEHTryCatch) {
1866   verifyFormat("__try {\n"
1867                "  int a = b * c;\n"
1868                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1869                "  // Do nothing.\n"
1870                "}");
1871 
1872   verifyFormat("__try {\n"
1873                "  int a = b * c;\n"
1874                "} __finally {\n"
1875                "  // Do nothing.\n"
1876                "}");
1877 
1878   verifyFormat("DEBUG({\n"
1879                "  __try {\n"
1880                "  } __finally {\n"
1881                "  }\n"
1882                "});\n");
1883 }
1884 
1885 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1886   verifyFormat("try {\n"
1887                "  f();\n"
1888                "} catch {\n"
1889                "  g();\n"
1890                "}");
1891   verifyFormat("try {\n"
1892                "  f();\n"
1893                "} catch (A a) MACRO(x) {\n"
1894                "  g();\n"
1895                "} catch (B b) MACRO(x) {\n"
1896                "  g();\n"
1897                "}");
1898 }
1899 
1900 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1901   FormatStyle Style = getLLVMStyle();
1902   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1903                           FormatStyle::BS_WebKit}) {
1904     Style.BreakBeforeBraces = BraceStyle;
1905     verifyFormat("try {\n"
1906                  "  // something\n"
1907                  "} catch (...) {\n"
1908                  "  // something\n"
1909                  "}",
1910                  Style);
1911   }
1912   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1913   verifyFormat("try {\n"
1914                "  // something\n"
1915                "}\n"
1916                "catch (...) {\n"
1917                "  // something\n"
1918                "}",
1919                Style);
1920   verifyFormat("__try {\n"
1921                "  // something\n"
1922                "}\n"
1923                "__finally {\n"
1924                "  // something\n"
1925                "}",
1926                Style);
1927   verifyFormat("@try {\n"
1928                "  // something\n"
1929                "}\n"
1930                "@finally {\n"
1931                "  // something\n"
1932                "}",
1933                Style);
1934   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1935   verifyFormat("try\n"
1936                "{\n"
1937                "  // something\n"
1938                "}\n"
1939                "catch (...)\n"
1940                "{\n"
1941                "  // something\n"
1942                "}",
1943                Style);
1944   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1945   verifyFormat("try\n"
1946                "  {\n"
1947                "    // something\n"
1948                "  }\n"
1949                "catch (...)\n"
1950                "  {\n"
1951                "    // something\n"
1952                "  }",
1953                Style);
1954   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1955   Style.BraceWrapping.BeforeCatch = true;
1956   verifyFormat("try {\n"
1957                "  // something\n"
1958                "}\n"
1959                "catch (...) {\n"
1960                "  // something\n"
1961                "}",
1962                Style);
1963 }
1964 
1965 TEST_F(FormatTest, StaticInitializers) {
1966   verifyFormat("static SomeClass SC = {1, 'a'};");
1967 
1968   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1969                "    100000000, "
1970                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1971 
1972   // Here, everything other than the "}" would fit on a line.
1973   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1974                "    10000000000000000000000000};");
1975   EXPECT_EQ("S s = {a,\n"
1976             "\n"
1977             "       b};",
1978             format("S s = {\n"
1979                    "  a,\n"
1980                    "\n"
1981                    "  b\n"
1982                    "};"));
1983 
1984   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1985   // line. However, the formatting looks a bit off and this probably doesn't
1986   // happen often in practice.
1987   verifyFormat("static int Variable[1] = {\n"
1988                "    {1000000000000000000000000000000000000}};",
1989                getLLVMStyleWithColumns(40));
1990 }
1991 
1992 TEST_F(FormatTest, DesignatedInitializers) {
1993   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1994   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1995                "                    .bbbbbbbbbb = 2,\n"
1996                "                    .cccccccccc = 3,\n"
1997                "                    .dddddddddd = 4,\n"
1998                "                    .eeeeeeeeee = 5};");
1999   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2000                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2001                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2002                "    .ccccccccccccccccccccccccccc = 3,\n"
2003                "    .ddddddddddddddddddddddddddd = 4,\n"
2004                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2005 
2006   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2007 
2008   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2009   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2010                "                    [2] = bbbbbbbbbb,\n"
2011                "                    [3] = cccccccccc,\n"
2012                "                    [4] = dddddddddd,\n"
2013                "                    [5] = eeeeeeeeee};");
2014   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2015                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2016                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2017                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2018                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2019                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2020 }
2021 
2022 TEST_F(FormatTest, NestedStaticInitializers) {
2023   verifyFormat("static A x = {{{}}};\n");
2024   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2025                "               {init1, init2, init3, init4}}};",
2026                getLLVMStyleWithColumns(50));
2027 
2028   verifyFormat("somes Status::global_reps[3] = {\n"
2029                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2030                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2031                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2032                getLLVMStyleWithColumns(60));
2033   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2034                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2035                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2036                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2037   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2038                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2039                "rect.fTop}};");
2040 
2041   verifyFormat(
2042       "SomeArrayOfSomeType a = {\n"
2043       "    {{1, 2, 3},\n"
2044       "     {1, 2, 3},\n"
2045       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2046       "      333333333333333333333333333333},\n"
2047       "     {1, 2, 3},\n"
2048       "     {1, 2, 3}}};");
2049   verifyFormat(
2050       "SomeArrayOfSomeType a = {\n"
2051       "    {{1, 2, 3}},\n"
2052       "    {{1, 2, 3}},\n"
2053       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2054       "      333333333333333333333333333333}},\n"
2055       "    {{1, 2, 3}},\n"
2056       "    {{1, 2, 3}}};");
2057 
2058   verifyFormat("struct {\n"
2059                "  unsigned bit;\n"
2060                "  const char *const name;\n"
2061                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2062                "                 {kOsWin, \"Windows\"},\n"
2063                "                 {kOsLinux, \"Linux\"},\n"
2064                "                 {kOsCrOS, \"Chrome OS\"}};");
2065   verifyFormat("struct {\n"
2066                "  unsigned bit;\n"
2067                "  const char *const name;\n"
2068                "} kBitsToOs[] = {\n"
2069                "    {kOsMac, \"Mac\"},\n"
2070                "    {kOsWin, \"Windows\"},\n"
2071                "    {kOsLinux, \"Linux\"},\n"
2072                "    {kOsCrOS, \"Chrome OS\"},\n"
2073                "};");
2074 }
2075 
2076 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2077   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2078                "                      \\\n"
2079                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2080 }
2081 
2082 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2083   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2084                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2085 
2086   // Do break defaulted and deleted functions.
2087   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2088                "    default;",
2089                getLLVMStyleWithColumns(40));
2090   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2091                "    delete;",
2092                getLLVMStyleWithColumns(40));
2093 }
2094 
2095 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2096   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2097                getLLVMStyleWithColumns(40));
2098   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2099                getLLVMStyleWithColumns(40));
2100   EXPECT_EQ("#define Q                              \\\n"
2101             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2102             "  \"aaaaaaaa.cpp\"",
2103             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2104                    getLLVMStyleWithColumns(40)));
2105 }
2106 
2107 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2108   EXPECT_EQ("# 123 \"A string literal\"",
2109             format("   #     123    \"A string literal\""));
2110 }
2111 
2112 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2113   EXPECT_EQ("#;", format("#;"));
2114   verifyFormat("#\n;\n;\n;");
2115 }
2116 
2117 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2118   EXPECT_EQ("#line 42 \"test\"\n",
2119             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2120   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2121                                     getLLVMStyleWithColumns(12)));
2122 }
2123 
2124 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2125   EXPECT_EQ("#line 42 \"test\"",
2126             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2127   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2128 }
2129 
2130 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2131   verifyFormat("#define A \\x20");
2132   verifyFormat("#define A \\ x20");
2133   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2134   verifyFormat("#define A ''");
2135   verifyFormat("#define A ''qqq");
2136   verifyFormat("#define A `qqq");
2137   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2138   EXPECT_EQ("const char *c = STRINGIFY(\n"
2139             "\\na : b);",
2140             format("const char * c = STRINGIFY(\n"
2141                    "\\na : b);"));
2142 
2143   verifyFormat("a\r\\");
2144   verifyFormat("a\v\\");
2145   verifyFormat("a\f\\");
2146 }
2147 
2148 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2149   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2150   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2151   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2152   // FIXME: We never break before the macro name.
2153   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2154 
2155   verifyFormat("#define A A\n#define A A");
2156   verifyFormat("#define A(X) A\n#define A A");
2157 
2158   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2159   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2160 }
2161 
2162 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2163   EXPECT_EQ("// somecomment\n"
2164             "#include \"a.h\"\n"
2165             "#define A(  \\\n"
2166             "    A, B)\n"
2167             "#include \"b.h\"\n"
2168             "// somecomment\n",
2169             format("  // somecomment\n"
2170                    "  #include \"a.h\"\n"
2171                    "#define A(A,\\\n"
2172                    "    B)\n"
2173                    "    #include \"b.h\"\n"
2174                    " // somecomment\n",
2175                    getLLVMStyleWithColumns(13)));
2176 }
2177 
2178 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2179 
2180 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2181   EXPECT_EQ("#define A    \\\n"
2182             "  c;         \\\n"
2183             "  e;\n"
2184             "f;",
2185             format("#define A c; e;\n"
2186                    "f;",
2187                    getLLVMStyleWithColumns(14)));
2188 }
2189 
2190 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2191 
2192 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2193   EXPECT_EQ("int x,\n"
2194             "#define A\n"
2195             "    y;",
2196             format("int x,\n#define A\ny;"));
2197 }
2198 
2199 TEST_F(FormatTest, HashInMacroDefinition) {
2200   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2201   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2202   verifyFormat("#define A  \\\n"
2203                "  {        \\\n"
2204                "    f(#c); \\\n"
2205                "  }",
2206                getLLVMStyleWithColumns(11));
2207 
2208   verifyFormat("#define A(X)         \\\n"
2209                "  void function##X()",
2210                getLLVMStyleWithColumns(22));
2211 
2212   verifyFormat("#define A(a, b, c)   \\\n"
2213                "  void a##b##c()",
2214                getLLVMStyleWithColumns(22));
2215 
2216   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2217 }
2218 
2219 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2220   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2221   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2222 }
2223 
2224 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2225   EXPECT_EQ("#define A b;", format("#define A \\\n"
2226                                    "          \\\n"
2227                                    "  b;",
2228                                    getLLVMStyleWithColumns(25)));
2229   EXPECT_EQ("#define A \\\n"
2230             "          \\\n"
2231             "  a;      \\\n"
2232             "  b;",
2233             format("#define A \\\n"
2234                    "          \\\n"
2235                    "  a;      \\\n"
2236                    "  b;",
2237                    getLLVMStyleWithColumns(11)));
2238   EXPECT_EQ("#define A \\\n"
2239             "  a;      \\\n"
2240             "          \\\n"
2241             "  b;",
2242             format("#define A \\\n"
2243                    "  a;      \\\n"
2244                    "          \\\n"
2245                    "  b;",
2246                    getLLVMStyleWithColumns(11)));
2247 }
2248 
2249 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2250   verifyIncompleteFormat("#define A :");
2251   verifyFormat("#define SOMECASES  \\\n"
2252                "  case 1:          \\\n"
2253                "  case 2\n",
2254                getLLVMStyleWithColumns(20));
2255   verifyFormat("#define MACRO(a) \\\n"
2256                "  if (a)         \\\n"
2257                "    f();         \\\n"
2258                "  else           \\\n"
2259                "    g()",
2260                getLLVMStyleWithColumns(18));
2261   verifyFormat("#define A template <typename T>");
2262   verifyIncompleteFormat("#define STR(x) #x\n"
2263                          "f(STR(this_is_a_string_literal{));");
2264   verifyFormat("#pragma omp threadprivate( \\\n"
2265                "    y)), // expected-warning",
2266                getLLVMStyleWithColumns(28));
2267   verifyFormat("#d, = };");
2268   verifyFormat("#if \"a");
2269   verifyIncompleteFormat("({\n"
2270                          "#define b     \\\n"
2271                          "  }           \\\n"
2272                          "  a\n"
2273                          "a",
2274                          getLLVMStyleWithColumns(15));
2275   verifyFormat("#define A     \\\n"
2276                "  {           \\\n"
2277                "    {\n"
2278                "#define B     \\\n"
2279                "  }           \\\n"
2280                "  }",
2281                getLLVMStyleWithColumns(15));
2282   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2283   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2284   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2285   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2286 }
2287 
2288 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2289   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2290   EXPECT_EQ("class A : public QObject {\n"
2291             "  Q_OBJECT\n"
2292             "\n"
2293             "  A() {}\n"
2294             "};",
2295             format("class A  :  public QObject {\n"
2296                    "     Q_OBJECT\n"
2297                    "\n"
2298                    "  A() {\n}\n"
2299                    "}  ;"));
2300   EXPECT_EQ("MACRO\n"
2301             "/*static*/ int i;",
2302             format("MACRO\n"
2303                    " /*static*/ int   i;"));
2304   EXPECT_EQ("SOME_MACRO\n"
2305             "namespace {\n"
2306             "void f();\n"
2307             "} // namespace",
2308             format("SOME_MACRO\n"
2309                    "  namespace    {\n"
2310                    "void   f(  );\n"
2311                    "} // namespace"));
2312   // Only if the identifier contains at least 5 characters.
2313   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2314   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2315   // Only if everything is upper case.
2316   EXPECT_EQ("class A : public QObject {\n"
2317             "  Q_Object A() {}\n"
2318             "};",
2319             format("class A  :  public QObject {\n"
2320                    "     Q_Object\n"
2321                    "  A() {\n}\n"
2322                    "}  ;"));
2323 
2324   // Only if the next line can actually start an unwrapped line.
2325   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2326             format("SOME_WEIRD_LOG_MACRO\n"
2327                    "<< SomeThing;"));
2328 
2329   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2330                "(n, buffers))\n",
2331                getChromiumStyle(FormatStyle::LK_Cpp));
2332 }
2333 
2334 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2335   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2336             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2337             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2338             "class X {};\n"
2339             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2340             "int *createScopDetectionPass() { return 0; }",
2341             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2342                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2343                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2344                    "  class X {};\n"
2345                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2346                    "  int *createScopDetectionPass() { return 0; }"));
2347   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2348   // braces, so that inner block is indented one level more.
2349   EXPECT_EQ("int q() {\n"
2350             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2351             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2352             "  IPC_END_MESSAGE_MAP()\n"
2353             "}",
2354             format("int q() {\n"
2355                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2356                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2357                    "  IPC_END_MESSAGE_MAP()\n"
2358                    "}"));
2359 
2360   // Same inside macros.
2361   EXPECT_EQ("#define LIST(L) \\\n"
2362             "  L(A)          \\\n"
2363             "  L(B)          \\\n"
2364             "  L(C)",
2365             format("#define LIST(L) \\\n"
2366                    "  L(A) \\\n"
2367                    "  L(B) \\\n"
2368                    "  L(C)",
2369                    getGoogleStyle()));
2370 
2371   // These must not be recognized as macros.
2372   EXPECT_EQ("int q() {\n"
2373             "  f(x);\n"
2374             "  f(x) {}\n"
2375             "  f(x)->g();\n"
2376             "  f(x)->*g();\n"
2377             "  f(x).g();\n"
2378             "  f(x) = x;\n"
2379             "  f(x) += x;\n"
2380             "  f(x) -= x;\n"
2381             "  f(x) *= x;\n"
2382             "  f(x) /= x;\n"
2383             "  f(x) %= x;\n"
2384             "  f(x) &= x;\n"
2385             "  f(x) |= x;\n"
2386             "  f(x) ^= x;\n"
2387             "  f(x) >>= x;\n"
2388             "  f(x) <<= x;\n"
2389             "  f(x)[y].z();\n"
2390             "  LOG(INFO) << x;\n"
2391             "  ifstream(x) >> x;\n"
2392             "}\n",
2393             format("int q() {\n"
2394                    "  f(x)\n;\n"
2395                    "  f(x)\n {}\n"
2396                    "  f(x)\n->g();\n"
2397                    "  f(x)\n->*g();\n"
2398                    "  f(x)\n.g();\n"
2399                    "  f(x)\n = x;\n"
2400                    "  f(x)\n += x;\n"
2401                    "  f(x)\n -= x;\n"
2402                    "  f(x)\n *= x;\n"
2403                    "  f(x)\n /= x;\n"
2404                    "  f(x)\n %= x;\n"
2405                    "  f(x)\n &= x;\n"
2406                    "  f(x)\n |= x;\n"
2407                    "  f(x)\n ^= x;\n"
2408                    "  f(x)\n >>= x;\n"
2409                    "  f(x)\n <<= x;\n"
2410                    "  f(x)\n[y].z();\n"
2411                    "  LOG(INFO)\n << x;\n"
2412                    "  ifstream(x)\n >> x;\n"
2413                    "}\n"));
2414   EXPECT_EQ("int q() {\n"
2415             "  F(x)\n"
2416             "  if (1) {\n"
2417             "  }\n"
2418             "  F(x)\n"
2419             "  while (1) {\n"
2420             "  }\n"
2421             "  F(x)\n"
2422             "  G(x);\n"
2423             "  F(x)\n"
2424             "  try {\n"
2425             "    Q();\n"
2426             "  } catch (...) {\n"
2427             "  }\n"
2428             "}\n",
2429             format("int q() {\n"
2430                    "F(x)\n"
2431                    "if (1) {}\n"
2432                    "F(x)\n"
2433                    "while (1) {}\n"
2434                    "F(x)\n"
2435                    "G(x);\n"
2436                    "F(x)\n"
2437                    "try { Q(); } catch (...) {}\n"
2438                    "}\n"));
2439   EXPECT_EQ("class A {\n"
2440             "  A() : t(0) {}\n"
2441             "  A(int i) noexcept() : {}\n"
2442             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2443             "  try : t(0) {\n"
2444             "  } catch (...) {\n"
2445             "  }\n"
2446             "};",
2447             format("class A {\n"
2448                    "  A()\n : t(0) {}\n"
2449                    "  A(int i)\n noexcept() : {}\n"
2450                    "  A(X x)\n"
2451                    "  try : t(0) {} catch (...) {}\n"
2452                    "};"));
2453   EXPECT_EQ("class SomeClass {\n"
2454             "public:\n"
2455             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2456             "};",
2457             format("class SomeClass {\n"
2458                    "public:\n"
2459                    "  SomeClass()\n"
2460                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2461                    "};"));
2462   EXPECT_EQ("class SomeClass {\n"
2463             "public:\n"
2464             "  SomeClass()\n"
2465             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2466             "};",
2467             format("class SomeClass {\n"
2468                    "public:\n"
2469                    "  SomeClass()\n"
2470                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2471                    "};",
2472                    getLLVMStyleWithColumns(40)));
2473 
2474   verifyFormat("MACRO(>)");
2475 }
2476 
2477 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2478   verifyFormat("#define A \\\n"
2479                "  f({     \\\n"
2480                "    g();  \\\n"
2481                "  });",
2482                getLLVMStyleWithColumns(11));
2483 }
2484 
2485 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2486   FormatStyle Style = getLLVMStyle();
2487   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2488   Style.ColumnLimit = 40;
2489   verifyFormat("#ifdef _WIN32\n"
2490                "#define A 0\n"
2491                "#ifdef VAR2\n"
2492                "#define B 1\n"
2493                "#include <someheader.h>\n"
2494                "#define MACRO                          \\\n"
2495                "  some_very_long_func_aaaaaaaaaa();\n"
2496                "#endif\n"
2497                "#else\n"
2498                "#define A 1\n"
2499                "#endif",
2500                Style);
2501   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2502   verifyFormat("#ifdef _WIN32\n"
2503                "#  define A 0\n"
2504                "#  ifdef VAR2\n"
2505                "#    define B 1\n"
2506                "#    include <someheader.h>\n"
2507                "#    define MACRO                      \\\n"
2508                "      some_very_long_func_aaaaaaaaaa();\n"
2509                "#  endif\n"
2510                "#else\n"
2511                "#  define A 1\n"
2512                "#endif",
2513                Style);
2514   verifyFormat("#if A\n"
2515                "#  define MACRO                        \\\n"
2516                "    void a(int x) {                    \\\n"
2517                "      b();                             \\\n"
2518                "      c();                             \\\n"
2519                "      d();                             \\\n"
2520                "      e();                             \\\n"
2521                "      f();                             \\\n"
2522                "    }\n"
2523                "#endif",
2524                Style);
2525   // Comments before include guard.
2526   verifyFormat("// file comment\n"
2527                "// file comment\n"
2528                "#ifndef HEADER_H\n"
2529                "#define HEADER_H\n"
2530                "code();\n"
2531                "#endif",
2532                Style);
2533   // Test with include guards.
2534   verifyFormat("#ifndef HEADER_H\n"
2535                "#define HEADER_H\n"
2536                "code();\n"
2537                "#endif",
2538                Style);
2539   // Include guards must have a #define with the same variable immediately
2540   // after #ifndef.
2541   verifyFormat("#ifndef NOT_GUARD\n"
2542                "#  define FOO\n"
2543                "code();\n"
2544                "#endif",
2545                Style);
2546 
2547   // Include guards must cover the entire file.
2548   verifyFormat("code();\n"
2549                "code();\n"
2550                "#ifndef NOT_GUARD\n"
2551                "#  define NOT_GUARD\n"
2552                "code();\n"
2553                "#endif",
2554                Style);
2555   verifyFormat("#ifndef NOT_GUARD\n"
2556                "#  define NOT_GUARD\n"
2557                "code();\n"
2558                "#endif\n"
2559                "code();",
2560                Style);
2561   // Test with trailing blank lines.
2562   verifyFormat("#ifndef HEADER_H\n"
2563                "#define HEADER_H\n"
2564                "code();\n"
2565                "#endif\n",
2566                Style);
2567   // Include guards don't have #else.
2568   verifyFormat("#ifndef NOT_GUARD\n"
2569                "#  define NOT_GUARD\n"
2570                "code();\n"
2571                "#else\n"
2572                "#endif",
2573                Style);
2574   verifyFormat("#ifndef NOT_GUARD\n"
2575                "#  define NOT_GUARD\n"
2576                "code();\n"
2577                "#elif FOO\n"
2578                "#endif",
2579                Style);
2580   // Non-identifier #define after potential include guard.
2581   verifyFormat("#ifndef FOO\n"
2582                "#  define 1\n"
2583                "#endif\n",
2584                Style);
2585   // #if closes past last non-preprocessor line.
2586   verifyFormat("#ifndef FOO\n"
2587                "#define FOO\n"
2588                "#if 1\n"
2589                "int i;\n"
2590                "#  define A 0\n"
2591                "#endif\n"
2592                "#endif\n",
2593                Style);
2594   // FIXME: This doesn't handle the case where there's code between the
2595   // #ifndef and #define but all other conditions hold. This is because when
2596   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2597   // previous code line yet, so we can't detect it.
2598   EXPECT_EQ("#ifndef NOT_GUARD\n"
2599             "code();\n"
2600             "#define NOT_GUARD\n"
2601             "code();\n"
2602             "#endif",
2603             format("#ifndef NOT_GUARD\n"
2604                    "code();\n"
2605                    "#  define NOT_GUARD\n"
2606                    "code();\n"
2607                    "#endif",
2608                    Style));
2609   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2610   // be outside an include guard. Examples are #pragma once and
2611   // #pragma GCC diagnostic, or anything else that does not change the meaning
2612   // of the file if it's included multiple times.
2613   EXPECT_EQ("#ifdef WIN32\n"
2614             "#  pragma once\n"
2615             "#endif\n"
2616             "#ifndef HEADER_H\n"
2617             "#  define HEADER_H\n"
2618             "code();\n"
2619             "#endif",
2620             format("#ifdef WIN32\n"
2621                    "#  pragma once\n"
2622                    "#endif\n"
2623                    "#ifndef HEADER_H\n"
2624                    "#define HEADER_H\n"
2625                    "code();\n"
2626                    "#endif",
2627                    Style));
2628   // FIXME: This does not detect when there is a single non-preprocessor line
2629   // in front of an include-guard-like structure where other conditions hold
2630   // because ScopedLineState hides the line.
2631   EXPECT_EQ("code();\n"
2632             "#ifndef HEADER_H\n"
2633             "#define HEADER_H\n"
2634             "code();\n"
2635             "#endif",
2636             format("code();\n"
2637                    "#ifndef HEADER_H\n"
2638                    "#  define HEADER_H\n"
2639                    "code();\n"
2640                    "#endif",
2641                    Style));
2642   // Keep comments aligned with #, otherwise indent comments normally. These
2643   // tests cannot use verifyFormat because messUp manipulates leading
2644   // whitespace.
2645   {
2646     const char *Expected = ""
2647                            "void f() {\n"
2648                            "#if 1\n"
2649                            "// Preprocessor aligned.\n"
2650                            "#  define A 0\n"
2651                            "  // Code. Separated by blank line.\n"
2652                            "\n"
2653                            "#  define B 0\n"
2654                            "  // Code. Not aligned with #\n"
2655                            "#  define C 0\n"
2656                            "#endif";
2657     const char *ToFormat = ""
2658                            "void f() {\n"
2659                            "#if 1\n"
2660                            "// Preprocessor aligned.\n"
2661                            "#  define A 0\n"
2662                            "// Code. Separated by blank line.\n"
2663                            "\n"
2664                            "#  define B 0\n"
2665                            "   // Code. Not aligned with #\n"
2666                            "#  define C 0\n"
2667                            "#endif";
2668     EXPECT_EQ(Expected, format(ToFormat, Style));
2669     EXPECT_EQ(Expected, format(Expected, Style));
2670   }
2671   // Keep block quotes aligned.
2672   {
2673     const char *Expected = ""
2674                            "void f() {\n"
2675                            "#if 1\n"
2676                            "/* Preprocessor aligned. */\n"
2677                            "#  define A 0\n"
2678                            "  /* Code. Separated by blank line. */\n"
2679                            "\n"
2680                            "#  define B 0\n"
2681                            "  /* Code. Not aligned with # */\n"
2682                            "#  define C 0\n"
2683                            "#endif";
2684     const char *ToFormat = ""
2685                            "void f() {\n"
2686                            "#if 1\n"
2687                            "/* Preprocessor aligned. */\n"
2688                            "#  define A 0\n"
2689                            "/* Code. Separated by blank line. */\n"
2690                            "\n"
2691                            "#  define B 0\n"
2692                            "   /* Code. Not aligned with # */\n"
2693                            "#  define C 0\n"
2694                            "#endif";
2695     EXPECT_EQ(Expected, format(ToFormat, Style));
2696     EXPECT_EQ(Expected, format(Expected, Style));
2697   }
2698   // Keep comments aligned with un-indented directives.
2699   {
2700     const char *Expected = ""
2701                            "void f() {\n"
2702                            "// Preprocessor aligned.\n"
2703                            "#define A 0\n"
2704                            "  // Code. Separated by blank line.\n"
2705                            "\n"
2706                            "#define B 0\n"
2707                            "  // Code. Not aligned with #\n"
2708                            "#define C 0\n";
2709     const char *ToFormat = ""
2710                            "void f() {\n"
2711                            "// Preprocessor aligned.\n"
2712                            "#define A 0\n"
2713                            "// Code. Separated by blank line.\n"
2714                            "\n"
2715                            "#define B 0\n"
2716                            "   // Code. Not aligned with #\n"
2717                            "#define C 0\n";
2718     EXPECT_EQ(Expected, format(ToFormat, Style));
2719     EXPECT_EQ(Expected, format(Expected, Style));
2720   }
2721   // Test with tabs.
2722   Style.UseTab = FormatStyle::UT_Always;
2723   Style.IndentWidth = 8;
2724   Style.TabWidth = 8;
2725   verifyFormat("#ifdef _WIN32\n"
2726                "#\tdefine A 0\n"
2727                "#\tifdef VAR2\n"
2728                "#\t\tdefine B 1\n"
2729                "#\t\tinclude <someheader.h>\n"
2730                "#\t\tdefine MACRO          \\\n"
2731                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2732                "#\tendif\n"
2733                "#else\n"
2734                "#\tdefine A 1\n"
2735                "#endif",
2736                Style);
2737 
2738   // Regression test: Multiline-macro inside include guards.
2739   verifyFormat("#ifndef HEADER_H\n"
2740                "#define HEADER_H\n"
2741                "#define A()        \\\n"
2742                "  int i;           \\\n"
2743                "  int j;\n"
2744                "#endif // HEADER_H",
2745                getLLVMStyleWithColumns(20));
2746 }
2747 
2748 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2749   verifyFormat("{\n  { a #c; }\n}");
2750 }
2751 
2752 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2753   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2754             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2755   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2756             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2757 }
2758 
2759 TEST_F(FormatTest, EscapedNewlines) {
2760   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2761   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2762             format("#define A \\\nint i;\\\n  int j;", Narrow));
2763   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2764   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2765   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2766   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2767 
2768   FormatStyle AlignLeft = getLLVMStyle();
2769   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2770   EXPECT_EQ("#define MACRO(x) \\\n"
2771             "private:         \\\n"
2772             "  int x(int a);\n",
2773             format("#define MACRO(x) \\\n"
2774                    "private:         \\\n"
2775                    "  int x(int a);\n",
2776                    AlignLeft));
2777 
2778   // CRLF line endings
2779   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2780             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2781   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2782   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2783   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2784   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2785   EXPECT_EQ("#define MACRO(x) \\\r\n"
2786             "private:         \\\r\n"
2787             "  int x(int a);\r\n",
2788             format("#define MACRO(x) \\\r\n"
2789                    "private:         \\\r\n"
2790                    "  int x(int a);\r\n",
2791                    AlignLeft));
2792 
2793   FormatStyle DontAlign = getLLVMStyle();
2794   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2795   DontAlign.MaxEmptyLinesToKeep = 3;
2796   // FIXME: can't use verifyFormat here because the newline before
2797   // "public:" is not inserted the first time it's reformatted
2798   EXPECT_EQ("#define A \\\n"
2799             "  class Foo { \\\n"
2800             "    void bar(); \\\n"
2801             "\\\n"
2802             "\\\n"
2803             "\\\n"
2804             "  public: \\\n"
2805             "    void baz(); \\\n"
2806             "  };",
2807             format("#define A \\\n"
2808                    "  class Foo { \\\n"
2809                    "    void bar(); \\\n"
2810                    "\\\n"
2811                    "\\\n"
2812                    "\\\n"
2813                    "  public: \\\n"
2814                    "    void baz(); \\\n"
2815                    "  };",
2816                    DontAlign));
2817 }
2818 
2819 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2820   verifyFormat("#define A \\\n"
2821                "  int v(  \\\n"
2822                "      a); \\\n"
2823                "  int i;",
2824                getLLVMStyleWithColumns(11));
2825 }
2826 
2827 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2828   EXPECT_EQ(
2829       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2830       "                      \\\n"
2831       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2832       "\n"
2833       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2834       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2835       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2836              "\\\n"
2837              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2838              "  \n"
2839              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2840              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2841 }
2842 
2843 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2844   EXPECT_EQ("int\n"
2845             "#define A\n"
2846             "    a;",
2847             format("int\n#define A\na;"));
2848   verifyFormat("functionCallTo(\n"
2849                "    someOtherFunction(\n"
2850                "        withSomeParameters, whichInSequence,\n"
2851                "        areLongerThanALine(andAnotherCall,\n"
2852                "#define A B\n"
2853                "                           withMoreParamters,\n"
2854                "                           whichStronglyInfluenceTheLayout),\n"
2855                "        andMoreParameters),\n"
2856                "    trailing);",
2857                getLLVMStyleWithColumns(69));
2858   verifyFormat("Foo::Foo()\n"
2859                "#ifdef BAR\n"
2860                "    : baz(0)\n"
2861                "#endif\n"
2862                "{\n"
2863                "}");
2864   verifyFormat("void f() {\n"
2865                "  if (true)\n"
2866                "#ifdef A\n"
2867                "    f(42);\n"
2868                "  x();\n"
2869                "#else\n"
2870                "    g();\n"
2871                "  x();\n"
2872                "#endif\n"
2873                "}");
2874   verifyFormat("void f(param1, param2,\n"
2875                "       param3,\n"
2876                "#ifdef A\n"
2877                "       param4(param5,\n"
2878                "#ifdef A1\n"
2879                "              param6,\n"
2880                "#ifdef A2\n"
2881                "              param7),\n"
2882                "#else\n"
2883                "              param8),\n"
2884                "       param9,\n"
2885                "#endif\n"
2886                "       param10,\n"
2887                "#endif\n"
2888                "       param11)\n"
2889                "#else\n"
2890                "       param12)\n"
2891                "#endif\n"
2892                "{\n"
2893                "  x();\n"
2894                "}",
2895                getLLVMStyleWithColumns(28));
2896   verifyFormat("#if 1\n"
2897                "int i;");
2898   verifyFormat("#if 1\n"
2899                "#endif\n"
2900                "#if 1\n"
2901                "#else\n"
2902                "#endif\n");
2903   verifyFormat("DEBUG({\n"
2904                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2905                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2906                "});\n"
2907                "#if a\n"
2908                "#else\n"
2909                "#endif");
2910 
2911   verifyIncompleteFormat("void f(\n"
2912                          "#if A\n"
2913                          ");\n"
2914                          "#else\n"
2915                          "#endif");
2916 }
2917 
2918 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2919   verifyFormat("#endif\n"
2920                "#if B");
2921 }
2922 
2923 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2924   FormatStyle SingleLine = getLLVMStyle();
2925   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2926   verifyFormat("#if 0\n"
2927                "#elif 1\n"
2928                "#endif\n"
2929                "void foo() {\n"
2930                "  if (test) foo2();\n"
2931                "}",
2932                SingleLine);
2933 }
2934 
2935 TEST_F(FormatTest, LayoutBlockInsideParens) {
2936   verifyFormat("functionCall({ int i; });");
2937   verifyFormat("functionCall({\n"
2938                "  int i;\n"
2939                "  int j;\n"
2940                "});");
2941   verifyFormat("functionCall(\n"
2942                "    {\n"
2943                "      int i;\n"
2944                "      int j;\n"
2945                "    },\n"
2946                "    aaaa, bbbb, cccc);");
2947   verifyFormat("functionA(functionB({\n"
2948                "            int i;\n"
2949                "            int j;\n"
2950                "          }),\n"
2951                "          aaaa, bbbb, cccc);");
2952   verifyFormat("functionCall(\n"
2953                "    {\n"
2954                "      int i;\n"
2955                "      int j;\n"
2956                "    },\n"
2957                "    aaaa, bbbb, // comment\n"
2958                "    cccc);");
2959   verifyFormat("functionA(functionB({\n"
2960                "            int i;\n"
2961                "            int j;\n"
2962                "          }),\n"
2963                "          aaaa, bbbb, // comment\n"
2964                "          cccc);");
2965   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2966   verifyFormat("functionCall(aaaa, bbbb, {\n"
2967                "  int i;\n"
2968                "  int j;\n"
2969                "});");
2970   verifyFormat(
2971       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2972       "    {\n"
2973       "      int i; // break\n"
2974       "    },\n"
2975       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2976       "                                     ccccccccccccccccc));");
2977   verifyFormat("DEBUG({\n"
2978                "  if (a)\n"
2979                "    f();\n"
2980                "});");
2981 }
2982 
2983 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2984   EXPECT_EQ("SOME_MACRO { int i; }\n"
2985             "int i;",
2986             format("  SOME_MACRO  {int i;}  int i;"));
2987 }
2988 
2989 TEST_F(FormatTest, LayoutNestedBlocks) {
2990   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2991                "  struct s {\n"
2992                "    int i;\n"
2993                "  };\n"
2994                "  s kBitsToOs[] = {{10}};\n"
2995                "  for (int i = 0; i < 10; ++i)\n"
2996                "    return;\n"
2997                "}");
2998   verifyFormat("call(parameter, {\n"
2999                "  something();\n"
3000                "  // Comment using all columns.\n"
3001                "  somethingelse();\n"
3002                "});",
3003                getLLVMStyleWithColumns(40));
3004   verifyFormat("DEBUG( //\n"
3005                "    { f(); }, a);");
3006   verifyFormat("DEBUG( //\n"
3007                "    {\n"
3008                "      f(); //\n"
3009                "    },\n"
3010                "    a);");
3011 
3012   EXPECT_EQ("call(parameter, {\n"
3013             "  something();\n"
3014             "  // Comment too\n"
3015             "  // looooooooooong.\n"
3016             "  somethingElse();\n"
3017             "});",
3018             format("call(parameter, {\n"
3019                    "  something();\n"
3020                    "  // Comment too looooooooooong.\n"
3021                    "  somethingElse();\n"
3022                    "});",
3023                    getLLVMStyleWithColumns(29)));
3024   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3025   EXPECT_EQ("DEBUG({ // comment\n"
3026             "  int i;\n"
3027             "});",
3028             format("DEBUG({ // comment\n"
3029                    "int  i;\n"
3030                    "});"));
3031   EXPECT_EQ("DEBUG({\n"
3032             "  int i;\n"
3033             "\n"
3034             "  // comment\n"
3035             "  int j;\n"
3036             "});",
3037             format("DEBUG({\n"
3038                    "  int  i;\n"
3039                    "\n"
3040                    "  // comment\n"
3041                    "  int  j;\n"
3042                    "});"));
3043 
3044   verifyFormat("DEBUG({\n"
3045                "  if (a)\n"
3046                "    return;\n"
3047                "});");
3048   verifyGoogleFormat("DEBUG({\n"
3049                      "  if (a) return;\n"
3050                      "});");
3051   FormatStyle Style = getGoogleStyle();
3052   Style.ColumnLimit = 45;
3053   verifyFormat("Debug(aaaaa,\n"
3054                "      {\n"
3055                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3056                "      },\n"
3057                "      a);",
3058                Style);
3059 
3060   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3061 
3062   verifyNoCrash("^{v^{a}}");
3063 }
3064 
3065 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3066   EXPECT_EQ("#define MACRO()                     \\\n"
3067             "  Debug(aaa, /* force line break */ \\\n"
3068             "        {                           \\\n"
3069             "          int i;                    \\\n"
3070             "          int j;                    \\\n"
3071             "        })",
3072             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3073                    "          {  int   i;  int  j;   })",
3074                    getGoogleStyle()));
3075 
3076   EXPECT_EQ("#define A                                       \\\n"
3077             "  [] {                                          \\\n"
3078             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3079             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3080             "  }",
3081             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3082                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3083                    getGoogleStyle()));
3084 }
3085 
3086 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3087   EXPECT_EQ("{}", format("{}"));
3088   verifyFormat("enum E {};");
3089   verifyFormat("enum E {}");
3090 }
3091 
3092 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3093   FormatStyle Style = getLLVMStyle();
3094   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3095   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3096   verifyFormat("FOO_BEGIN\n"
3097                "  FOO_ENTRY\n"
3098                "FOO_END", Style);
3099   verifyFormat("FOO_BEGIN\n"
3100                "  NESTED_FOO_BEGIN\n"
3101                "    NESTED_FOO_ENTRY\n"
3102                "  NESTED_FOO_END\n"
3103                "FOO_END", Style);
3104   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3105                "  int x;\n"
3106                "  x = 1;\n"
3107                "FOO_END(Baz)", Style);
3108 }
3109 
3110 //===----------------------------------------------------------------------===//
3111 // Line break tests.
3112 //===----------------------------------------------------------------------===//
3113 
3114 TEST_F(FormatTest, PreventConfusingIndents) {
3115   verifyFormat(
3116       "void f() {\n"
3117       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3118       "                         parameter, parameter, parameter)),\n"
3119       "                     SecondLongCall(parameter));\n"
3120       "}");
3121   verifyFormat(
3122       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3123       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3124       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3125       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3126   verifyFormat(
3127       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3128       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3129       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3130       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3131   verifyFormat(
3132       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3133       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3134       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3135       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3136   verifyFormat("int a = bbbb && ccc &&\n"
3137                "        fffff(\n"
3138                "#define A Just forcing a new line\n"
3139                "            ddd);");
3140 }
3141 
3142 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3143   verifyFormat(
3144       "bool aaaaaaa =\n"
3145       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3146       "    bbbbbbbb();");
3147   verifyFormat(
3148       "bool aaaaaaa =\n"
3149       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3150       "    bbbbbbbb();");
3151 
3152   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3153                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3154                "    ccccccccc == ddddddddddd;");
3155   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3156                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3157                "    ccccccccc == ddddddddddd;");
3158   verifyFormat(
3159       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3160       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3161       "    ccccccccc == ddddddddddd;");
3162 
3163   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3164                "                 aaaaaa) &&\n"
3165                "         bbbbbb && cccccc;");
3166   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3167                "                 aaaaaa) >>\n"
3168                "         bbbbbb;");
3169   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3170                "    SourceMgr.getSpellingColumnNumber(\n"
3171                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3172                "    1);");
3173 
3174   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3175                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3176                "    cccccc) {\n}");
3177   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3178                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3179                "              cccccc) {\n}");
3180   verifyFormat("b = a &&\n"
3181                "    // Comment\n"
3182                "    b.c && d;");
3183 
3184   // If the LHS of a comparison is not a binary expression itself, the
3185   // additional linebreak confuses many people.
3186   verifyFormat(
3187       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3188       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3189       "}");
3190   verifyFormat(
3191       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3192       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3193       "}");
3194   verifyFormat(
3195       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3196       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3197       "}");
3198   verifyFormat(
3199       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3200       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3201       "}");
3202   // Even explicit parentheses stress the precedence enough to make the
3203   // additional break unnecessary.
3204   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3205                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3206                "}");
3207   // This cases is borderline, but with the indentation it is still readable.
3208   verifyFormat(
3209       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3210       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3211       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3212       "}",
3213       getLLVMStyleWithColumns(75));
3214 
3215   // If the LHS is a binary expression, we should still use the additional break
3216   // as otherwise the formatting hides the operator precedence.
3217   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3218                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3219                "    5) {\n"
3220                "}");
3221   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3222                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3223                "    5) {\n"
3224                "}");
3225 
3226   FormatStyle OnePerLine = getLLVMStyle();
3227   OnePerLine.BinPackParameters = false;
3228   verifyFormat(
3229       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3230       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3231       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3232       OnePerLine);
3233 
3234   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3235                "                .aaa(aaaaaaaaaaaaa) *\n"
3236                "            aaaaaaa +\n"
3237                "        aaaaaaa;",
3238                getLLVMStyleWithColumns(40));
3239 }
3240 
3241 TEST_F(FormatTest, ExpressionIndentation) {
3242   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3243                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3244                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3245                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3246                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3247                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3248                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3249                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3250                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3251   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3252                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3253                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3254                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3255   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3256                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3257                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3258                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3259   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3260                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3261                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3262                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3263   verifyFormat("if () {\n"
3264                "} else if (aaaaa && bbbbb > // break\n"
3265                "                        ccccc) {\n"
3266                "}");
3267   verifyFormat("if () {\n"
3268                "} else if (aaaaa &&\n"
3269                "           bbbbb > // break\n"
3270                "               ccccc &&\n"
3271                "           ddddd) {\n"
3272                "}");
3273 
3274   // Presence of a trailing comment used to change indentation of b.
3275   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3276                "       b;\n"
3277                "return aaaaaaaaaaaaaaaaaaa +\n"
3278                "       b; //",
3279                getLLVMStyleWithColumns(30));
3280 }
3281 
3282 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3283   // Not sure what the best system is here. Like this, the LHS can be found
3284   // immediately above an operator (everything with the same or a higher
3285   // indent). The RHS is aligned right of the operator and so compasses
3286   // everything until something with the same indent as the operator is found.
3287   // FIXME: Is this a good system?
3288   FormatStyle Style = getLLVMStyle();
3289   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3290   verifyFormat(
3291       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3292       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3293       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3294       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3295       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3296       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3297       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3298       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3299       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3300       Style);
3301   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3302                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3303                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3304                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3305                Style);
3306   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3307                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3308                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3309                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3310                Style);
3311   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3312                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3313                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3314                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3315                Style);
3316   verifyFormat("if () {\n"
3317                "} else if (aaaaa\n"
3318                "           && bbbbb // break\n"
3319                "                  > ccccc) {\n"
3320                "}",
3321                Style);
3322   verifyFormat("return (a)\n"
3323                "       // comment\n"
3324                "       + b;",
3325                Style);
3326   verifyFormat(
3327       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3328       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3329       "             + cc;",
3330       Style);
3331 
3332   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3333                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3334                Style);
3335 
3336   // Forced by comments.
3337   verifyFormat(
3338       "unsigned ContentSize =\n"
3339       "    sizeof(int16_t)   // DWARF ARange version number\n"
3340       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3341       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3342       "    + sizeof(int8_t); // Segment Size (in bytes)");
3343 
3344   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3345                "       == boost::fusion::at_c<1>(iiii).second;",
3346                Style);
3347 
3348   Style.ColumnLimit = 60;
3349   verifyFormat("zzzzzzzzzz\n"
3350                "    = bbbbbbbbbbbbbbbbb\n"
3351                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3352                Style);
3353 }
3354 
3355 TEST_F(FormatTest, EnforcedOperatorWraps) {
3356   // Here we'd like to wrap after the || operators, but a comment is forcing an
3357   // earlier wrap.
3358   verifyFormat("bool x = aaaaa //\n"
3359                "         || bbbbb\n"
3360                "         //\n"
3361                "         || cccc;");
3362 }
3363 
3364 TEST_F(FormatTest, NoOperandAlignment) {
3365   FormatStyle Style = getLLVMStyle();
3366   Style.AlignOperands = false;
3367   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3368                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3369                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3370                Style);
3371   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3372   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3373                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3374                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3375                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3376                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3377                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3378                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3379                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3380                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3381                Style);
3382 
3383   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3384                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3385                "    + cc;",
3386                Style);
3387   verifyFormat("int a = aa\n"
3388                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3389                "        * cccccccccccccccccccccccccccccccccccc;\n",
3390                Style);
3391 
3392   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3393   verifyFormat("return (a > b\n"
3394                "    // comment1\n"
3395                "    // comment2\n"
3396                "    || c);",
3397                Style);
3398 }
3399 
3400 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3401   FormatStyle Style = getLLVMStyle();
3402   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3403   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3404                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3405                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3406                Style);
3407 }
3408 
3409 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3410   FormatStyle Style = getLLVMStyle();
3411   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3412   Style.BinPackArguments = false;
3413   Style.ColumnLimit = 40;
3414   verifyFormat("void test() {\n"
3415                "  someFunction(\n"
3416                "      this + argument + is + quite\n"
3417                "      + long + so + it + gets + wrapped\n"
3418                "      + but + remains + bin - packed);\n"
3419                "}",
3420                Style);
3421   verifyFormat("void test() {\n"
3422                "  someFunction(arg1,\n"
3423                "               this + argument + is\n"
3424                "                   + quite + long + so\n"
3425                "                   + it + gets + wrapped\n"
3426                "                   + but + remains + bin\n"
3427                "                   - packed,\n"
3428                "               arg3);\n"
3429                "}",
3430                Style);
3431   verifyFormat("void test() {\n"
3432                "  someFunction(\n"
3433                "      arg1,\n"
3434                "      this + argument + has\n"
3435                "          + anotherFunc(nested,\n"
3436                "                        calls + whose\n"
3437                "                            + arguments\n"
3438                "                            + are + also\n"
3439                "                            + wrapped,\n"
3440                "                        in + addition)\n"
3441                "          + to + being + bin - packed,\n"
3442                "      arg3);\n"
3443                "}",
3444                Style);
3445 
3446   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3447   verifyFormat("void test() {\n"
3448                "  someFunction(\n"
3449                "      arg1,\n"
3450                "      this + argument + has +\n"
3451                "          anotherFunc(nested,\n"
3452                "                      calls + whose +\n"
3453                "                          arguments +\n"
3454                "                          are + also +\n"
3455                "                          wrapped,\n"
3456                "                      in + addition) +\n"
3457                "          to + being + bin - packed,\n"
3458                "      arg3);\n"
3459                "}",
3460                Style);
3461 }
3462 
3463 TEST_F(FormatTest, ConstructorInitializers) {
3464   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3465   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3466                getLLVMStyleWithColumns(45));
3467   verifyFormat("Constructor()\n"
3468                "    : Inttializer(FitsOnTheLine) {}",
3469                getLLVMStyleWithColumns(44));
3470   verifyFormat("Constructor()\n"
3471                "    : Inttializer(FitsOnTheLine) {}",
3472                getLLVMStyleWithColumns(43));
3473 
3474   verifyFormat("template <typename T>\n"
3475                "Constructor() : Initializer(FitsOnTheLine) {}",
3476                getLLVMStyleWithColumns(45));
3477 
3478   verifyFormat(
3479       "SomeClass::Constructor()\n"
3480       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3481 
3482   verifyFormat(
3483       "SomeClass::Constructor()\n"
3484       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3485       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3486   verifyFormat(
3487       "SomeClass::Constructor()\n"
3488       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3489       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3490   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3491                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3492                "    : aaaaaaaaaa(aaaaaa) {}");
3493 
3494   verifyFormat("Constructor()\n"
3495                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3496                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3497                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3498                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3499 
3500   verifyFormat("Constructor()\n"
3501                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3502                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3503 
3504   verifyFormat("Constructor(int Parameter = 0)\n"
3505                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3506                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3507   verifyFormat("Constructor()\n"
3508                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3509                "}",
3510                getLLVMStyleWithColumns(60));
3511   verifyFormat("Constructor()\n"
3512                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3513                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3514 
3515   // Here a line could be saved by splitting the second initializer onto two
3516   // lines, but that is not desirable.
3517   verifyFormat("Constructor()\n"
3518                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3519                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3520                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3521 
3522   FormatStyle OnePerLine = getLLVMStyle();
3523   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3524   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3525   verifyFormat("SomeClass::Constructor()\n"
3526                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3527                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3528                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3529                OnePerLine);
3530   verifyFormat("SomeClass::Constructor()\n"
3531                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3532                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3533                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3534                OnePerLine);
3535   verifyFormat("MyClass::MyClass(int var)\n"
3536                "    : some_var_(var),            // 4 space indent\n"
3537                "      some_other_var_(var + 1) { // lined up\n"
3538                "}",
3539                OnePerLine);
3540   verifyFormat("Constructor()\n"
3541                "    : aaaaa(aaaaaa),\n"
3542                "      aaaaa(aaaaaa),\n"
3543                "      aaaaa(aaaaaa),\n"
3544                "      aaaaa(aaaaaa),\n"
3545                "      aaaaa(aaaaaa) {}",
3546                OnePerLine);
3547   verifyFormat("Constructor()\n"
3548                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3549                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3550                OnePerLine);
3551   OnePerLine.BinPackParameters = false;
3552   verifyFormat(
3553       "Constructor()\n"
3554       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3555       "          aaaaaaaaaaa().aaa(),\n"
3556       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3557       OnePerLine);
3558   OnePerLine.ColumnLimit = 60;
3559   verifyFormat("Constructor()\n"
3560                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3561                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3562                OnePerLine);
3563 
3564   EXPECT_EQ("Constructor()\n"
3565             "    : // Comment forcing unwanted break.\n"
3566             "      aaaa(aaaa) {}",
3567             format("Constructor() :\n"
3568                    "    // Comment forcing unwanted break.\n"
3569                    "    aaaa(aaaa) {}"));
3570 }
3571 
3572 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3573   FormatStyle Style = getLLVMStyle();
3574   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3575 
3576   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3577   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3578                getStyleWithColumns(Style, 45));
3579   verifyFormat("Constructor() :\n"
3580                "    Initializer(FitsOnTheLine) {}",
3581                getStyleWithColumns(Style, 44));
3582   verifyFormat("Constructor() :\n"
3583                "    Initializer(FitsOnTheLine) {}",
3584                getStyleWithColumns(Style, 43));
3585 
3586   verifyFormat("template <typename T>\n"
3587                "Constructor() : Initializer(FitsOnTheLine) {}",
3588                getStyleWithColumns(Style, 50));
3589 
3590   verifyFormat(
3591       "SomeClass::Constructor() :\n"
3592       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3593 	  Style);
3594 
3595   verifyFormat(
3596       "SomeClass::Constructor() :\n"
3597       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3598       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3599 	  Style);
3600   verifyFormat(
3601       "SomeClass::Constructor() :\n"
3602       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3603       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3604 	  Style);
3605   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3606                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3607                "    aaaaaaaaaa(aaaaaa) {}",
3608 			   Style);
3609 
3610   verifyFormat("Constructor() :\n"
3611                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3612                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3613                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3614                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3615 			   Style);
3616 
3617   verifyFormat("Constructor() :\n"
3618                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3619                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3620 			   Style);
3621 
3622   verifyFormat("Constructor(int Parameter = 0) :\n"
3623                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3624                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3625 			   Style);
3626   verifyFormat("Constructor() :\n"
3627                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3628                "}",
3629                getStyleWithColumns(Style, 60));
3630   verifyFormat("Constructor() :\n"
3631                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3632                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3633 			   Style);
3634 
3635   // Here a line could be saved by splitting the second initializer onto two
3636   // lines, but that is not desirable.
3637   verifyFormat("Constructor() :\n"
3638                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3639                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3640                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3641 			   Style);
3642 
3643   FormatStyle OnePerLine = Style;
3644   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3645   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3646   verifyFormat("SomeClass::Constructor() :\n"
3647                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3648                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3649                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3650                OnePerLine);
3651   verifyFormat("SomeClass::Constructor() :\n"
3652                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3653                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3654                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3655                OnePerLine);
3656   verifyFormat("MyClass::MyClass(int var) :\n"
3657                "    some_var_(var),            // 4 space indent\n"
3658                "    some_other_var_(var + 1) { // lined up\n"
3659                "}",
3660                OnePerLine);
3661   verifyFormat("Constructor() :\n"
3662                "    aaaaa(aaaaaa),\n"
3663                "    aaaaa(aaaaaa),\n"
3664                "    aaaaa(aaaaaa),\n"
3665                "    aaaaa(aaaaaa),\n"
3666                "    aaaaa(aaaaaa) {}",
3667                OnePerLine);
3668   verifyFormat("Constructor() :\n"
3669                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3670                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3671                OnePerLine);
3672   OnePerLine.BinPackParameters = false;
3673   verifyFormat(
3674       "Constructor() :\n"
3675       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3676       "        aaaaaaaaaaa().aaa(),\n"
3677       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3678       OnePerLine);
3679   OnePerLine.ColumnLimit = 60;
3680   verifyFormat("Constructor() :\n"
3681                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3682                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3683                OnePerLine);
3684 
3685   EXPECT_EQ("Constructor() :\n"
3686             "    // Comment forcing unwanted break.\n"
3687             "    aaaa(aaaa) {}",
3688             format("Constructor() :\n"
3689                    "    // Comment forcing unwanted break.\n"
3690                    "    aaaa(aaaa) {}",
3691 				   Style));
3692 
3693   Style.ColumnLimit = 0;
3694   verifyFormat("SomeClass::Constructor() :\n"
3695                "    a(a) {}",
3696                Style);
3697   verifyFormat("SomeClass::Constructor() noexcept :\n"
3698                "    a(a) {}",
3699                Style);
3700   verifyFormat("SomeClass::Constructor() :\n"
3701 			   "    a(a), b(b), c(c) {}",
3702                Style);
3703   verifyFormat("SomeClass::Constructor() :\n"
3704                "    a(a) {\n"
3705                "  foo();\n"
3706                "  bar();\n"
3707                "}",
3708                Style);
3709 
3710   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3711   verifyFormat("SomeClass::Constructor() :\n"
3712 			   "    a(a), b(b), c(c) {\n"
3713 			   "}",
3714                Style);
3715   verifyFormat("SomeClass::Constructor() :\n"
3716                "    a(a) {\n"
3717 			   "}",
3718                Style);
3719 
3720   Style.ColumnLimit = 80;
3721   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3722   Style.ConstructorInitializerIndentWidth = 2;
3723   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3724                Style);
3725   verifyFormat("SomeClass::Constructor() :\n"
3726                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3727                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3728                Style);
3729 }
3730 
3731 #ifndef EXPENSIVE_CHECKS
3732 // Expensive checks enables libstdc++ checking which includes validating the
3733 // state of ranges used in std::priority_queue - this blows out the
3734 // runtime/scalability of the function and makes this test unacceptably slow.
3735 TEST_F(FormatTest, MemoizationTests) {
3736   // This breaks if the memoization lookup does not take \c Indent and
3737   // \c LastSpace into account.
3738   verifyFormat(
3739       "extern CFRunLoopTimerRef\n"
3740       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3741       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3742       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3743       "                     CFRunLoopTimerContext *context) {}");
3744 
3745   // Deep nesting somewhat works around our memoization.
3746   verifyFormat(
3747       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3748       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3749       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3750       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3751       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3752       getLLVMStyleWithColumns(65));
3753   verifyFormat(
3754       "aaaaa(\n"
3755       "    aaaaa,\n"
3756       "    aaaaa(\n"
3757       "        aaaaa,\n"
3758       "        aaaaa(\n"
3759       "            aaaaa,\n"
3760       "            aaaaa(\n"
3761       "                aaaaa,\n"
3762       "                aaaaa(\n"
3763       "                    aaaaa,\n"
3764       "                    aaaaa(\n"
3765       "                        aaaaa,\n"
3766       "                        aaaaa(\n"
3767       "                            aaaaa,\n"
3768       "                            aaaaa(\n"
3769       "                                aaaaa,\n"
3770       "                                aaaaa(\n"
3771       "                                    aaaaa,\n"
3772       "                                    aaaaa(\n"
3773       "                                        aaaaa,\n"
3774       "                                        aaaaa(\n"
3775       "                                            aaaaa,\n"
3776       "                                            aaaaa(\n"
3777       "                                                aaaaa,\n"
3778       "                                                aaaaa))))))))))));",
3779       getLLVMStyleWithColumns(65));
3780   verifyFormat(
3781       "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"
3782       "                                  a),\n"
3783       "                                a),\n"
3784       "                              a),\n"
3785       "                            a),\n"
3786       "                          a),\n"
3787       "                        a),\n"
3788       "                      a),\n"
3789       "                    a),\n"
3790       "                  a),\n"
3791       "                a),\n"
3792       "              a),\n"
3793       "            a),\n"
3794       "          a),\n"
3795       "        a),\n"
3796       "      a),\n"
3797       "    a),\n"
3798       "  a)",
3799       getLLVMStyleWithColumns(65));
3800 
3801   // This test takes VERY long when memoization is broken.
3802   FormatStyle OnePerLine = getLLVMStyle();
3803   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3804   OnePerLine.BinPackParameters = false;
3805   std::string input = "Constructor()\n"
3806                       "    : aaaa(a,\n";
3807   for (unsigned i = 0, e = 80; i != e; ++i) {
3808     input += "           a,\n";
3809   }
3810   input += "           a) {}";
3811   verifyFormat(input, OnePerLine);
3812 }
3813 #endif
3814 
3815 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3816   verifyFormat(
3817       "void f() {\n"
3818       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3819       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3820       "    f();\n"
3821       "}");
3822   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3823                "    Intervals[i - 1].getRange().getLast()) {\n}");
3824 }
3825 
3826 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3827   // Principially, we break function declarations in a certain order:
3828   // 1) break amongst arguments.
3829   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3830                "                              Cccccccccccccc cccccccccccccc);");
3831   verifyFormat("template <class TemplateIt>\n"
3832                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3833                "                            TemplateIt *stop) {}");
3834 
3835   // 2) break after return type.
3836   verifyFormat(
3837       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3838       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3839       getGoogleStyle());
3840 
3841   // 3) break after (.
3842   verifyFormat(
3843       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3844       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3845       getGoogleStyle());
3846 
3847   // 4) break before after nested name specifiers.
3848   verifyFormat(
3849       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3850       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3851       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3852       getGoogleStyle());
3853 
3854   // However, there are exceptions, if a sufficient amount of lines can be
3855   // saved.
3856   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3857   // more adjusting.
3858   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3859                "                                  Cccccccccccccc cccccccccc,\n"
3860                "                                  Cccccccccccccc cccccccccc,\n"
3861                "                                  Cccccccccccccc cccccccccc,\n"
3862                "                                  Cccccccccccccc cccccccccc);");
3863   verifyFormat(
3864       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3865       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3866       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3867       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3868       getGoogleStyle());
3869   verifyFormat(
3870       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3871       "                                          Cccccccccccccc cccccccccc,\n"
3872       "                                          Cccccccccccccc cccccccccc,\n"
3873       "                                          Cccccccccccccc cccccccccc,\n"
3874       "                                          Cccccccccccccc cccccccccc,\n"
3875       "                                          Cccccccccccccc cccccccccc,\n"
3876       "                                          Cccccccccccccc cccccccccc);");
3877   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3878                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3879                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3880                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3881                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3882 
3883   // Break after multi-line parameters.
3884   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3885                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3886                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3887                "    bbbb bbbb);");
3888   verifyFormat("void SomeLoooooooooooongFunction(\n"
3889                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3890                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3891                "    int bbbbbbbbbbbbb);");
3892 
3893   // Treat overloaded operators like other functions.
3894   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3895                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3896   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3897                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3898   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3899                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3900   verifyGoogleFormat(
3901       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3902       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3903   verifyGoogleFormat(
3904       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3905       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3906   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3907                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3908   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3909                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3910   verifyGoogleFormat(
3911       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3912       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3913       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3914   verifyGoogleFormat(
3915       "template <typename T>\n"
3916       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3917       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3918       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3919 
3920   FormatStyle Style = getLLVMStyle();
3921   Style.PointerAlignment = FormatStyle::PAS_Left;
3922   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3923                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3924                Style);
3925   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3926                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3927                Style);
3928 }
3929 
3930 TEST_F(FormatTest, TrailingReturnType) {
3931   verifyFormat("auto foo() -> int;\n");
3932   verifyFormat("struct S {\n"
3933                "  auto bar() const -> int;\n"
3934                "};");
3935   verifyFormat("template <size_t Order, typename T>\n"
3936                "auto load_img(const std::string &filename)\n"
3937                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3938   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3939                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3940   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3941   verifyFormat("template <typename T>\n"
3942                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3943                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3944 
3945   // Not trailing return types.
3946   verifyFormat("void f() { auto a = b->c(); }");
3947 }
3948 
3949 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3950   // Avoid breaking before trailing 'const' or other trailing annotations, if
3951   // they are not function-like.
3952   FormatStyle Style = getGoogleStyle();
3953   Style.ColumnLimit = 47;
3954   verifyFormat("void someLongFunction(\n"
3955                "    int someLoooooooooooooongParameter) const {\n}",
3956                getLLVMStyleWithColumns(47));
3957   verifyFormat("LoooooongReturnType\n"
3958                "someLoooooooongFunction() const {}",
3959                getLLVMStyleWithColumns(47));
3960   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3961                "    const {}",
3962                Style);
3963   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3964                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3965   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3966                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3967   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3968                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3969   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3970                "                   aaaaaaaaaaa aaaaa) const override;");
3971   verifyGoogleFormat(
3972       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3973       "    const override;");
3974 
3975   // Even if the first parameter has to be wrapped.
3976   verifyFormat("void someLongFunction(\n"
3977                "    int someLongParameter) const {}",
3978                getLLVMStyleWithColumns(46));
3979   verifyFormat("void someLongFunction(\n"
3980                "    int someLongParameter) const {}",
3981                Style);
3982   verifyFormat("void someLongFunction(\n"
3983                "    int someLongParameter) override {}",
3984                Style);
3985   verifyFormat("void someLongFunction(\n"
3986                "    int someLongParameter) OVERRIDE {}",
3987                Style);
3988   verifyFormat("void someLongFunction(\n"
3989                "    int someLongParameter) final {}",
3990                Style);
3991   verifyFormat("void someLongFunction(\n"
3992                "    int someLongParameter) FINAL {}",
3993                Style);
3994   verifyFormat("void someLongFunction(\n"
3995                "    int parameter) const override {}",
3996                Style);
3997 
3998   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3999   verifyFormat("void someLongFunction(\n"
4000                "    int someLongParameter) const\n"
4001                "{\n"
4002                "}",
4003                Style);
4004 
4005   // Unless these are unknown annotations.
4006   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4007                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4008                "    LONG_AND_UGLY_ANNOTATION;");
4009 
4010   // Breaking before function-like trailing annotations is fine to keep them
4011   // close to their arguments.
4012   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4013                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4014   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4015                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4016   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4017                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4018   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4019                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4020   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4021 
4022   verifyFormat(
4023       "void aaaaaaaaaaaaaaaaaa()\n"
4024       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4025       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4026   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4027                "    __attribute__((unused));");
4028   verifyGoogleFormat(
4029       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4030       "    GUARDED_BY(aaaaaaaaaaaa);");
4031   verifyGoogleFormat(
4032       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4033       "    GUARDED_BY(aaaaaaaaaaaa);");
4034   verifyGoogleFormat(
4035       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4036       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4037   verifyGoogleFormat(
4038       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4039       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4040 }
4041 
4042 TEST_F(FormatTest, FunctionAnnotations) {
4043   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4044                "int OldFunction(const string &parameter) {}");
4045   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4046                "string OldFunction(const string &parameter) {}");
4047   verifyFormat("template <typename T>\n"
4048                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4049                "string OldFunction(const string &parameter) {}");
4050 
4051   // Not function annotations.
4052   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4053                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4054   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4055                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4056   verifyFormat("MACRO(abc).function() // wrap\n"
4057                "    << abc;");
4058   verifyFormat("MACRO(abc)->function() // wrap\n"
4059                "    << abc;");
4060   verifyFormat("MACRO(abc)::function() // wrap\n"
4061                "    << abc;");
4062 }
4063 
4064 TEST_F(FormatTest, BreaksDesireably) {
4065   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4066                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4067                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4068   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4069                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4070                "}");
4071 
4072   verifyFormat(
4073       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4074       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4075 
4076   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4077                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4078                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4079 
4080   verifyFormat(
4081       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4082       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4083       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4084       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4085       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4086 
4087   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4088                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4089 
4090   verifyFormat(
4091       "void f() {\n"
4092       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4093       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4094       "}");
4095   verifyFormat(
4096       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4097       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4098   verifyFormat(
4099       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4100       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4101   verifyFormat(
4102       "aaaaaa(aaa,\n"
4103       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4104       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4105       "       aaaa);");
4106   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4107                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4108                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4109 
4110   // Indent consistently independent of call expression and unary operator.
4111   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4112                "    dddddddddddddddddddddddddddddd));");
4113   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4114                "    dddddddddddddddddddddddddddddd));");
4115   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4116                "    dddddddddddddddddddddddddddddd));");
4117 
4118   // This test case breaks on an incorrect memoization, i.e. an optimization not
4119   // taking into account the StopAt value.
4120   verifyFormat(
4121       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4122       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4123       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4124       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4125 
4126   verifyFormat("{\n  {\n    {\n"
4127                "      Annotation.SpaceRequiredBefore =\n"
4128                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4129                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4130                "    }\n  }\n}");
4131 
4132   // Break on an outer level if there was a break on an inner level.
4133   EXPECT_EQ("f(g(h(a, // comment\n"
4134             "      b, c),\n"
4135             "    d, e),\n"
4136             "  x, y);",
4137             format("f(g(h(a, // comment\n"
4138                    "    b, c), d, e), x, y);"));
4139 
4140   // Prefer breaking similar line breaks.
4141   verifyFormat(
4142       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4143       "                             NSTrackingMouseEnteredAndExited |\n"
4144       "                             NSTrackingActiveAlways;");
4145 }
4146 
4147 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4148   FormatStyle NoBinPacking = getGoogleStyle();
4149   NoBinPacking.BinPackParameters = false;
4150   NoBinPacking.BinPackArguments = true;
4151   verifyFormat("void f() {\n"
4152                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4153                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4154                "}",
4155                NoBinPacking);
4156   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4157                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4158                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4159                NoBinPacking);
4160 
4161   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4162   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4163                "                        vector<int> bbbbbbbbbbbbbbb);",
4164                NoBinPacking);
4165   // FIXME: This behavior difference is probably not wanted. However, currently
4166   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4167   // template arguments from BreakBeforeParameter being set because of the
4168   // one-per-line formatting.
4169   verifyFormat(
4170       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4171       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4172       NoBinPacking);
4173   verifyFormat(
4174       "void fffffffffff(\n"
4175       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4176       "        aaaaaaaaaa);");
4177 }
4178 
4179 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4180   FormatStyle NoBinPacking = getGoogleStyle();
4181   NoBinPacking.BinPackParameters = false;
4182   NoBinPacking.BinPackArguments = false;
4183   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4184                "  aaaaaaaaaaaaaaaaaaaa,\n"
4185                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4186                NoBinPacking);
4187   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4188                "        aaaaaaaaaaaaa,\n"
4189                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4190                NoBinPacking);
4191   verifyFormat(
4192       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4193       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4194       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4195       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4196       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4197       NoBinPacking);
4198   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4199                "    .aaaaaaaaaaaaaaaaaa();",
4200                NoBinPacking);
4201   verifyFormat("void f() {\n"
4202                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4203                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4204                "}",
4205                NoBinPacking);
4206 
4207   verifyFormat(
4208       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4209       "             aaaaaaaaaaaa,\n"
4210       "             aaaaaaaaaaaa);",
4211       NoBinPacking);
4212   verifyFormat(
4213       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4214       "                               ddddddddddddddddddddddddddddd),\n"
4215       "             test);",
4216       NoBinPacking);
4217 
4218   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4219                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4220                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4221                "    aaaaaaaaaaaaaaaaaa;",
4222                NoBinPacking);
4223   verifyFormat("a(\"a\"\n"
4224                "  \"a\",\n"
4225                "  a);");
4226 
4227   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4228   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4229                "                aaaaaaaaa,\n"
4230                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4231                NoBinPacking);
4232   verifyFormat(
4233       "void f() {\n"
4234       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4235       "      .aaaaaaa();\n"
4236       "}",
4237       NoBinPacking);
4238   verifyFormat(
4239       "template <class SomeType, class SomeOtherType>\n"
4240       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4241       NoBinPacking);
4242 }
4243 
4244 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4245   FormatStyle Style = getLLVMStyleWithColumns(15);
4246   Style.ExperimentalAutoDetectBinPacking = true;
4247   EXPECT_EQ("aaa(aaaa,\n"
4248             "    aaaa,\n"
4249             "    aaaa);\n"
4250             "aaa(aaaa,\n"
4251             "    aaaa,\n"
4252             "    aaaa);",
4253             format("aaa(aaaa,\n" // one-per-line
4254                    "  aaaa,\n"
4255                    "    aaaa  );\n"
4256                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4257                    Style));
4258   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4259             "    aaaa);\n"
4260             "aaa(aaaa, aaaa,\n"
4261             "    aaaa);",
4262             format("aaa(aaaa,  aaaa,\n" // bin-packed
4263                    "    aaaa  );\n"
4264                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4265                    Style));
4266 }
4267 
4268 TEST_F(FormatTest, FormatsBuilderPattern) {
4269   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4270                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4271                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4272                "    .StartsWith(\".init\", ORDER_INIT)\n"
4273                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4274                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4275                "    .Default(ORDER_TEXT);\n");
4276 
4277   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4278                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4279   verifyFormat(
4280       "aaaaaaa->aaaaaaa\n"
4281       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4282       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4283       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4284   verifyFormat(
4285       "aaaaaaa->aaaaaaa\n"
4286       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4287       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4288   verifyFormat(
4289       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4290       "    aaaaaaaaaaaaaa);");
4291   verifyFormat(
4292       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4293       "    aaaaaa->aaaaaaaaaaaa()\n"
4294       "        ->aaaaaaaaaaaaaaaa(\n"
4295       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4296       "        ->aaaaaaaaaaaaaaaaa();");
4297   verifyGoogleFormat(
4298       "void f() {\n"
4299       "  someo->Add((new util::filetools::Handler(dir))\n"
4300       "                 ->OnEvent1(NewPermanentCallback(\n"
4301       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4302       "                 ->OnEvent2(NewPermanentCallback(\n"
4303       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4304       "                 ->OnEvent3(NewPermanentCallback(\n"
4305       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4306       "                 ->OnEvent5(NewPermanentCallback(\n"
4307       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4308       "                 ->OnEvent6(NewPermanentCallback(\n"
4309       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4310       "}");
4311 
4312   verifyFormat(
4313       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4314   verifyFormat("aaaaaaaaaaaaaaa()\n"
4315                "    .aaaaaaaaaaaaaaa()\n"
4316                "    .aaaaaaaaaaaaaaa()\n"
4317                "    .aaaaaaaaaaaaaaa()\n"
4318                "    .aaaaaaaaaaaaaaa();");
4319   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4320                "    .aaaaaaaaaaaaaaa()\n"
4321                "    .aaaaaaaaaaaaaaa()\n"
4322                "    .aaaaaaaaaaaaaaa();");
4323   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4324                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4325                "    .aaaaaaaaaaaaaaa();");
4326   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4327                "    ->aaaaaaaaaaaaaae(0)\n"
4328                "    ->aaaaaaaaaaaaaaa();");
4329 
4330   // Don't linewrap after very short segments.
4331   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4332                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4333                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4334   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4335                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4336                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4337   verifyFormat("aaa()\n"
4338                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4339                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4340                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4341 
4342   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4343                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4344                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4345   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4346                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4347                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4348 
4349   // Prefer not to break after empty parentheses.
4350   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4351                "    First->LastNewlineOffset);");
4352 
4353   // Prefer not to create "hanging" indents.
4354   verifyFormat(
4355       "return !soooooooooooooome_map\n"
4356       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4357       "            .second;");
4358   verifyFormat(
4359       "return aaaaaaaaaaaaaaaa\n"
4360       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4361       "    .aaaa(aaaaaaaaaaaaaa);");
4362   // No hanging indent here.
4363   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4364                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4365   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4366                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4367   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4368                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4369                getLLVMStyleWithColumns(60));
4370   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4371                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4372                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4373                getLLVMStyleWithColumns(59));
4374   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4375                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4376                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4377 }
4378 
4379 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4380   verifyFormat(
4381       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4382       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4383   verifyFormat(
4384       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4385       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4386 
4387   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4388                "    ccccccccccccccccccccccccc) {\n}");
4389   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4390                "    ccccccccccccccccccccccccc) {\n}");
4391 
4392   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4393                "    ccccccccccccccccccccccccc) {\n}");
4394   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4395                "    ccccccccccccccccccccccccc) {\n}");
4396 
4397   verifyFormat(
4398       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4399       "    ccccccccccccccccccccccccc) {\n}");
4400   verifyFormat(
4401       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4402       "    ccccccccccccccccccccccccc) {\n}");
4403 
4404   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4405                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4406                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4407                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4408   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4409                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4410                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4411                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4412 
4413   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4414                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4415                "    aaaaaaaaaaaaaaa != aa) {\n}");
4416   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4417                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4418                "    aaaaaaaaaaaaaaa != aa) {\n}");
4419 }
4420 
4421 TEST_F(FormatTest, BreaksAfterAssignments) {
4422   verifyFormat(
4423       "unsigned Cost =\n"
4424       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4425       "                        SI->getPointerAddressSpaceee());\n");
4426   verifyFormat(
4427       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4428       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4429 
4430   verifyFormat(
4431       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4432       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4433   verifyFormat("unsigned OriginalStartColumn =\n"
4434                "    SourceMgr.getSpellingColumnNumber(\n"
4435                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4436                "    1;");
4437 }
4438 
4439 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4440   FormatStyle Style = getLLVMStyle();
4441   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4442                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4443                Style);
4444 
4445   Style.PenaltyBreakAssignment = 20;
4446   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4447                "                                 cccccccccccccccccccccccccc;",
4448                Style);
4449 }
4450 
4451 TEST_F(FormatTest, AlignsAfterAssignments) {
4452   verifyFormat(
4453       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4454       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4455   verifyFormat(
4456       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4457       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4458   verifyFormat(
4459       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4460       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4461   verifyFormat(
4462       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4463       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4464   verifyFormat(
4465       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4466       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4467       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4468 }
4469 
4470 TEST_F(FormatTest, AlignsAfterReturn) {
4471   verifyFormat(
4472       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4473       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4474   verifyFormat(
4475       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4476       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4477   verifyFormat(
4478       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4479       "       aaaaaaaaaaaaaaaaaaaaaa();");
4480   verifyFormat(
4481       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4482       "        aaaaaaaaaaaaaaaaaaaaaa());");
4483   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4484                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4485   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4486                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4487                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4488   verifyFormat("return\n"
4489                "    // true if code is one of a or b.\n"
4490                "    code == a || code == b;");
4491 }
4492 
4493 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4494   verifyFormat(
4495       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4496       "                                                aaaaaaaaa aaaaaaa) {}");
4497   verifyFormat(
4498       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4499       "                                               aaaaaaaaaaa aaaaaaaaa);");
4500   verifyFormat(
4501       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4502       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4503   FormatStyle Style = getLLVMStyle();
4504   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4505   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4506                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4507                Style);
4508   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4509                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4510                Style);
4511   verifyFormat("SomeLongVariableName->someFunction(\n"
4512                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4513                Style);
4514   verifyFormat(
4515       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4516       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4517       Style);
4518   verifyFormat(
4519       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4520       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4521       Style);
4522   verifyFormat(
4523       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4524       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4525       Style);
4526 
4527   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4528                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4529                "        b));",
4530                Style);
4531 
4532   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4533   Style.BinPackArguments = false;
4534   Style.BinPackParameters = false;
4535   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4536                "    aaaaaaaaaaa aaaaaaaa,\n"
4537                "    aaaaaaaaa aaaaaaa,\n"
4538                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4539                Style);
4540   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4541                "    aaaaaaaaaaa aaaaaaaaa,\n"
4542                "    aaaaaaaaaaa aaaaaaaaa,\n"
4543                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4544                Style);
4545   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4546                "    aaaaaaaaaaaaaaa,\n"
4547                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4548                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4549                Style);
4550   verifyFormat(
4551       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4552       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4553       Style);
4554   verifyFormat(
4555       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4556       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4557       Style);
4558   verifyFormat(
4559       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4560       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4561       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4562       "    aaaaaaaaaaaaaaaa);",
4563       Style);
4564   verifyFormat(
4565       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4566       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4567       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4568       "    aaaaaaaaaaaaaaaa);",
4569       Style);
4570 }
4571 
4572 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4573   FormatStyle Style = getLLVMStyleWithColumns(40);
4574   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4575                "          bbbbbbbbbbbbbbbbbbbbbb);",
4576                Style);
4577   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4578   Style.AlignOperands = false;
4579   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4580                "          bbbbbbbbbbbbbbbbbbbbbb);",
4581                Style);
4582   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4583   Style.AlignOperands = true;
4584   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4585                "          bbbbbbbbbbbbbbbbbbbbbb);",
4586                Style);
4587   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4588   Style.AlignOperands = false;
4589   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4590                "    bbbbbbbbbbbbbbbbbbbbbb);",
4591                Style);
4592 }
4593 
4594 TEST_F(FormatTest, BreaksConditionalExpressions) {
4595   verifyFormat(
4596       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4597       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4598       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4599   verifyFormat(
4600       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4601       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4602       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4603   verifyFormat(
4604       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4605       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4606   verifyFormat(
4607       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4608       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4609       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4610   verifyFormat(
4611       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4612       "                                                    : aaaaaaaaaaaaa);");
4613   verifyFormat(
4614       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4615       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4616       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4617       "                   aaaaaaaaaaaaa);");
4618   verifyFormat(
4619       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4620       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4621       "                   aaaaaaaaaaaaa);");
4622   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4623                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4624                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4625                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4626                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4627   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4628                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4629                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4630                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4631                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4632                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4633                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4634   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4635                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4636                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4637                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4638                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4639   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4640                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4641                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4642   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4643                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4644                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4645                "        : aaaaaaaaaaaaaaaa;");
4646   verifyFormat(
4647       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4648       "    ? aaaaaaaaaaaaaaa\n"
4649       "    : aaaaaaaaaaaaaaa;");
4650   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4651                "          aaaaaaaaa\n"
4652                "      ? b\n"
4653                "      : c);");
4654   verifyFormat("return aaaa == bbbb\n"
4655                "           // comment\n"
4656                "           ? aaaa\n"
4657                "           : bbbb;");
4658   verifyFormat("unsigned Indent =\n"
4659                "    format(TheLine.First,\n"
4660                "           IndentForLevel[TheLine.Level] >= 0\n"
4661                "               ? IndentForLevel[TheLine.Level]\n"
4662                "               : TheLine * 2,\n"
4663                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4664                getLLVMStyleWithColumns(60));
4665   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4666                "                  ? aaaaaaaaaaaaaaa\n"
4667                "                  : bbbbbbbbbbbbbbb //\n"
4668                "                        ? ccccccccccccccc\n"
4669                "                        : ddddddddddddddd;");
4670   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4671                "                  ? aaaaaaaaaaaaaaa\n"
4672                "                  : (bbbbbbbbbbbbbbb //\n"
4673                "                         ? ccccccccccccccc\n"
4674                "                         : ddddddddddddddd);");
4675   verifyFormat(
4676       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4677       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4678       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4679       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4680       "                                      : aaaaaaaaaa;");
4681   verifyFormat(
4682       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4683       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4684       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4685 
4686   FormatStyle NoBinPacking = getLLVMStyle();
4687   NoBinPacking.BinPackArguments = false;
4688   verifyFormat(
4689       "void f() {\n"
4690       "  g(aaa,\n"
4691       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4692       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4693       "        ? aaaaaaaaaaaaaaa\n"
4694       "        : aaaaaaaaaaaaaaa);\n"
4695       "}",
4696       NoBinPacking);
4697   verifyFormat(
4698       "void f() {\n"
4699       "  g(aaa,\n"
4700       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4701       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4702       "        ?: aaaaaaaaaaaaaaa);\n"
4703       "}",
4704       NoBinPacking);
4705 
4706   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4707                "             // comment.\n"
4708                "             ccccccccccccccccccccccccccccccccccccccc\n"
4709                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4710                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4711 
4712   // Assignments in conditional expressions. Apparently not uncommon :-(.
4713   verifyFormat("return a != b\n"
4714                "           // comment\n"
4715                "           ? a = b\n"
4716                "           : a = b;");
4717   verifyFormat("return a != b\n"
4718                "           // comment\n"
4719                "           ? a = a != b\n"
4720                "                     // comment\n"
4721                "                     ? a = b\n"
4722                "                     : a\n"
4723                "           : a;\n");
4724   verifyFormat("return a != b\n"
4725                "           // comment\n"
4726                "           ? a\n"
4727                "           : a = a != b\n"
4728                "                     // comment\n"
4729                "                     ? a = b\n"
4730                "                     : a;");
4731 }
4732 
4733 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4734   FormatStyle Style = getLLVMStyle();
4735   Style.BreakBeforeTernaryOperators = false;
4736   Style.ColumnLimit = 70;
4737   verifyFormat(
4738       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4739       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4740       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4741       Style);
4742   verifyFormat(
4743       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4744       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4745       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4746       Style);
4747   verifyFormat(
4748       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4749       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4750       Style);
4751   verifyFormat(
4752       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4753       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4754       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4755       Style);
4756   verifyFormat(
4757       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4758       "                                                      aaaaaaaaaaaaa);",
4759       Style);
4760   verifyFormat(
4761       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4762       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4763       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4764       "                   aaaaaaaaaaaaa);",
4765       Style);
4766   verifyFormat(
4767       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4768       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4769       "                   aaaaaaaaaaaaa);",
4770       Style);
4771   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4772                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4773                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4774                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4775                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4776                Style);
4777   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4778                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4779                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4780                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4781                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4782                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4783                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4784                Style);
4785   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4786                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4787                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4788                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4789                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4790                Style);
4791   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4792                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4793                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4794                Style);
4795   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4796                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4797                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4798                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4799                Style);
4800   verifyFormat(
4801       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4802       "    aaaaaaaaaaaaaaa :\n"
4803       "    aaaaaaaaaaaaaaa;",
4804       Style);
4805   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4806                "          aaaaaaaaa ?\n"
4807                "      b :\n"
4808                "      c);",
4809                Style);
4810   verifyFormat("unsigned Indent =\n"
4811                "    format(TheLine.First,\n"
4812                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4813                "               IndentForLevel[TheLine.Level] :\n"
4814                "               TheLine * 2,\n"
4815                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4816                Style);
4817   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4818                "                  aaaaaaaaaaaaaaa :\n"
4819                "                  bbbbbbbbbbbbbbb ? //\n"
4820                "                      ccccccccccccccc :\n"
4821                "                      ddddddddddddddd;",
4822                Style);
4823   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4824                "                  aaaaaaaaaaaaaaa :\n"
4825                "                  (bbbbbbbbbbbbbbb ? //\n"
4826                "                       ccccccccccccccc :\n"
4827                "                       ddddddddddddddd);",
4828                Style);
4829   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4830                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4831                "            ccccccccccccccccccccccccccc;",
4832                Style);
4833   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4834                "           aaaaa :\n"
4835                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4836                Style);
4837 }
4838 
4839 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4840   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4841                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4842   verifyFormat("bool a = true, b = false;");
4843 
4844   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4845                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4846                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4847                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4848   verifyFormat(
4849       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4850       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4851       "     d = e && f;");
4852   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4853                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4854   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4855                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4856   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4857                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4858 
4859   FormatStyle Style = getGoogleStyle();
4860   Style.PointerAlignment = FormatStyle::PAS_Left;
4861   Style.DerivePointerAlignment = false;
4862   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4863                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4864                "    *b = bbbbbbbbbbbbbbbbbbb;",
4865                Style);
4866   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4867                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4868                Style);
4869   verifyFormat("vector<int*> a, b;", Style);
4870   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4871 }
4872 
4873 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4874   verifyFormat("arr[foo ? bar : baz];");
4875   verifyFormat("f()[foo ? bar : baz];");
4876   verifyFormat("(a + b)[foo ? bar : baz];");
4877   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4878 }
4879 
4880 TEST_F(FormatTest, AlignsStringLiterals) {
4881   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4882                "                                      \"short literal\");");
4883   verifyFormat(
4884       "looooooooooooooooooooooooongFunction(\n"
4885       "    \"short literal\"\n"
4886       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4887   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4888                "             \" string literals\",\n"
4889                "             and, other, parameters);");
4890   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4891             "      \"5678\";",
4892             format("fun + \"1243\" /* comment */\n"
4893                    "    \"5678\";",
4894                    getLLVMStyleWithColumns(28)));
4895   EXPECT_EQ(
4896       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4897       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4898       "         \"aaaaaaaaaaaaaaaa\";",
4899       format("aaaaaa ="
4900              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4901              "aaaaaaaaaaaaaaaaaaaaa\" "
4902              "\"aaaaaaaaaaaaaaaa\";"));
4903   verifyFormat("a = a + \"a\"\n"
4904                "        \"a\"\n"
4905                "        \"a\";");
4906   verifyFormat("f(\"a\", \"b\"\n"
4907                "       \"c\");");
4908 
4909   verifyFormat(
4910       "#define LL_FORMAT \"ll\"\n"
4911       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4912       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4913 
4914   verifyFormat("#define A(X)          \\\n"
4915                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4916                "  \"ccccc\"",
4917                getLLVMStyleWithColumns(23));
4918   verifyFormat("#define A \"def\"\n"
4919                "f(\"abc\" A \"ghi\"\n"
4920                "  \"jkl\");");
4921 
4922   verifyFormat("f(L\"a\"\n"
4923                "  L\"b\");");
4924   verifyFormat("#define A(X)            \\\n"
4925                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4926                "  L\"ccccc\"",
4927                getLLVMStyleWithColumns(25));
4928 
4929   verifyFormat("f(@\"a\"\n"
4930                "  @\"b\");");
4931   verifyFormat("NSString s = @\"a\"\n"
4932                "             @\"b\"\n"
4933                "             @\"c\";");
4934   verifyFormat("NSString s = @\"a\"\n"
4935                "              \"b\"\n"
4936                "              \"c\";");
4937 }
4938 
4939 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4940   FormatStyle Style = getLLVMStyle();
4941   // No declarations or definitions should be moved to own line.
4942   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4943   verifyFormat("class A {\n"
4944                "  int f() { return 1; }\n"
4945                "  int g();\n"
4946                "};\n"
4947                "int f() { return 1; }\n"
4948                "int g();\n",
4949                Style);
4950 
4951   // All declarations and definitions should have the return type moved to its
4952   // own
4953   // line.
4954   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4955   verifyFormat("class E {\n"
4956                "  int\n"
4957                "  f() {\n"
4958                "    return 1;\n"
4959                "  }\n"
4960                "  int\n"
4961                "  g();\n"
4962                "};\n"
4963                "int\n"
4964                "f() {\n"
4965                "  return 1;\n"
4966                "}\n"
4967                "int\n"
4968                "g();\n",
4969                Style);
4970 
4971   // Top-level definitions, and no kinds of declarations should have the
4972   // return type moved to its own line.
4973   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4974   verifyFormat("class B {\n"
4975                "  int f() { return 1; }\n"
4976                "  int g();\n"
4977                "};\n"
4978                "int\n"
4979                "f() {\n"
4980                "  return 1;\n"
4981                "}\n"
4982                "int g();\n",
4983                Style);
4984 
4985   // Top-level definitions and declarations should have the return type moved
4986   // to its own line.
4987   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4988   verifyFormat("class C {\n"
4989                "  int f() { return 1; }\n"
4990                "  int g();\n"
4991                "};\n"
4992                "int\n"
4993                "f() {\n"
4994                "  return 1;\n"
4995                "}\n"
4996                "int\n"
4997                "g();\n",
4998                Style);
4999 
5000   // All definitions should have the return type moved to its own line, but no
5001   // kinds of declarations.
5002   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5003   verifyFormat("class D {\n"
5004                "  int\n"
5005                "  f() {\n"
5006                "    return 1;\n"
5007                "  }\n"
5008                "  int g();\n"
5009                "};\n"
5010                "int\n"
5011                "f() {\n"
5012                "  return 1;\n"
5013                "}\n"
5014                "int g();\n",
5015                Style);
5016   verifyFormat("const char *\n"
5017                "f(void) {\n" // Break here.
5018                "  return \"\";\n"
5019                "}\n"
5020                "const char *bar(void);\n", // No break here.
5021                Style);
5022   verifyFormat("template <class T>\n"
5023                "T *\n"
5024                "f(T &c) {\n" // Break here.
5025                "  return NULL;\n"
5026                "}\n"
5027                "template <class T> T *f(T &c);\n", // No break here.
5028                Style);
5029   verifyFormat("class C {\n"
5030                "  int\n"
5031                "  operator+() {\n"
5032                "    return 1;\n"
5033                "  }\n"
5034                "  int\n"
5035                "  operator()() {\n"
5036                "    return 1;\n"
5037                "  }\n"
5038                "};\n",
5039                Style);
5040   verifyFormat("void\n"
5041                "A::operator()() {}\n"
5042                "void\n"
5043                "A::operator>>() {}\n"
5044                "void\n"
5045                "A::operator+() {}\n",
5046                Style);
5047   verifyFormat("void *operator new(std::size_t s);", // No break here.
5048                Style);
5049   verifyFormat("void *\n"
5050                "operator new(std::size_t s) {}",
5051                Style);
5052   verifyFormat("void *\n"
5053                "operator delete[](void *ptr) {}",
5054                Style);
5055   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5056   verifyFormat("const char *\n"
5057                "f(void)\n" // Break here.
5058                "{\n"
5059                "  return \"\";\n"
5060                "}\n"
5061                "const char *bar(void);\n", // No break here.
5062                Style);
5063   verifyFormat("template <class T>\n"
5064                "T *\n"     // Problem here: no line break
5065                "f(T &c)\n" // Break here.
5066                "{\n"
5067                "  return NULL;\n"
5068                "}\n"
5069                "template <class T> T *f(T &c);\n", // No break here.
5070                Style);
5071 }
5072 
5073 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5074   FormatStyle NoBreak = getLLVMStyle();
5075   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5076   FormatStyle Break = getLLVMStyle();
5077   Break.AlwaysBreakBeforeMultilineStrings = true;
5078   verifyFormat("aaaa = \"bbbb\"\n"
5079                "       \"cccc\";",
5080                NoBreak);
5081   verifyFormat("aaaa =\n"
5082                "    \"bbbb\"\n"
5083                "    \"cccc\";",
5084                Break);
5085   verifyFormat("aaaa(\"bbbb\"\n"
5086                "     \"cccc\");",
5087                NoBreak);
5088   verifyFormat("aaaa(\n"
5089                "    \"bbbb\"\n"
5090                "    \"cccc\");",
5091                Break);
5092   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5093                "          \"cccc\");",
5094                NoBreak);
5095   verifyFormat("aaaa(qqq,\n"
5096                "     \"bbbb\"\n"
5097                "     \"cccc\");",
5098                Break);
5099   verifyFormat("aaaa(qqq,\n"
5100                "     L\"bbbb\"\n"
5101                "     L\"cccc\");",
5102                Break);
5103   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5104                "                      \"bbbb\"));",
5105                Break);
5106   verifyFormat("string s = someFunction(\n"
5107                "    \"abc\"\n"
5108                "    \"abc\");",
5109                Break);
5110 
5111   // As we break before unary operators, breaking right after them is bad.
5112   verifyFormat("string foo = abc ? \"x\"\n"
5113                "                   \"blah blah blah blah blah blah\"\n"
5114                "                 : \"y\";",
5115                Break);
5116 
5117   // Don't break if there is no column gain.
5118   verifyFormat("f(\"aaaa\"\n"
5119                "  \"bbbb\");",
5120                Break);
5121 
5122   // Treat literals with escaped newlines like multi-line string literals.
5123   EXPECT_EQ("x = \"a\\\n"
5124             "b\\\n"
5125             "c\";",
5126             format("x = \"a\\\n"
5127                    "b\\\n"
5128                    "c\";",
5129                    NoBreak));
5130   EXPECT_EQ("xxxx =\n"
5131             "    \"a\\\n"
5132             "b\\\n"
5133             "c\";",
5134             format("xxxx = \"a\\\n"
5135                    "b\\\n"
5136                    "c\";",
5137                    Break));
5138 
5139   EXPECT_EQ("NSString *const kString =\n"
5140             "    @\"aaaa\"\n"
5141             "    @\"bbbb\";",
5142             format("NSString *const kString = @\"aaaa\"\n"
5143                    "@\"bbbb\";",
5144                    Break));
5145 
5146   Break.ColumnLimit = 0;
5147   verifyFormat("const char *hello = \"hello llvm\";", Break);
5148 }
5149 
5150 TEST_F(FormatTest, AlignsPipes) {
5151   verifyFormat(
5152       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5153       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5154       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5155   verifyFormat(
5156       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5157       "                     << aaaaaaaaaaaaaaaaaaaa;");
5158   verifyFormat(
5159       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5160       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5161   verifyFormat(
5162       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5163       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5164   verifyFormat(
5165       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5166       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5167       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5168   verifyFormat(
5169       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5170       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5171       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5172   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5173                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5174                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5175                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5176   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5177                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5178   verifyFormat(
5179       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5180       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5181   verifyFormat(
5182       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5183       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5184 
5185   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5186                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5187   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5188                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5189                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5190                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5191   verifyFormat("LOG_IF(aaa == //\n"
5192                "       bbb)\n"
5193                "    << a << b;");
5194 
5195   // But sometimes, breaking before the first "<<" is desirable.
5196   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5197                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5198   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5199                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5200                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5201   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5202                "    << BEF << IsTemplate << Description << E->getType();");
5203   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5204                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5205                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5206   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5207                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5208                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5209                "    << aaa;");
5210 
5211   verifyFormat(
5212       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5213       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5214 
5215   // Incomplete string literal.
5216   EXPECT_EQ("llvm::errs() << \"\n"
5217             "             << a;",
5218             format("llvm::errs() << \"\n<<a;"));
5219 
5220   verifyFormat("void f() {\n"
5221                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5222                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5223                "}");
5224 
5225   // Handle 'endl'.
5226   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5227                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5228   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5229 
5230   // Handle '\n'.
5231   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5232                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5233   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5234                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5235   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5236                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5237   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5238 }
5239 
5240 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5241   verifyFormat("return out << \"somepacket = {\\n\"\n"
5242                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5243                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5244                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5245                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5246                "           << \"}\";");
5247 
5248   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5249                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5250                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5251   verifyFormat(
5252       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5253       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5254       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5255       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5256       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5257   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5258                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5259   verifyFormat(
5260       "void f() {\n"
5261       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5262       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5263       "}");
5264 
5265   // Breaking before the first "<<" is generally not desirable.
5266   verifyFormat(
5267       "llvm::errs()\n"
5268       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5269       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5270       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5271       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5272       getLLVMStyleWithColumns(70));
5273   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5274                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5275                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5276                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5277                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5278                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5279                getLLVMStyleWithColumns(70));
5280 
5281   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5282                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5283                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5284   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5285                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5286                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5287   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5288                "           (aaaa + aaaa);",
5289                getLLVMStyleWithColumns(40));
5290   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5291                "                  (aaaaaaa + aaaaa));",
5292                getLLVMStyleWithColumns(40));
5293   verifyFormat(
5294       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5295       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5296       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5297 }
5298 
5299 TEST_F(FormatTest, UnderstandsEquals) {
5300   verifyFormat(
5301       "aaaaaaaaaaaaaaaaa =\n"
5302       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5303   verifyFormat(
5304       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5305       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5306   verifyFormat(
5307       "if (a) {\n"
5308       "  f();\n"
5309       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5310       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5311       "}");
5312 
5313   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5314                "        100000000 + 10000000) {\n}");
5315 }
5316 
5317 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5318   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5319                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5320 
5321   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5322                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5323 
5324   verifyFormat(
5325       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5326       "                                                          Parameter2);");
5327 
5328   verifyFormat(
5329       "ShortObject->shortFunction(\n"
5330       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5331       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5332 
5333   verifyFormat("loooooooooooooongFunction(\n"
5334                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5335 
5336   verifyFormat(
5337       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5338       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5339 
5340   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5341                "    .WillRepeatedly(Return(SomeValue));");
5342   verifyFormat("void f() {\n"
5343                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5344                "      .Times(2)\n"
5345                "      .WillRepeatedly(Return(SomeValue));\n"
5346                "}");
5347   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5348                "    ccccccccccccccccccccccc);");
5349   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5350                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5351                "          .aaaaa(aaaaa),\n"
5352                "      aaaaaaaaaaaaaaaaaaaaa);");
5353   verifyFormat("void f() {\n"
5354                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5355                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5356                "}");
5357   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5358                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5359                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5360                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5361                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5362   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5363                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5364                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5365                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5366                "}");
5367 
5368   // Here, it is not necessary to wrap at "." or "->".
5369   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5370                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5371   verifyFormat(
5372       "aaaaaaaaaaa->aaaaaaaaa(\n"
5373       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5374       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5375 
5376   verifyFormat(
5377       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5378       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5379   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5380                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5381   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5382                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5383 
5384   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5385                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5386                "    .a();");
5387 
5388   FormatStyle NoBinPacking = getLLVMStyle();
5389   NoBinPacking.BinPackParameters = false;
5390   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5391                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5392                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5393                "                         aaaaaaaaaaaaaaaaaaa,\n"
5394                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5395                NoBinPacking);
5396 
5397   // If there is a subsequent call, change to hanging indentation.
5398   verifyFormat(
5399       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5400       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5401       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5402   verifyFormat(
5403       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5404       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5405   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5406                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5407                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5408   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5409                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5410                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5411 }
5412 
5413 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5414   verifyFormat("template <typename T>\n"
5415                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5416   verifyFormat("template <typename T>\n"
5417                "// T should be one of {A, B}.\n"
5418                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5419   verifyFormat(
5420       "template <typename T>\n"
5421       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5422   verifyFormat("template <typename T>\n"
5423                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5424                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5425   verifyFormat(
5426       "template <typename T>\n"
5427       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5428       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5429   verifyFormat(
5430       "template <typename T>\n"
5431       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5432       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5433       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5434   verifyFormat("template <typename T>\n"
5435                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5436                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5437   verifyFormat(
5438       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5439       "          typename T4 = char>\n"
5440       "void f();");
5441   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5442                "          template <typename> class cccccccccccccccccccccc,\n"
5443                "          typename ddddddddddddd>\n"
5444                "class C {};");
5445   verifyFormat(
5446       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5447       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5448 
5449   verifyFormat("void f() {\n"
5450                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5451                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5452                "}");
5453 
5454   verifyFormat("template <typename T> class C {};");
5455   verifyFormat("template <typename T> void f();");
5456   verifyFormat("template <typename T> void f() {}");
5457   verifyFormat(
5458       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5459       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5460       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5461       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5462       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5463       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5464       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5465       getLLVMStyleWithColumns(72));
5466   EXPECT_EQ("static_cast<A< //\n"
5467             "    B> *>(\n"
5468             "\n"
5469             ");",
5470             format("static_cast<A<//\n"
5471                    "    B>*>(\n"
5472                    "\n"
5473                    "    );"));
5474   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5475                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5476 
5477   FormatStyle AlwaysBreak = getLLVMStyle();
5478   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5479   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5480   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5481   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5482   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5483                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5484                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5485   verifyFormat("template <template <typename> class Fooooooo,\n"
5486                "          template <typename> class Baaaaaaar>\n"
5487                "struct C {};",
5488                AlwaysBreak);
5489   verifyFormat("template <typename T> // T can be A, B or C.\n"
5490                "struct C {};",
5491                AlwaysBreak);
5492   verifyFormat("template <enum E> class A {\n"
5493                "public:\n"
5494                "  E *f();\n"
5495                "};");
5496 }
5497 
5498 TEST_F(FormatTest, WrapsTemplateParameters) {
5499   FormatStyle Style = getLLVMStyle();
5500   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5501   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5502   verifyFormat(
5503       "template <typename... a> struct q {};\n"
5504       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5505       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5506       "    y;",
5507       Style);
5508   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5509   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5510   verifyFormat(
5511       "template <typename... a> struct r {};\n"
5512       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5513       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5514       "    y;",
5515       Style);
5516   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5517   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5518   verifyFormat(
5519       "template <typename... a> struct s {};\n"
5520       "extern s<\n"
5521       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5522       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5523       "    y;",
5524       Style);
5525   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5526   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5527   verifyFormat(
5528       "template <typename... a> struct t {};\n"
5529       "extern t<\n"
5530       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5531       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5532       "    y;",
5533       Style);
5534 }
5535 
5536 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5537   verifyFormat(
5538       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5539       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5540   verifyFormat(
5541       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5542       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5543       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5544 
5545   // FIXME: Should we have the extra indent after the second break?
5546   verifyFormat(
5547       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5548       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5549       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5550 
5551   verifyFormat(
5552       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5553       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5554 
5555   // Breaking at nested name specifiers is generally not desirable.
5556   verifyFormat(
5557       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5558       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5559 
5560   verifyFormat(
5561       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5562       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5563       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5564       "                   aaaaaaaaaaaaaaaaaaaaa);",
5565       getLLVMStyleWithColumns(74));
5566 
5567   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5568                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5569                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5570 }
5571 
5572 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5573   verifyFormat("A<int> a;");
5574   verifyFormat("A<A<A<int>>> a;");
5575   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5576   verifyFormat("bool x = a < 1 || 2 > a;");
5577   verifyFormat("bool x = 5 < f<int>();");
5578   verifyFormat("bool x = f<int>() > 5;");
5579   verifyFormat("bool x = 5 < a<int>::x;");
5580   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5581   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5582 
5583   verifyGoogleFormat("A<A<int>> a;");
5584   verifyGoogleFormat("A<A<A<int>>> a;");
5585   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5586   verifyGoogleFormat("A<A<int> > a;");
5587   verifyGoogleFormat("A<A<A<int> > > a;");
5588   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5589   verifyGoogleFormat("A<::A<int>> a;");
5590   verifyGoogleFormat("A<::A> a;");
5591   verifyGoogleFormat("A< ::A> a;");
5592   verifyGoogleFormat("A< ::A<int> > a;");
5593   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5594   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5595   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5596   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5597   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5598             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5599 
5600   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5601 
5602   verifyFormat("test >> a >> b;");
5603   verifyFormat("test << a >> b;");
5604 
5605   verifyFormat("f<int>();");
5606   verifyFormat("template <typename T> void f() {}");
5607   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5608   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5609                "sizeof(char)>::type>;");
5610   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5611   verifyFormat("f(a.operator()<A>());");
5612   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5613                "      .template operator()<A>());",
5614                getLLVMStyleWithColumns(35));
5615 
5616   // Not template parameters.
5617   verifyFormat("return a < b && c > d;");
5618   verifyFormat("void f() {\n"
5619                "  while (a < b && c > d) {\n"
5620                "  }\n"
5621                "}");
5622   verifyFormat("template <typename... Types>\n"
5623                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5624 
5625   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5626                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5627                getLLVMStyleWithColumns(60));
5628   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5629   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5630   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5631 }
5632 
5633 TEST_F(FormatTest, BitshiftOperatorWidth) {
5634   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5635             "                   bar */",
5636             format("int    a=1<<2;  /* foo\n"
5637                    "                   bar */"));
5638 
5639   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5640             "                     bar */",
5641             format("int  b  =256>>1 ;  /* foo\n"
5642                    "                      bar */"));
5643 }
5644 
5645 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5646   verifyFormat("COMPARE(a, ==, b);");
5647   verifyFormat("auto s = sizeof...(Ts) - 1;");
5648 }
5649 
5650 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5651   verifyFormat("int A::*x;");
5652   verifyFormat("int (S::*func)(void *);");
5653   verifyFormat("void f() { int (S::*func)(void *); }");
5654   verifyFormat("typedef bool *(Class::*Member)() const;");
5655   verifyFormat("void f() {\n"
5656                "  (a->*f)();\n"
5657                "  a->*x;\n"
5658                "  (a.*f)();\n"
5659                "  ((*a).*f)();\n"
5660                "  a.*x;\n"
5661                "}");
5662   verifyFormat("void f() {\n"
5663                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5664                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5665                "}");
5666   verifyFormat(
5667       "(aaaaaaaaaa->*bbbbbbb)(\n"
5668       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5669   FormatStyle Style = getLLVMStyle();
5670   Style.PointerAlignment = FormatStyle::PAS_Left;
5671   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5672 }
5673 
5674 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5675   verifyFormat("int a = -2;");
5676   verifyFormat("f(-1, -2, -3);");
5677   verifyFormat("a[-1] = 5;");
5678   verifyFormat("int a = 5 + -2;");
5679   verifyFormat("if (i == -1) {\n}");
5680   verifyFormat("if (i != -1) {\n}");
5681   verifyFormat("if (i > -1) {\n}");
5682   verifyFormat("if (i < -1) {\n}");
5683   verifyFormat("++(a->f());");
5684   verifyFormat("--(a->f());");
5685   verifyFormat("(a->f())++;");
5686   verifyFormat("a[42]++;");
5687   verifyFormat("if (!(a->f())) {\n}");
5688   verifyFormat("if (!+i) {\n}");
5689   verifyFormat("~&a;");
5690 
5691   verifyFormat("a-- > b;");
5692   verifyFormat("b ? -a : c;");
5693   verifyFormat("n * sizeof char16;");
5694   verifyFormat("n * alignof char16;", getGoogleStyle());
5695   verifyFormat("sizeof(char);");
5696   verifyFormat("alignof(char);", getGoogleStyle());
5697 
5698   verifyFormat("return -1;");
5699   verifyFormat("switch (a) {\n"
5700                "case -1:\n"
5701                "  break;\n"
5702                "}");
5703   verifyFormat("#define X -1");
5704   verifyFormat("#define X -kConstant");
5705 
5706   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5707   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5708 
5709   verifyFormat("int a = /* confusing comment */ -1;");
5710   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5711   verifyFormat("int a = i /* confusing comment */++;");
5712 }
5713 
5714 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5715   verifyFormat("if (!aaaaaaaaaa( // break\n"
5716                "        aaaaa)) {\n"
5717                "}");
5718   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5719                "    aaaaa));");
5720   verifyFormat("*aaa = aaaaaaa( // break\n"
5721                "    bbbbbb);");
5722 }
5723 
5724 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5725   verifyFormat("bool operator<();");
5726   verifyFormat("bool operator>();");
5727   verifyFormat("bool operator=();");
5728   verifyFormat("bool operator==();");
5729   verifyFormat("bool operator!=();");
5730   verifyFormat("int operator+();");
5731   verifyFormat("int operator++();");
5732   verifyFormat("int operator++(int) volatile noexcept;");
5733   verifyFormat("bool operator,();");
5734   verifyFormat("bool operator();");
5735   verifyFormat("bool operator()();");
5736   verifyFormat("bool operator[]();");
5737   verifyFormat("operator bool();");
5738   verifyFormat("operator int();");
5739   verifyFormat("operator void *();");
5740   verifyFormat("operator SomeType<int>();");
5741   verifyFormat("operator SomeType<int, int>();");
5742   verifyFormat("operator SomeType<SomeType<int>>();");
5743   verifyFormat("void *operator new(std::size_t size);");
5744   verifyFormat("void *operator new[](std::size_t size);");
5745   verifyFormat("void operator delete(void *ptr);");
5746   verifyFormat("void operator delete[](void *ptr);");
5747   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5748                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5749   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5750                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5751 
5752   verifyFormat(
5753       "ostream &operator<<(ostream &OutputStream,\n"
5754       "                    SomeReallyLongType WithSomeReallyLongValue);");
5755   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5756                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5757                "  return left.group < right.group;\n"
5758                "}");
5759   verifyFormat("SomeType &operator=(const SomeType &S);");
5760   verifyFormat("f.template operator()<int>();");
5761 
5762   verifyGoogleFormat("operator void*();");
5763   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5764   verifyGoogleFormat("operator ::A();");
5765 
5766   verifyFormat("using A::operator+;");
5767   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5768                "int i;");
5769 }
5770 
5771 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5772   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5773   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5774   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5775   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5776   verifyFormat("Deleted &operator=(const Deleted &) &;");
5777   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5778   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5779   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5780   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5781   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5782   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5783   verifyFormat("void Fn(T const &) const &;");
5784   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5785   verifyFormat("template <typename T>\n"
5786                "void F(T) && = delete;",
5787                getGoogleStyle());
5788 
5789   FormatStyle AlignLeft = getLLVMStyle();
5790   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5791   verifyFormat("void A::b() && {}", AlignLeft);
5792   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5793   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5794                AlignLeft);
5795   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5796   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5797   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5798   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5799   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5800   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5801   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5802   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5803 
5804   FormatStyle Spaces = getLLVMStyle();
5805   Spaces.SpacesInCStyleCastParentheses = true;
5806   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5807   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5808   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5809   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5810 
5811   Spaces.SpacesInCStyleCastParentheses = false;
5812   Spaces.SpacesInParentheses = true;
5813   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5814   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5815   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5816   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5817 }
5818 
5819 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5820   verifyFormat("void f() {\n"
5821                "  A *a = new A;\n"
5822                "  A *a = new (placement) A;\n"
5823                "  delete a;\n"
5824                "  delete (A *)a;\n"
5825                "}");
5826   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5827                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5828   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5829                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5830                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5831   verifyFormat("delete[] h->p;");
5832 }
5833 
5834 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5835   verifyFormat("int *f(int *a) {}");
5836   verifyFormat("int main(int argc, char **argv) {}");
5837   verifyFormat("Test::Test(int b) : a(b * b) {}");
5838   verifyIndependentOfContext("f(a, *a);");
5839   verifyFormat("void g() { f(*a); }");
5840   verifyIndependentOfContext("int a = b * 10;");
5841   verifyIndependentOfContext("int a = 10 * b;");
5842   verifyIndependentOfContext("int a = b * c;");
5843   verifyIndependentOfContext("int a += b * c;");
5844   verifyIndependentOfContext("int a -= b * c;");
5845   verifyIndependentOfContext("int a *= b * c;");
5846   verifyIndependentOfContext("int a /= b * c;");
5847   verifyIndependentOfContext("int a = *b;");
5848   verifyIndependentOfContext("int a = *b * c;");
5849   verifyIndependentOfContext("int a = b * *c;");
5850   verifyIndependentOfContext("int a = b * (10);");
5851   verifyIndependentOfContext("S << b * (10);");
5852   verifyIndependentOfContext("return 10 * b;");
5853   verifyIndependentOfContext("return *b * *c;");
5854   verifyIndependentOfContext("return a & ~b;");
5855   verifyIndependentOfContext("f(b ? *c : *d);");
5856   verifyIndependentOfContext("int a = b ? *c : *d;");
5857   verifyIndependentOfContext("*b = a;");
5858   verifyIndependentOfContext("a * ~b;");
5859   verifyIndependentOfContext("a * !b;");
5860   verifyIndependentOfContext("a * +b;");
5861   verifyIndependentOfContext("a * -b;");
5862   verifyIndependentOfContext("a * ++b;");
5863   verifyIndependentOfContext("a * --b;");
5864   verifyIndependentOfContext("a[4] * b;");
5865   verifyIndependentOfContext("a[a * a] = 1;");
5866   verifyIndependentOfContext("f() * b;");
5867   verifyIndependentOfContext("a * [self dostuff];");
5868   verifyIndependentOfContext("int x = a * (a + b);");
5869   verifyIndependentOfContext("(a *)(a + b);");
5870   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5871   verifyIndependentOfContext("int *pa = (int *)&a;");
5872   verifyIndependentOfContext("return sizeof(int **);");
5873   verifyIndependentOfContext("return sizeof(int ******);");
5874   verifyIndependentOfContext("return (int **&)a;");
5875   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5876   verifyFormat("void f(Type (*parameter)[10]) {}");
5877   verifyFormat("void f(Type (&parameter)[10]) {}");
5878   verifyGoogleFormat("return sizeof(int**);");
5879   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5880   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5881   verifyFormat("auto a = [](int **&, int ***) {};");
5882   verifyFormat("auto PointerBinding = [](const char *S) {};");
5883   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5884   verifyFormat("[](const decltype(*a) &value) {}");
5885   verifyFormat("decltype(a * b) F();");
5886   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5887   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5888   verifyIndependentOfContext("typedef void (*f)(int *a);");
5889   verifyIndependentOfContext("int i{a * b};");
5890   verifyIndependentOfContext("aaa && aaa->f();");
5891   verifyIndependentOfContext("int x = ~*p;");
5892   verifyFormat("Constructor() : a(a), area(width * height) {}");
5893   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5894   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5895   verifyFormat("void f() { f(a, c * d); }");
5896   verifyFormat("void f() { f(new a(), c * d); }");
5897   verifyFormat("void f(const MyOverride &override);");
5898   verifyFormat("void f(const MyFinal &final);");
5899   verifyIndependentOfContext("bool a = f() && override.f();");
5900   verifyIndependentOfContext("bool a = f() && final.f();");
5901 
5902   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5903 
5904   verifyIndependentOfContext("A<int *> a;");
5905   verifyIndependentOfContext("A<int **> a;");
5906   verifyIndependentOfContext("A<int *, int *> a;");
5907   verifyIndependentOfContext("A<int *[]> a;");
5908   verifyIndependentOfContext(
5909       "const char *const p = reinterpret_cast<const char *const>(q);");
5910   verifyIndependentOfContext("A<int **, int **> a;");
5911   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5912   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5913   verifyFormat("for (; a && b;) {\n}");
5914   verifyFormat("bool foo = true && [] { return false; }();");
5915 
5916   verifyFormat(
5917       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5918       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5919 
5920   verifyGoogleFormat("int const* a = &b;");
5921   verifyGoogleFormat("**outparam = 1;");
5922   verifyGoogleFormat("*outparam = a * b;");
5923   verifyGoogleFormat("int main(int argc, char** argv) {}");
5924   verifyGoogleFormat("A<int*> a;");
5925   verifyGoogleFormat("A<int**> a;");
5926   verifyGoogleFormat("A<int*, int*> a;");
5927   verifyGoogleFormat("A<int**, int**> a;");
5928   verifyGoogleFormat("f(b ? *c : *d);");
5929   verifyGoogleFormat("int a = b ? *c : *d;");
5930   verifyGoogleFormat("Type* t = **x;");
5931   verifyGoogleFormat("Type* t = *++*x;");
5932   verifyGoogleFormat("*++*x;");
5933   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5934   verifyGoogleFormat("Type* t = x++ * y;");
5935   verifyGoogleFormat(
5936       "const char* const p = reinterpret_cast<const char* const>(q);");
5937   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5938   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5939   verifyGoogleFormat("template <typename T>\n"
5940                      "void f(int i = 0, SomeType** temps = NULL);");
5941 
5942   FormatStyle Left = getLLVMStyle();
5943   Left.PointerAlignment = FormatStyle::PAS_Left;
5944   verifyFormat("x = *a(x) = *a(y);", Left);
5945   verifyFormat("for (;; *a = b) {\n}", Left);
5946   verifyFormat("return *this += 1;", Left);
5947   verifyFormat("throw *x;", Left);
5948   verifyFormat("delete *x;", Left);
5949   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
5950   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
5951   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
5952 
5953   verifyIndependentOfContext("a = *(x + y);");
5954   verifyIndependentOfContext("a = &(x + y);");
5955   verifyIndependentOfContext("*(x + y).call();");
5956   verifyIndependentOfContext("&(x + y)->call();");
5957   verifyFormat("void f() { &(*I).first; }");
5958 
5959   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5960   verifyFormat(
5961       "int *MyValues = {\n"
5962       "    *A, // Operator detection might be confused by the '{'\n"
5963       "    *BB // Operator detection might be confused by previous comment\n"
5964       "};");
5965 
5966   verifyIndependentOfContext("if (int *a = &b)");
5967   verifyIndependentOfContext("if (int &a = *b)");
5968   verifyIndependentOfContext("if (a & b[i])");
5969   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5970   verifyIndependentOfContext("if (*b[i])");
5971   verifyIndependentOfContext("if (int *a = (&b))");
5972   verifyIndependentOfContext("while (int *a = &b)");
5973   verifyIndependentOfContext("size = sizeof *a;");
5974   verifyIndependentOfContext("if (a && (b = c))");
5975   verifyFormat("void f() {\n"
5976                "  for (const int &v : Values) {\n"
5977                "  }\n"
5978                "}");
5979   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5980   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5981   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5982 
5983   verifyFormat("#define A (!a * b)");
5984   verifyFormat("#define MACRO     \\\n"
5985                "  int *i = a * b; \\\n"
5986                "  void f(a *b);",
5987                getLLVMStyleWithColumns(19));
5988 
5989   verifyIndependentOfContext("A = new SomeType *[Length];");
5990   verifyIndependentOfContext("A = new SomeType *[Length]();");
5991   verifyIndependentOfContext("T **t = new T *;");
5992   verifyIndependentOfContext("T **t = new T *();");
5993   verifyGoogleFormat("A = new SomeType*[Length]();");
5994   verifyGoogleFormat("A = new SomeType*[Length];");
5995   verifyGoogleFormat("T** t = new T*;");
5996   verifyGoogleFormat("T** t = new T*();");
5997 
5998   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5999   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6000   verifyFormat("template <bool a, bool b> "
6001                "typename t::if<x && y>::type f() {}");
6002   verifyFormat("template <int *y> f() {}");
6003   verifyFormat("vector<int *> v;");
6004   verifyFormat("vector<int *const> v;");
6005   verifyFormat("vector<int *const **const *> v;");
6006   verifyFormat("vector<int *volatile> v;");
6007   verifyFormat("vector<a * b> v;");
6008   verifyFormat("foo<b && false>();");
6009   verifyFormat("foo<b & 1>();");
6010   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6011   verifyFormat(
6012       "template <class T, class = typename std::enable_if<\n"
6013       "                       std::is_integral<T>::value &&\n"
6014       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6015       "void F();",
6016       getLLVMStyleWithColumns(70));
6017   verifyFormat(
6018       "template <class T,\n"
6019       "          class = typename std::enable_if<\n"
6020       "              std::is_integral<T>::value &&\n"
6021       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6022       "          class U>\n"
6023       "void F();",
6024       getLLVMStyleWithColumns(70));
6025   verifyFormat(
6026       "template <class T,\n"
6027       "          class = typename ::std::enable_if<\n"
6028       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6029       "void F();",
6030       getGoogleStyleWithColumns(68));
6031 
6032   verifyIndependentOfContext("MACRO(int *i);");
6033   verifyIndependentOfContext("MACRO(auto *a);");
6034   verifyIndependentOfContext("MACRO(const A *a);");
6035   verifyIndependentOfContext("MACRO(A *const a);");
6036   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6037   verifyFormat("void f() { f(float{1}, a * a); }");
6038   // FIXME: Is there a way to make this work?
6039   // verifyIndependentOfContext("MACRO(A *a);");
6040 
6041   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6042   verifyFormat("return options != nullptr && operator==(*options);");
6043 
6044   EXPECT_EQ("#define OP(x)                                    \\\n"
6045             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6046             "    return s << a.DebugString();                 \\\n"
6047             "  }",
6048             format("#define OP(x) \\\n"
6049                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6050                    "    return s << a.DebugString(); \\\n"
6051                    "  }",
6052                    getLLVMStyleWithColumns(50)));
6053 
6054   // FIXME: We cannot handle this case yet; we might be able to figure out that
6055   // foo<x> d > v; doesn't make sense.
6056   verifyFormat("foo<a<b && c> d> v;");
6057 
6058   FormatStyle PointerMiddle = getLLVMStyle();
6059   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6060   verifyFormat("delete *x;", PointerMiddle);
6061   verifyFormat("int * x;", PointerMiddle);
6062   verifyFormat("int *[] x;", PointerMiddle);
6063   verifyFormat("template <int * y> f() {}", PointerMiddle);
6064   verifyFormat("int * f(int * a) {}", PointerMiddle);
6065   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6066   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6067   verifyFormat("A<int *> a;", PointerMiddle);
6068   verifyFormat("A<int **> a;", PointerMiddle);
6069   verifyFormat("A<int *, int *> a;", PointerMiddle);
6070   verifyFormat("A<int *[]> a;", PointerMiddle);
6071   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6072   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6073   verifyFormat("T ** t = new T *;", PointerMiddle);
6074 
6075   // Member function reference qualifiers aren't binary operators.
6076   verifyFormat("string // break\n"
6077                "operator()() & {}");
6078   verifyFormat("string // break\n"
6079                "operator()() && {}");
6080   verifyGoogleFormat("template <typename T>\n"
6081                      "auto x() & -> int {}");
6082 }
6083 
6084 TEST_F(FormatTest, UnderstandsAttributes) {
6085   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6086   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6087                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6088   FormatStyle AfterType = getLLVMStyle();
6089   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6090   verifyFormat("__attribute__((nodebug)) void\n"
6091                "foo() {}\n",
6092                AfterType);
6093 }
6094 
6095 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6096   verifyFormat("SomeType s [[unused]] (InitValue);");
6097   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6098   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6099   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6100   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6101   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6102                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6103 
6104   // Make sure we do not mistake attributes for array subscripts.
6105   verifyFormat("int a() {}\n"
6106                "[[unused]] int b() {}\n");
6107 
6108   // On the other hand, we still need to correctly find array subscripts.
6109   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6110 
6111   // Make sure we do not parse attributes as lambda introducers.
6112   FormatStyle MultiLineFunctions = getLLVMStyle();
6113   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6114   verifyFormat("[[unused]] int b() {\n"
6115                "  return 42;\n"
6116                "}\n",
6117                MultiLineFunctions);
6118 }
6119 
6120 TEST_F(FormatTest, UnderstandsEllipsis) {
6121   verifyFormat("int printf(const char *fmt, ...);");
6122   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6123   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6124 
6125   FormatStyle PointersLeft = getLLVMStyle();
6126   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6127   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6128 }
6129 
6130 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6131   EXPECT_EQ("int *a;\n"
6132             "int *a;\n"
6133             "int *a;",
6134             format("int *a;\n"
6135                    "int* a;\n"
6136                    "int *a;",
6137                    getGoogleStyle()));
6138   EXPECT_EQ("int* a;\n"
6139             "int* a;\n"
6140             "int* a;",
6141             format("int* a;\n"
6142                    "int* a;\n"
6143                    "int *a;",
6144                    getGoogleStyle()));
6145   EXPECT_EQ("int *a;\n"
6146             "int *a;\n"
6147             "int *a;",
6148             format("int *a;\n"
6149                    "int * a;\n"
6150                    "int *  a;",
6151                    getGoogleStyle()));
6152   EXPECT_EQ("auto x = [] {\n"
6153             "  int *a;\n"
6154             "  int *a;\n"
6155             "  int *a;\n"
6156             "};",
6157             format("auto x=[]{int *a;\n"
6158                    "int * a;\n"
6159                    "int *  a;};",
6160                    getGoogleStyle()));
6161 }
6162 
6163 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6164   verifyFormat("int f(int &&a) {}");
6165   verifyFormat("int f(int a, char &&b) {}");
6166   verifyFormat("void f() { int &&a = b; }");
6167   verifyGoogleFormat("int f(int a, char&& b) {}");
6168   verifyGoogleFormat("void f() { int&& a = b; }");
6169 
6170   verifyIndependentOfContext("A<int &&> a;");
6171   verifyIndependentOfContext("A<int &&, int &&> a;");
6172   verifyGoogleFormat("A<int&&> a;");
6173   verifyGoogleFormat("A<int&&, int&&> a;");
6174 
6175   // Not rvalue references:
6176   verifyFormat("template <bool B, bool C> class A {\n"
6177                "  static_assert(B && C, \"Something is wrong\");\n"
6178                "};");
6179   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6180   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6181   verifyFormat("#define A(a, b) (a && b)");
6182 }
6183 
6184 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6185   verifyFormat("void f() {\n"
6186                "  x[aaaaaaaaa -\n"
6187                "    b] = 23;\n"
6188                "}",
6189                getLLVMStyleWithColumns(15));
6190 }
6191 
6192 TEST_F(FormatTest, FormatsCasts) {
6193   verifyFormat("Type *A = static_cast<Type *>(P);");
6194   verifyFormat("Type *A = (Type *)P;");
6195   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6196   verifyFormat("int a = (int)(2.0f);");
6197   verifyFormat("int a = (int)2.0f;");
6198   verifyFormat("x[(int32)y];");
6199   verifyFormat("x = (int32)y;");
6200   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6201   verifyFormat("int a = (int)*b;");
6202   verifyFormat("int a = (int)2.0f;");
6203   verifyFormat("int a = (int)~0;");
6204   verifyFormat("int a = (int)++a;");
6205   verifyFormat("int a = (int)sizeof(int);");
6206   verifyFormat("int a = (int)+2;");
6207   verifyFormat("my_int a = (my_int)2.0f;");
6208   verifyFormat("my_int a = (my_int)sizeof(int);");
6209   verifyFormat("return (my_int)aaa;");
6210   verifyFormat("#define x ((int)-1)");
6211   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6212   verifyFormat("#define p(q) ((int *)&q)");
6213   verifyFormat("fn(a)(b) + 1;");
6214 
6215   verifyFormat("void f() { my_int a = (my_int)*b; }");
6216   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6217   verifyFormat("my_int a = (my_int)~0;");
6218   verifyFormat("my_int a = (my_int)++a;");
6219   verifyFormat("my_int a = (my_int)-2;");
6220   verifyFormat("my_int a = (my_int)1;");
6221   verifyFormat("my_int a = (my_int *)1;");
6222   verifyFormat("my_int a = (const my_int)-1;");
6223   verifyFormat("my_int a = (const my_int *)-1;");
6224   verifyFormat("my_int a = (my_int)(my_int)-1;");
6225   verifyFormat("my_int a = (ns::my_int)-2;");
6226   verifyFormat("case (my_int)ONE:");
6227   verifyFormat("auto x = (X)this;");
6228 
6229   // FIXME: single value wrapped with paren will be treated as cast.
6230   verifyFormat("void f(int i = (kValue)*kMask) {}");
6231 
6232   verifyFormat("{ (void)F; }");
6233 
6234   // Don't break after a cast's
6235   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6236                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6237                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6238 
6239   // These are not casts.
6240   verifyFormat("void f(int *) {}");
6241   verifyFormat("f(foo)->b;");
6242   verifyFormat("f(foo).b;");
6243   verifyFormat("f(foo)(b);");
6244   verifyFormat("f(foo)[b];");
6245   verifyFormat("[](foo) { return 4; }(bar);");
6246   verifyFormat("(*funptr)(foo)[4];");
6247   verifyFormat("funptrs[4](foo)[4];");
6248   verifyFormat("void f(int *);");
6249   verifyFormat("void f(int *) = 0;");
6250   verifyFormat("void f(SmallVector<int>) {}");
6251   verifyFormat("void f(SmallVector<int>);");
6252   verifyFormat("void f(SmallVector<int>) = 0;");
6253   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6254   verifyFormat("int a = sizeof(int) * b;");
6255   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6256   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6257   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6258   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6259 
6260   // These are not casts, but at some point were confused with casts.
6261   verifyFormat("virtual void foo(int *) override;");
6262   verifyFormat("virtual void foo(char &) const;");
6263   verifyFormat("virtual void foo(int *a, char *) const;");
6264   verifyFormat("int a = sizeof(int *) + b;");
6265   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6266   verifyFormat("bool b = f(g<int>) && c;");
6267   verifyFormat("typedef void (*f)(int i) func;");
6268 
6269   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6270                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6271   // FIXME: The indentation here is not ideal.
6272   verifyFormat(
6273       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6274       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6275       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6276 }
6277 
6278 TEST_F(FormatTest, FormatsFunctionTypes) {
6279   verifyFormat("A<bool()> a;");
6280   verifyFormat("A<SomeType()> a;");
6281   verifyFormat("A<void (*)(int, std::string)> a;");
6282   verifyFormat("A<void *(int)>;");
6283   verifyFormat("void *(*a)(int *, SomeType *);");
6284   verifyFormat("int (*func)(void *);");
6285   verifyFormat("void f() { int (*func)(void *); }");
6286   verifyFormat("template <class CallbackClass>\n"
6287                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6288 
6289   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6290   verifyGoogleFormat("void* (*a)(int);");
6291   verifyGoogleFormat(
6292       "template <class CallbackClass>\n"
6293       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6294 
6295   // Other constructs can look somewhat like function types:
6296   verifyFormat("A<sizeof(*x)> a;");
6297   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6298   verifyFormat("some_var = function(*some_pointer_var)[0];");
6299   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6300   verifyFormat("int x = f(&h)();");
6301   verifyFormat("returnsFunction(&param1, &param2)(param);");
6302   verifyFormat("std::function<\n"
6303                "    LooooooooooongTemplatedType<\n"
6304                "        SomeType>*(\n"
6305                "        LooooooooooooooooongType type)>\n"
6306                "    function;",
6307                getGoogleStyleWithColumns(40));
6308 }
6309 
6310 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6311   verifyFormat("A (*foo_)[6];");
6312   verifyFormat("vector<int> (*foo_)[6];");
6313 }
6314 
6315 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6316   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6317                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6318   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6319                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6320   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6321                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6322 
6323   // Different ways of ()-initializiation.
6324   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6325                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6326   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6327                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6328   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6329                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6330   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6331                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6332 
6333   // Lambdas should not confuse the variable declaration heuristic.
6334   verifyFormat("LooooooooooooooooongType\n"
6335                "    variable(nullptr, [](A *a) {});",
6336                getLLVMStyleWithColumns(40));
6337 }
6338 
6339 TEST_F(FormatTest, BreaksLongDeclarations) {
6340   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6341                "    AnotherNameForTheLongType;");
6342   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6343                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6344   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6345                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6346   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6347                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6348   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6349                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6350   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6351                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6352   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6353                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6354   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6355                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6356   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6357                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6358   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6359                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6360   FormatStyle Indented = getLLVMStyle();
6361   Indented.IndentWrappedFunctionNames = true;
6362   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6363                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6364                Indented);
6365   verifyFormat(
6366       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6367       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6368       Indented);
6369   verifyFormat(
6370       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6371       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6372       Indented);
6373   verifyFormat(
6374       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6375       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6376       Indented);
6377 
6378   // FIXME: Without the comment, this breaks after "(".
6379   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6380                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6381                getGoogleStyle());
6382 
6383   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6384                "                  int LoooooooooooooooooooongParam2) {}");
6385   verifyFormat(
6386       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6387       "                                   SourceLocation L, IdentifierIn *II,\n"
6388       "                                   Type *T) {}");
6389   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6390                "ReallyReaaallyLongFunctionName(\n"
6391                "    const std::string &SomeParameter,\n"
6392                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6393                "        &ReallyReallyLongParameterName,\n"
6394                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6395                "        &AnotherLongParameterName) {}");
6396   verifyFormat("template <typename A>\n"
6397                "SomeLoooooooooooooooooooooongType<\n"
6398                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6399                "Function() {}");
6400 
6401   verifyGoogleFormat(
6402       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6403       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6404   verifyGoogleFormat(
6405       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6406       "                                   SourceLocation L) {}");
6407   verifyGoogleFormat(
6408       "some_namespace::LongReturnType\n"
6409       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6410       "    int first_long_parameter, int second_parameter) {}");
6411 
6412   verifyGoogleFormat("template <typename T>\n"
6413                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6414                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6415   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6416                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6417 
6418   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6419                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6420                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6421   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6422                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6423                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6424   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6425                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6426                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6427                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6428 
6429   verifyFormat("template <typename T> // Templates on own line.\n"
6430                "static int            // Some comment.\n"
6431                "MyFunction(int a);",
6432                getLLVMStyle());
6433 }
6434 
6435 TEST_F(FormatTest, FormatsArrays) {
6436   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6437                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6438   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6439                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6440   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6441                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6442   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6443                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6444   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6445                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6446   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6447                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6448                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6449   verifyFormat(
6450       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6451       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6452       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6453   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6454                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6455 
6456   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6457                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6458   verifyFormat(
6459       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6460       "                                  .aaaaaaa[0]\n"
6461       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6462   verifyFormat("a[::b::c];");
6463 
6464   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6465 
6466   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6467   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6468 }
6469 
6470 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6471   verifyFormat("(a)->b();");
6472   verifyFormat("--a;");
6473 }
6474 
6475 TEST_F(FormatTest, HandlesIncludeDirectives) {
6476   verifyFormat("#include <string>\n"
6477                "#include <a/b/c.h>\n"
6478                "#include \"a/b/string\"\n"
6479                "#include \"string.h\"\n"
6480                "#include \"string.h\"\n"
6481                "#include <a-a>\n"
6482                "#include < path with space >\n"
6483                "#include_next <test.h>"
6484                "#include \"abc.h\" // this is included for ABC\n"
6485                "#include \"some long include\" // with a comment\n"
6486                "#include \"some very long include path\"\n"
6487                "#include <some/very/long/include/path>\n",
6488                getLLVMStyleWithColumns(35));
6489   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6490   EXPECT_EQ("#include <a>", format("#include<a>"));
6491 
6492   verifyFormat("#import <string>");
6493   verifyFormat("#import <a/b/c.h>");
6494   verifyFormat("#import \"a/b/string\"");
6495   verifyFormat("#import \"string.h\"");
6496   verifyFormat("#import \"string.h\"");
6497   verifyFormat("#if __has_include(<strstream>)\n"
6498                "#include <strstream>\n"
6499                "#endif");
6500 
6501   verifyFormat("#define MY_IMPORT <a/b>");
6502 
6503   verifyFormat("#if __has_include(<a/b>)");
6504   verifyFormat("#if __has_include_next(<a/b>)");
6505   verifyFormat("#define F __has_include(<a/b>)");
6506   verifyFormat("#define F __has_include_next(<a/b>)");
6507 
6508   // Protocol buffer definition or missing "#".
6509   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6510                getLLVMStyleWithColumns(30));
6511 
6512   FormatStyle Style = getLLVMStyle();
6513   Style.AlwaysBreakBeforeMultilineStrings = true;
6514   Style.ColumnLimit = 0;
6515   verifyFormat("#import \"abc.h\"", Style);
6516 
6517   // But 'import' might also be a regular C++ namespace.
6518   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6519                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6520 }
6521 
6522 //===----------------------------------------------------------------------===//
6523 // Error recovery tests.
6524 //===----------------------------------------------------------------------===//
6525 
6526 TEST_F(FormatTest, IncompleteParameterLists) {
6527   FormatStyle NoBinPacking = getLLVMStyle();
6528   NoBinPacking.BinPackParameters = false;
6529   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6530                "                        double *min_x,\n"
6531                "                        double *max_x,\n"
6532                "                        double *min_y,\n"
6533                "                        double *max_y,\n"
6534                "                        double *min_z,\n"
6535                "                        double *max_z, ) {}",
6536                NoBinPacking);
6537 }
6538 
6539 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6540   verifyFormat("void f() { return; }\n42");
6541   verifyFormat("void f() {\n"
6542                "  if (0)\n"
6543                "    return;\n"
6544                "}\n"
6545                "42");
6546   verifyFormat("void f() { return }\n42");
6547   verifyFormat("void f() {\n"
6548                "  if (0)\n"
6549                "    return\n"
6550                "}\n"
6551                "42");
6552 }
6553 
6554 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6555   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6556   EXPECT_EQ("void f() {\n"
6557             "  if (a)\n"
6558             "    return\n"
6559             "}",
6560             format("void  f  (  )  {  if  ( a )  return  }"));
6561   EXPECT_EQ("namespace N {\n"
6562             "void f()\n"
6563             "}",
6564             format("namespace  N  {  void f()  }"));
6565   EXPECT_EQ("namespace N {\n"
6566             "void f() {}\n"
6567             "void g()\n"
6568             "} // namespace N",
6569             format("namespace N  { void f( ) { } void g( ) }"));
6570 }
6571 
6572 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6573   verifyFormat("int aaaaaaaa =\n"
6574                "    // Overlylongcomment\n"
6575                "    b;",
6576                getLLVMStyleWithColumns(20));
6577   verifyFormat("function(\n"
6578                "    ShortArgument,\n"
6579                "    LoooooooooooongArgument);\n",
6580                getLLVMStyleWithColumns(20));
6581 }
6582 
6583 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6584   verifyFormat("public:");
6585   verifyFormat("class A {\n"
6586                "public\n"
6587                "  void f() {}\n"
6588                "};");
6589   verifyFormat("public\n"
6590                "int qwerty;");
6591   verifyFormat("public\n"
6592                "B {}");
6593   verifyFormat("public\n"
6594                "{}");
6595   verifyFormat("public\n"
6596                "B { int x; }");
6597 }
6598 
6599 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6600   verifyFormat("{");
6601   verifyFormat("#})");
6602   verifyNoCrash("(/**/[:!] ?[).");
6603 }
6604 
6605 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6606   verifyFormat("do {\n}");
6607   verifyFormat("do {\n}\n"
6608                "f();");
6609   verifyFormat("do {\n}\n"
6610                "wheeee(fun);");
6611   verifyFormat("do {\n"
6612                "  f();\n"
6613                "}");
6614 }
6615 
6616 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6617   verifyFormat("if {\n  foo;\n  foo();\n}");
6618   verifyFormat("switch {\n  foo;\n  foo();\n}");
6619   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6620   verifyFormat("while {\n  foo;\n  foo();\n}");
6621   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6622 }
6623 
6624 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6625   verifyIncompleteFormat("namespace {\n"
6626                          "class Foo { Foo (\n"
6627                          "};\n"
6628                          "} // namespace");
6629 }
6630 
6631 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6632   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6633   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6634   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6635   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6636 
6637   EXPECT_EQ("{\n"
6638             "  {\n"
6639             "    breakme(\n"
6640             "        qwe);\n"
6641             "  }\n",
6642             format("{\n"
6643                    "    {\n"
6644                    " breakme(qwe);\n"
6645                    "}\n",
6646                    getLLVMStyleWithColumns(10)));
6647 }
6648 
6649 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6650   verifyFormat("int x = {\n"
6651                "    avariable,\n"
6652                "    b(alongervariable)};",
6653                getLLVMStyleWithColumns(25));
6654 }
6655 
6656 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6657   verifyFormat("return (a)(b){1, 2, 3};");
6658 }
6659 
6660 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6661   verifyFormat("vector<int> x{1, 2, 3, 4};");
6662   verifyFormat("vector<int> x{\n"
6663                "    1,\n"
6664                "    2,\n"
6665                "    3,\n"
6666                "    4,\n"
6667                "};");
6668   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6669   verifyFormat("f({1, 2});");
6670   verifyFormat("auto v = Foo{-1};");
6671   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6672   verifyFormat("Class::Class : member{1, 2, 3} {}");
6673   verifyFormat("new vector<int>{1, 2, 3};");
6674   verifyFormat("new int[3]{1, 2, 3};");
6675   verifyFormat("new int{1};");
6676   verifyFormat("return {arg1, arg2};");
6677   verifyFormat("return {arg1, SomeType{parameter}};");
6678   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6679   verifyFormat("new T{arg1, arg2};");
6680   verifyFormat("f(MyMap[{composite, key}]);");
6681   verifyFormat("class Class {\n"
6682                "  T member = {arg1, arg2};\n"
6683                "};");
6684   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6685   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6686   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6687   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6688   verifyFormat("int a = std::is_integral<int>{} + 0;");
6689 
6690   verifyFormat("int foo(int i) { return fo1{}(i); }");
6691   verifyFormat("int foo(int i) { return fo1{}(i); }");
6692   verifyFormat("auto i = decltype(x){};");
6693   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6694   verifyFormat("Node n{1, Node{1000}, //\n"
6695                "       2};");
6696   verifyFormat("Aaaa aaaaaaa{\n"
6697                "    {\n"
6698                "        aaaa,\n"
6699                "    },\n"
6700                "};");
6701   verifyFormat("class C : public D {\n"
6702                "  SomeClass SC{2};\n"
6703                "};");
6704   verifyFormat("class C : public A {\n"
6705                "  class D : public B {\n"
6706                "    void f() { int i{2}; }\n"
6707                "  };\n"
6708                "};");
6709   verifyFormat("#define A {a, a},");
6710 
6711   // Binpacking only if there is no trailing comma
6712   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6713                "                      cccccccccc, dddddddddd};",
6714 			   getLLVMStyleWithColumns(50));
6715   verifyFormat("const Aaaaaa aaaaa = {\n"
6716                "    aaaaaaaaaaa,\n"
6717                "    bbbbbbbbbbb,\n"
6718                "    ccccccccccc,\n"
6719                "    ddddddddddd,\n"
6720                "};", getLLVMStyleWithColumns(50));
6721 
6722   // Cases where distinguising braced lists and blocks is hard.
6723   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6724   verifyFormat("void f() {\n"
6725                "  return; // comment\n"
6726                "}\n"
6727                "SomeType t;");
6728   verifyFormat("void f() {\n"
6729                "  if (a) {\n"
6730                "    f();\n"
6731                "  }\n"
6732                "}\n"
6733                "SomeType t;");
6734 
6735   // In combination with BinPackArguments = false.
6736   FormatStyle NoBinPacking = getLLVMStyle();
6737   NoBinPacking.BinPackArguments = false;
6738   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6739                "                      bbbbb,\n"
6740                "                      ccccc,\n"
6741                "                      ddddd,\n"
6742                "                      eeeee,\n"
6743                "                      ffffff,\n"
6744                "                      ggggg,\n"
6745                "                      hhhhhh,\n"
6746                "                      iiiiii,\n"
6747                "                      jjjjjj,\n"
6748                "                      kkkkkk};",
6749                NoBinPacking);
6750   verifyFormat("const Aaaaaa aaaaa = {\n"
6751                "    aaaaa,\n"
6752                "    bbbbb,\n"
6753                "    ccccc,\n"
6754                "    ddddd,\n"
6755                "    eeeee,\n"
6756                "    ffffff,\n"
6757                "    ggggg,\n"
6758                "    hhhhhh,\n"
6759                "    iiiiii,\n"
6760                "    jjjjjj,\n"
6761                "    kkkkkk,\n"
6762                "};",
6763                NoBinPacking);
6764   verifyFormat(
6765       "const Aaaaaa aaaaa = {\n"
6766       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6767       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6768       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6769       "};",
6770       NoBinPacking);
6771 
6772   // FIXME: The alignment of these trailing comments might be bad. Then again,
6773   // this might be utterly useless in real code.
6774   verifyFormat("Constructor::Constructor()\n"
6775                "    : some_value{         //\n"
6776                "                 aaaaaaa, //\n"
6777                "                 bbbbbbb} {}");
6778 
6779   // In braced lists, the first comment is always assumed to belong to the
6780   // first element. Thus, it can be moved to the next or previous line as
6781   // appropriate.
6782   EXPECT_EQ("function({// First element:\n"
6783             "          1,\n"
6784             "          // Second element:\n"
6785             "          2});",
6786             format("function({\n"
6787                    "    // First element:\n"
6788                    "    1,\n"
6789                    "    // Second element:\n"
6790                    "    2});"));
6791   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6792             "    // First element:\n"
6793             "    1,\n"
6794             "    // Second element:\n"
6795             "    2};",
6796             format("std::vector<int> MyNumbers{// First element:\n"
6797                    "                           1,\n"
6798                    "                           // Second element:\n"
6799                    "                           2};",
6800                    getLLVMStyleWithColumns(30)));
6801   // A trailing comma should still lead to an enforced line break and no
6802   // binpacking.
6803   EXPECT_EQ("vector<int> SomeVector = {\n"
6804             "    // aaa\n"
6805             "    1,\n"
6806             "    2,\n"
6807             "};",
6808             format("vector<int> SomeVector = { // aaa\n"
6809                    "    1, 2, };"));
6810 
6811   FormatStyle ExtraSpaces = getLLVMStyle();
6812   ExtraSpaces.Cpp11BracedListStyle = false;
6813   ExtraSpaces.ColumnLimit = 75;
6814   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6815   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6816   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6817   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6818   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6819   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6820   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6821   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6822   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6823   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6824   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6825   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6826   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6827   verifyFormat("class Class {\n"
6828                "  T member = { arg1, arg2 };\n"
6829                "};",
6830                ExtraSpaces);
6831   verifyFormat(
6832       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6833       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6834       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6835       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6836       ExtraSpaces);
6837   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6838   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6839                ExtraSpaces);
6840   verifyFormat(
6841       "someFunction(OtherParam,\n"
6842       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6843       "                         param1, param2,\n"
6844       "                         // comment 2\n"
6845       "                         param3, param4 });",
6846       ExtraSpaces);
6847   verifyFormat(
6848       "std::this_thread::sleep_for(\n"
6849       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6850       ExtraSpaces);
6851   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6852                "    aaaaaaa,\n"
6853                "    aaaaaaaaaa,\n"
6854                "    aaaaa,\n"
6855                "    aaaaaaaaaaaaaaa,\n"
6856                "    aaa,\n"
6857                "    aaaaaaaaaa,\n"
6858                "    a,\n"
6859                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6860                "    aaaaaaaaaaaa,\n"
6861                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6862                "    aaaaaaa,\n"
6863                "    a};");
6864   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6865   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6866   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6867 }
6868 
6869 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6870   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6871                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6872                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6873                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6874                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6875                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6876   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6877                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6878                "                 1, 22, 333, 4444, 55555, //\n"
6879                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6880                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6881   verifyFormat(
6882       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6883       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6884       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6885       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6886       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6887       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6888       "                 7777777};");
6889   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6890                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6891                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6892   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6893                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6894                "    // Separating comment.\n"
6895                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6896   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6897                "    // Leading comment\n"
6898                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6899                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6900   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6901                "                 1, 1, 1, 1};",
6902                getLLVMStyleWithColumns(39));
6903   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6904                "                 1, 1, 1, 1};",
6905                getLLVMStyleWithColumns(38));
6906   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6907                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6908                getLLVMStyleWithColumns(43));
6909   verifyFormat(
6910       "static unsigned SomeValues[10][3] = {\n"
6911       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6912       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6913   verifyFormat("static auto fields = new vector<string>{\n"
6914                "    \"aaaaaaaaaaaaa\",\n"
6915                "    \"aaaaaaaaaaaaa\",\n"
6916                "    \"aaaaaaaaaaaa\",\n"
6917                "    \"aaaaaaaaaaaaaa\",\n"
6918                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6919                "    \"aaaaaaaaaaaa\",\n"
6920                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6921                "};");
6922   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6923   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6924                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6925                "                 3, cccccccccccccccccccccc};",
6926                getLLVMStyleWithColumns(60));
6927 
6928   // Trailing commas.
6929   verifyFormat("vector<int> x = {\n"
6930                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6931                "};",
6932                getLLVMStyleWithColumns(39));
6933   verifyFormat("vector<int> x = {\n"
6934                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6935                "};",
6936                getLLVMStyleWithColumns(39));
6937   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6938                "                 1, 1, 1, 1,\n"
6939                "                 /**/ /**/};",
6940                getLLVMStyleWithColumns(39));
6941 
6942   // Trailing comment in the first line.
6943   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6944                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6945                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6946                "    11111111,   22222222,   333333333,   44444444};");
6947   // Trailing comment in the last line.
6948   verifyFormat("int aaaaa[] = {\n"
6949                "    1, 2, 3, // comment\n"
6950                "    4, 5, 6  // comment\n"
6951                "};");
6952 
6953   // With nested lists, we should either format one item per line or all nested
6954   // lists one on line.
6955   // FIXME: For some nested lists, we can do better.
6956   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6957                "        {aaaaaaaaaaaaaaaaaaa},\n"
6958                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6959                "        {aaaaaaaaaaaaaaaaa}};",
6960                getLLVMStyleWithColumns(60));
6961   verifyFormat(
6962       "SomeStruct my_struct_array = {\n"
6963       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6964       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6965       "    {aaa, aaa},\n"
6966       "    {aaa, aaa},\n"
6967       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6968       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6969       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6970 
6971   // No column layout should be used here.
6972   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6973                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6974 
6975   verifyNoCrash("a<,");
6976 
6977   // No braced initializer here.
6978   verifyFormat("void f() {\n"
6979                "  struct Dummy {};\n"
6980                "  f(v);\n"
6981                "}");
6982 
6983   // Long lists should be formatted in columns even if they are nested.
6984   verifyFormat(
6985       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6986       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6987       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6988       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6989       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6990       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6991 
6992   // Allow "single-column" layout even if that violates the column limit. There
6993   // isn't going to be a better way.
6994   verifyFormat("std::vector<int> a = {\n"
6995                "    aaaaaaaa,\n"
6996                "    aaaaaaaa,\n"
6997                "    aaaaaaaa,\n"
6998                "    aaaaaaaa,\n"
6999                "    aaaaaaaaaa,\n"
7000                "    aaaaaaaa,\n"
7001                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7002                getLLVMStyleWithColumns(30));
7003   verifyFormat("vector<int> aaaa = {\n"
7004                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7005                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7006                "    aaaaaa.aaaaaaa,\n"
7007                "    aaaaaa.aaaaaaa,\n"
7008                "    aaaaaa.aaaaaaa,\n"
7009                "    aaaaaa.aaaaaaa,\n"
7010                "};");
7011 
7012   // Don't create hanging lists.
7013   verifyFormat("someFunction(Param, {List1, List2,\n"
7014                "                     List3});",
7015                getLLVMStyleWithColumns(35));
7016   verifyFormat("someFunction(Param, Param,\n"
7017                "             {List1, List2,\n"
7018                "              List3});",
7019                getLLVMStyleWithColumns(35));
7020   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7021                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7022 }
7023 
7024 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7025   FormatStyle DoNotMerge = getLLVMStyle();
7026   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7027 
7028   verifyFormat("void f() { return 42; }");
7029   verifyFormat("void f() {\n"
7030                "  return 42;\n"
7031                "}",
7032                DoNotMerge);
7033   verifyFormat("void f() {\n"
7034                "  // Comment\n"
7035                "}");
7036   verifyFormat("{\n"
7037                "#error {\n"
7038                "  int a;\n"
7039                "}");
7040   verifyFormat("{\n"
7041                "  int a;\n"
7042                "#error {\n"
7043                "}");
7044   verifyFormat("void f() {} // comment");
7045   verifyFormat("void f() { int a; } // comment");
7046   verifyFormat("void f() {\n"
7047                "} // comment",
7048                DoNotMerge);
7049   verifyFormat("void f() {\n"
7050                "  int a;\n"
7051                "} // comment",
7052                DoNotMerge);
7053   verifyFormat("void f() {\n"
7054                "} // comment",
7055                getLLVMStyleWithColumns(15));
7056 
7057   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7058   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7059 
7060   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7061   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7062   verifyFormat("class C {\n"
7063                "  C()\n"
7064                "      : iiiiiiii(nullptr),\n"
7065                "        kkkkkkk(nullptr),\n"
7066                "        mmmmmmm(nullptr),\n"
7067                "        nnnnnnn(nullptr) {}\n"
7068                "};",
7069                getGoogleStyle());
7070 
7071   FormatStyle NoColumnLimit = getLLVMStyle();
7072   NoColumnLimit.ColumnLimit = 0;
7073   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7074   EXPECT_EQ("class C {\n"
7075             "  A() : b(0) {}\n"
7076             "};",
7077             format("class C{A():b(0){}};", NoColumnLimit));
7078   EXPECT_EQ("A()\n"
7079             "    : b(0) {\n"
7080             "}",
7081             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7082 
7083   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7084   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7085       FormatStyle::SFS_None;
7086   EXPECT_EQ("A()\n"
7087             "    : b(0) {\n"
7088             "}",
7089             format("A():b(0){}", DoNotMergeNoColumnLimit));
7090   EXPECT_EQ("A()\n"
7091             "    : b(0) {\n"
7092             "}",
7093             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7094 
7095   verifyFormat("#define A          \\\n"
7096                "  void f() {       \\\n"
7097                "    int i;         \\\n"
7098                "  }",
7099                getLLVMStyleWithColumns(20));
7100   verifyFormat("#define A           \\\n"
7101                "  void f() { int i; }",
7102                getLLVMStyleWithColumns(21));
7103   verifyFormat("#define A            \\\n"
7104                "  void f() {         \\\n"
7105                "    int i;           \\\n"
7106                "  }                  \\\n"
7107                "  int j;",
7108                getLLVMStyleWithColumns(22));
7109   verifyFormat("#define A             \\\n"
7110                "  void f() { int i; } \\\n"
7111                "  int j;",
7112                getLLVMStyleWithColumns(23));
7113 }
7114 
7115 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7116   FormatStyle MergeEmptyOnly = getLLVMStyle();
7117   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7118   verifyFormat("class C {\n"
7119                "  int f() {}\n"
7120                "};",
7121                MergeEmptyOnly);
7122   verifyFormat("class C {\n"
7123                "  int f() {\n"
7124                "    return 42;\n"
7125                "  }\n"
7126                "};",
7127                MergeEmptyOnly);
7128   verifyFormat("int f() {}", MergeEmptyOnly);
7129   verifyFormat("int f() {\n"
7130                "  return 42;\n"
7131                "}",
7132                MergeEmptyOnly);
7133 
7134   // Also verify behavior when BraceWrapping.AfterFunction = true
7135   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7136   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7137   verifyFormat("int f() {}", MergeEmptyOnly);
7138   verifyFormat("class C {\n"
7139                "  int f() {}\n"
7140                "};",
7141                MergeEmptyOnly);
7142 }
7143 
7144 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7145   FormatStyle MergeInlineOnly = getLLVMStyle();
7146   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7147   verifyFormat("class C {\n"
7148                "  int f() { return 42; }\n"
7149                "};",
7150                MergeInlineOnly);
7151   verifyFormat("int f() {\n"
7152                "  return 42;\n"
7153                "}",
7154                MergeInlineOnly);
7155 
7156   // SFS_Inline implies SFS_Empty
7157   verifyFormat("class C {\n"
7158                "  int f() {}\n"
7159                "};",
7160                MergeInlineOnly);
7161   verifyFormat("int f() {}", MergeInlineOnly);
7162 
7163   // Also verify behavior when BraceWrapping.AfterFunction = true
7164   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7165   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7166   verifyFormat("class C {\n"
7167                "  int f() { return 42; }\n"
7168                "};",
7169                MergeInlineOnly);
7170   verifyFormat("int f()\n"
7171                "{\n"
7172                "  return 42;\n"
7173                "}",
7174                MergeInlineOnly);
7175 
7176   // SFS_Inline implies SFS_Empty
7177   verifyFormat("int f() {}", MergeInlineOnly);
7178   verifyFormat("class C {\n"
7179                "  int f() {}\n"
7180                "};",
7181                MergeInlineOnly);
7182 }
7183 
7184 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7185   FormatStyle MergeInlineOnly = getLLVMStyle();
7186   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7187       FormatStyle::SFS_InlineOnly;
7188   verifyFormat("class C {\n"
7189                "  int f() { return 42; }\n"
7190                "};",
7191                MergeInlineOnly);
7192   verifyFormat("int f() {\n"
7193                "  return 42;\n"
7194                "}",
7195                MergeInlineOnly);
7196 
7197   // SFS_InlineOnly does not imply SFS_Empty
7198   verifyFormat("class C {\n"
7199                "  int f() {}\n"
7200                "};",
7201                MergeInlineOnly);
7202   verifyFormat("int f() {\n"
7203                "}",
7204                MergeInlineOnly);
7205 
7206   // Also verify behavior when BraceWrapping.AfterFunction = true
7207   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7208   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7209   verifyFormat("class C {\n"
7210                "  int f() { return 42; }\n"
7211                "};",
7212                MergeInlineOnly);
7213   verifyFormat("int f()\n"
7214                "{\n"
7215                "  return 42;\n"
7216                "}",
7217                MergeInlineOnly);
7218 
7219   // SFS_InlineOnly does not imply SFS_Empty
7220   verifyFormat("int f()\n"
7221                "{\n"
7222                "}",
7223                MergeInlineOnly);
7224   verifyFormat("class C {\n"
7225                "  int f() {}\n"
7226                "};",
7227                MergeInlineOnly);
7228 }
7229 
7230 TEST_F(FormatTest, SplitEmptyFunction) {
7231   FormatStyle Style = getLLVMStyle();
7232   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7233   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7234   Style.BraceWrapping.AfterFunction = true;
7235   Style.BraceWrapping.SplitEmptyFunction = false;
7236   Style.ColumnLimit = 40;
7237 
7238   verifyFormat("int f()\n"
7239                "{}",
7240                Style);
7241   verifyFormat("int f()\n"
7242                "{\n"
7243                "  return 42;\n"
7244                "}",
7245                Style);
7246   verifyFormat("int f()\n"
7247                "{\n"
7248                "  // some comment\n"
7249                "}",
7250                Style);
7251 
7252   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7253   verifyFormat("int f() {}", Style);
7254   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7255                "{}",
7256                Style);
7257   verifyFormat("int f()\n"
7258                "{\n"
7259                "  return 0;\n"
7260                "}",
7261                Style);
7262 
7263   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7264   verifyFormat("class Foo {\n"
7265                "  int f() {}\n"
7266                "};\n",
7267                Style);
7268   verifyFormat("class Foo {\n"
7269                "  int f() { return 0; }\n"
7270                "};\n",
7271                Style);
7272   verifyFormat("class Foo {\n"
7273                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7274                "  {}\n"
7275                "};\n",
7276                Style);
7277   verifyFormat("class Foo {\n"
7278                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7279                "  {\n"
7280                "    return 0;\n"
7281                "  }\n"
7282                "};\n",
7283                Style);
7284 
7285   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7286   verifyFormat("int f() {}", Style);
7287   verifyFormat("int f() { return 0; }", Style);
7288   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7289                "{}",
7290                Style);
7291   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7292                "{\n"
7293                "  return 0;\n"
7294                "}",
7295                Style);
7296 }
7297 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7298   FormatStyle Style = getLLVMStyle();
7299   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7300   verifyFormat("#ifdef A\n"
7301                "int f() {}\n"
7302                "#else\n"
7303                "int g() {}\n"
7304                "#endif",
7305                Style);
7306 }
7307 
7308 TEST_F(FormatTest, SplitEmptyClass) {
7309   FormatStyle Style = getLLVMStyle();
7310   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7311   Style.BraceWrapping.AfterClass = true;
7312   Style.BraceWrapping.SplitEmptyRecord = false;
7313 
7314   verifyFormat("class Foo\n"
7315                "{};",
7316                Style);
7317   verifyFormat("/* something */ class Foo\n"
7318                "{};",
7319                Style);
7320   verifyFormat("template <typename X> class Foo\n"
7321                "{};",
7322                Style);
7323   verifyFormat("class Foo\n"
7324                "{\n"
7325                "  Foo();\n"
7326                "};",
7327                Style);
7328   verifyFormat("typedef class Foo\n"
7329                "{\n"
7330                "} Foo_t;",
7331                Style);
7332 }
7333 
7334 TEST_F(FormatTest, SplitEmptyStruct) {
7335   FormatStyle Style = getLLVMStyle();
7336   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7337   Style.BraceWrapping.AfterStruct = true;
7338   Style.BraceWrapping.SplitEmptyRecord = false;
7339 
7340   verifyFormat("struct Foo\n"
7341                "{};",
7342                Style);
7343   verifyFormat("/* something */ struct Foo\n"
7344                "{};",
7345                Style);
7346   verifyFormat("template <typename X> struct Foo\n"
7347                "{};",
7348                Style);
7349   verifyFormat("struct Foo\n"
7350                "{\n"
7351                "  Foo();\n"
7352                "};",
7353                Style);
7354   verifyFormat("typedef struct Foo\n"
7355                "{\n"
7356                "} Foo_t;",
7357                Style);
7358   //typedef struct Bar {} Bar_t;
7359 }
7360 
7361 TEST_F(FormatTest, SplitEmptyUnion) {
7362   FormatStyle Style = getLLVMStyle();
7363   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7364   Style.BraceWrapping.AfterUnion = true;
7365   Style.BraceWrapping.SplitEmptyRecord = false;
7366 
7367   verifyFormat("union Foo\n"
7368                "{};",
7369                Style);
7370   verifyFormat("/* something */ union Foo\n"
7371                "{};",
7372                Style);
7373   verifyFormat("union Foo\n"
7374                "{\n"
7375                "  A,\n"
7376                "};",
7377                Style);
7378   verifyFormat("typedef union Foo\n"
7379                "{\n"
7380                "} Foo_t;",
7381                Style);
7382 }
7383 
7384 TEST_F(FormatTest, SplitEmptyNamespace) {
7385   FormatStyle Style = getLLVMStyle();
7386   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7387   Style.BraceWrapping.AfterNamespace = true;
7388   Style.BraceWrapping.SplitEmptyNamespace = false;
7389 
7390   verifyFormat("namespace Foo\n"
7391                "{};",
7392                Style);
7393   verifyFormat("/* something */ namespace Foo\n"
7394                "{};",
7395                Style);
7396   verifyFormat("inline namespace Foo\n"
7397                "{};",
7398                Style);
7399   verifyFormat("namespace Foo\n"
7400                "{\n"
7401                "void Bar();\n"
7402                "};",
7403                Style);
7404 }
7405 
7406 TEST_F(FormatTest, NeverMergeShortRecords) {
7407   FormatStyle Style = getLLVMStyle();
7408 
7409   verifyFormat("class Foo {\n"
7410                "  Foo();\n"
7411                "};",
7412                Style);
7413   verifyFormat("typedef class Foo {\n"
7414                "  Foo();\n"
7415                "} Foo_t;",
7416                Style);
7417   verifyFormat("struct Foo {\n"
7418                "  Foo();\n"
7419                "};",
7420                Style);
7421   verifyFormat("typedef struct Foo {\n"
7422                "  Foo();\n"
7423                "} Foo_t;",
7424                Style);
7425   verifyFormat("union Foo {\n"
7426                "  A,\n"
7427                "};",
7428                Style);
7429   verifyFormat("typedef union Foo {\n"
7430                "  A,\n"
7431                "} Foo_t;",
7432                Style);
7433   verifyFormat("namespace Foo {\n"
7434                "void Bar();\n"
7435                "};",
7436                Style);
7437 
7438   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7439   Style.BraceWrapping.AfterClass = true;
7440   Style.BraceWrapping.AfterStruct = true;
7441   Style.BraceWrapping.AfterUnion = true;
7442   Style.BraceWrapping.AfterNamespace = true;
7443   verifyFormat("class Foo\n"
7444                "{\n"
7445                "  Foo();\n"
7446                "};",
7447                Style);
7448   verifyFormat("typedef class Foo\n"
7449                "{\n"
7450                "  Foo();\n"
7451                "} Foo_t;",
7452                Style);
7453   verifyFormat("struct Foo\n"
7454                "{\n"
7455                "  Foo();\n"
7456                "};",
7457                Style);
7458   verifyFormat("typedef struct Foo\n"
7459                "{\n"
7460                "  Foo();\n"
7461                "} Foo_t;",
7462                Style);
7463   verifyFormat("union Foo\n"
7464                "{\n"
7465                "  A,\n"
7466                "};",
7467                Style);
7468   verifyFormat("typedef union Foo\n"
7469                "{\n"
7470                "  A,\n"
7471                "} Foo_t;",
7472                Style);
7473   verifyFormat("namespace Foo\n"
7474                "{\n"
7475                "void Bar();\n"
7476                "};",
7477                Style);
7478 }
7479 
7480 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7481   // Elaborate type variable declarations.
7482   verifyFormat("struct foo a = {bar};\nint n;");
7483   verifyFormat("class foo a = {bar};\nint n;");
7484   verifyFormat("union foo a = {bar};\nint n;");
7485 
7486   // Elaborate types inside function definitions.
7487   verifyFormat("struct foo f() {}\nint n;");
7488   verifyFormat("class foo f() {}\nint n;");
7489   verifyFormat("union foo f() {}\nint n;");
7490 
7491   // Templates.
7492   verifyFormat("template <class X> void f() {}\nint n;");
7493   verifyFormat("template <struct X> void f() {}\nint n;");
7494   verifyFormat("template <union X> void f() {}\nint n;");
7495 
7496   // Actual definitions...
7497   verifyFormat("struct {\n} n;");
7498   verifyFormat(
7499       "template <template <class T, class Y>, class Z> class X {\n} n;");
7500   verifyFormat("union Z {\n  int n;\n} x;");
7501   verifyFormat("class MACRO Z {\n} n;");
7502   verifyFormat("class MACRO(X) Z {\n} n;");
7503   verifyFormat("class __attribute__(X) Z {\n} n;");
7504   verifyFormat("class __declspec(X) Z {\n} n;");
7505   verifyFormat("class A##B##C {\n} n;");
7506   verifyFormat("class alignas(16) Z {\n} n;");
7507   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7508   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7509 
7510   // Redefinition from nested context:
7511   verifyFormat("class A::B::C {\n} n;");
7512 
7513   // Template definitions.
7514   verifyFormat(
7515       "template <typename F>\n"
7516       "Matcher(const Matcher<F> &Other,\n"
7517       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7518       "                             !is_same<F, T>::value>::type * = 0)\n"
7519       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7520 
7521   // FIXME: This is still incorrectly handled at the formatter side.
7522   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7523   verifyFormat("int i = SomeFunction(a<b, a> b);");
7524 
7525   // FIXME:
7526   // This now gets parsed incorrectly as class definition.
7527   // verifyFormat("class A<int> f() {\n}\nint n;");
7528 
7529   // Elaborate types where incorrectly parsing the structural element would
7530   // break the indent.
7531   verifyFormat("if (true)\n"
7532                "  class X x;\n"
7533                "else\n"
7534                "  f();\n");
7535 
7536   // This is simply incomplete. Formatting is not important, but must not crash.
7537   verifyFormat("class A:");
7538 }
7539 
7540 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7541   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7542             format("#error Leave     all         white!!!!! space* alone!\n"));
7543   EXPECT_EQ(
7544       "#warning Leave     all         white!!!!! space* alone!\n",
7545       format("#warning Leave     all         white!!!!! space* alone!\n"));
7546   EXPECT_EQ("#error 1", format("  #  error   1"));
7547   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7548 }
7549 
7550 TEST_F(FormatTest, FormatHashIfExpressions) {
7551   verifyFormat("#if AAAA && BBBB");
7552   verifyFormat("#if (AAAA && BBBB)");
7553   verifyFormat("#elif (AAAA && BBBB)");
7554   // FIXME: Come up with a better indentation for #elif.
7555   verifyFormat(
7556       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7557       "    defined(BBBBBBBB)\n"
7558       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7559       "    defined(BBBBBBBB)\n"
7560       "#endif",
7561       getLLVMStyleWithColumns(65));
7562 }
7563 
7564 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7565   FormatStyle AllowsMergedIf = getGoogleStyle();
7566   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7567   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7568   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7569   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7570   EXPECT_EQ("if (true) return 42;",
7571             format("if (true)\nreturn 42;", AllowsMergedIf));
7572   FormatStyle ShortMergedIf = AllowsMergedIf;
7573   ShortMergedIf.ColumnLimit = 25;
7574   verifyFormat("#define A \\\n"
7575                "  if (true) return 42;",
7576                ShortMergedIf);
7577   verifyFormat("#define A \\\n"
7578                "  f();    \\\n"
7579                "  if (true)\n"
7580                "#define B",
7581                ShortMergedIf);
7582   verifyFormat("#define A \\\n"
7583                "  f();    \\\n"
7584                "  if (true)\n"
7585                "g();",
7586                ShortMergedIf);
7587   verifyFormat("{\n"
7588                "#ifdef A\n"
7589                "  // Comment\n"
7590                "  if (true) continue;\n"
7591                "#endif\n"
7592                "  // Comment\n"
7593                "  if (true) continue;\n"
7594                "}",
7595                ShortMergedIf);
7596   ShortMergedIf.ColumnLimit = 33;
7597   verifyFormat("#define A \\\n"
7598                "  if constexpr (true) return 42;",
7599                ShortMergedIf);
7600   ShortMergedIf.ColumnLimit = 29;
7601   verifyFormat("#define A                   \\\n"
7602                "  if (aaaaaaaaaa) return 1; \\\n"
7603                "  return 2;",
7604                ShortMergedIf);
7605   ShortMergedIf.ColumnLimit = 28;
7606   verifyFormat("#define A         \\\n"
7607                "  if (aaaaaaaaaa) \\\n"
7608                "    return 1;     \\\n"
7609                "  return 2;",
7610                ShortMergedIf);
7611   verifyFormat("#define A                \\\n"
7612                "  if constexpr (aaaaaaa) \\\n"
7613                "    return 1;            \\\n"
7614                "  return 2;",
7615                ShortMergedIf);
7616 }
7617 
7618 TEST_F(FormatTest, FormatStarDependingOnContext) {
7619   verifyFormat("void f(int *a);");
7620   verifyFormat("void f() { f(fint * b); }");
7621   verifyFormat("class A {\n  void f(int *a);\n};");
7622   verifyFormat("class A {\n  int *a;\n};");
7623   verifyFormat("namespace a {\n"
7624                "namespace b {\n"
7625                "class A {\n"
7626                "  void f() {}\n"
7627                "  int *a;\n"
7628                "};\n"
7629                "} // namespace b\n"
7630                "} // namespace a");
7631 }
7632 
7633 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7634   verifyFormat("while");
7635   verifyFormat("operator");
7636 }
7637 
7638 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7639   // This code would be painfully slow to format if we didn't skip it.
7640   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
7641                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7642                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7643                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7644                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7645                    "A(1, 1)\n"
7646                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7647                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7648                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7649                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7650                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7651                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7652                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7653                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7654                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7655                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7656   // Deeply nested part is untouched, rest is formatted.
7657   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7658             format(std::string("int    i;\n") + Code + "int    j;\n",
7659                    getLLVMStyle(), SC_ExpectIncomplete));
7660 }
7661 
7662 //===----------------------------------------------------------------------===//
7663 // Objective-C tests.
7664 //===----------------------------------------------------------------------===//
7665 
7666 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7667   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7668   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7669             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7670   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7671   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7672   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7673             format("-(NSInteger)Method3:(id)anObject;"));
7674   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7675             format("-(NSInteger)Method4:(id)anObject;"));
7676   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7677             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7678   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7679             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7680   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7681             "forAllCells:(BOOL)flag;",
7682             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7683                    "forAllCells:(BOOL)flag;"));
7684 
7685   // Very long objectiveC method declaration.
7686   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7687                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7688   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7689                "                    inRange:(NSRange)range\n"
7690                "                   outRange:(NSRange)out_range\n"
7691                "                  outRange1:(NSRange)out_range1\n"
7692                "                  outRange2:(NSRange)out_range2\n"
7693                "                  outRange3:(NSRange)out_range3\n"
7694                "                  outRange4:(NSRange)out_range4\n"
7695                "                  outRange5:(NSRange)out_range5\n"
7696                "                  outRange6:(NSRange)out_range6\n"
7697                "                  outRange7:(NSRange)out_range7\n"
7698                "                  outRange8:(NSRange)out_range8\n"
7699                "                  outRange9:(NSRange)out_range9;");
7700 
7701   // When the function name has to be wrapped.
7702   FormatStyle Style = getLLVMStyle();
7703   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
7704   // and always indents instead.
7705   Style.IndentWrappedFunctionNames = false;
7706   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7707                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7708                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7709                "}",
7710                Style);
7711   Style.IndentWrappedFunctionNames = true;
7712   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7713                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
7714                "               anotherName:(NSString)dddddddddddddd {\n"
7715                "}",
7716                Style);
7717 
7718   verifyFormat("- (int)sum:(vector<int>)numbers;");
7719   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7720   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7721   // protocol lists (but not for template classes):
7722   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7723 
7724   verifyFormat("- (int (*)())foo:(int (*)())f;");
7725   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7726 
7727   // If there's no return type (very rare in practice!), LLVM and Google style
7728   // agree.
7729   verifyFormat("- foo;");
7730   verifyFormat("- foo:(int)f;");
7731   verifyGoogleFormat("- foo:(int)foo;");
7732 }
7733 
7734 
7735 TEST_F(FormatTest, BreaksStringLiterals) {
7736   EXPECT_EQ("\"some text \"\n"
7737             "\"other\";",
7738             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7739   EXPECT_EQ("\"some text \"\n"
7740             "\"other\";",
7741             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7742   EXPECT_EQ(
7743       "#define A  \\\n"
7744       "  \"some \"  \\\n"
7745       "  \"text \"  \\\n"
7746       "  \"other\";",
7747       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7748   EXPECT_EQ(
7749       "#define A  \\\n"
7750       "  \"so \"    \\\n"
7751       "  \"text \"  \\\n"
7752       "  \"other\";",
7753       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7754 
7755   EXPECT_EQ("\"some text\"",
7756             format("\"some text\"", getLLVMStyleWithColumns(1)));
7757   EXPECT_EQ("\"some text\"",
7758             format("\"some text\"", getLLVMStyleWithColumns(11)));
7759   EXPECT_EQ("\"some \"\n"
7760             "\"text\"",
7761             format("\"some text\"", getLLVMStyleWithColumns(10)));
7762   EXPECT_EQ("\"some \"\n"
7763             "\"text\"",
7764             format("\"some text\"", getLLVMStyleWithColumns(7)));
7765   EXPECT_EQ("\"some\"\n"
7766             "\" tex\"\n"
7767             "\"t\"",
7768             format("\"some text\"", getLLVMStyleWithColumns(6)));
7769   EXPECT_EQ("\"some\"\n"
7770             "\" tex\"\n"
7771             "\" and\"",
7772             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7773   EXPECT_EQ("\"some\"\n"
7774             "\"/tex\"\n"
7775             "\"/and\"",
7776             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7777 
7778   EXPECT_EQ("variable =\n"
7779             "    \"long string \"\n"
7780             "    \"literal\";",
7781             format("variable = \"long string literal\";",
7782                    getLLVMStyleWithColumns(20)));
7783 
7784   EXPECT_EQ("variable = f(\n"
7785             "    \"long string \"\n"
7786             "    \"literal\",\n"
7787             "    short,\n"
7788             "    loooooooooooooooooooong);",
7789             format("variable = f(\"long string literal\", short, "
7790                    "loooooooooooooooooooong);",
7791                    getLLVMStyleWithColumns(20)));
7792 
7793   EXPECT_EQ(
7794       "f(g(\"long string \"\n"
7795       "    \"literal\"),\n"
7796       "  b);",
7797       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7798   EXPECT_EQ("f(g(\"long string \"\n"
7799             "    \"literal\",\n"
7800             "    a),\n"
7801             "  b);",
7802             format("f(g(\"long string literal\", a), b);",
7803                    getLLVMStyleWithColumns(20)));
7804   EXPECT_EQ(
7805       "f(\"one two\".split(\n"
7806       "    variable));",
7807       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7808   EXPECT_EQ("f(\"one two three four five six \"\n"
7809             "  \"seven\".split(\n"
7810             "      really_looooong_variable));",
7811             format("f(\"one two three four five six seven\"."
7812                    "split(really_looooong_variable));",
7813                    getLLVMStyleWithColumns(33)));
7814 
7815   EXPECT_EQ("f(\"some \"\n"
7816             "  \"text\",\n"
7817             "  other);",
7818             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7819 
7820   // Only break as a last resort.
7821   verifyFormat(
7822       "aaaaaaaaaaaaaaaaaaaa(\n"
7823       "    aaaaaaaaaaaaaaaaaaaa,\n"
7824       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7825 
7826   EXPECT_EQ("\"splitmea\"\n"
7827             "\"trandomp\"\n"
7828             "\"oint\"",
7829             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7830 
7831   EXPECT_EQ("\"split/\"\n"
7832             "\"pathat/\"\n"
7833             "\"slashes\"",
7834             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7835 
7836   EXPECT_EQ("\"split/\"\n"
7837             "\"pathat/\"\n"
7838             "\"slashes\"",
7839             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7840   EXPECT_EQ("\"split at \"\n"
7841             "\"spaces/at/\"\n"
7842             "\"slashes.at.any$\"\n"
7843             "\"non-alphanumeric%\"\n"
7844             "\"1111111111characte\"\n"
7845             "\"rs\"",
7846             format("\"split at "
7847                    "spaces/at/"
7848                    "slashes.at."
7849                    "any$non-"
7850                    "alphanumeric%"
7851                    "1111111111characte"
7852                    "rs\"",
7853                    getLLVMStyleWithColumns(20)));
7854 
7855   // Verify that splitting the strings understands
7856   // Style::AlwaysBreakBeforeMultilineStrings.
7857   EXPECT_EQ(
7858       "aaaaaaaaaaaa(\n"
7859       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7860       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7861       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7862              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7863              "aaaaaaaaaaaaaaaaaaaaaa\");",
7864              getGoogleStyle()));
7865   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7866             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7867             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7868                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7869                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7870                    getGoogleStyle()));
7871   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7872             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7873             format("llvm::outs() << "
7874                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7875                    "aaaaaaaaaaaaaaaaaaa\";"));
7876   EXPECT_EQ("ffff(\n"
7877             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7878             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7879             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7880                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7881                    getGoogleStyle()));
7882 
7883   FormatStyle Style = getLLVMStyleWithColumns(12);
7884   Style.BreakStringLiterals = false;
7885   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7886 
7887   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7888   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7889   EXPECT_EQ("#define A \\\n"
7890             "  \"some \" \\\n"
7891             "  \"text \" \\\n"
7892             "  \"other\";",
7893             format("#define A \"some text other\";", AlignLeft));
7894 }
7895 
7896 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
7897   EXPECT_EQ("C a = \"some more \"\n"
7898             "      \"text\";",
7899             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
7900 }
7901 
7902 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7903   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7904   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7905   EXPECT_EQ("int i = a(b());",
7906             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7907 }
7908 
7909 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7910   EXPECT_EQ(
7911       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7912       "(\n"
7913       "    \"x\t\");",
7914       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7915              "aaaaaaa("
7916              "\"x\t\");"));
7917 }
7918 
7919 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7920   EXPECT_EQ(
7921       "u8\"utf8 string \"\n"
7922       "u8\"literal\";",
7923       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7924   EXPECT_EQ(
7925       "u\"utf16 string \"\n"
7926       "u\"literal\";",
7927       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7928   EXPECT_EQ(
7929       "U\"utf32 string \"\n"
7930       "U\"literal\";",
7931       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7932   EXPECT_EQ("L\"wide string \"\n"
7933             "L\"literal\";",
7934             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7935   EXPECT_EQ("@\"NSString \"\n"
7936             "@\"literal\";",
7937             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7938   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7939 
7940   // This input makes clang-format try to split the incomplete unicode escape
7941   // sequence, which used to lead to a crasher.
7942   verifyNoCrash(
7943       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7944       getLLVMStyleWithColumns(60));
7945 }
7946 
7947 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7948   FormatStyle Style = getGoogleStyleWithColumns(15);
7949   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7950   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7951   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7952   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7953   EXPECT_EQ("u8R\"x(raw literal)x\";",
7954             format("u8R\"x(raw literal)x\";", Style));
7955 }
7956 
7957 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7958   FormatStyle Style = getLLVMStyleWithColumns(20);
7959   EXPECT_EQ(
7960       "_T(\"aaaaaaaaaaaaaa\")\n"
7961       "_T(\"aaaaaaaaaaaaaa\")\n"
7962       "_T(\"aaaaaaaaaaaa\")",
7963       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7964   EXPECT_EQ("f(x,\n"
7965             "  _T(\"aaaaaaaaaaaa\")\n"
7966             "  _T(\"aaa\"),\n"
7967             "  z);",
7968             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7969 
7970   // FIXME: Handle embedded spaces in one iteration.
7971   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7972   //            "_T(\"aaaaaaaaaaaaa\")\n"
7973   //            "_T(\"aaaaaaaaaaaaa\")\n"
7974   //            "_T(\"a\")",
7975   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7976   //                   getLLVMStyleWithColumns(20)));
7977   EXPECT_EQ(
7978       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7979       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7980   EXPECT_EQ("f(\n"
7981             "#if !TEST\n"
7982             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7983             "#endif\n"
7984             ");",
7985             format("f(\n"
7986                    "#if !TEST\n"
7987                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7988                    "#endif\n"
7989                    ");"));
7990   EXPECT_EQ("f(\n"
7991             "\n"
7992             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7993             format("f(\n"
7994                    "\n"
7995                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7996 }
7997 
7998 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7999   // In a function call with two operands, the second can be broken with no line
8000   // break before it.
8001   EXPECT_EQ("func(a, \"long long \"\n"
8002             "        \"long long\");",
8003             format("func(a, \"long long long long\");",
8004                    getLLVMStyleWithColumns(24)));
8005   // In a function call with three operands, the second must be broken with a
8006   // line break before it.
8007   EXPECT_EQ("func(a,\n"
8008             "     \"long long long \"\n"
8009             "     \"long\",\n"
8010             "     c);",
8011             format("func(a, \"long long long long\", c);",
8012                    getLLVMStyleWithColumns(24)));
8013   // In a function call with three operands, the third must be broken with a
8014   // line break before it.
8015   EXPECT_EQ("func(a, b,\n"
8016             "     \"long long long \"\n"
8017             "     \"long\");",
8018             format("func(a, b, \"long long long long\");",
8019                    getLLVMStyleWithColumns(24)));
8020   // In a function call with three operands, both the second and the third must
8021   // be broken with a line break before them.
8022   EXPECT_EQ("func(a,\n"
8023             "     \"long long long \"\n"
8024             "     \"long\",\n"
8025             "     \"long long long \"\n"
8026             "     \"long\");",
8027             format("func(a, \"long long long long\", \"long long long long\");",
8028                    getLLVMStyleWithColumns(24)));
8029   // In a chain of << with two operands, the second can be broken with no line
8030   // break before it.
8031   EXPECT_EQ("a << \"line line \"\n"
8032             "     \"line\";",
8033             format("a << \"line line line\";",
8034                    getLLVMStyleWithColumns(20)));
8035   // In a chain of << with three operands, the second can be broken with no line
8036   // break before it.
8037   EXPECT_EQ("abcde << \"line \"\n"
8038             "         \"line line\"\n"
8039             "      << c;",
8040             format("abcde << \"line line line\" << c;",
8041                    getLLVMStyleWithColumns(20)));
8042   // In a chain of << with three operands, the third must be broken with a line
8043   // break before it.
8044   EXPECT_EQ("a << b\n"
8045             "  << \"line line \"\n"
8046             "     \"line\";",
8047             format("a << b << \"line line line\";",
8048                    getLLVMStyleWithColumns(20)));
8049   // In a chain of << with three operands, the second can be broken with no line
8050   // break before it and the third must be broken with a line break before it.
8051   EXPECT_EQ("abcd << \"line line \"\n"
8052             "        \"line\"\n"
8053             "     << \"line line \"\n"
8054             "        \"line\";",
8055             format("abcd << \"line line line\" << \"line line line\";",
8056                    getLLVMStyleWithColumns(20)));
8057   // In a chain of binary operators with two operands, the second can be broken
8058   // with no line break before it.
8059   EXPECT_EQ("abcd + \"line line \"\n"
8060             "       \"line line\";",
8061             format("abcd + \"line line line line\";",
8062                    getLLVMStyleWithColumns(20)));
8063   // In a chain of binary operators with three operands, the second must be
8064   // broken with a line break before it.
8065   EXPECT_EQ("abcd +\n"
8066             "    \"line line \"\n"
8067             "    \"line line\" +\n"
8068             "    e;",
8069             format("abcd + \"line line line line\" + e;",
8070                    getLLVMStyleWithColumns(20)));
8071   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8072   // the first must be broken with a line break before it.
8073   FormatStyle Style = getLLVMStyleWithColumns(25);
8074   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8075   EXPECT_EQ("someFunction(\n"
8076             "    \"long long long \"\n"
8077             "    \"long\",\n"
8078             "    a);",
8079             format("someFunction(\"long long long long\", a);", Style));
8080 }
8081 
8082 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8083   EXPECT_EQ(
8084       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8085       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8086       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8087       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8088              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8089              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8090 }
8091 
8092 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8093   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8094             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8095   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8096             "multiline raw string literal xxxxxxxxxxxxxx\n"
8097             ")x\",\n"
8098             "              a),\n"
8099             "            b);",
8100             format("fffffffffff(g(R\"x(\n"
8101                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8102                    ")x\", a), b);",
8103                    getGoogleStyleWithColumns(20)));
8104   EXPECT_EQ("fffffffffff(\n"
8105             "    g(R\"x(qqq\n"
8106             "multiline raw string literal xxxxxxxxxxxxxx\n"
8107             ")x\",\n"
8108             "      a),\n"
8109             "    b);",
8110             format("fffffffffff(g(R\"x(qqq\n"
8111                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8112                    ")x\", a), b);",
8113                    getGoogleStyleWithColumns(20)));
8114 
8115   EXPECT_EQ("fffffffffff(R\"x(\n"
8116             "multiline raw string literal xxxxxxxxxxxxxx\n"
8117             ")x\");",
8118             format("fffffffffff(R\"x(\n"
8119                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8120                    ")x\");",
8121                    getGoogleStyleWithColumns(20)));
8122   EXPECT_EQ("fffffffffff(R\"x(\n"
8123             "multiline raw string literal xxxxxxxxxxxxxx\n"
8124             ")x\" + bbbbbb);",
8125             format("fffffffffff(R\"x(\n"
8126                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8127                    ")x\" +   bbbbbb);",
8128                    getGoogleStyleWithColumns(20)));
8129   EXPECT_EQ("fffffffffff(\n"
8130             "    R\"x(\n"
8131             "multiline raw string literal xxxxxxxxxxxxxx\n"
8132             ")x\" +\n"
8133             "    bbbbbb);",
8134             format("fffffffffff(\n"
8135                    " R\"x(\n"
8136                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8137                    ")x\" + bbbbbb);",
8138                    getGoogleStyleWithColumns(20)));
8139   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8140             format("fffffffffff(\n"
8141                    " R\"(single line raw string)\" + bbbbbb);"));
8142 }
8143 
8144 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8145   verifyFormat("string a = \"unterminated;");
8146   EXPECT_EQ("function(\"unterminated,\n"
8147             "         OtherParameter);",
8148             format("function(  \"unterminated,\n"
8149                    "    OtherParameter);"));
8150 }
8151 
8152 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8153   FormatStyle Style = getLLVMStyle();
8154   Style.Standard = FormatStyle::LS_Cpp03;
8155   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8156             format("#define x(_a) printf(\"foo\"_a);", Style));
8157 }
8158 
8159 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8160 
8161 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8162   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8163             "             \"ddeeefff\");",
8164             format("someFunction(\"aaabbbcccdddeeefff\");",
8165                    getLLVMStyleWithColumns(25)));
8166   EXPECT_EQ("someFunction1234567890(\n"
8167             "    \"aaabbbcccdddeeefff\");",
8168             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8169                    getLLVMStyleWithColumns(26)));
8170   EXPECT_EQ("someFunction1234567890(\n"
8171             "    \"aaabbbcccdddeeeff\"\n"
8172             "    \"f\");",
8173             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8174                    getLLVMStyleWithColumns(25)));
8175   EXPECT_EQ("someFunction1234567890(\n"
8176             "    \"aaabbbcccdddeeeff\"\n"
8177             "    \"f\");",
8178             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8179                    getLLVMStyleWithColumns(24)));
8180   EXPECT_EQ("someFunction(\n"
8181             "    \"aaabbbcc ddde \"\n"
8182             "    \"efff\");",
8183             format("someFunction(\"aaabbbcc ddde efff\");",
8184                    getLLVMStyleWithColumns(25)));
8185   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8186             "             \"ddeeefff\");",
8187             format("someFunction(\"aaabbbccc ddeeefff\");",
8188                    getLLVMStyleWithColumns(25)));
8189   EXPECT_EQ("someFunction1234567890(\n"
8190             "    \"aaabb \"\n"
8191             "    \"cccdddeeefff\");",
8192             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8193                    getLLVMStyleWithColumns(25)));
8194   EXPECT_EQ("#define A          \\\n"
8195             "  string s =       \\\n"
8196             "      \"123456789\"  \\\n"
8197             "      \"0\";         \\\n"
8198             "  int i;",
8199             format("#define A string s = \"1234567890\"; int i;",
8200                    getLLVMStyleWithColumns(20)));
8201   EXPECT_EQ("someFunction(\n"
8202             "    \"aaabbbcc \"\n"
8203             "    \"dddeeefff\");",
8204             format("someFunction(\"aaabbbcc dddeeefff\");",
8205                    getLLVMStyleWithColumns(25)));
8206 }
8207 
8208 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8209   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8210   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8211   EXPECT_EQ("\"test\"\n"
8212             "\"\\n\"",
8213             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8214   EXPECT_EQ("\"tes\\\\\"\n"
8215             "\"n\"",
8216             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8217   EXPECT_EQ("\"\\\\\\\\\"\n"
8218             "\"\\n\"",
8219             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8220   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8221   EXPECT_EQ("\"\\uff01\"\n"
8222             "\"test\"",
8223             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8224   EXPECT_EQ("\"\\Uff01ff02\"",
8225             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8226   EXPECT_EQ("\"\\x000000000001\"\n"
8227             "\"next\"",
8228             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8229   EXPECT_EQ("\"\\x000000000001next\"",
8230             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8231   EXPECT_EQ("\"\\x000000000001\"",
8232             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8233   EXPECT_EQ("\"test\"\n"
8234             "\"\\000000\"\n"
8235             "\"000001\"",
8236             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8237   EXPECT_EQ("\"test\\000\"\n"
8238             "\"00000000\"\n"
8239             "\"1\"",
8240             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8241 }
8242 
8243 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8244   verifyFormat("void f() {\n"
8245                "  return g() {}\n"
8246                "  void h() {}");
8247   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8248                "g();\n"
8249                "}");
8250 }
8251 
8252 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8253   verifyFormat(
8254       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8255 }
8256 
8257 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8258   verifyFormat("class X {\n"
8259                "  void f() {\n"
8260                "  }\n"
8261                "};",
8262                getLLVMStyleWithColumns(12));
8263 }
8264 
8265 TEST_F(FormatTest, ConfigurableIndentWidth) {
8266   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8267   EightIndent.IndentWidth = 8;
8268   EightIndent.ContinuationIndentWidth = 8;
8269   verifyFormat("void f() {\n"
8270                "        someFunction();\n"
8271                "        if (true) {\n"
8272                "                f();\n"
8273                "        }\n"
8274                "}",
8275                EightIndent);
8276   verifyFormat("class X {\n"
8277                "        void f() {\n"
8278                "        }\n"
8279                "};",
8280                EightIndent);
8281   verifyFormat("int x[] = {\n"
8282                "        call(),\n"
8283                "        call()};",
8284                EightIndent);
8285 }
8286 
8287 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8288   verifyFormat("double\n"
8289                "f();",
8290                getLLVMStyleWithColumns(8));
8291 }
8292 
8293 TEST_F(FormatTest, ConfigurableUseOfTab) {
8294   FormatStyle Tab = getLLVMStyleWithColumns(42);
8295   Tab.IndentWidth = 8;
8296   Tab.UseTab = FormatStyle::UT_Always;
8297   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8298 
8299   EXPECT_EQ("if (aaaaaaaa && // q\n"
8300             "    bb)\t\t// w\n"
8301             "\t;",
8302             format("if (aaaaaaaa &&// q\n"
8303                    "bb)// w\n"
8304                    ";",
8305                    Tab));
8306   EXPECT_EQ("if (aaa && bbb) // w\n"
8307             "\t;",
8308             format("if(aaa&&bbb)// w\n"
8309                    ";",
8310                    Tab));
8311 
8312   verifyFormat("class X {\n"
8313                "\tvoid f() {\n"
8314                "\t\tsomeFunction(parameter1,\n"
8315                "\t\t\t     parameter2);\n"
8316                "\t}\n"
8317                "};",
8318                Tab);
8319   verifyFormat("#define A                        \\\n"
8320                "\tvoid f() {               \\\n"
8321                "\t\tsomeFunction(    \\\n"
8322                "\t\t    parameter1,  \\\n"
8323                "\t\t    parameter2); \\\n"
8324                "\t}",
8325                Tab);
8326 
8327   Tab.TabWidth = 4;
8328   Tab.IndentWidth = 8;
8329   verifyFormat("class TabWidth4Indent8 {\n"
8330                "\t\tvoid f() {\n"
8331                "\t\t\t\tsomeFunction(parameter1,\n"
8332                "\t\t\t\t\t\t\t parameter2);\n"
8333                "\t\t}\n"
8334                "};",
8335                Tab);
8336 
8337   Tab.TabWidth = 4;
8338   Tab.IndentWidth = 4;
8339   verifyFormat("class TabWidth4Indent4 {\n"
8340                "\tvoid f() {\n"
8341                "\t\tsomeFunction(parameter1,\n"
8342                "\t\t\t\t\t parameter2);\n"
8343                "\t}\n"
8344                "};",
8345                Tab);
8346 
8347   Tab.TabWidth = 8;
8348   Tab.IndentWidth = 4;
8349   verifyFormat("class TabWidth8Indent4 {\n"
8350                "    void f() {\n"
8351                "\tsomeFunction(parameter1,\n"
8352                "\t\t     parameter2);\n"
8353                "    }\n"
8354                "};",
8355                Tab);
8356 
8357   Tab.TabWidth = 8;
8358   Tab.IndentWidth = 8;
8359   EXPECT_EQ("/*\n"
8360             "\t      a\t\tcomment\n"
8361             "\t      in multiple lines\n"
8362             "       */",
8363             format("   /*\t \t \n"
8364                    " \t \t a\t\tcomment\t \t\n"
8365                    " \t \t in multiple lines\t\n"
8366                    " \t  */",
8367                    Tab));
8368 
8369   Tab.UseTab = FormatStyle::UT_ForIndentation;
8370   verifyFormat("{\n"
8371                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8372                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8373                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8374                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8375                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8376                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8377                "};",
8378                Tab);
8379   verifyFormat("enum AA {\n"
8380                "\ta1, // Force multiple lines\n"
8381                "\ta2,\n"
8382                "\ta3\n"
8383                "};",
8384                Tab);
8385   EXPECT_EQ("if (aaaaaaaa && // q\n"
8386             "    bb)         // w\n"
8387             "\t;",
8388             format("if (aaaaaaaa &&// q\n"
8389                    "bb)// w\n"
8390                    ";",
8391                    Tab));
8392   verifyFormat("class X {\n"
8393                "\tvoid f() {\n"
8394                "\t\tsomeFunction(parameter1,\n"
8395                "\t\t             parameter2);\n"
8396                "\t}\n"
8397                "};",
8398                Tab);
8399   verifyFormat("{\n"
8400                "\tQ(\n"
8401                "\t    {\n"
8402                "\t\t    int a;\n"
8403                "\t\t    someFunction(aaaaaaaa,\n"
8404                "\t\t                 bbbbbbb);\n"
8405                "\t    },\n"
8406                "\t    p);\n"
8407                "}",
8408                Tab);
8409   EXPECT_EQ("{\n"
8410             "\t/* aaaa\n"
8411             "\t   bbbb */\n"
8412             "}",
8413             format("{\n"
8414                    "/* aaaa\n"
8415                    "   bbbb */\n"
8416                    "}",
8417                    Tab));
8418   EXPECT_EQ("{\n"
8419             "\t/*\n"
8420             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8421             "\t  bbbbbbbbbbbbb\n"
8422             "\t*/\n"
8423             "}",
8424             format("{\n"
8425                    "/*\n"
8426                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8427                    "*/\n"
8428                    "}",
8429                    Tab));
8430   EXPECT_EQ("{\n"
8431             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8432             "\t// bbbbbbbbbbbbb\n"
8433             "}",
8434             format("{\n"
8435                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8436                    "}",
8437                    Tab));
8438   EXPECT_EQ("{\n"
8439             "\t/*\n"
8440             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8441             "\t  bbbbbbbbbbbbb\n"
8442             "\t*/\n"
8443             "}",
8444             format("{\n"
8445                    "\t/*\n"
8446                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8447                    "\t*/\n"
8448                    "}",
8449                    Tab));
8450   EXPECT_EQ("{\n"
8451             "\t/*\n"
8452             "\n"
8453             "\t*/\n"
8454             "}",
8455             format("{\n"
8456                    "\t/*\n"
8457                    "\n"
8458                    "\t*/\n"
8459                    "}",
8460                    Tab));
8461   EXPECT_EQ("{\n"
8462             "\t/*\n"
8463             " asdf\n"
8464             "\t*/\n"
8465             "}",
8466             format("{\n"
8467                    "\t/*\n"
8468                    " asdf\n"
8469                    "\t*/\n"
8470                    "}",
8471                    Tab));
8472 
8473   Tab.UseTab = FormatStyle::UT_Never;
8474   EXPECT_EQ("/*\n"
8475             "              a\t\tcomment\n"
8476             "              in multiple lines\n"
8477             "       */",
8478             format("   /*\t \t \n"
8479                    " \t \t a\t\tcomment\t \t\n"
8480                    " \t \t in multiple lines\t\n"
8481                    " \t  */",
8482                    Tab));
8483   EXPECT_EQ("/* some\n"
8484             "   comment */",
8485             format(" \t \t /* some\n"
8486                    " \t \t    comment */",
8487                    Tab));
8488   EXPECT_EQ("int a; /* some\n"
8489             "   comment */",
8490             format(" \t \t int a; /* some\n"
8491                    " \t \t    comment */",
8492                    Tab));
8493 
8494   EXPECT_EQ("int a; /* some\n"
8495             "comment */",
8496             format(" \t \t int\ta; /* some\n"
8497                    " \t \t    comment */",
8498                    Tab));
8499   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8500             "    comment */",
8501             format(" \t \t f(\"\t\t\"); /* some\n"
8502                    " \t \t    comment */",
8503                    Tab));
8504   EXPECT_EQ("{\n"
8505             "  /*\n"
8506             "   * Comment\n"
8507             "   */\n"
8508             "  int i;\n"
8509             "}",
8510             format("{\n"
8511                    "\t/*\n"
8512                    "\t * Comment\n"
8513                    "\t */\n"
8514                    "\t int i;\n"
8515                    "}"));
8516 
8517   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8518   Tab.TabWidth = 8;
8519   Tab.IndentWidth = 8;
8520   EXPECT_EQ("if (aaaaaaaa && // q\n"
8521             "    bb)         // w\n"
8522             "\t;",
8523             format("if (aaaaaaaa &&// q\n"
8524                    "bb)// w\n"
8525                    ";",
8526                    Tab));
8527   EXPECT_EQ("if (aaa && bbb) // w\n"
8528             "\t;",
8529             format("if(aaa&&bbb)// w\n"
8530                    ";",
8531                    Tab));
8532   verifyFormat("class X {\n"
8533                "\tvoid f() {\n"
8534                "\t\tsomeFunction(parameter1,\n"
8535                "\t\t\t     parameter2);\n"
8536                "\t}\n"
8537                "};",
8538                Tab);
8539   verifyFormat("#define A                        \\\n"
8540                "\tvoid f() {               \\\n"
8541                "\t\tsomeFunction(    \\\n"
8542                "\t\t    parameter1,  \\\n"
8543                "\t\t    parameter2); \\\n"
8544                "\t}",
8545                Tab);
8546   Tab.TabWidth = 4;
8547   Tab.IndentWidth = 8;
8548   verifyFormat("class TabWidth4Indent8 {\n"
8549                "\t\tvoid f() {\n"
8550                "\t\t\t\tsomeFunction(parameter1,\n"
8551                "\t\t\t\t\t\t\t parameter2);\n"
8552                "\t\t}\n"
8553                "};",
8554                Tab);
8555   Tab.TabWidth = 4;
8556   Tab.IndentWidth = 4;
8557   verifyFormat("class TabWidth4Indent4 {\n"
8558                "\tvoid f() {\n"
8559                "\t\tsomeFunction(parameter1,\n"
8560                "\t\t\t\t\t parameter2);\n"
8561                "\t}\n"
8562                "};",
8563                Tab);
8564   Tab.TabWidth = 8;
8565   Tab.IndentWidth = 4;
8566   verifyFormat("class TabWidth8Indent4 {\n"
8567                "    void f() {\n"
8568                "\tsomeFunction(parameter1,\n"
8569                "\t\t     parameter2);\n"
8570                "    }\n"
8571                "};",
8572                Tab);
8573   Tab.TabWidth = 8;
8574   Tab.IndentWidth = 8;
8575   EXPECT_EQ("/*\n"
8576             "\t      a\t\tcomment\n"
8577             "\t      in multiple lines\n"
8578             "       */",
8579             format("   /*\t \t \n"
8580                    " \t \t a\t\tcomment\t \t\n"
8581                    " \t \t in multiple lines\t\n"
8582                    " \t  */",
8583                    Tab));
8584   verifyFormat("{\n"
8585                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8586                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8587                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8588                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8589                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8590                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8591                "};",
8592                Tab);
8593   verifyFormat("enum AA {\n"
8594                "\ta1, // Force multiple lines\n"
8595                "\ta2,\n"
8596                "\ta3\n"
8597                "};",
8598                Tab);
8599   EXPECT_EQ("if (aaaaaaaa && // q\n"
8600             "    bb)         // w\n"
8601             "\t;",
8602             format("if (aaaaaaaa &&// q\n"
8603                    "bb)// w\n"
8604                    ";",
8605                    Tab));
8606   verifyFormat("class X {\n"
8607                "\tvoid f() {\n"
8608                "\t\tsomeFunction(parameter1,\n"
8609                "\t\t\t     parameter2);\n"
8610                "\t}\n"
8611                "};",
8612                Tab);
8613   verifyFormat("{\n"
8614                "\tQ(\n"
8615                "\t    {\n"
8616                "\t\t    int a;\n"
8617                "\t\t    someFunction(aaaaaaaa,\n"
8618                "\t\t\t\t bbbbbbb);\n"
8619                "\t    },\n"
8620                "\t    p);\n"
8621                "}",
8622                Tab);
8623   EXPECT_EQ("{\n"
8624             "\t/* aaaa\n"
8625             "\t   bbbb */\n"
8626             "}",
8627             format("{\n"
8628                    "/* aaaa\n"
8629                    "   bbbb */\n"
8630                    "}",
8631                    Tab));
8632   EXPECT_EQ("{\n"
8633             "\t/*\n"
8634             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8635             "\t  bbbbbbbbbbbbb\n"
8636             "\t*/\n"
8637             "}",
8638             format("{\n"
8639                    "/*\n"
8640                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8641                    "*/\n"
8642                    "}",
8643                    Tab));
8644   EXPECT_EQ("{\n"
8645             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8646             "\t// bbbbbbbbbbbbb\n"
8647             "}",
8648             format("{\n"
8649                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8650                    "}",
8651                    Tab));
8652   EXPECT_EQ("{\n"
8653             "\t/*\n"
8654             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8655             "\t  bbbbbbbbbbbbb\n"
8656             "\t*/\n"
8657             "}",
8658             format("{\n"
8659                    "\t/*\n"
8660                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8661                    "\t*/\n"
8662                    "}",
8663                    Tab));
8664   EXPECT_EQ("{\n"
8665             "\t/*\n"
8666             "\n"
8667             "\t*/\n"
8668             "}",
8669             format("{\n"
8670                    "\t/*\n"
8671                    "\n"
8672                    "\t*/\n"
8673                    "}",
8674                    Tab));
8675   EXPECT_EQ("{\n"
8676             "\t/*\n"
8677             " asdf\n"
8678             "\t*/\n"
8679             "}",
8680             format("{\n"
8681                    "\t/*\n"
8682                    " asdf\n"
8683                    "\t*/\n"
8684                    "}",
8685                    Tab));
8686   EXPECT_EQ("/*\n"
8687             "\t      a\t\tcomment\n"
8688             "\t      in multiple lines\n"
8689             "       */",
8690             format("   /*\t \t \n"
8691                    " \t \t a\t\tcomment\t \t\n"
8692                    " \t \t in multiple lines\t\n"
8693                    " \t  */",
8694                    Tab));
8695   EXPECT_EQ("/* some\n"
8696             "   comment */",
8697             format(" \t \t /* some\n"
8698                    " \t \t    comment */",
8699                    Tab));
8700   EXPECT_EQ("int a; /* some\n"
8701             "   comment */",
8702             format(" \t \t int a; /* some\n"
8703                    " \t \t    comment */",
8704                    Tab));
8705   EXPECT_EQ("int a; /* some\n"
8706             "comment */",
8707             format(" \t \t int\ta; /* some\n"
8708                    " \t \t    comment */",
8709                    Tab));
8710   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8711             "    comment */",
8712             format(" \t \t f(\"\t\t\"); /* some\n"
8713                    " \t \t    comment */",
8714                    Tab));
8715   EXPECT_EQ("{\n"
8716             "  /*\n"
8717             "   * Comment\n"
8718             "   */\n"
8719             "  int i;\n"
8720             "}",
8721             format("{\n"
8722                    "\t/*\n"
8723                    "\t * Comment\n"
8724                    "\t */\n"
8725                    "\t int i;\n"
8726                    "}"));
8727   Tab.AlignConsecutiveAssignments = true;
8728   Tab.AlignConsecutiveDeclarations = true;
8729   Tab.TabWidth = 4;
8730   Tab.IndentWidth = 4;
8731   verifyFormat("class Assign {\n"
8732                "\tvoid f() {\n"
8733                "\t\tint         x      = 123;\n"
8734                "\t\tint         random = 4;\n"
8735                "\t\tstd::string alphabet =\n"
8736                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8737                "\t}\n"
8738                "};",
8739                Tab);
8740 }
8741 
8742 TEST_F(FormatTest, CalculatesOriginalColumn) {
8743   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8744             "q\"; /* some\n"
8745             "       comment */",
8746             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8747                    "q\"; /* some\n"
8748                    "       comment */",
8749                    getLLVMStyle()));
8750   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8751             "/* some\n"
8752             "   comment */",
8753             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8754                    " /* some\n"
8755                    "    comment */",
8756                    getLLVMStyle()));
8757   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8758             "qqq\n"
8759             "/* some\n"
8760             "   comment */",
8761             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8762                    "qqq\n"
8763                    " /* some\n"
8764                    "    comment */",
8765                    getLLVMStyle()));
8766   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8767             "wwww; /* some\n"
8768             "         comment */",
8769             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8770                    "wwww; /* some\n"
8771                    "         comment */",
8772                    getLLVMStyle()));
8773 }
8774 
8775 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8776   FormatStyle NoSpace = getLLVMStyle();
8777   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8778 
8779   verifyFormat("while(true)\n"
8780                "  continue;",
8781                NoSpace);
8782   verifyFormat("for(;;)\n"
8783                "  continue;",
8784                NoSpace);
8785   verifyFormat("if(true)\n"
8786                "  f();\n"
8787                "else if(true)\n"
8788                "  f();",
8789                NoSpace);
8790   verifyFormat("do {\n"
8791                "  do_something();\n"
8792                "} while(something());",
8793                NoSpace);
8794   verifyFormat("switch(x) {\n"
8795                "default:\n"
8796                "  break;\n"
8797                "}",
8798                NoSpace);
8799   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8800   verifyFormat("size_t x = sizeof(x);", NoSpace);
8801   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8802   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8803   verifyFormat("alignas(128) char a[128];", NoSpace);
8804   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8805   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8806   verifyFormat("int f() throw(Deprecated);", NoSpace);
8807   verifyFormat("typedef void (*cb)(int);", NoSpace);
8808   verifyFormat("T A::operator()();", NoSpace);
8809   verifyFormat("X A::operator++(T);", NoSpace);
8810 
8811   FormatStyle Space = getLLVMStyle();
8812   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8813 
8814   verifyFormat("int f ();", Space);
8815   verifyFormat("void f (int a, T b) {\n"
8816                "  while (true)\n"
8817                "    continue;\n"
8818                "}",
8819                Space);
8820   verifyFormat("if (true)\n"
8821                "  f ();\n"
8822                "else if (true)\n"
8823                "  f ();",
8824                Space);
8825   verifyFormat("do {\n"
8826                "  do_something ();\n"
8827                "} while (something ());",
8828                Space);
8829   verifyFormat("switch (x) {\n"
8830                "default:\n"
8831                "  break;\n"
8832                "}",
8833                Space);
8834   verifyFormat("A::A () : a (1) {}", Space);
8835   verifyFormat("void f () __attribute__ ((asdf));", Space);
8836   verifyFormat("*(&a + 1);\n"
8837                "&((&a)[1]);\n"
8838                "a[(b + c) * d];\n"
8839                "(((a + 1) * 2) + 3) * 4;",
8840                Space);
8841   verifyFormat("#define A(x) x", Space);
8842   verifyFormat("#define A (x) x", Space);
8843   verifyFormat("#if defined(x)\n"
8844                "#endif",
8845                Space);
8846   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8847   verifyFormat("size_t x = sizeof (x);", Space);
8848   verifyFormat("auto f (int x) -> decltype (x);", Space);
8849   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8850   verifyFormat("alignas (128) char a[128];", Space);
8851   verifyFormat("size_t x = alignof (MyType);", Space);
8852   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8853   verifyFormat("int f () throw (Deprecated);", Space);
8854   verifyFormat("typedef void (*cb) (int);", Space);
8855   verifyFormat("T A::operator() ();", Space);
8856   verifyFormat("X A::operator++ (T);", Space);
8857 }
8858 
8859 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8860   FormatStyle Spaces = getLLVMStyle();
8861 
8862   Spaces.SpacesInParentheses = true;
8863   verifyFormat("do_something( ::globalVar );", Spaces);
8864   verifyFormat("call( x, y, z );", Spaces);
8865   verifyFormat("call();", Spaces);
8866   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8867   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8868                Spaces);
8869   verifyFormat("while ( (bool)1 )\n"
8870                "  continue;",
8871                Spaces);
8872   verifyFormat("for ( ;; )\n"
8873                "  continue;",
8874                Spaces);
8875   verifyFormat("if ( true )\n"
8876                "  f();\n"
8877                "else if ( true )\n"
8878                "  f();",
8879                Spaces);
8880   verifyFormat("do {\n"
8881                "  do_something( (int)i );\n"
8882                "} while ( something() );",
8883                Spaces);
8884   verifyFormat("switch ( x ) {\n"
8885                "default:\n"
8886                "  break;\n"
8887                "}",
8888                Spaces);
8889 
8890   Spaces.SpacesInParentheses = false;
8891   Spaces.SpacesInCStyleCastParentheses = true;
8892   verifyFormat("Type *A = ( Type * )P;", Spaces);
8893   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8894   verifyFormat("x = ( int32 )y;", Spaces);
8895   verifyFormat("int a = ( int )(2.0f);", Spaces);
8896   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8897   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8898   verifyFormat("#define x (( int )-1)", Spaces);
8899 
8900   // Run the first set of tests again with:
8901   Spaces.SpacesInParentheses = false;
8902   Spaces.SpaceInEmptyParentheses = true;
8903   Spaces.SpacesInCStyleCastParentheses = true;
8904   verifyFormat("call(x, y, z);", Spaces);
8905   verifyFormat("call( );", Spaces);
8906   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8907   verifyFormat("while (( bool )1)\n"
8908                "  continue;",
8909                Spaces);
8910   verifyFormat("for (;;)\n"
8911                "  continue;",
8912                Spaces);
8913   verifyFormat("if (true)\n"
8914                "  f( );\n"
8915                "else if (true)\n"
8916                "  f( );",
8917                Spaces);
8918   verifyFormat("do {\n"
8919                "  do_something(( int )i);\n"
8920                "} while (something( ));",
8921                Spaces);
8922   verifyFormat("switch (x) {\n"
8923                "default:\n"
8924                "  break;\n"
8925                "}",
8926                Spaces);
8927 
8928   // Run the first set of tests again with:
8929   Spaces.SpaceAfterCStyleCast = true;
8930   verifyFormat("call(x, y, z);", Spaces);
8931   verifyFormat("call( );", Spaces);
8932   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8933   verifyFormat("while (( bool ) 1)\n"
8934                "  continue;",
8935                Spaces);
8936   verifyFormat("for (;;)\n"
8937                "  continue;",
8938                Spaces);
8939   verifyFormat("if (true)\n"
8940                "  f( );\n"
8941                "else if (true)\n"
8942                "  f( );",
8943                Spaces);
8944   verifyFormat("do {\n"
8945                "  do_something(( int ) i);\n"
8946                "} while (something( ));",
8947                Spaces);
8948   verifyFormat("switch (x) {\n"
8949                "default:\n"
8950                "  break;\n"
8951                "}",
8952                Spaces);
8953 
8954   // Run subset of tests again with:
8955   Spaces.SpacesInCStyleCastParentheses = false;
8956   Spaces.SpaceAfterCStyleCast = true;
8957   verifyFormat("while ((bool) 1)\n"
8958                "  continue;",
8959                Spaces);
8960   verifyFormat("do {\n"
8961                "  do_something((int) i);\n"
8962                "} while (something( ));",
8963                Spaces);
8964 }
8965 
8966 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8967   verifyFormat("int a[5];");
8968   verifyFormat("a[3] += 42;");
8969 
8970   FormatStyle Spaces = getLLVMStyle();
8971   Spaces.SpacesInSquareBrackets = true;
8972   // Lambdas unchanged.
8973   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8974   verifyFormat("return [i, args...] {};", Spaces);
8975 
8976   // Not lambdas.
8977   verifyFormat("int a[ 5 ];", Spaces);
8978   verifyFormat("a[ 3 ] += 42;", Spaces);
8979   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8980   verifyFormat("double &operator[](int i) { return 0; }\n"
8981                "int i;",
8982                Spaces);
8983   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8984   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8985   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8986 }
8987 
8988 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8989   verifyFormat("int a = 5;");
8990   verifyFormat("a += 42;");
8991   verifyFormat("a or_eq 8;");
8992 
8993   FormatStyle Spaces = getLLVMStyle();
8994   Spaces.SpaceBeforeAssignmentOperators = false;
8995   verifyFormat("int a= 5;", Spaces);
8996   verifyFormat("a+= 42;", Spaces);
8997   verifyFormat("a or_eq 8;", Spaces);
8998 }
8999 
9000 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9001   verifyFormat("class Foo : public Bar {};");
9002   verifyFormat("Foo::Foo() : foo(1) {}");
9003   verifyFormat("for (auto a : b) {\n}");
9004   verifyFormat("int x = a ? b : c;");
9005   verifyFormat("{\n"
9006                "label0:\n"
9007                "  int x = 0;\n"
9008                "}");
9009   verifyFormat("switch (x) {\n"
9010                "case 1:\n"
9011                "default:\n"
9012                "}");
9013 
9014   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9015   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9016   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9017   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9018   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9019   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9020   verifyFormat("{\n"
9021                "label1:\n"
9022                "  int x = 0;\n"
9023                "}",
9024                CtorInitializerStyle);
9025   verifyFormat("switch (x) {\n"
9026                "case 1:\n"
9027                "default:\n"
9028                "}",
9029                CtorInitializerStyle);
9030   CtorInitializerStyle.BreakConstructorInitializers =
9031       FormatStyle::BCIS_AfterColon;
9032   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9033                "    aaaaaaaaaaaaaaaa(1),\n"
9034                "    bbbbbbbbbbbbbbbb(2) {}",
9035                CtorInitializerStyle);
9036   CtorInitializerStyle.BreakConstructorInitializers =
9037       FormatStyle::BCIS_BeforeComma;
9038   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9039                "    : aaaaaaaaaaaaaaaa(1)\n"
9040                "    , bbbbbbbbbbbbbbbb(2) {}",
9041                CtorInitializerStyle);
9042   CtorInitializerStyle.BreakConstructorInitializers =
9043       FormatStyle::BCIS_BeforeColon;
9044   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9045                "    : aaaaaaaaaaaaaaaa(1),\n"
9046                "      bbbbbbbbbbbbbbbb(2) {}",
9047                CtorInitializerStyle);
9048   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9049   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9050                ": aaaaaaaaaaaaaaaa(1),\n"
9051                "  bbbbbbbbbbbbbbbb(2) {}",
9052                CtorInitializerStyle);
9053 
9054   FormatStyle InheritanceStyle = getLLVMStyle();
9055   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9056   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9057   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9058   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9059   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9060   verifyFormat("{\n"
9061                "label2:\n"
9062                "  int x = 0;\n"
9063                "}",
9064                InheritanceStyle);
9065   verifyFormat("switch (x) {\n"
9066                "case 1:\n"
9067                "default:\n"
9068                "}",
9069                InheritanceStyle);
9070 
9071   FormatStyle ForLoopStyle = getLLVMStyle();
9072   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9073   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9074   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9075   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9076   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9077   verifyFormat("{\n"
9078                "label2:\n"
9079                "  int x = 0;\n"
9080                "}",
9081                ForLoopStyle);
9082   verifyFormat("switch (x) {\n"
9083                "case 1:\n"
9084                "default:\n"
9085                "}",
9086                ForLoopStyle);
9087 
9088   FormatStyle NoSpaceStyle = getLLVMStyle();
9089   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9090   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9091   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9092   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9093   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9094   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9095   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9096   verifyFormat("{\n"
9097                "label3:\n"
9098                "  int x = 0;\n"
9099                "}",
9100                NoSpaceStyle);
9101   verifyFormat("switch (x) {\n"
9102                "case 1:\n"
9103                "default:\n"
9104                "}",
9105                NoSpaceStyle);
9106 }
9107 
9108 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9109   FormatStyle Alignment = getLLVMStyle();
9110   Alignment.AlignConsecutiveAssignments = false;
9111   verifyFormat("int a = 5;\n"
9112                "int oneTwoThree = 123;",
9113                Alignment);
9114   verifyFormat("int a = 5;\n"
9115                "int oneTwoThree = 123;",
9116                Alignment);
9117 
9118   Alignment.AlignConsecutiveAssignments = true;
9119   verifyFormat("int a           = 5;\n"
9120                "int oneTwoThree = 123;",
9121                Alignment);
9122   verifyFormat("int a           = method();\n"
9123                "int oneTwoThree = 133;",
9124                Alignment);
9125   verifyFormat("a &= 5;\n"
9126                "bcd *= 5;\n"
9127                "ghtyf += 5;\n"
9128                "dvfvdb -= 5;\n"
9129                "a /= 5;\n"
9130                "vdsvsv %= 5;\n"
9131                "sfdbddfbdfbb ^= 5;\n"
9132                "dvsdsv |= 5;\n"
9133                "int dsvvdvsdvvv = 123;",
9134                Alignment);
9135   verifyFormat("int i = 1, j = 10;\n"
9136                "something = 2000;",
9137                Alignment);
9138   verifyFormat("something = 2000;\n"
9139                "int i = 1, j = 10;\n",
9140                Alignment);
9141   verifyFormat("something = 2000;\n"
9142                "another   = 911;\n"
9143                "int i = 1, j = 10;\n"
9144                "oneMore = 1;\n"
9145                "i       = 2;",
9146                Alignment);
9147   verifyFormat("int a   = 5;\n"
9148                "int one = 1;\n"
9149                "method();\n"
9150                "int oneTwoThree = 123;\n"
9151                "int oneTwo      = 12;",
9152                Alignment);
9153   verifyFormat("int oneTwoThree = 123;\n"
9154                "int oneTwo      = 12;\n"
9155                "method();\n",
9156                Alignment);
9157   verifyFormat("int oneTwoThree = 123; // comment\n"
9158                "int oneTwo      = 12;  // comment",
9159                Alignment);
9160   EXPECT_EQ("int a = 5;\n"
9161             "\n"
9162             "int oneTwoThree = 123;",
9163             format("int a       = 5;\n"
9164                    "\n"
9165                    "int oneTwoThree= 123;",
9166                    Alignment));
9167   EXPECT_EQ("int a   = 5;\n"
9168             "int one = 1;\n"
9169             "\n"
9170             "int oneTwoThree = 123;",
9171             format("int a = 5;\n"
9172                    "int one = 1;\n"
9173                    "\n"
9174                    "int oneTwoThree = 123;",
9175                    Alignment));
9176   EXPECT_EQ("int a   = 5;\n"
9177             "int one = 1;\n"
9178             "\n"
9179             "int oneTwoThree = 123;\n"
9180             "int oneTwo      = 12;",
9181             format("int a = 5;\n"
9182                    "int one = 1;\n"
9183                    "\n"
9184                    "int oneTwoThree = 123;\n"
9185                    "int oneTwo = 12;",
9186                    Alignment));
9187   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9188   verifyFormat("#define A \\\n"
9189                "  int aaaa       = 12; \\\n"
9190                "  int b          = 23; \\\n"
9191                "  int ccc        = 234; \\\n"
9192                "  int dddddddddd = 2345;",
9193                Alignment);
9194   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9195   verifyFormat("#define A               \\\n"
9196                "  int aaaa       = 12;  \\\n"
9197                "  int b          = 23;  \\\n"
9198                "  int ccc        = 234; \\\n"
9199                "  int dddddddddd = 2345;",
9200                Alignment);
9201   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9202   verifyFormat("#define A                                                      "
9203                "                \\\n"
9204                "  int aaaa       = 12;                                         "
9205                "                \\\n"
9206                "  int b          = 23;                                         "
9207                "                \\\n"
9208                "  int ccc        = 234;                                        "
9209                "                \\\n"
9210                "  int dddddddddd = 2345;",
9211                Alignment);
9212   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9213                "k = 4, int l = 5,\n"
9214                "                  int m = 6) {\n"
9215                "  int j      = 10;\n"
9216                "  otherThing = 1;\n"
9217                "}",
9218                Alignment);
9219   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9220                "  int i   = 1;\n"
9221                "  int j   = 2;\n"
9222                "  int big = 10000;\n"
9223                "}",
9224                Alignment);
9225   verifyFormat("class C {\n"
9226                "public:\n"
9227                "  int i            = 1;\n"
9228                "  virtual void f() = 0;\n"
9229                "};",
9230                Alignment);
9231   verifyFormat("int i = 1;\n"
9232                "if (SomeType t = getSomething()) {\n"
9233                "}\n"
9234                "int j   = 2;\n"
9235                "int big = 10000;",
9236                Alignment);
9237   verifyFormat("int j = 7;\n"
9238                "for (int k = 0; k < N; ++k) {\n"
9239                "}\n"
9240                "int j   = 2;\n"
9241                "int big = 10000;\n"
9242                "}",
9243                Alignment);
9244   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9245   verifyFormat("int i = 1;\n"
9246                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9247                "    = someLooooooooooooooooongFunction();\n"
9248                "int j = 2;",
9249                Alignment);
9250   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9251   verifyFormat("int i = 1;\n"
9252                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9253                "    someLooooooooooooooooongFunction();\n"
9254                "int j = 2;",
9255                Alignment);
9256 
9257   verifyFormat("auto lambda = []() {\n"
9258                "  auto i = 0;\n"
9259                "  return 0;\n"
9260                "};\n"
9261                "int i  = 0;\n"
9262                "auto v = type{\n"
9263                "    i = 1,   //\n"
9264                "    (i = 2), //\n"
9265                "    i = 3    //\n"
9266                "};",
9267                Alignment);
9268 
9269   verifyFormat(
9270       "int i      = 1;\n"
9271       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9272       "                          loooooooooooooooooooooongParameterB);\n"
9273       "int j      = 2;",
9274       Alignment);
9275 
9276   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9277                "          typename B   = very_long_type_name_1,\n"
9278                "          typename T_2 = very_long_type_name_2>\n"
9279                "auto foo() {}\n",
9280                Alignment);
9281   verifyFormat("int a, b = 1;\n"
9282                "int c  = 2;\n"
9283                "int dd = 3;\n",
9284                Alignment);
9285   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9286                "float b[1][] = {{3.f}};\n",
9287                Alignment);
9288   verifyFormat("for (int i = 0; i < 1; i++)\n"
9289                "  int x = 1;\n",
9290                Alignment);
9291   verifyFormat("for (i = 0; i < 1; i++)\n"
9292                "  x = 1;\n"
9293                "y = 1;\n",
9294                Alignment);
9295 }
9296 
9297 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9298   FormatStyle Alignment = getLLVMStyle();
9299   Alignment.AlignConsecutiveDeclarations = false;
9300   verifyFormat("float const a = 5;\n"
9301                "int oneTwoThree = 123;",
9302                Alignment);
9303   verifyFormat("int a = 5;\n"
9304                "float const oneTwoThree = 123;",
9305                Alignment);
9306 
9307   Alignment.AlignConsecutiveDeclarations = true;
9308   verifyFormat("float const a = 5;\n"
9309                "int         oneTwoThree = 123;",
9310                Alignment);
9311   verifyFormat("int         a = method();\n"
9312                "float const oneTwoThree = 133;",
9313                Alignment);
9314   verifyFormat("int i = 1, j = 10;\n"
9315                "something = 2000;",
9316                Alignment);
9317   verifyFormat("something = 2000;\n"
9318                "int i = 1, j = 10;\n",
9319                Alignment);
9320   verifyFormat("float      something = 2000;\n"
9321                "double     another = 911;\n"
9322                "int        i = 1, j = 10;\n"
9323                "const int *oneMore = 1;\n"
9324                "unsigned   i = 2;",
9325                Alignment);
9326   verifyFormat("float a = 5;\n"
9327                "int   one = 1;\n"
9328                "method();\n"
9329                "const double       oneTwoThree = 123;\n"
9330                "const unsigned int oneTwo = 12;",
9331                Alignment);
9332   verifyFormat("int      oneTwoThree{0}; // comment\n"
9333                "unsigned oneTwo;         // comment",
9334                Alignment);
9335   EXPECT_EQ("float const a = 5;\n"
9336             "\n"
9337             "int oneTwoThree = 123;",
9338             format("float const   a = 5;\n"
9339                    "\n"
9340                    "int           oneTwoThree= 123;",
9341                    Alignment));
9342   EXPECT_EQ("float a = 5;\n"
9343             "int   one = 1;\n"
9344             "\n"
9345             "unsigned oneTwoThree = 123;",
9346             format("float    a = 5;\n"
9347                    "int      one = 1;\n"
9348                    "\n"
9349                    "unsigned oneTwoThree = 123;",
9350                    Alignment));
9351   EXPECT_EQ("float a = 5;\n"
9352             "int   one = 1;\n"
9353             "\n"
9354             "unsigned oneTwoThree = 123;\n"
9355             "int      oneTwo = 12;",
9356             format("float    a = 5;\n"
9357                    "int one = 1;\n"
9358                    "\n"
9359                    "unsigned oneTwoThree = 123;\n"
9360                    "int oneTwo = 12;",
9361                    Alignment));
9362   // Function prototype alignment
9363   verifyFormat("int    a();\n"
9364                "double b();",
9365                Alignment);
9366   verifyFormat("int    a(int x);\n"
9367                "double b();",
9368                Alignment);
9369   unsigned OldColumnLimit = Alignment.ColumnLimit;
9370   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9371   // otherwise the function parameters will be re-flowed onto a single line.
9372   Alignment.ColumnLimit = 0;
9373   EXPECT_EQ("int    a(int   x,\n"
9374             "         float y);\n"
9375             "double b(int    x,\n"
9376             "         double y);",
9377             format("int a(int x,\n"
9378                    " float y);\n"
9379                    "double b(int x,\n"
9380                    " double y);",
9381                    Alignment));
9382   // This ensures that function parameters of function declarations are
9383   // correctly indented when their owning functions are indented.
9384   // The failure case here is for 'double y' to not be indented enough.
9385   EXPECT_EQ("double a(int x);\n"
9386             "int    b(int    y,\n"
9387             "         double z);",
9388             format("double a(int x);\n"
9389                    "int b(int y,\n"
9390                    " double z);",
9391                    Alignment));
9392   // Set ColumnLimit low so that we induce wrapping immediately after
9393   // the function name and opening paren.
9394   Alignment.ColumnLimit = 13;
9395   verifyFormat("int function(\n"
9396                "    int  x,\n"
9397                "    bool y);",
9398                Alignment);
9399   Alignment.ColumnLimit = OldColumnLimit;
9400   // Ensure function pointers don't screw up recursive alignment
9401   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9402                "double b();",
9403                Alignment);
9404   Alignment.AlignConsecutiveAssignments = true;
9405   // Ensure recursive alignment is broken by function braces, so that the
9406   // "a = 1" does not align with subsequent assignments inside the function
9407   // body.
9408   verifyFormat("int func(int a = 1) {\n"
9409                "  int b  = 2;\n"
9410                "  int cc = 3;\n"
9411                "}",
9412                Alignment);
9413   verifyFormat("float      something = 2000;\n"
9414                "double     another   = 911;\n"
9415                "int        i = 1, j = 10;\n"
9416                "const int *oneMore = 1;\n"
9417                "unsigned   i       = 2;",
9418                Alignment);
9419   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9420                "unsigned oneTwo      = 0;   // comment",
9421                Alignment);
9422   // Make sure that scope is correctly tracked, in the absence of braces
9423   verifyFormat("for (int i = 0; i < n; i++)\n"
9424                "  j = i;\n"
9425                "double x = 1;\n",
9426                Alignment);
9427   verifyFormat("if (int i = 0)\n"
9428                "  j = i;\n"
9429                "double x = 1;\n",
9430                Alignment);
9431   // Ensure operator[] and operator() are comprehended
9432   verifyFormat("struct test {\n"
9433                "  long long int foo();\n"
9434                "  int           operator[](int a);\n"
9435                "  double        bar();\n"
9436                "};\n",
9437                Alignment);
9438   verifyFormat("struct test {\n"
9439                "  long long int foo();\n"
9440                "  int           operator()(int a);\n"
9441                "  double        bar();\n"
9442                "};\n",
9443                Alignment);
9444   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9445             "  int const i   = 1;\n"
9446             "  int *     j   = 2;\n"
9447             "  int       big = 10000;\n"
9448             "\n"
9449             "  unsigned oneTwoThree = 123;\n"
9450             "  int      oneTwo      = 12;\n"
9451             "  method();\n"
9452             "  float k  = 2;\n"
9453             "  int   ll = 10000;\n"
9454             "}",
9455             format("void SomeFunction(int parameter= 0) {\n"
9456                    " int const  i= 1;\n"
9457                    "  int *j=2;\n"
9458                    " int big  =  10000;\n"
9459                    "\n"
9460                    "unsigned oneTwoThree  =123;\n"
9461                    "int oneTwo = 12;\n"
9462                    "  method();\n"
9463                    "float k= 2;\n"
9464                    "int ll=10000;\n"
9465                    "}",
9466                    Alignment));
9467   Alignment.AlignConsecutiveAssignments = false;
9468   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9469   verifyFormat("#define A \\\n"
9470                "  int       aaaa = 12; \\\n"
9471                "  float     b = 23; \\\n"
9472                "  const int ccc = 234; \\\n"
9473                "  unsigned  dddddddddd = 2345;",
9474                Alignment);
9475   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9476   verifyFormat("#define A              \\\n"
9477                "  int       aaaa = 12; \\\n"
9478                "  float     b = 23;    \\\n"
9479                "  const int ccc = 234; \\\n"
9480                "  unsigned  dddddddddd = 2345;",
9481                Alignment);
9482   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9483   Alignment.ColumnLimit = 30;
9484   verifyFormat("#define A                    \\\n"
9485                "  int       aaaa = 12;       \\\n"
9486                "  float     b = 23;          \\\n"
9487                "  const int ccc = 234;       \\\n"
9488                "  int       dddddddddd = 2345;",
9489                Alignment);
9490   Alignment.ColumnLimit = 80;
9491   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9492                "k = 4, int l = 5,\n"
9493                "                  int m = 6) {\n"
9494                "  const int j = 10;\n"
9495                "  otherThing = 1;\n"
9496                "}",
9497                Alignment);
9498   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9499                "  int const i = 1;\n"
9500                "  int *     j = 2;\n"
9501                "  int       big = 10000;\n"
9502                "}",
9503                Alignment);
9504   verifyFormat("class C {\n"
9505                "public:\n"
9506                "  int          i = 1;\n"
9507                "  virtual void f() = 0;\n"
9508                "};",
9509                Alignment);
9510   verifyFormat("float i = 1;\n"
9511                "if (SomeType t = getSomething()) {\n"
9512                "}\n"
9513                "const unsigned j = 2;\n"
9514                "int            big = 10000;",
9515                Alignment);
9516   verifyFormat("float j = 7;\n"
9517                "for (int k = 0; k < N; ++k) {\n"
9518                "}\n"
9519                "unsigned j = 2;\n"
9520                "int      big = 10000;\n"
9521                "}",
9522                Alignment);
9523   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9524   verifyFormat("float              i = 1;\n"
9525                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9526                "    = someLooooooooooooooooongFunction();\n"
9527                "int j = 2;",
9528                Alignment);
9529   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9530   verifyFormat("int                i = 1;\n"
9531                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9532                "    someLooooooooooooooooongFunction();\n"
9533                "int j = 2;",
9534                Alignment);
9535 
9536   Alignment.AlignConsecutiveAssignments = true;
9537   verifyFormat("auto lambda = []() {\n"
9538                "  auto  ii = 0;\n"
9539                "  float j  = 0;\n"
9540                "  return 0;\n"
9541                "};\n"
9542                "int   i  = 0;\n"
9543                "float i2 = 0;\n"
9544                "auto  v  = type{\n"
9545                "    i = 1,   //\n"
9546                "    (i = 2), //\n"
9547                "    i = 3    //\n"
9548                "};",
9549                Alignment);
9550   Alignment.AlignConsecutiveAssignments = false;
9551 
9552   verifyFormat(
9553       "int      i = 1;\n"
9554       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9555       "                          loooooooooooooooooooooongParameterB);\n"
9556       "int      j = 2;",
9557       Alignment);
9558 
9559   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9560   // We expect declarations and assignments to align, as long as it doesn't
9561   // exceed the column limit, starting a new alignment sequence whenever it
9562   // happens.
9563   Alignment.AlignConsecutiveAssignments = true;
9564   Alignment.ColumnLimit = 30;
9565   verifyFormat("float    ii              = 1;\n"
9566                "unsigned j               = 2;\n"
9567                "int someVerylongVariable = 1;\n"
9568                "AnotherLongType  ll = 123456;\n"
9569                "VeryVeryLongType k  = 2;\n"
9570                "int              myvar = 1;",
9571                Alignment);
9572   Alignment.ColumnLimit = 80;
9573   Alignment.AlignConsecutiveAssignments = false;
9574 
9575   verifyFormat(
9576       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9577       "          typename LongType, typename B>\n"
9578       "auto foo() {}\n",
9579       Alignment);
9580   verifyFormat("float a, b = 1;\n"
9581                "int   c = 2;\n"
9582                "int   dd = 3;\n",
9583                Alignment);
9584   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9585                "float b[1][] = {{3.f}};\n",
9586                Alignment);
9587   Alignment.AlignConsecutiveAssignments = true;
9588   verifyFormat("float a, b = 1;\n"
9589                "int   c  = 2;\n"
9590                "int   dd = 3;\n",
9591                Alignment);
9592   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9593                "float b[1][] = {{3.f}};\n",
9594                Alignment);
9595   Alignment.AlignConsecutiveAssignments = false;
9596 
9597   Alignment.ColumnLimit = 30;
9598   Alignment.BinPackParameters = false;
9599   verifyFormat("void foo(float     a,\n"
9600                "         float     b,\n"
9601                "         int       c,\n"
9602                "         uint32_t *d) {\n"
9603                "  int *  e = 0;\n"
9604                "  float  f = 0;\n"
9605                "  double g = 0;\n"
9606                "}\n"
9607                "void bar(ino_t     a,\n"
9608                "         int       b,\n"
9609                "         uint32_t *c,\n"
9610                "         bool      d) {}\n",
9611                Alignment);
9612   Alignment.BinPackParameters = true;
9613   Alignment.ColumnLimit = 80;
9614 
9615   // Bug 33507
9616   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9617   verifyFormat(
9618       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9619       "  static const Version verVs2017;\n"
9620       "  return true;\n"
9621       "});\n",
9622       Alignment);
9623   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9624 }
9625 
9626 TEST_F(FormatTest, LinuxBraceBreaking) {
9627   FormatStyle LinuxBraceStyle = getLLVMStyle();
9628   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9629   verifyFormat("namespace a\n"
9630                "{\n"
9631                "class A\n"
9632                "{\n"
9633                "  void f()\n"
9634                "  {\n"
9635                "    if (true) {\n"
9636                "      a();\n"
9637                "      b();\n"
9638                "    } else {\n"
9639                "      a();\n"
9640                "    }\n"
9641                "  }\n"
9642                "  void g() { return; }\n"
9643                "};\n"
9644                "struct B {\n"
9645                "  int x;\n"
9646                "};\n"
9647                "} // namespace a\n",
9648                LinuxBraceStyle);
9649   verifyFormat("enum X {\n"
9650                "  Y = 0,\n"
9651                "}\n",
9652                LinuxBraceStyle);
9653   verifyFormat("struct S {\n"
9654                "  int Type;\n"
9655                "  union {\n"
9656                "    int x;\n"
9657                "    double y;\n"
9658                "  } Value;\n"
9659                "  class C\n"
9660                "  {\n"
9661                "    MyFavoriteType Value;\n"
9662                "  } Class;\n"
9663                "}\n",
9664                LinuxBraceStyle);
9665 }
9666 
9667 TEST_F(FormatTest, MozillaBraceBreaking) {
9668   FormatStyle MozillaBraceStyle = getLLVMStyle();
9669   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9670   MozillaBraceStyle.FixNamespaceComments = false;
9671   verifyFormat("namespace a {\n"
9672                "class A\n"
9673                "{\n"
9674                "  void f()\n"
9675                "  {\n"
9676                "    if (true) {\n"
9677                "      a();\n"
9678                "      b();\n"
9679                "    }\n"
9680                "  }\n"
9681                "  void g() { return; }\n"
9682                "};\n"
9683                "enum E\n"
9684                "{\n"
9685                "  A,\n"
9686                "  // foo\n"
9687                "  B,\n"
9688                "  C\n"
9689                "};\n"
9690                "struct B\n"
9691                "{\n"
9692                "  int x;\n"
9693                "};\n"
9694                "}\n",
9695                MozillaBraceStyle);
9696   verifyFormat("struct S\n"
9697                "{\n"
9698                "  int Type;\n"
9699                "  union\n"
9700                "  {\n"
9701                "    int x;\n"
9702                "    double y;\n"
9703                "  } Value;\n"
9704                "  class C\n"
9705                "  {\n"
9706                "    MyFavoriteType Value;\n"
9707                "  } Class;\n"
9708                "}\n",
9709                MozillaBraceStyle);
9710 }
9711 
9712 TEST_F(FormatTest, StroustrupBraceBreaking) {
9713   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9714   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9715   verifyFormat("namespace a {\n"
9716                "class A {\n"
9717                "  void f()\n"
9718                "  {\n"
9719                "    if (true) {\n"
9720                "      a();\n"
9721                "      b();\n"
9722                "    }\n"
9723                "  }\n"
9724                "  void g() { return; }\n"
9725                "};\n"
9726                "struct B {\n"
9727                "  int x;\n"
9728                "};\n"
9729                "} // namespace a\n",
9730                StroustrupBraceStyle);
9731 
9732   verifyFormat("void foo()\n"
9733                "{\n"
9734                "  if (a) {\n"
9735                "    a();\n"
9736                "  }\n"
9737                "  else {\n"
9738                "    b();\n"
9739                "  }\n"
9740                "}\n",
9741                StroustrupBraceStyle);
9742 
9743   verifyFormat("#ifdef _DEBUG\n"
9744                "int foo(int i = 0)\n"
9745                "#else\n"
9746                "int foo(int i = 5)\n"
9747                "#endif\n"
9748                "{\n"
9749                "  return i;\n"
9750                "}",
9751                StroustrupBraceStyle);
9752 
9753   verifyFormat("void foo() {}\n"
9754                "void bar()\n"
9755                "#ifdef _DEBUG\n"
9756                "{\n"
9757                "  foo();\n"
9758                "}\n"
9759                "#else\n"
9760                "{\n"
9761                "}\n"
9762                "#endif",
9763                StroustrupBraceStyle);
9764 
9765   verifyFormat("void foobar() { int i = 5; }\n"
9766                "#ifdef _DEBUG\n"
9767                "void bar() {}\n"
9768                "#else\n"
9769                "void bar() { foobar(); }\n"
9770                "#endif",
9771                StroustrupBraceStyle);
9772 }
9773 
9774 TEST_F(FormatTest, AllmanBraceBreaking) {
9775   FormatStyle AllmanBraceStyle = getLLVMStyle();
9776   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9777 
9778   EXPECT_EQ("namespace a\n"
9779             "{\n"
9780             "void f();\n"
9781             "void g();\n"
9782             "} // namespace a\n",
9783             format("namespace a\n"
9784                    "{\n"
9785                    "void f();\n"
9786                    "void g();\n"
9787                    "}\n",
9788                    AllmanBraceStyle));
9789 
9790   verifyFormat("namespace a\n"
9791                "{\n"
9792                "class A\n"
9793                "{\n"
9794                "  void f()\n"
9795                "  {\n"
9796                "    if (true)\n"
9797                "    {\n"
9798                "      a();\n"
9799                "      b();\n"
9800                "    }\n"
9801                "  }\n"
9802                "  void g() { return; }\n"
9803                "};\n"
9804                "struct B\n"
9805                "{\n"
9806                "  int x;\n"
9807                "};\n"
9808                "} // namespace a",
9809                AllmanBraceStyle);
9810 
9811   verifyFormat("void f()\n"
9812                "{\n"
9813                "  if (true)\n"
9814                "  {\n"
9815                "    a();\n"
9816                "  }\n"
9817                "  else if (false)\n"
9818                "  {\n"
9819                "    b();\n"
9820                "  }\n"
9821                "  else\n"
9822                "  {\n"
9823                "    c();\n"
9824                "  }\n"
9825                "}\n",
9826                AllmanBraceStyle);
9827 
9828   verifyFormat("void f()\n"
9829                "{\n"
9830                "  for (int i = 0; i < 10; ++i)\n"
9831                "  {\n"
9832                "    a();\n"
9833                "  }\n"
9834                "  while (false)\n"
9835                "  {\n"
9836                "    b();\n"
9837                "  }\n"
9838                "  do\n"
9839                "  {\n"
9840                "    c();\n"
9841                "  } while (false)\n"
9842                "}\n",
9843                AllmanBraceStyle);
9844 
9845   verifyFormat("void f(int a)\n"
9846                "{\n"
9847                "  switch (a)\n"
9848                "  {\n"
9849                "  case 0:\n"
9850                "    break;\n"
9851                "  case 1:\n"
9852                "  {\n"
9853                "    break;\n"
9854                "  }\n"
9855                "  case 2:\n"
9856                "  {\n"
9857                "  }\n"
9858                "  break;\n"
9859                "  default:\n"
9860                "    break;\n"
9861                "  }\n"
9862                "}\n",
9863                AllmanBraceStyle);
9864 
9865   verifyFormat("enum X\n"
9866                "{\n"
9867                "  Y = 0,\n"
9868                "}\n",
9869                AllmanBraceStyle);
9870   verifyFormat("enum X\n"
9871                "{\n"
9872                "  Y = 0\n"
9873                "}\n",
9874                AllmanBraceStyle);
9875 
9876   verifyFormat("@interface BSApplicationController ()\n"
9877                "{\n"
9878                "@private\n"
9879                "  id _extraIvar;\n"
9880                "}\n"
9881                "@end\n",
9882                AllmanBraceStyle);
9883 
9884   verifyFormat("#ifdef _DEBUG\n"
9885                "int foo(int i = 0)\n"
9886                "#else\n"
9887                "int foo(int i = 5)\n"
9888                "#endif\n"
9889                "{\n"
9890                "  return i;\n"
9891                "}",
9892                AllmanBraceStyle);
9893 
9894   verifyFormat("void foo() {}\n"
9895                "void bar()\n"
9896                "#ifdef _DEBUG\n"
9897                "{\n"
9898                "  foo();\n"
9899                "}\n"
9900                "#else\n"
9901                "{\n"
9902                "}\n"
9903                "#endif",
9904                AllmanBraceStyle);
9905 
9906   verifyFormat("void foobar() { int i = 5; }\n"
9907                "#ifdef _DEBUG\n"
9908                "void bar() {}\n"
9909                "#else\n"
9910                "void bar() { foobar(); }\n"
9911                "#endif",
9912                AllmanBraceStyle);
9913 
9914   // This shouldn't affect ObjC blocks..
9915   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9916                "  // ...\n"
9917                "  int i;\n"
9918                "}];",
9919                AllmanBraceStyle);
9920   verifyFormat("void (^block)(void) = ^{\n"
9921                "  // ...\n"
9922                "  int i;\n"
9923                "};",
9924                AllmanBraceStyle);
9925   // .. or dict literals.
9926   verifyFormat("void f()\n"
9927                "{\n"
9928                "  // ...\n"
9929                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9930                "}",
9931                AllmanBraceStyle);
9932   verifyFormat("void f()\n"
9933                "{\n"
9934                "  // ...\n"
9935                "  [object someMethod:@{a : @\"b\"}];\n"
9936                "}",
9937                AllmanBraceStyle);
9938   verifyFormat("int f()\n"
9939                "{ // comment\n"
9940                "  return 42;\n"
9941                "}",
9942                AllmanBraceStyle);
9943 
9944   AllmanBraceStyle.ColumnLimit = 19;
9945   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9946   AllmanBraceStyle.ColumnLimit = 18;
9947   verifyFormat("void f()\n"
9948                "{\n"
9949                "  int i;\n"
9950                "}",
9951                AllmanBraceStyle);
9952   AllmanBraceStyle.ColumnLimit = 80;
9953 
9954   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9955   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9956   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9957   verifyFormat("void f(bool b)\n"
9958                "{\n"
9959                "  if (b)\n"
9960                "  {\n"
9961                "    return;\n"
9962                "  }\n"
9963                "}\n",
9964                BreakBeforeBraceShortIfs);
9965   verifyFormat("void f(bool b)\n"
9966                "{\n"
9967                "  if constexpr (b)\n"
9968                "  {\n"
9969                "    return;\n"
9970                "  }\n"
9971                "}\n",
9972                BreakBeforeBraceShortIfs);
9973   verifyFormat("void f(bool b)\n"
9974                "{\n"
9975                "  if (b) return;\n"
9976                "}\n",
9977                BreakBeforeBraceShortIfs);
9978   verifyFormat("void f(bool b)\n"
9979                "{\n"
9980                "  if constexpr (b) return;\n"
9981                "}\n",
9982                BreakBeforeBraceShortIfs);
9983   verifyFormat("void f(bool b)\n"
9984                "{\n"
9985                "  while (b)\n"
9986                "  {\n"
9987                "    return;\n"
9988                "  }\n"
9989                "}\n",
9990                BreakBeforeBraceShortIfs);
9991 }
9992 
9993 TEST_F(FormatTest, GNUBraceBreaking) {
9994   FormatStyle GNUBraceStyle = getLLVMStyle();
9995   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9996   verifyFormat("namespace a\n"
9997                "{\n"
9998                "class A\n"
9999                "{\n"
10000                "  void f()\n"
10001                "  {\n"
10002                "    int a;\n"
10003                "    {\n"
10004                "      int b;\n"
10005                "    }\n"
10006                "    if (true)\n"
10007                "      {\n"
10008                "        a();\n"
10009                "        b();\n"
10010                "      }\n"
10011                "  }\n"
10012                "  void g() { return; }\n"
10013                "}\n"
10014                "} // namespace a",
10015                GNUBraceStyle);
10016 
10017   verifyFormat("void f()\n"
10018                "{\n"
10019                "  if (true)\n"
10020                "    {\n"
10021                "      a();\n"
10022                "    }\n"
10023                "  else if (false)\n"
10024                "    {\n"
10025                "      b();\n"
10026                "    }\n"
10027                "  else\n"
10028                "    {\n"
10029                "      c();\n"
10030                "    }\n"
10031                "}\n",
10032                GNUBraceStyle);
10033 
10034   verifyFormat("void f()\n"
10035                "{\n"
10036                "  for (int i = 0; i < 10; ++i)\n"
10037                "    {\n"
10038                "      a();\n"
10039                "    }\n"
10040                "  while (false)\n"
10041                "    {\n"
10042                "      b();\n"
10043                "    }\n"
10044                "  do\n"
10045                "    {\n"
10046                "      c();\n"
10047                "    }\n"
10048                "  while (false);\n"
10049                "}\n",
10050                GNUBraceStyle);
10051 
10052   verifyFormat("void f(int a)\n"
10053                "{\n"
10054                "  switch (a)\n"
10055                "    {\n"
10056                "    case 0:\n"
10057                "      break;\n"
10058                "    case 1:\n"
10059                "      {\n"
10060                "        break;\n"
10061                "      }\n"
10062                "    case 2:\n"
10063                "      {\n"
10064                "      }\n"
10065                "      break;\n"
10066                "    default:\n"
10067                "      break;\n"
10068                "    }\n"
10069                "}\n",
10070                GNUBraceStyle);
10071 
10072   verifyFormat("enum X\n"
10073                "{\n"
10074                "  Y = 0,\n"
10075                "}\n",
10076                GNUBraceStyle);
10077 
10078   verifyFormat("@interface BSApplicationController ()\n"
10079                "{\n"
10080                "@private\n"
10081                "  id _extraIvar;\n"
10082                "}\n"
10083                "@end\n",
10084                GNUBraceStyle);
10085 
10086   verifyFormat("#ifdef _DEBUG\n"
10087                "int foo(int i = 0)\n"
10088                "#else\n"
10089                "int foo(int i = 5)\n"
10090                "#endif\n"
10091                "{\n"
10092                "  return i;\n"
10093                "}",
10094                GNUBraceStyle);
10095 
10096   verifyFormat("void foo() {}\n"
10097                "void bar()\n"
10098                "#ifdef _DEBUG\n"
10099                "{\n"
10100                "  foo();\n"
10101                "}\n"
10102                "#else\n"
10103                "{\n"
10104                "}\n"
10105                "#endif",
10106                GNUBraceStyle);
10107 
10108   verifyFormat("void foobar() { int i = 5; }\n"
10109                "#ifdef _DEBUG\n"
10110                "void bar() {}\n"
10111                "#else\n"
10112                "void bar() { foobar(); }\n"
10113                "#endif",
10114                GNUBraceStyle);
10115 }
10116 
10117 TEST_F(FormatTest, WebKitBraceBreaking) {
10118   FormatStyle WebKitBraceStyle = getLLVMStyle();
10119   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10120   WebKitBraceStyle.FixNamespaceComments = false;
10121   verifyFormat("namespace a {\n"
10122                "class A {\n"
10123                "  void f()\n"
10124                "  {\n"
10125                "    if (true) {\n"
10126                "      a();\n"
10127                "      b();\n"
10128                "    }\n"
10129                "  }\n"
10130                "  void g() { return; }\n"
10131                "};\n"
10132                "enum E {\n"
10133                "  A,\n"
10134                "  // foo\n"
10135                "  B,\n"
10136                "  C\n"
10137                "};\n"
10138                "struct B {\n"
10139                "  int x;\n"
10140                "};\n"
10141                "}\n",
10142                WebKitBraceStyle);
10143   verifyFormat("struct S {\n"
10144                "  int Type;\n"
10145                "  union {\n"
10146                "    int x;\n"
10147                "    double y;\n"
10148                "  } Value;\n"
10149                "  class C {\n"
10150                "    MyFavoriteType Value;\n"
10151                "  } Class;\n"
10152                "};\n",
10153                WebKitBraceStyle);
10154 }
10155 
10156 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10157   verifyFormat("void f() {\n"
10158                "  try {\n"
10159                "  } catch (const Exception &e) {\n"
10160                "  }\n"
10161                "}\n",
10162                getLLVMStyle());
10163 }
10164 
10165 TEST_F(FormatTest, UnderstandsPragmas) {
10166   verifyFormat("#pragma omp reduction(| : var)");
10167   verifyFormat("#pragma omp reduction(+ : var)");
10168 
10169   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10170             "(including parentheses).",
10171             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10172                    "(including parentheses)."));
10173 }
10174 
10175 TEST_F(FormatTest, UnderstandPragmaOption) {
10176   verifyFormat("#pragma option -C -A");
10177 
10178   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10179 }
10180 
10181 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10182   FormatStyle Style = getLLVMStyle();
10183   Style.ColumnLimit = 20;
10184 
10185   verifyFormat("int a; // the\n"
10186                "       // comment", Style);
10187   EXPECT_EQ("int a; /* first line\n"
10188             "        * second\n"
10189             "        * line third\n"
10190             "        * line\n"
10191             "        */",
10192             format("int a; /* first line\n"
10193                    "        * second\n"
10194                    "        * line third\n"
10195                    "        * line\n"
10196                    "        */",
10197                    Style));
10198   EXPECT_EQ("int a; // first line\n"
10199             "       // second\n"
10200             "       // line third\n"
10201             "       // line",
10202             format("int a; // first line\n"
10203                    "       // second line\n"
10204                    "       // third line",
10205                    Style));
10206 
10207   Style.PenaltyExcessCharacter = 90;
10208   verifyFormat("int a; // the comment", Style);
10209   EXPECT_EQ("int a; // the comment\n"
10210             "       // aaa",
10211             format("int a; // the comment aaa", Style));
10212   EXPECT_EQ("int a; /* first line\n"
10213             "        * second line\n"
10214             "        * third line\n"
10215             "        */",
10216             format("int a; /* first line\n"
10217                    "        * second line\n"
10218                    "        * third line\n"
10219                    "        */",
10220                    Style));
10221   EXPECT_EQ("int a; // first line\n"
10222             "       // second line\n"
10223             "       // third line",
10224             format("int a; // first line\n"
10225                    "       // second line\n"
10226                    "       // third line",
10227                    Style));
10228   // FIXME: Investigate why this is not getting the same layout as the test
10229   // above.
10230   EXPECT_EQ("int a; /* first line\n"
10231             "        * second line\n"
10232             "        * third line\n"
10233             "        */",
10234             format("int a; /* first line second line third line"
10235                    "\n*/",
10236                    Style));
10237 
10238   EXPECT_EQ("// foo bar baz bazfoo\n"
10239             "// foo bar foo bar\n",
10240             format("// foo bar baz bazfoo\n"
10241                    "// foo bar foo           bar\n",
10242                    Style));
10243   EXPECT_EQ("// foo bar baz bazfoo\n"
10244             "// foo bar foo bar\n",
10245             format("// foo bar baz      bazfoo\n"
10246                    "// foo            bar foo bar\n",
10247                    Style));
10248 
10249   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10250   // next one.
10251   EXPECT_EQ("// foo bar baz bazfoo\n"
10252             "// bar foo bar\n",
10253             format("// foo bar baz      bazfoo bar\n"
10254                    "// foo            bar\n",
10255                    Style));
10256 
10257   EXPECT_EQ("// foo bar baz bazfoo\n"
10258             "// foo bar baz bazfoo\n"
10259             "// bar foo bar\n",
10260             format("// foo bar baz      bazfoo\n"
10261                    "// foo bar baz      bazfoo bar\n"
10262                    "// foo bar\n",
10263                    Style));
10264 
10265   EXPECT_EQ("// foo bar baz bazfoo\n"
10266             "// foo bar baz bazfoo\n"
10267             "// bar foo bar\n",
10268             format("// foo bar baz      bazfoo\n"
10269                    "// foo bar baz      bazfoo bar\n"
10270                    "// foo           bar\n",
10271                    Style));
10272 
10273   // Make sure we do not keep protruding characters if strict mode reflow is
10274   // cheaper than keeping protruding characters.
10275   Style.ColumnLimit = 21;
10276   EXPECT_EQ("// foo foo foo foo\n"
10277             "// foo foo foo foo\n"
10278             "// foo foo foo foo\n",
10279             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10280                            Style));
10281 
10282   EXPECT_EQ("int a = /* long block\n"
10283             "           comment */\n"
10284             "    42;",
10285             format("int a = /* long block comment */ 42;", Style));
10286 }
10287 
10288 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10289   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10290   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10291                                   << " differs from Style #0"
10292 
10293 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10294   SmallVector<FormatStyle, 3> Styles;
10295   Styles.resize(3);
10296 
10297   Styles[0] = getLLVMStyle();
10298   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10299   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10300   EXPECT_ALL_STYLES_EQUAL(Styles);
10301 
10302   Styles[0] = getGoogleStyle();
10303   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10304   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10305   EXPECT_ALL_STYLES_EQUAL(Styles);
10306 
10307   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10308   EXPECT_TRUE(
10309       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10310   EXPECT_TRUE(
10311       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10312   EXPECT_ALL_STYLES_EQUAL(Styles);
10313 
10314   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10315   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10316   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10317   EXPECT_ALL_STYLES_EQUAL(Styles);
10318 
10319   Styles[0] = getMozillaStyle();
10320   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10321   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10322   EXPECT_ALL_STYLES_EQUAL(Styles);
10323 
10324   Styles[0] = getWebKitStyle();
10325   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10326   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10327   EXPECT_ALL_STYLES_EQUAL(Styles);
10328 
10329   Styles[0] = getGNUStyle();
10330   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10331   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10332   EXPECT_ALL_STYLES_EQUAL(Styles);
10333 
10334   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10335 }
10336 
10337 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10338   SmallVector<FormatStyle, 8> Styles;
10339   Styles.resize(2);
10340 
10341   Styles[0] = getGoogleStyle();
10342   Styles[1] = getLLVMStyle();
10343   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10344   EXPECT_ALL_STYLES_EQUAL(Styles);
10345 
10346   Styles.resize(5);
10347   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10348   Styles[1] = getLLVMStyle();
10349   Styles[1].Language = FormatStyle::LK_JavaScript;
10350   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10351 
10352   Styles[2] = getLLVMStyle();
10353   Styles[2].Language = FormatStyle::LK_JavaScript;
10354   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10355                                   "BasedOnStyle: Google",
10356                                   &Styles[2])
10357                    .value());
10358 
10359   Styles[3] = getLLVMStyle();
10360   Styles[3].Language = FormatStyle::LK_JavaScript;
10361   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10362                                   "Language: JavaScript",
10363                                   &Styles[3])
10364                    .value());
10365 
10366   Styles[4] = getLLVMStyle();
10367   Styles[4].Language = FormatStyle::LK_JavaScript;
10368   EXPECT_EQ(0, parseConfiguration("---\n"
10369                                   "BasedOnStyle: LLVM\n"
10370                                   "IndentWidth: 123\n"
10371                                   "---\n"
10372                                   "BasedOnStyle: Google\n"
10373                                   "Language: JavaScript",
10374                                   &Styles[4])
10375                    .value());
10376   EXPECT_ALL_STYLES_EQUAL(Styles);
10377 }
10378 
10379 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10380   Style.FIELD = false;                                                         \
10381   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10382   EXPECT_TRUE(Style.FIELD);                                                    \
10383   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10384   EXPECT_FALSE(Style.FIELD);
10385 
10386 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10387 
10388 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10389   Style.STRUCT.FIELD = false;                                                  \
10390   EXPECT_EQ(0,                                                                 \
10391             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10392                 .value());                                                     \
10393   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10394   EXPECT_EQ(0,                                                                 \
10395             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10396                 .value());                                                     \
10397   EXPECT_FALSE(Style.STRUCT.FIELD);
10398 
10399 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10400   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10401 
10402 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10403   EXPECT_NE(VALUE, Style.FIELD);                                               \
10404   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10405   EXPECT_EQ(VALUE, Style.FIELD)
10406 
10407 TEST_F(FormatTest, ParsesConfigurationBools) {
10408   FormatStyle Style = {};
10409   Style.Language = FormatStyle::LK_Cpp;
10410   CHECK_PARSE_BOOL(AlignOperands);
10411   CHECK_PARSE_BOOL(AlignTrailingComments);
10412   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10413   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10414   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10415   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10416   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10417   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10418   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10419   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10420   CHECK_PARSE_BOOL(BinPackArguments);
10421   CHECK_PARSE_BOOL(BinPackParameters);
10422   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10423   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10424   CHECK_PARSE_BOOL(BreakStringLiterals);
10425   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
10426   CHECK_PARSE_BOOL(CompactNamespaces);
10427   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10428   CHECK_PARSE_BOOL(DerivePointerAlignment);
10429   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10430   CHECK_PARSE_BOOL(DisableFormat);
10431   CHECK_PARSE_BOOL(IndentCaseLabels);
10432   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10433   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10434   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10435   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10436   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10437   CHECK_PARSE_BOOL(ReflowComments);
10438   CHECK_PARSE_BOOL(SortIncludes);
10439   CHECK_PARSE_BOOL(SortUsingDeclarations);
10440   CHECK_PARSE_BOOL(SpacesInParentheses);
10441   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10442   CHECK_PARSE_BOOL(SpacesInAngles);
10443   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10444   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10445   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10446   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10447   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10448   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10449   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10450   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10451   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10452 
10453   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10454   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10455   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10456   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10457   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10458   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10459   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10460   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10461   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10462   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10463   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10464   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10465   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10466   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10467   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10468 }
10469 
10470 #undef CHECK_PARSE_BOOL
10471 
10472 TEST_F(FormatTest, ParsesConfiguration) {
10473   FormatStyle Style = {};
10474   Style.Language = FormatStyle::LK_Cpp;
10475   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10476   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10477               ConstructorInitializerIndentWidth, 1234u);
10478   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10479   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10480   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10481   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10482               PenaltyBreakAssignment, 1234u);
10483   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10484               PenaltyBreakBeforeFirstCallParameter, 1234u);
10485   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10486   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10487               PenaltyReturnTypeOnItsOwnLine, 1234u);
10488   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10489               SpacesBeforeTrailingComments, 1234u);
10490   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10491   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10492   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10493 
10494   Style.PointerAlignment = FormatStyle::PAS_Middle;
10495   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10496               FormatStyle::PAS_Left);
10497   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10498               FormatStyle::PAS_Right);
10499   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10500               FormatStyle::PAS_Middle);
10501   // For backward compatibility:
10502   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10503               FormatStyle::PAS_Left);
10504   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10505               FormatStyle::PAS_Right);
10506   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10507               FormatStyle::PAS_Middle);
10508 
10509   Style.Standard = FormatStyle::LS_Auto;
10510   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10511   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10512   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10513   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10514   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10515 
10516   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10517   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10518               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10519   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10520               FormatStyle::BOS_None);
10521   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10522               FormatStyle::BOS_All);
10523   // For backward compatibility:
10524   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10525               FormatStyle::BOS_None);
10526   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10527               FormatStyle::BOS_All);
10528 
10529   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10530   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10531               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10532   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10533               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10534   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10535               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10536   // For backward compatibility:
10537   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10538               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10539 
10540   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10541   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10542               FormatStyle::BAS_Align);
10543   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10544               FormatStyle::BAS_DontAlign);
10545   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10546               FormatStyle::BAS_AlwaysBreak);
10547   // For backward compatibility:
10548   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10549               FormatStyle::BAS_DontAlign);
10550   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10551               FormatStyle::BAS_Align);
10552 
10553   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10554   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10555               FormatStyle::ENAS_DontAlign);
10556   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10557               FormatStyle::ENAS_Left);
10558   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10559               FormatStyle::ENAS_Right);
10560   // For backward compatibility:
10561   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10562               FormatStyle::ENAS_Left);
10563   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10564               FormatStyle::ENAS_Right);
10565 
10566   Style.UseTab = FormatStyle::UT_ForIndentation;
10567   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10568   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10569   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10570   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10571               FormatStyle::UT_ForContinuationAndIndentation);
10572   // For backward compatibility:
10573   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10574   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10575 
10576   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10577   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10578               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10579   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10580               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10581   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10582               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10583   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10584               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10585   // For backward compatibility:
10586   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10587               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10588   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10589               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10590 
10591   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10592   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10593               FormatStyle::SBPO_Never);
10594   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10595               FormatStyle::SBPO_Always);
10596   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10597               FormatStyle::SBPO_ControlStatements);
10598   // For backward compatibility:
10599   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10600               FormatStyle::SBPO_Never);
10601   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10602               FormatStyle::SBPO_ControlStatements);
10603 
10604   Style.ColumnLimit = 123;
10605   FormatStyle BaseStyle = getLLVMStyle();
10606   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10607   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10608 
10609   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10610   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10611               FormatStyle::BS_Attach);
10612   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10613               FormatStyle::BS_Linux);
10614   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10615               FormatStyle::BS_Mozilla);
10616   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10617               FormatStyle::BS_Stroustrup);
10618   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10619               FormatStyle::BS_Allman);
10620   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10621   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10622               FormatStyle::BS_WebKit);
10623   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10624               FormatStyle::BS_Custom);
10625 
10626   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10627   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10628               FormatStyle::RTBS_None);
10629   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10630               FormatStyle::RTBS_All);
10631   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10632               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10633   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10634               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10635   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10636               AlwaysBreakAfterReturnType,
10637               FormatStyle::RTBS_TopLevelDefinitions);
10638 
10639   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10640   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10641               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10642   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10643               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10644   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10645               AlwaysBreakAfterDefinitionReturnType,
10646               FormatStyle::DRTBS_TopLevel);
10647 
10648   Style.NamespaceIndentation = FormatStyle::NI_All;
10649   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10650               FormatStyle::NI_None);
10651   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10652               FormatStyle::NI_Inner);
10653   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10654               FormatStyle::NI_All);
10655 
10656   // FIXME: This is required because parsing a configuration simply overwrites
10657   // the first N elements of the list instead of resetting it.
10658   Style.ForEachMacros.clear();
10659   std::vector<std::string> BoostForeach;
10660   BoostForeach.push_back("BOOST_FOREACH");
10661   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10662   std::vector<std::string> BoostAndQForeach;
10663   BoostAndQForeach.push_back("BOOST_FOREACH");
10664   BoostAndQForeach.push_back("Q_FOREACH");
10665   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10666               BoostAndQForeach);
10667 
10668   Style.IncludeCategories.clear();
10669   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10670                                                                   {".*", 1}};
10671   CHECK_PARSE("IncludeCategories:\n"
10672               "  - Regex: abc/.*\n"
10673               "    Priority: 2\n"
10674               "  - Regex: .*\n"
10675               "    Priority: 1",
10676               IncludeCategories, ExpectedCategories);
10677   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10678 
10679   Style.RawStringFormats.clear();
10680   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10681       {
10682           FormatStyle::LK_TextProto,
10683           {"pb", "proto"},
10684           {"PARSE_TEXT_PROTO"},
10685           /*CanonicalDelimiter=*/"",
10686           "llvm",
10687       },
10688       {
10689           FormatStyle::LK_Cpp,
10690           {"cc", "cpp"},
10691           {"C_CODEBLOCK", "CPPEVAL"},
10692           /*CanonicalDelimiter=*/"cc",
10693           /*BasedOnStyle=*/"",
10694       },
10695   };
10696 
10697   CHECK_PARSE("RawStringFormats:\n"
10698               "  - Language: TextProto\n"
10699               "    Delimiters:\n"
10700               "      - 'pb'\n"
10701               "      - 'proto'\n"
10702               "    EnclosingFunctions:\n"
10703               "      - 'PARSE_TEXT_PROTO'\n"
10704               "    BasedOnStyle: llvm\n"
10705               "  - Language: Cpp\n"
10706               "    Delimiters:\n"
10707               "      - 'cc'\n"
10708               "      - 'cpp'\n"
10709               "    EnclosingFunctions:\n"
10710               "      - 'C_CODEBLOCK'\n"
10711               "      - 'CPPEVAL'\n"
10712               "    CanonicalDelimiter: 'cc'",
10713               RawStringFormats, ExpectedRawStringFormats);
10714 }
10715 
10716 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10717   FormatStyle Style = {};
10718   Style.Language = FormatStyle::LK_Cpp;
10719   CHECK_PARSE("Language: Cpp\n"
10720               "IndentWidth: 12",
10721               IndentWidth, 12u);
10722   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10723                                "IndentWidth: 34",
10724                                &Style),
10725             ParseError::Unsuitable);
10726   EXPECT_EQ(12u, Style.IndentWidth);
10727   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10728   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10729 
10730   Style.Language = FormatStyle::LK_JavaScript;
10731   CHECK_PARSE("Language: JavaScript\n"
10732               "IndentWidth: 12",
10733               IndentWidth, 12u);
10734   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10735   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10736                                "IndentWidth: 34",
10737                                &Style),
10738             ParseError::Unsuitable);
10739   EXPECT_EQ(23u, Style.IndentWidth);
10740   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10741   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10742 
10743   CHECK_PARSE("BasedOnStyle: LLVM\n"
10744               "IndentWidth: 67",
10745               IndentWidth, 67u);
10746 
10747   CHECK_PARSE("---\n"
10748               "Language: JavaScript\n"
10749               "IndentWidth: 12\n"
10750               "---\n"
10751               "Language: Cpp\n"
10752               "IndentWidth: 34\n"
10753               "...\n",
10754               IndentWidth, 12u);
10755 
10756   Style.Language = FormatStyle::LK_Cpp;
10757   CHECK_PARSE("---\n"
10758               "Language: JavaScript\n"
10759               "IndentWidth: 12\n"
10760               "---\n"
10761               "Language: Cpp\n"
10762               "IndentWidth: 34\n"
10763               "...\n",
10764               IndentWidth, 34u);
10765   CHECK_PARSE("---\n"
10766               "IndentWidth: 78\n"
10767               "---\n"
10768               "Language: JavaScript\n"
10769               "IndentWidth: 56\n"
10770               "...\n",
10771               IndentWidth, 78u);
10772 
10773   Style.ColumnLimit = 123;
10774   Style.IndentWidth = 234;
10775   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10776   Style.TabWidth = 345;
10777   EXPECT_FALSE(parseConfiguration("---\n"
10778                                   "IndentWidth: 456\n"
10779                                   "BreakBeforeBraces: Allman\n"
10780                                   "---\n"
10781                                   "Language: JavaScript\n"
10782                                   "IndentWidth: 111\n"
10783                                   "TabWidth: 111\n"
10784                                   "---\n"
10785                                   "Language: Cpp\n"
10786                                   "BreakBeforeBraces: Stroustrup\n"
10787                                   "TabWidth: 789\n"
10788                                   "...\n",
10789                                   &Style));
10790   EXPECT_EQ(123u, Style.ColumnLimit);
10791   EXPECT_EQ(456u, Style.IndentWidth);
10792   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10793   EXPECT_EQ(789u, Style.TabWidth);
10794 
10795   EXPECT_EQ(parseConfiguration("---\n"
10796                                "Language: JavaScript\n"
10797                                "IndentWidth: 56\n"
10798                                "---\n"
10799                                "IndentWidth: 78\n"
10800                                "...\n",
10801                                &Style),
10802             ParseError::Error);
10803   EXPECT_EQ(parseConfiguration("---\n"
10804                                "Language: JavaScript\n"
10805                                "IndentWidth: 56\n"
10806                                "---\n"
10807                                "Language: JavaScript\n"
10808                                "IndentWidth: 78\n"
10809                                "...\n",
10810                                &Style),
10811             ParseError::Error);
10812 
10813   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10814 }
10815 
10816 #undef CHECK_PARSE
10817 
10818 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10819   FormatStyle Style = {};
10820   Style.Language = FormatStyle::LK_JavaScript;
10821   Style.BreakBeforeTernaryOperators = true;
10822   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10823   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10824 
10825   Style.BreakBeforeTernaryOperators = true;
10826   EXPECT_EQ(0, parseConfiguration("---\n"
10827                                   "BasedOnStyle: Google\n"
10828                                   "---\n"
10829                                   "Language: JavaScript\n"
10830                                   "IndentWidth: 76\n"
10831                                   "...\n",
10832                                   &Style)
10833                    .value());
10834   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10835   EXPECT_EQ(76u, Style.IndentWidth);
10836   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10837 }
10838 
10839 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10840   FormatStyle Style = getLLVMStyle();
10841   std::string YAML = configurationAsText(Style);
10842   FormatStyle ParsedStyle = {};
10843   ParsedStyle.Language = FormatStyle::LK_Cpp;
10844   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10845   EXPECT_EQ(Style, ParsedStyle);
10846 }
10847 
10848 TEST_F(FormatTest, WorksFor8bitEncodings) {
10849   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10850             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10851             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10852             "\"\xef\xee\xf0\xf3...\"",
10853             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10854                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10855                    "\xef\xee\xf0\xf3...\"",
10856                    getLLVMStyleWithColumns(12)));
10857 }
10858 
10859 TEST_F(FormatTest, HandlesUTF8BOM) {
10860   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10861   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10862             format("\xef\xbb\xbf#include <iostream>"));
10863   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10864             format("\xef\xbb\xbf\n#include <iostream>"));
10865 }
10866 
10867 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10868 #if !defined(_MSC_VER)
10869 
10870 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10871   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10872                getLLVMStyleWithColumns(35));
10873   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10874                getLLVMStyleWithColumns(31));
10875   verifyFormat("// Однажды в студёную зимнюю пору...",
10876                getLLVMStyleWithColumns(36));
10877   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10878   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10879                getLLVMStyleWithColumns(39));
10880   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10881                getLLVMStyleWithColumns(35));
10882 }
10883 
10884 TEST_F(FormatTest, SplitsUTF8Strings) {
10885   // Non-printable characters' width is currently considered to be the length in
10886   // bytes in UTF8. The characters can be displayed in very different manner
10887   // (zero-width, single width with a substitution glyph, expanded to their code
10888   // (e.g. "<8d>"), so there's no single correct way to handle them.
10889   EXPECT_EQ("\"aaaaÄ\"\n"
10890             "\"\xc2\x8d\";",
10891             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10892   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10893             "\"\xc2\x8d\";",
10894             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10895   EXPECT_EQ("\"Однажды, в \"\n"
10896             "\"студёную \"\n"
10897             "\"зимнюю \"\n"
10898             "\"пору,\"",
10899             format("\"Однажды, в студёную зимнюю пору,\"",
10900                    getLLVMStyleWithColumns(13)));
10901   EXPECT_EQ(
10902       "\"一 二 三 \"\n"
10903       "\"四 五六 \"\n"
10904       "\"七 八 九 \"\n"
10905       "\"十\"",
10906       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10907   EXPECT_EQ("\"一\t\"\n"
10908             "\"二 \t\"\n"
10909             "\"三 四 \"\n"
10910             "\"五\t\"\n"
10911             "\"六 \t\"\n"
10912             "\"七 \"\n"
10913             "\"八九十\tqq\"",
10914             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10915                    getLLVMStyleWithColumns(11)));
10916 
10917   // UTF8 character in an escape sequence.
10918   EXPECT_EQ("\"aaaaaa\"\n"
10919             "\"\\\xC2\x8D\"",
10920             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10921 }
10922 
10923 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10924   EXPECT_EQ("const char *sssss =\n"
10925             "    \"一二三四五六七八\\\n"
10926             " 九 十\";",
10927             format("const char *sssss = \"一二三四五六七八\\\n"
10928                    " 九 十\";",
10929                    getLLVMStyleWithColumns(30)));
10930 }
10931 
10932 TEST_F(FormatTest, SplitsUTF8LineComments) {
10933   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10934             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10935   EXPECT_EQ("// Я из лесу\n"
10936             "// вышел; был\n"
10937             "// сильный\n"
10938             "// мороз.",
10939             format("// Я из лесу вышел; был сильный мороз.",
10940                    getLLVMStyleWithColumns(13)));
10941   EXPECT_EQ("// 一二三\n"
10942             "// 四五六七\n"
10943             "// 八  九\n"
10944             "// 十",
10945             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10946 }
10947 
10948 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10949   EXPECT_EQ("/* Гляжу,\n"
10950             " * поднимается\n"
10951             " * медленно в\n"
10952             " * гору\n"
10953             " * Лошадка,\n"
10954             " * везущая\n"
10955             " * хворосту\n"
10956             " * воз. */",
10957             format("/* Гляжу, поднимается медленно в гору\n"
10958                    " * Лошадка, везущая хворосту воз. */",
10959                    getLLVMStyleWithColumns(13)));
10960   EXPECT_EQ(
10961       "/* 一二三\n"
10962       " * 四五六七\n"
10963       " * 八  九\n"
10964       " * 十  */",
10965       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10966   EXPECT_EQ("/* �������� ��������\n"
10967             " * ��������\n"
10968             " * ������-�� */",
10969             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10970 }
10971 
10972 #endif // _MSC_VER
10973 
10974 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10975   FormatStyle Style = getLLVMStyle();
10976 
10977   Style.ConstructorInitializerIndentWidth = 4;
10978   verifyFormat(
10979       "SomeClass::Constructor()\n"
10980       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10981       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10982       Style);
10983 
10984   Style.ConstructorInitializerIndentWidth = 2;
10985   verifyFormat(
10986       "SomeClass::Constructor()\n"
10987       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10988       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10989       Style);
10990 
10991   Style.ConstructorInitializerIndentWidth = 0;
10992   verifyFormat(
10993       "SomeClass::Constructor()\n"
10994       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10995       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10996       Style);
10997   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10998   verifyFormat(
10999       "SomeLongTemplateVariableName<\n"
11000       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11001       Style);
11002   verifyFormat(
11003       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11004       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11005       Style);
11006 }
11007 
11008 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11009   FormatStyle Style = getLLVMStyle();
11010   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11011   Style.ConstructorInitializerIndentWidth = 4;
11012   verifyFormat("SomeClass::Constructor()\n"
11013                "    : a(a)\n"
11014                "    , b(b)\n"
11015                "    , c(c) {}",
11016                Style);
11017   verifyFormat("SomeClass::Constructor()\n"
11018                "    : a(a) {}",
11019                Style);
11020 
11021   Style.ColumnLimit = 0;
11022   verifyFormat("SomeClass::Constructor()\n"
11023                "    : a(a) {}",
11024                Style);
11025   verifyFormat("SomeClass::Constructor() noexcept\n"
11026                "    : a(a) {}",
11027                Style);
11028   verifyFormat("SomeClass::Constructor()\n"
11029                "    : a(a)\n"
11030                "    , b(b)\n"
11031                "    , c(c) {}",
11032                Style);
11033   verifyFormat("SomeClass::Constructor()\n"
11034                "    : a(a) {\n"
11035                "  foo();\n"
11036                "  bar();\n"
11037                "}",
11038                Style);
11039 
11040   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11041   verifyFormat("SomeClass::Constructor()\n"
11042                "    : a(a)\n"
11043                "    , b(b)\n"
11044                "    , c(c) {\n}",
11045                Style);
11046   verifyFormat("SomeClass::Constructor()\n"
11047                "    : a(a) {\n}",
11048                Style);
11049 
11050   Style.ColumnLimit = 80;
11051   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11052   Style.ConstructorInitializerIndentWidth = 2;
11053   verifyFormat("SomeClass::Constructor()\n"
11054                "  : a(a)\n"
11055                "  , b(b)\n"
11056                "  , c(c) {}",
11057                Style);
11058 
11059   Style.ConstructorInitializerIndentWidth = 0;
11060   verifyFormat("SomeClass::Constructor()\n"
11061                ": a(a)\n"
11062                ", b(b)\n"
11063                ", c(c) {}",
11064                Style);
11065 
11066   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11067   Style.ConstructorInitializerIndentWidth = 4;
11068   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11069   verifyFormat(
11070       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11071       Style);
11072   verifyFormat(
11073       "SomeClass::Constructor()\n"
11074       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11075       Style);
11076   Style.ConstructorInitializerIndentWidth = 4;
11077   Style.ColumnLimit = 60;
11078   verifyFormat("SomeClass::Constructor()\n"
11079                "    : aaaaaaaa(aaaaaaaa)\n"
11080                "    , aaaaaaaa(aaaaaaaa)\n"
11081                "    , aaaaaaaa(aaaaaaaa) {}",
11082                Style);
11083 }
11084 
11085 TEST_F(FormatTest, Destructors) {
11086   verifyFormat("void F(int &i) { i.~int(); }");
11087   verifyFormat("void F(int &i) { i->~int(); }");
11088 }
11089 
11090 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11091   FormatStyle Style = getWebKitStyle();
11092 
11093   // Don't indent in outer namespaces.
11094   verifyFormat("namespace outer {\n"
11095                "int i;\n"
11096                "namespace inner {\n"
11097                "    int i;\n"
11098                "} // namespace inner\n"
11099                "} // namespace outer\n"
11100                "namespace other_outer {\n"
11101                "int i;\n"
11102                "}",
11103                Style);
11104 
11105   // Don't indent case labels.
11106   verifyFormat("switch (variable) {\n"
11107                "case 1:\n"
11108                "case 2:\n"
11109                "    doSomething();\n"
11110                "    break;\n"
11111                "default:\n"
11112                "    ++variable;\n"
11113                "}",
11114                Style);
11115 
11116   // Wrap before binary operators.
11117   EXPECT_EQ("void f()\n"
11118             "{\n"
11119             "    if (aaaaaaaaaaaaaaaa\n"
11120             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11121             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11122             "        return;\n"
11123             "}",
11124             format("void f() {\n"
11125                    "if (aaaaaaaaaaaaaaaa\n"
11126                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11127                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11128                    "return;\n"
11129                    "}",
11130                    Style));
11131 
11132   // Allow functions on a single line.
11133   verifyFormat("void f() { return; }", Style);
11134 
11135   // Constructor initializers are formatted one per line with the "," on the
11136   // new line.
11137   verifyFormat("Constructor()\n"
11138                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11139                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11140                "          aaaaaaaaaaaaaa)\n"
11141                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11142                "{\n"
11143                "}",
11144                Style);
11145   verifyFormat("SomeClass::Constructor()\n"
11146                "    : a(a)\n"
11147                "{\n"
11148                "}",
11149                Style);
11150   EXPECT_EQ("SomeClass::Constructor()\n"
11151             "    : a(a)\n"
11152             "{\n"
11153             "}",
11154             format("SomeClass::Constructor():a(a){}", Style));
11155   verifyFormat("SomeClass::Constructor()\n"
11156                "    : a(a)\n"
11157                "    , b(b)\n"
11158                "    , c(c)\n"
11159                "{\n"
11160                "}",
11161                Style);
11162   verifyFormat("SomeClass::Constructor()\n"
11163                "    : a(a)\n"
11164                "{\n"
11165                "    foo();\n"
11166                "    bar();\n"
11167                "}",
11168                Style);
11169 
11170   // Access specifiers should be aligned left.
11171   verifyFormat("class C {\n"
11172                "public:\n"
11173                "    int i;\n"
11174                "};",
11175                Style);
11176 
11177   // Do not align comments.
11178   verifyFormat("int a; // Do not\n"
11179                "double b; // align comments.",
11180                Style);
11181 
11182   // Do not align operands.
11183   EXPECT_EQ("ASSERT(aaaa\n"
11184             "    || bbbb);",
11185             format("ASSERT ( aaaa\n||bbbb);", Style));
11186 
11187   // Accept input's line breaks.
11188   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11189             "    || bbbbbbbbbbbbbbb) {\n"
11190             "    i++;\n"
11191             "}",
11192             format("if (aaaaaaaaaaaaaaa\n"
11193                    "|| bbbbbbbbbbbbbbb) { i++; }",
11194                    Style));
11195   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11196             "    i++;\n"
11197             "}",
11198             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11199 
11200   // Don't automatically break all macro definitions (llvm.org/PR17842).
11201   verifyFormat("#define aNumber 10", Style);
11202   // However, generally keep the line breaks that the user authored.
11203   EXPECT_EQ("#define aNumber \\\n"
11204             "    10",
11205             format("#define aNumber \\\n"
11206                    " 10",
11207                    Style));
11208 
11209   // Keep empty and one-element array literals on a single line.
11210   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11211             "                                  copyItems:YES];",
11212             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11213                    "copyItems:YES];",
11214                    Style));
11215   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11216             "                                  copyItems:YES];",
11217             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11218                    "             copyItems:YES];",
11219                    Style));
11220   // FIXME: This does not seem right, there should be more indentation before
11221   // the array literal's entries. Nested blocks have the same problem.
11222   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11223             "    @\"a\",\n"
11224             "    @\"a\"\n"
11225             "]\n"
11226             "                                  copyItems:YES];",
11227             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11228                    "     @\"a\",\n"
11229                    "     @\"a\"\n"
11230                    "     ]\n"
11231                    "       copyItems:YES];",
11232                    Style));
11233   EXPECT_EQ(
11234       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11235       "                                  copyItems:YES];",
11236       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11237              "   copyItems:YES];",
11238              Style));
11239 
11240   verifyFormat("[self.a b:c c:d];", Style);
11241   EXPECT_EQ("[self.a b:c\n"
11242             "        c:d];",
11243             format("[self.a b:c\n"
11244                    "c:d];",
11245                    Style));
11246 }
11247 
11248 TEST_F(FormatTest, FormatsLambdas) {
11249   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11250   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11251   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11252   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11253   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11254   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11255   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11256   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11257   verifyFormat("int x = f(*+[] {});");
11258   verifyFormat("void f() {\n"
11259                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11260                "}\n");
11261   verifyFormat("void f() {\n"
11262                "  other(x.begin(), //\n"
11263                "        x.end(),   //\n"
11264                "        [&](int, int) { return 1; });\n"
11265                "}\n");
11266   verifyFormat("SomeFunction([]() { // A cool function...\n"
11267                "  return 43;\n"
11268                "});");
11269   EXPECT_EQ("SomeFunction([]() {\n"
11270             "#define A a\n"
11271             "  return 43;\n"
11272             "});",
11273             format("SomeFunction([](){\n"
11274                    "#define A a\n"
11275                    "return 43;\n"
11276                    "});"));
11277   verifyFormat("void f() {\n"
11278                "  SomeFunction([](decltype(x), A *a) {});\n"
11279                "}");
11280   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11281                "    [](const aaaaaaaaaa &a) { return a; });");
11282   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11283                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11284                "});");
11285   verifyFormat("Constructor()\n"
11286                "    : Field([] { // comment\n"
11287                "        int i;\n"
11288                "      }) {}");
11289   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11290                "  return some_parameter.size();\n"
11291                "};");
11292   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11293                "    [](const string &s) { return s; };");
11294   verifyFormat("int i = aaaaaa ? 1 //\n"
11295                "               : [] {\n"
11296                "                   return 2; //\n"
11297                "                 }();");
11298   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11299                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11300                "                  return x == 2; // force break\n"
11301                "                });");
11302   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11303                "    [=](int iiiiiiiiiiii) {\n"
11304                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11305                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11306                "    });",
11307                getLLVMStyleWithColumns(60));
11308   verifyFormat("SomeFunction({[&] {\n"
11309                "                // comment\n"
11310                "              },\n"
11311                "              [&] {\n"
11312                "                // comment\n"
11313                "              }});");
11314   verifyFormat("SomeFunction({[&] {\n"
11315                "  // comment\n"
11316                "}});");
11317   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
11318                "                             [&]() { return true; },\n"
11319                "                         aaaaa aaaaaaaaa);");
11320 
11321   // Lambdas with return types.
11322   verifyFormat("int c = []() -> int { return 2; }();\n");
11323   verifyFormat("int c = []() -> int * { return 2; }();\n");
11324   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11325   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11326   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11327   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11328   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11329   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11330   verifyFormat("[a, a]() -> a<1> {};");
11331   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11332                "                   int j) -> int {\n"
11333                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11334                "};");
11335   verifyFormat(
11336       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11337       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11338       "      return aaaaaaaaaaaaaaaaa;\n"
11339       "    });",
11340       getLLVMStyleWithColumns(70));
11341   verifyFormat("[]() //\n"
11342                "    -> int {\n"
11343                "  return 1; //\n"
11344                "};");
11345 
11346   // Multiple lambdas in the same parentheses change indentation rules.
11347   verifyFormat("SomeFunction(\n"
11348                "    []() {\n"
11349                "      int i = 42;\n"
11350                "      return i;\n"
11351                "    },\n"
11352                "    []() {\n"
11353                "      int j = 43;\n"
11354                "      return j;\n"
11355                "    });");
11356 
11357   // More complex introducers.
11358   verifyFormat("return [i, args...] {};");
11359 
11360   // Not lambdas.
11361   verifyFormat("constexpr char hello[]{\"hello\"};");
11362   verifyFormat("double &operator[](int i) { return 0; }\n"
11363                "int i;");
11364   verifyFormat("std::unique_ptr<int[]> foo() {}");
11365   verifyFormat("int i = a[a][a]->f();");
11366   verifyFormat("int i = (*b)[a]->f();");
11367 
11368   // Other corner cases.
11369   verifyFormat("void f() {\n"
11370                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11371                "  );\n"
11372                "}");
11373 
11374   // Lambdas created through weird macros.
11375   verifyFormat("void f() {\n"
11376                "  MACRO((const AA &a) { return 1; });\n"
11377                "  MACRO((AA &a) { return 1; });\n"
11378                "}");
11379 
11380   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11381                "      doo_dah();\n"
11382                "      doo_dah();\n"
11383                "    })) {\n"
11384                "}");
11385   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
11386                "                doo_dah();\n"
11387                "                doo_dah();\n"
11388                "              })) {\n"
11389                "}");
11390   verifyFormat("auto lambda = []() {\n"
11391                "  int a = 2\n"
11392                "#if A\n"
11393                "          + 2\n"
11394                "#endif\n"
11395                "      ;\n"
11396                "};");
11397 
11398   // Lambdas with complex multiline introducers.
11399   verifyFormat(
11400       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11401       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
11402       "        -> ::std::unordered_set<\n"
11403       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
11404       "      //\n"
11405       "    });");
11406 }
11407 
11408 TEST_F(FormatTest, EmptyLinesInLambdas) {
11409   verifyFormat("auto lambda = []() {\n"
11410                "  x(); //\n"
11411                "};",
11412                "auto lambda = []() {\n"
11413                "\n"
11414                "  x(); //\n"
11415                "\n"
11416                "};");
11417 }
11418 
11419 TEST_F(FormatTest, FormatsBlocks) {
11420   FormatStyle ShortBlocks = getLLVMStyle();
11421   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11422   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11423   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11424   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11425   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11426   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11427   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11428 
11429   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11430   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11431   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11432 
11433   verifyFormat("[operation setCompletionBlock:^{\n"
11434                "  [self onOperationDone];\n"
11435                "}];");
11436   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11437                "  [self onOperationDone];\n"
11438                "}]};");
11439   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11440                "  f();\n"
11441                "}];");
11442   verifyFormat("int a = [operation block:^int(int *i) {\n"
11443                "  return 1;\n"
11444                "}];");
11445   verifyFormat("[myObject doSomethingWith:arg1\n"
11446                "                      aaa:^int(int *a) {\n"
11447                "                        return 1;\n"
11448                "                      }\n"
11449                "                      bbb:f(a * bbbbbbbb)];");
11450 
11451   verifyFormat("[operation setCompletionBlock:^{\n"
11452                "  [self.delegate newDataAvailable];\n"
11453                "}];",
11454                getLLVMStyleWithColumns(60));
11455   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11456                "  NSString *path = [self sessionFilePath];\n"
11457                "  if (path) {\n"
11458                "    // ...\n"
11459                "  }\n"
11460                "});");
11461   verifyFormat("[[SessionService sharedService]\n"
11462                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11463                "      if (window) {\n"
11464                "        [self windowDidLoad:window];\n"
11465                "      } else {\n"
11466                "        [self errorLoadingWindow];\n"
11467                "      }\n"
11468                "    }];");
11469   verifyFormat("void (^largeBlock)(void) = ^{\n"
11470                "  // ...\n"
11471                "};\n",
11472                getLLVMStyleWithColumns(40));
11473   verifyFormat("[[SessionService sharedService]\n"
11474                "    loadWindowWithCompletionBlock: //\n"
11475                "        ^(SessionWindow *window) {\n"
11476                "          if (window) {\n"
11477                "            [self windowDidLoad:window];\n"
11478                "          } else {\n"
11479                "            [self errorLoadingWindow];\n"
11480                "          }\n"
11481                "        }];",
11482                getLLVMStyleWithColumns(60));
11483   verifyFormat("[myObject doSomethingWith:arg1\n"
11484                "    firstBlock:^(Foo *a) {\n"
11485                "      // ...\n"
11486                "      int i;\n"
11487                "    }\n"
11488                "    secondBlock:^(Bar *b) {\n"
11489                "      // ...\n"
11490                "      int i;\n"
11491                "    }\n"
11492                "    thirdBlock:^Foo(Bar *b) {\n"
11493                "      // ...\n"
11494                "      int i;\n"
11495                "    }];");
11496   verifyFormat("[myObject doSomethingWith:arg1\n"
11497                "               firstBlock:-1\n"
11498                "              secondBlock:^(Bar *b) {\n"
11499                "                // ...\n"
11500                "                int i;\n"
11501                "              }];");
11502 
11503   verifyFormat("f(^{\n"
11504                "  @autoreleasepool {\n"
11505                "    if (a) {\n"
11506                "      g();\n"
11507                "    }\n"
11508                "  }\n"
11509                "});");
11510   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11511   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11512                "};");
11513 
11514   FormatStyle FourIndent = getLLVMStyle();
11515   FourIndent.ObjCBlockIndentWidth = 4;
11516   verifyFormat("[operation setCompletionBlock:^{\n"
11517                "    [self onOperationDone];\n"
11518                "}];",
11519                FourIndent);
11520 }
11521 
11522 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11523   FormatStyle ZeroColumn = getLLVMStyle();
11524   ZeroColumn.ColumnLimit = 0;
11525 
11526   verifyFormat("[[SessionService sharedService] "
11527                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11528                "  if (window) {\n"
11529                "    [self windowDidLoad:window];\n"
11530                "  } else {\n"
11531                "    [self errorLoadingWindow];\n"
11532                "  }\n"
11533                "}];",
11534                ZeroColumn);
11535   EXPECT_EQ("[[SessionService sharedService]\n"
11536             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11537             "      if (window) {\n"
11538             "        [self windowDidLoad:window];\n"
11539             "      } else {\n"
11540             "        [self errorLoadingWindow];\n"
11541             "      }\n"
11542             "    }];",
11543             format("[[SessionService sharedService]\n"
11544                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11545                    "                if (window) {\n"
11546                    "    [self windowDidLoad:window];\n"
11547                    "  } else {\n"
11548                    "    [self errorLoadingWindow];\n"
11549                    "  }\n"
11550                    "}];",
11551                    ZeroColumn));
11552   verifyFormat("[myObject doSomethingWith:arg1\n"
11553                "    firstBlock:^(Foo *a) {\n"
11554                "      // ...\n"
11555                "      int i;\n"
11556                "    }\n"
11557                "    secondBlock:^(Bar *b) {\n"
11558                "      // ...\n"
11559                "      int i;\n"
11560                "    }\n"
11561                "    thirdBlock:^Foo(Bar *b) {\n"
11562                "      // ...\n"
11563                "      int i;\n"
11564                "    }];",
11565                ZeroColumn);
11566   verifyFormat("f(^{\n"
11567                "  @autoreleasepool {\n"
11568                "    if (a) {\n"
11569                "      g();\n"
11570                "    }\n"
11571                "  }\n"
11572                "});",
11573                ZeroColumn);
11574   verifyFormat("void (^largeBlock)(void) = ^{\n"
11575                "  // ...\n"
11576                "};",
11577                ZeroColumn);
11578 
11579   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11580   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11581             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11582   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11583   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11584             "  int i;\n"
11585             "};",
11586             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11587 }
11588 
11589 TEST_F(FormatTest, SupportsCRLF) {
11590   EXPECT_EQ("int a;\r\n"
11591             "int b;\r\n"
11592             "int c;\r\n",
11593             format("int a;\r\n"
11594                    "  int b;\r\n"
11595                    "    int c;\r\n",
11596                    getLLVMStyle()));
11597   EXPECT_EQ("int a;\r\n"
11598             "int b;\r\n"
11599             "int c;\r\n",
11600             format("int a;\r\n"
11601                    "  int b;\n"
11602                    "    int c;\r\n",
11603                    getLLVMStyle()));
11604   EXPECT_EQ("int a;\n"
11605             "int b;\n"
11606             "int c;\n",
11607             format("int a;\r\n"
11608                    "  int b;\n"
11609                    "    int c;\n",
11610                    getLLVMStyle()));
11611   EXPECT_EQ("\"aaaaaaa \"\r\n"
11612             "\"bbbbbbb\";\r\n",
11613             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11614   EXPECT_EQ("#define A \\\r\n"
11615             "  b;      \\\r\n"
11616             "  c;      \\\r\n"
11617             "  d;\r\n",
11618             format("#define A \\\r\n"
11619                    "  b; \\\r\n"
11620                    "  c; d; \r\n",
11621                    getGoogleStyle()));
11622 
11623   EXPECT_EQ("/*\r\n"
11624             "multi line block comments\r\n"
11625             "should not introduce\r\n"
11626             "an extra carriage return\r\n"
11627             "*/\r\n",
11628             format("/*\r\n"
11629                    "multi line block comments\r\n"
11630                    "should not introduce\r\n"
11631                    "an extra carriage return\r\n"
11632                    "*/\r\n"));
11633 }
11634 
11635 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11636   verifyFormat("MY_CLASS(C) {\n"
11637                "  int i;\n"
11638                "  int j;\n"
11639                "};");
11640 }
11641 
11642 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11643   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11644   TwoIndent.ContinuationIndentWidth = 2;
11645 
11646   EXPECT_EQ("int i =\n"
11647             "  longFunction(\n"
11648             "    arg);",
11649             format("int i = longFunction(arg);", TwoIndent));
11650 
11651   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11652   SixIndent.ContinuationIndentWidth = 6;
11653 
11654   EXPECT_EQ("int i =\n"
11655             "      longFunction(\n"
11656             "            arg);",
11657             format("int i = longFunction(arg);", SixIndent));
11658 }
11659 
11660 TEST_F(FormatTest, SpacesInAngles) {
11661   FormatStyle Spaces = getLLVMStyle();
11662   Spaces.SpacesInAngles = true;
11663 
11664   verifyFormat("static_cast< int >(arg);", Spaces);
11665   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11666   verifyFormat("f< int, float >();", Spaces);
11667   verifyFormat("template <> g() {}", Spaces);
11668   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11669   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11670   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11671                Spaces);
11672 
11673   Spaces.Standard = FormatStyle::LS_Cpp03;
11674   Spaces.SpacesInAngles = true;
11675   verifyFormat("A< A< int > >();", Spaces);
11676 
11677   Spaces.SpacesInAngles = false;
11678   verifyFormat("A<A<int> >();", Spaces);
11679 
11680   Spaces.Standard = FormatStyle::LS_Cpp11;
11681   Spaces.SpacesInAngles = true;
11682   verifyFormat("A< A< int > >();", Spaces);
11683 
11684   Spaces.SpacesInAngles = false;
11685   verifyFormat("A<A<int>>();", Spaces);
11686 }
11687 
11688 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11689   FormatStyle Style = getLLVMStyle();
11690   Style.SpaceAfterTemplateKeyword = false;
11691   verifyFormat("template<int> void foo();", Style);
11692 }
11693 
11694 TEST_F(FormatTest, TripleAngleBrackets) {
11695   verifyFormat("f<<<1, 1>>>();");
11696   verifyFormat("f<<<1, 1, 1, s>>>();");
11697   verifyFormat("f<<<a, b, c, d>>>();");
11698   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11699   verifyFormat("f<param><<<1, 1>>>();");
11700   verifyFormat("f<1><<<1, 1>>>();");
11701   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11702   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11703                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11704   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11705                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11706 }
11707 
11708 TEST_F(FormatTest, MergeLessLessAtEnd) {
11709   verifyFormat("<<");
11710   EXPECT_EQ("< < <", format("\\\n<<<"));
11711   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11712                "aaallvm::outs() <<");
11713   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11714                "aaaallvm::outs()\n    <<");
11715 }
11716 
11717 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11718   std::string code = "#if A\n"
11719                      "#if B\n"
11720                      "a.\n"
11721                      "#endif\n"
11722                      "    a = 1;\n"
11723                      "#else\n"
11724                      "#endif\n"
11725                      "#if C\n"
11726                      "#else\n"
11727                      "#endif\n";
11728   EXPECT_EQ(code, format(code));
11729 }
11730 
11731 TEST_F(FormatTest, HandleConflictMarkers) {
11732   // Git/SVN conflict markers.
11733   EXPECT_EQ("int a;\n"
11734             "void f() {\n"
11735             "  callme(some(parameter1,\n"
11736             "<<<<<<< text by the vcs\n"
11737             "              parameter2),\n"
11738             "||||||| text by the vcs\n"
11739             "              parameter2),\n"
11740             "         parameter3,\n"
11741             "======= text by the vcs\n"
11742             "              parameter2, parameter3),\n"
11743             ">>>>>>> text by the vcs\n"
11744             "         otherparameter);\n",
11745             format("int a;\n"
11746                    "void f() {\n"
11747                    "  callme(some(parameter1,\n"
11748                    "<<<<<<< text by the vcs\n"
11749                    "  parameter2),\n"
11750                    "||||||| text by the vcs\n"
11751                    "  parameter2),\n"
11752                    "  parameter3,\n"
11753                    "======= text by the vcs\n"
11754                    "  parameter2,\n"
11755                    "  parameter3),\n"
11756                    ">>>>>>> text by the vcs\n"
11757                    "  otherparameter);\n"));
11758 
11759   // Perforce markers.
11760   EXPECT_EQ("void f() {\n"
11761             "  function(\n"
11762             ">>>> text by the vcs\n"
11763             "      parameter,\n"
11764             "==== text by the vcs\n"
11765             "      parameter,\n"
11766             "==== text by the vcs\n"
11767             "      parameter,\n"
11768             "<<<< text by the vcs\n"
11769             "      parameter);\n",
11770             format("void f() {\n"
11771                    "  function(\n"
11772                    ">>>> text by the vcs\n"
11773                    "  parameter,\n"
11774                    "==== text by the vcs\n"
11775                    "  parameter,\n"
11776                    "==== text by the vcs\n"
11777                    "  parameter,\n"
11778                    "<<<< text by the vcs\n"
11779                    "  parameter);\n"));
11780 
11781   EXPECT_EQ("<<<<<<<\n"
11782             "|||||||\n"
11783             "=======\n"
11784             ">>>>>>>",
11785             format("<<<<<<<\n"
11786                    "|||||||\n"
11787                    "=======\n"
11788                    ">>>>>>>"));
11789 
11790   EXPECT_EQ("<<<<<<<\n"
11791             "|||||||\n"
11792             "int i;\n"
11793             "=======\n"
11794             ">>>>>>>",
11795             format("<<<<<<<\n"
11796                    "|||||||\n"
11797                    "int i;\n"
11798                    "=======\n"
11799                    ">>>>>>>"));
11800 
11801   // FIXME: Handle parsing of macros around conflict markers correctly:
11802   EXPECT_EQ("#define Macro \\\n"
11803             "<<<<<<<\n"
11804             "Something \\\n"
11805             "|||||||\n"
11806             "Else \\\n"
11807             "=======\n"
11808             "Other \\\n"
11809             ">>>>>>>\n"
11810             "    End int i;\n",
11811             format("#define Macro \\\n"
11812                    "<<<<<<<\n"
11813                    "  Something \\\n"
11814                    "|||||||\n"
11815                    "  Else \\\n"
11816                    "=======\n"
11817                    "  Other \\\n"
11818                    ">>>>>>>\n"
11819                    "  End\n"
11820                    "int i;\n"));
11821 }
11822 
11823 TEST_F(FormatTest, DisableRegions) {
11824   EXPECT_EQ("int i;\n"
11825             "// clang-format off\n"
11826             "  int j;\n"
11827             "// clang-format on\n"
11828             "int k;",
11829             format(" int  i;\n"
11830                    "   // clang-format off\n"
11831                    "  int j;\n"
11832                    " // clang-format on\n"
11833                    "   int   k;"));
11834   EXPECT_EQ("int i;\n"
11835             "/* clang-format off */\n"
11836             "  int j;\n"
11837             "/* clang-format on */\n"
11838             "int k;",
11839             format(" int  i;\n"
11840                    "   /* clang-format off */\n"
11841                    "  int j;\n"
11842                    " /* clang-format on */\n"
11843                    "   int   k;"));
11844 
11845   // Don't reflow comments within disabled regions.
11846   EXPECT_EQ(
11847       "// clang-format off\n"
11848       "// long long long long long long line\n"
11849       "/* clang-format on */\n"
11850       "/* long long long\n"
11851       " * long long long\n"
11852       " * line */\n"
11853       "int i;\n"
11854       "/* clang-format off */\n"
11855       "/* long long long long long long line */\n",
11856       format("// clang-format off\n"
11857              "// long long long long long long line\n"
11858              "/* clang-format on */\n"
11859              "/* long long long long long long line */\n"
11860              "int i;\n"
11861              "/* clang-format off */\n"
11862              "/* long long long long long long line */\n",
11863              getLLVMStyleWithColumns(20)));
11864 }
11865 
11866 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11867   format("? ) =");
11868   verifyNoCrash("#define a\\\n /**/}");
11869 }
11870 
11871 TEST_F(FormatTest, FormatsTableGenCode) {
11872   FormatStyle Style = getLLVMStyle();
11873   Style.Language = FormatStyle::LK_TableGen;
11874   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11875 }
11876 
11877 TEST_F(FormatTest, ArrayOfTemplates) {
11878   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
11879             format("auto a = new unique_ptr<int > [ 10];"));
11880 
11881   FormatStyle Spaces = getLLVMStyle();
11882   Spaces.SpacesInSquareBrackets = true;
11883   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
11884             format("auto a = new unique_ptr<int > [10];", Spaces));
11885 }
11886 
11887 TEST_F(FormatTest, ArrayAsTemplateType) {
11888   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
11889             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
11890 
11891   FormatStyle Spaces = getLLVMStyle();
11892   Spaces.SpacesInSquareBrackets = true;
11893   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
11894             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
11895 }
11896 
11897 TEST_F(FormatTest, NoSpaceAfterSuper) {
11898     verifyFormat("__super::FooBar();");
11899 }
11900 
11901 TEST(FormatStyle, GetStyleWithEmptyFileName) {
11902   vfs::InMemoryFileSystem FS;
11903   auto Style1 = getStyle("file", "", "Google", "", &FS);
11904   ASSERT_TRUE((bool)Style1);
11905   ASSERT_EQ(*Style1, getGoogleStyle());
11906 }
11907 
11908 TEST(FormatStyle, GetStyleOfFile) {
11909   vfs::InMemoryFileSystem FS;
11910   // Test 1: format file in the same directory.
11911   ASSERT_TRUE(
11912       FS.addFile("/a/.clang-format", 0,
11913                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11914   ASSERT_TRUE(
11915       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11916   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
11917   ASSERT_TRUE((bool)Style1);
11918   ASSERT_EQ(*Style1, getLLVMStyle());
11919 
11920   // Test 2.1: fallback to default.
11921   ASSERT_TRUE(
11922       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11923   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
11924   ASSERT_TRUE((bool)Style2);
11925   ASSERT_EQ(*Style2, getMozillaStyle());
11926 
11927   // Test 2.2: no format on 'none' fallback style.
11928   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11929   ASSERT_TRUE((bool)Style2);
11930   ASSERT_EQ(*Style2, getNoStyle());
11931 
11932   // Test 2.3: format if config is found with no based style while fallback is
11933   // 'none'.
11934   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
11935                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
11936   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11937   ASSERT_TRUE((bool)Style2);
11938   ASSERT_EQ(*Style2, getLLVMStyle());
11939 
11940   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
11941   Style2 = getStyle("{}", "a.h", "none", "", &FS);
11942   ASSERT_TRUE((bool)Style2);
11943   ASSERT_EQ(*Style2, getLLVMStyle());
11944 
11945   // Test 3: format file in parent directory.
11946   ASSERT_TRUE(
11947       FS.addFile("/c/.clang-format", 0,
11948                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11949   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11950                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11951   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11952   ASSERT_TRUE((bool)Style3);
11953   ASSERT_EQ(*Style3, getGoogleStyle());
11954 
11955   // Test 4: error on invalid fallback style
11956   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11957   ASSERT_FALSE((bool)Style4);
11958   llvm::consumeError(Style4.takeError());
11959 
11960   // Test 5: error on invalid yaml on command line
11961   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11962   ASSERT_FALSE((bool)Style5);
11963   llvm::consumeError(Style5.takeError());
11964 
11965   // Test 6: error on invalid style
11966   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11967   ASSERT_FALSE((bool)Style6);
11968   llvm::consumeError(Style6.takeError());
11969 
11970   // Test 7: found config file, error on parsing it
11971   ASSERT_TRUE(
11972       FS.addFile("/d/.clang-format", 0,
11973                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11974                                                   "InvalidKey: InvalidValue")));
11975   ASSERT_TRUE(
11976       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11977   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11978   ASSERT_FALSE((bool)Style7);
11979   llvm::consumeError(Style7.takeError());
11980 }
11981 
11982 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11983   // Column limit is 20.
11984   std::string Code = "Type *a =\n"
11985                      "    new Type();\n"
11986                      "g(iiiii, 0, jjjjj,\n"
11987                      "  0, kkkkk, 0, mm);\n"
11988                      "int  bad     = format   ;";
11989   std::string Expected = "auto a = new Type();\n"
11990                          "g(iiiii, nullptr,\n"
11991                          "  jjjjj, nullptr,\n"
11992                          "  kkkkk, nullptr,\n"
11993                          "  mm);\n"
11994                          "int  bad     = format   ;";
11995   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11996   tooling::Replacements Replaces = toReplacements(
11997       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11998                             "auto "),
11999        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12000                             "nullptr"),
12001        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12002                             "nullptr"),
12003        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12004                             "nullptr")});
12005 
12006   format::FormatStyle Style = format::getLLVMStyle();
12007   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12008   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12009   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12010       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12011   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12012   EXPECT_TRUE(static_cast<bool>(Result));
12013   EXPECT_EQ(Expected, *Result);
12014 }
12015 
12016 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12017   std::string Code = "#include \"a.h\"\n"
12018                      "#include \"c.h\"\n"
12019                      "\n"
12020                      "int main() {\n"
12021                      "  return 0;\n"
12022                      "}";
12023   std::string Expected = "#include \"a.h\"\n"
12024                          "#include \"b.h\"\n"
12025                          "#include \"c.h\"\n"
12026                          "\n"
12027                          "int main() {\n"
12028                          "  return 0;\n"
12029                          "}";
12030   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12031   tooling::Replacements Replaces = toReplacements(
12032       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12033                             "#include \"b.h\"\n")});
12034 
12035   format::FormatStyle Style = format::getLLVMStyle();
12036   Style.SortIncludes = true;
12037   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12038   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12039       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12040   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12041   EXPECT_TRUE(static_cast<bool>(Result));
12042   EXPECT_EQ(Expected, *Result);
12043 }
12044 
12045 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12046   EXPECT_EQ("using std::cin;\n"
12047             "using std::cout;",
12048             format("using std::cout;\n"
12049                    "using std::cin;", getGoogleStyle()));
12050 }
12051 
12052 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12053   format::FormatStyle Style = format::getLLVMStyle();
12054   Style.Standard = FormatStyle::LS_Cpp03;
12055   // cpp03 recognize this string as identifier u8 and literal character 'a'
12056   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12057 }
12058 
12059 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12060   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12061   // all modes, including C++11, C++14 and C++17
12062   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12063 }
12064 
12065 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12066   EXPECT_EQ("<!-- ;> -->",
12067             format("<!-- ;> -->", getGoogleStyle()));
12068   EXPECT_EQ(" <!-- >; -->",
12069             format(" <!-- >; -->", getGoogleStyle()));
12070 }
12071 
12072 TEST_F(FormatTest, StructuredBindings) {
12073   // Structured bindings is a C++17 feature.
12074   // all modes, including C++11, C++14 and C++17
12075   verifyFormat("auto [a, b] = f();");
12076   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12077   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12078   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12079   EXPECT_EQ("auto const volatile [a, b] = f();",
12080             format("auto  const   volatile[a, b] = f();"));
12081   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12082   EXPECT_EQ("auto &[a, b, c] = f();",
12083             format("auto   &[  a  ,  b,c   ] = f();"));
12084   EXPECT_EQ("auto &&[a, b, c] = f();",
12085             format("auto   &&[  a  ,  b,c   ] = f();"));
12086   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12087   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12088             format("auto  const  volatile  &&[a, b] = f();"));
12089   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12090   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12091   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12092             format("const  auto   volatile  &&[a, b] = f();"));
12093   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12094             format("volatile  const  auto   &&[a, b] = f();"));
12095   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12096 
12097   // Make sure we don't mistake structured bindings for lambdas.
12098   FormatStyle PointerMiddle = getLLVMStyle();
12099   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12100   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12101   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12102   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12103   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12104   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12105   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12106   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12107   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12108   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12109   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12110   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12111   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12112 
12113   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12114             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12115   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12116             format("for (const auto   &   [a, b] : some_range) {\n}"));
12117   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12118             format("for (const auto[a, b] : some_range) {\n}"));
12119   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12120   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12121   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12122   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12123   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12124   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12125   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12126   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12127 
12128   format::FormatStyle Spaces = format::getLLVMStyle();
12129   Spaces.SpacesInSquareBrackets = true;
12130   verifyFormat("auto [ a, b ] = f();", Spaces);
12131   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12132   verifyFormat("auto &[ a, b ] = f();", Spaces);
12133   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12134   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12135 }
12136 
12137 TEST_F(FormatTest, FileAndCode) {
12138   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12139   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12140   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12141   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12142   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12143   EXPECT_EQ(
12144       FormatStyle::LK_ObjC,
12145       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12146   EXPECT_EQ(FormatStyle::LK_ObjC,
12147             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12148   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12149   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12150   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12151   EXPECT_EQ(FormatStyle::LK_ObjC,
12152             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12153   EXPECT_EQ(
12154       FormatStyle::LK_ObjC,
12155       guessLanguage("foo.h",
12156                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12157   EXPECT_EQ(
12158       FormatStyle::LK_Cpp,
12159       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12160 }
12161 
12162 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12163   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12164   EXPECT_EQ(FormatStyle::LK_ObjC,
12165             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12166   EXPECT_EQ(FormatStyle::LK_Cpp,
12167             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12168   EXPECT_EQ(
12169       FormatStyle::LK_Cpp,
12170       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12171   EXPECT_EQ(FormatStyle::LK_ObjC,
12172             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12173   EXPECT_EQ(FormatStyle::LK_Cpp,
12174             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12175   EXPECT_EQ(FormatStyle::LK_ObjC,
12176             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12177   EXPECT_EQ(FormatStyle::LK_Cpp,
12178             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12179   EXPECT_EQ(FormatStyle::LK_Cpp,
12180             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12181   EXPECT_EQ(FormatStyle::LK_ObjC,
12182             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12183   EXPECT_EQ(FormatStyle::LK_Cpp,
12184             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12185   EXPECT_EQ(
12186       FormatStyle::LK_Cpp,
12187       guessLanguage("foo.h",
12188                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12189   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12190 }
12191 
12192 TEST_F(FormatTest, GuessLanguageWithCaret) {
12193   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12194   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12195   EXPECT_EQ(FormatStyle::LK_ObjC,
12196             guessLanguage("foo.h", "int(^)(char, float);"));
12197   EXPECT_EQ(FormatStyle::LK_ObjC,
12198             guessLanguage("foo.h", "int(^foo)(char, float);"));
12199   EXPECT_EQ(FormatStyle::LK_ObjC,
12200             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12201   EXPECT_EQ(FormatStyle::LK_ObjC,
12202             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12203   EXPECT_EQ(
12204       FormatStyle::LK_ObjC,
12205       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12206 }
12207 
12208 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12209   EXPECT_EQ(FormatStyle::LK_Cpp,
12210             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12211   EXPECT_EQ(FormatStyle::LK_ObjC,
12212             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12213   EXPECT_EQ(
12214       FormatStyle::LK_Cpp,
12215       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12216   EXPECT_EQ(
12217       FormatStyle::LK_ObjC,
12218       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12219 }
12220 
12221 } // end namespace
12222 } // end namespace format
12223 } // end namespace clang
12224