1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "gtest/gtest.h"
18 
19 #define DEBUG_TYPE "format-test"
20 
21 using clang::tooling::ReplacementTest;
22 using clang::tooling::toReplacements;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck {
33     SC_ExpectComplete,
34     SC_ExpectIncomplete,
35     SC_DoNotCheck
36   };
37 
38   std::string format(llvm::StringRef Code,
39                      const FormatStyle &Style = getLLVMStyle(),
40                      StatusCheck CheckComplete = SC_ExpectComplete) {
41     LLVM_DEBUG(llvm::errs() << "---\n");
42     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
43     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
44     FormattingAttemptStatus Status;
45     tooling::Replacements Replaces =
46         reformat(Style, Code, Ranges, "<stdin>", &Status);
47     if (CheckComplete != SC_DoNotCheck) {
48       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
49       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
50           << Code << "\n\n";
51     }
52     ReplacementCount = Replaces.size();
53     auto Result = applyAllReplacements(Code, Replaces);
54     EXPECT_TRUE(static_cast<bool>(Result));
55     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
56     return *Result;
57   }
58 
59   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
60     Style.ColumnLimit = ColumnLimit;
61     return Style;
62   }
63 
64   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
66   }
67 
68   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
69     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
70   }
71 
72   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
73                     const FormatStyle &Style = getLLVMStyle()) {
74     EXPECT_EQ(Expected.str(), format(Expected, Style))
75         << "Expected code is not stable";
76     EXPECT_EQ(Expected.str(), format(Code, Style));
77     if (Style.Language == FormatStyle::LK_Cpp) {
78       // Objective-C++ is a superset of C++, so everything checked for C++
79       // needs to be checked for Objective-C++ as well.
80       FormatStyle ObjCStyle = Style;
81       ObjCStyle.Language = FormatStyle::LK_ObjC;
82       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
83     }
84   }
85 
86   void verifyFormat(llvm::StringRef Code,
87                     const FormatStyle &Style = getLLVMStyle()) {
88     verifyFormat(Code, test::messUp(Code), Style);
89   }
90 
91   void verifyIncompleteFormat(llvm::StringRef Code,
92                               const FormatStyle &Style = getLLVMStyle()) {
93     EXPECT_EQ(Code.str(),
94               format(test::messUp(Code), Style, SC_ExpectIncomplete));
95   }
96 
97   void verifyGoogleFormat(llvm::StringRef Code) {
98     verifyFormat(Code, getGoogleStyle());
99   }
100 
101   void verifyIndependentOfContext(llvm::StringRef text) {
102     verifyFormat(text);
103     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
104   }
105 
106   /// \brief Verify that clang-format does not crash on the given input.
107   void verifyNoCrash(llvm::StringRef Code,
108                      const FormatStyle &Style = getLLVMStyle()) {
109     format(Code, Style, SC_DoNotCheck);
110   }
111 
112   int ReplacementCount;
113 };
114 
115 TEST_F(FormatTest, MessUp) {
116   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
117   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
118   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
119   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
120   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
121 }
122 
123 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
124   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
125 }
126 
127 TEST_F(FormatTest, LLVMStyleOverride) {
128   EXPECT_EQ(FormatStyle::LK_Proto,
129             getLLVMStyle(FormatStyle::LK_Proto).Language);
130 }
131 
132 //===----------------------------------------------------------------------===//
133 // Basic function tests.
134 //===----------------------------------------------------------------------===//
135 
136 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
137   EXPECT_EQ(";", format(";"));
138 }
139 
140 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
141   EXPECT_EQ("int i;", format("  int i;"));
142   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
143   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
144   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
145 }
146 
147 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
148   EXPECT_EQ("int i;", format("int\ni;"));
149 }
150 
151 TEST_F(FormatTest, FormatsNestedBlockStatements) {
152   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
153 }
154 
155 TEST_F(FormatTest, FormatsNestedCall) {
156   verifyFormat("Method(f1, f2(f3));");
157   verifyFormat("Method(f1(f2, f3()));");
158   verifyFormat("Method(f1(f2, (f3())));");
159 }
160 
161 TEST_F(FormatTest, NestedNameSpecifiers) {
162   verifyFormat("vector<::Type> v;");
163   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
164   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
165   verifyFormat("bool a = 2 < ::SomeFunction();");
166   verifyFormat("ALWAYS_INLINE ::std::string getName();");
167   verifyFormat("some::string getName();");
168 }
169 
170 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
171   EXPECT_EQ("if (a) {\n"
172             "  f();\n"
173             "}",
174             format("if(a){f();}"));
175   EXPECT_EQ(4, ReplacementCount);
176   EXPECT_EQ("if (a) {\n"
177             "  f();\n"
178             "}",
179             format("if (a) {\n"
180                    "  f();\n"
181                    "}"));
182   EXPECT_EQ(0, ReplacementCount);
183   EXPECT_EQ("/*\r\n"
184             "\r\n"
185             "*/\r\n",
186             format("/*\r\n"
187                    "\r\n"
188                    "*/\r\n"));
189   EXPECT_EQ(0, ReplacementCount);
190 }
191 
192 TEST_F(FormatTest, RemovesEmptyLines) {
193   EXPECT_EQ("class C {\n"
194             "  int i;\n"
195             "};",
196             format("class C {\n"
197                    " int i;\n"
198                    "\n"
199                    "};"));
200 
201   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
202   EXPECT_EQ("namespace N {\n"
203             "\n"
204             "int i;\n"
205             "}",
206             format("namespace N {\n"
207                    "\n"
208                    "int    i;\n"
209                    "}",
210                    getGoogleStyle()));
211   EXPECT_EQ("/* something */ namespace N {\n"
212             "\n"
213             "int i;\n"
214             "}",
215             format("/* something */ namespace N {\n"
216                    "\n"
217                    "int    i;\n"
218                    "}",
219                    getGoogleStyle()));
220   EXPECT_EQ("inline namespace N {\n"
221             "\n"
222             "int i;\n"
223             "}",
224             format("inline namespace N {\n"
225                    "\n"
226                    "int    i;\n"
227                    "}",
228                    getGoogleStyle()));
229   EXPECT_EQ("/* something */ inline namespace N {\n"
230             "\n"
231             "int i;\n"
232             "}",
233             format("/* something */ inline namespace N {\n"
234                    "\n"
235                    "int    i;\n"
236                    "}",
237                    getGoogleStyle()));
238   EXPECT_EQ("export namespace N {\n"
239             "\n"
240             "int i;\n"
241             "}",
242             format("export namespace N {\n"
243                    "\n"
244                    "int    i;\n"
245                    "}",
246                    getGoogleStyle()));
247   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
248             "\n"
249             "int i;\n"
250             "}",
251             format("extern /**/ \"C\" /**/ {\n"
252                    "\n"
253                    "int    i;\n"
254                    "}",
255                    getGoogleStyle()));
256 
257   // ...but do keep inlining and removing empty lines for non-block extern "C"
258   // functions.
259   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
260   EXPECT_EQ("extern \"C\" int f() {\n"
261             "  int i = 42;\n"
262             "  return i;\n"
263             "}",
264             format("extern \"C\" int f() {\n"
265                    "\n"
266                    "  int i = 42;\n"
267                    "  return i;\n"
268                    "}",
269                    getGoogleStyle()));
270 
271   // Remove empty lines at the beginning and end of blocks.
272   EXPECT_EQ("void f() {\n"
273             "\n"
274             "  if (a) {\n"
275             "\n"
276             "    f();\n"
277             "  }\n"
278             "}",
279             format("void f() {\n"
280                    "\n"
281                    "  if (a) {\n"
282                    "\n"
283                    "    f();\n"
284                    "\n"
285                    "  }\n"
286                    "\n"
287                    "}",
288                    getLLVMStyle()));
289   EXPECT_EQ("void f() {\n"
290             "  if (a) {\n"
291             "    f();\n"
292             "  }\n"
293             "}",
294             format("void f() {\n"
295                    "\n"
296                    "  if (a) {\n"
297                    "\n"
298                    "    f();\n"
299                    "\n"
300                    "  }\n"
301                    "\n"
302                    "}",
303                    getGoogleStyle()));
304 
305   // Don't remove empty lines in more complex control statements.
306   EXPECT_EQ("void f() {\n"
307             "  if (a) {\n"
308             "    f();\n"
309             "\n"
310             "  } else if (b) {\n"
311             "    f();\n"
312             "  }\n"
313             "}",
314             format("void f() {\n"
315                    "  if (a) {\n"
316                    "    f();\n"
317                    "\n"
318                    "  } else if (b) {\n"
319                    "    f();\n"
320                    "\n"
321                    "  }\n"
322                    "\n"
323                    "}"));
324 
325   // Don't remove empty lines before namespace endings.
326   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
327   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
328   EXPECT_EQ("namespace {\n"
329             "int i;\n"
330             "\n"
331             "}",
332             format("namespace {\n"
333                    "int i;\n"
334                    "\n"
335                    "}", LLVMWithNoNamespaceFix));
336   EXPECT_EQ("namespace {\n"
337             "int i;\n"
338             "}",
339             format("namespace {\n"
340                    "int i;\n"
341                    "}", LLVMWithNoNamespaceFix));
342   EXPECT_EQ("namespace {\n"
343             "int i;\n"
344             "\n"
345             "};",
346             format("namespace {\n"
347                    "int i;\n"
348                    "\n"
349                    "};", LLVMWithNoNamespaceFix));
350   EXPECT_EQ("namespace {\n"
351             "int i;\n"
352             "};",
353             format("namespace {\n"
354                    "int i;\n"
355                    "};", LLVMWithNoNamespaceFix));
356   EXPECT_EQ("namespace {\n"
357             "int i;\n"
358             "\n"
359             "}",
360             format("namespace {\n"
361                    "int i;\n"
362                    "\n"
363                    "}"));
364   EXPECT_EQ("namespace {\n"
365             "int i;\n"
366             "\n"
367             "} // namespace",
368             format("namespace {\n"
369                    "int i;\n"
370                    "\n"
371                    "}  // namespace"));
372 
373   FormatStyle Style = getLLVMStyle();
374   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
375   Style.MaxEmptyLinesToKeep = 2;
376   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
377   Style.BraceWrapping.AfterClass = true;
378   Style.BraceWrapping.AfterFunction = true;
379   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
380 
381   EXPECT_EQ("class Foo\n"
382             "{\n"
383             "  Foo() {}\n"
384             "\n"
385             "  void funk() {}\n"
386             "};",
387             format("class Foo\n"
388                    "{\n"
389                    "  Foo()\n"
390                    "  {\n"
391                    "  }\n"
392                    "\n"
393                    "  void funk() {}\n"
394                    "};",
395                    Style));
396 }
397 
398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
399   verifyFormat("x = (a) and (b);");
400   verifyFormat("x = (a) or (b);");
401   verifyFormat("x = (a) bitand (b);");
402   verifyFormat("x = (a) bitor (b);");
403   verifyFormat("x = (a) not_eq (b);");
404   verifyFormat("x = (a) and_eq (b);");
405   verifyFormat("x = (a) or_eq (b);");
406   verifyFormat("x = (a) xor (b);");
407 }
408 
409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
410   verifyFormat("x = compl(a);");
411   verifyFormat("x = not(a);");
412   verifyFormat("x = bitand(a);");
413   // Unary operator must not be merged with the next identifier
414   verifyFormat("x = compl a;");
415   verifyFormat("x = not a;");
416   verifyFormat("x = bitand a;");
417 }
418 
419 //===----------------------------------------------------------------------===//
420 // Tests for control statements.
421 //===----------------------------------------------------------------------===//
422 
423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
424   verifyFormat("if (true)\n  f();\ng();");
425   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
426   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
427   verifyFormat("if constexpr (true)\n"
428                "  f();\ng();");
429   verifyFormat("if constexpr (a)\n"
430                "  if constexpr (b)\n"
431                "    if constexpr (c)\n"
432                "      g();\n"
433                "h();");
434   verifyFormat("if constexpr (a)\n"
435                "  if constexpr (b) {\n"
436                "    f();\n"
437                "  }\n"
438                "g();");
439 
440   FormatStyle AllowsMergedIf = getLLVMStyle();
441   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
442   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
443       FormatStyle::SIS_WithoutElse;
444   verifyFormat("if (a)\n"
445                "  // comment\n"
446                "  f();",
447                AllowsMergedIf);
448   verifyFormat("{\n"
449                "  if (a)\n"
450                "  label:\n"
451                "    f();\n"
452                "}",
453                AllowsMergedIf);
454   verifyFormat("#define A \\\n"
455                "  if (a)  \\\n"
456                "  label:  \\\n"
457                "    f()",
458                AllowsMergedIf);
459   verifyFormat("if (a)\n"
460                "  ;",
461                AllowsMergedIf);
462   verifyFormat("if (a)\n"
463                "  if (b) return;",
464                AllowsMergedIf);
465 
466   verifyFormat("if (a) // Can't merge this\n"
467                "  f();\n",
468                AllowsMergedIf);
469   verifyFormat("if (a) /* still don't merge */\n"
470                "  f();",
471                AllowsMergedIf);
472   verifyFormat("if (a) { // Never merge this\n"
473                "  f();\n"
474                "}",
475                AllowsMergedIf);
476   verifyFormat("if (a) { /* Never merge this */\n"
477                "  f();\n"
478                "}",
479                AllowsMergedIf);
480 
481   AllowsMergedIf.ColumnLimit = 14;
482   verifyFormat("if (a) return;", AllowsMergedIf);
483   verifyFormat("if (aaaaaaaaa)\n"
484                "  return;",
485                AllowsMergedIf);
486 
487   AllowsMergedIf.ColumnLimit = 13;
488   verifyFormat("if (a)\n  return;", AllowsMergedIf);
489 }
490 
491 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
492   FormatStyle AllowsMergedIf = getLLVMStyle();
493   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
494   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
495       FormatStyle::SIS_WithoutElse;
496   verifyFormat("if (a)\n"
497                "  f();\n"
498                "else {\n"
499                "  g();\n"
500                "}",
501                AllowsMergedIf);
502   verifyFormat("if (a)\n"
503                "  f();\n"
504                "else\n"
505                "  g();\n",
506                AllowsMergedIf);
507 
508   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
509 
510   verifyFormat("if (a) f();\n"
511                "else {\n"
512                "  g();\n"
513                "}",
514                AllowsMergedIf);
515   verifyFormat("if (a) f();\n"
516                "else {\n"
517                "  if (a) f();\n"
518                "  else {\n"
519                "    g();\n"
520                "  }\n"
521                "  g();\n"
522                "}",
523                AllowsMergedIf);
524 }
525 
526 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
527   FormatStyle AllowsMergedLoops = getLLVMStyle();
528   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
529   verifyFormat("while (true) continue;", AllowsMergedLoops);
530   verifyFormat("for (;;) continue;", AllowsMergedLoops);
531   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
532   verifyFormat("while (true)\n"
533                "  ;",
534                AllowsMergedLoops);
535   verifyFormat("for (;;)\n"
536                "  ;",
537                AllowsMergedLoops);
538   verifyFormat("for (;;)\n"
539                "  for (;;) continue;",
540                AllowsMergedLoops);
541   verifyFormat("for (;;) // Can't merge this\n"
542                "  continue;",
543                AllowsMergedLoops);
544   verifyFormat("for (;;) /* still don't merge */\n"
545                "  continue;",
546                AllowsMergedLoops);
547 }
548 
549 TEST_F(FormatTest, FormatShortBracedStatements) {
550   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
551   AllowSimpleBracedStatements.ColumnLimit = 40;
552   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
553 
554   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
555       FormatStyle::SIS_WithoutElse;
556   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
557 
558   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
559   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
560   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
561 
562   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
563   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
564   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
565   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
566   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
567   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
568   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
569   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
570   verifyFormat("if (true) {\n"
571                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
572                "}",
573                AllowSimpleBracedStatements);
574   verifyFormat("if (true) { //\n"
575                "  f();\n"
576                "}",
577                AllowSimpleBracedStatements);
578   verifyFormat("if (true) {\n"
579                "  f();\n"
580                "  f();\n"
581                "}",
582                AllowSimpleBracedStatements);
583   verifyFormat("if (true) {\n"
584                "  f();\n"
585                "} else {\n"
586                "  f();\n"
587                "}",
588                AllowSimpleBracedStatements);
589 
590   verifyFormat("struct A2 {\n"
591                "  int X;\n"
592                "};",
593                AllowSimpleBracedStatements);
594   verifyFormat("typedef struct A2 {\n"
595                "  int X;\n"
596                "} A2_t;",
597                AllowSimpleBracedStatements);
598   verifyFormat("template <int> struct A2 {\n"
599                "  struct B {};\n"
600                "};",
601                AllowSimpleBracedStatements);
602 
603   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
604       FormatStyle::SIS_Never;
605   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
606   verifyFormat("if (true) {\n"
607                "  f();\n"
608                "}",
609                AllowSimpleBracedStatements);
610   verifyFormat("if (true) {\n"
611                "  f();\n"
612                "} else {\n"
613                "  f();\n"
614                "}",
615                AllowSimpleBracedStatements);
616 
617   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
618   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
619   verifyFormat("while (true) {\n"
620                "  f();\n"
621                "}",
622                AllowSimpleBracedStatements);
623   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
624   verifyFormat("for (;;) {\n"
625                "  f();\n"
626                "}",
627                AllowSimpleBracedStatements);
628 
629   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
630       FormatStyle::SIS_WithoutElse;
631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
632   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
633 
634   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
635   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
636   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
638   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
639   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
640   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
641   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
642   verifyFormat("if (true)\n"
643                "{\n"
644                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
645                "}",
646                AllowSimpleBracedStatements);
647   verifyFormat("if (true)\n"
648                "{ //\n"
649                "  f();\n"
650                "}",
651                AllowSimpleBracedStatements);
652   verifyFormat("if (true)\n"
653                "{\n"
654                "  f();\n"
655                "  f();\n"
656                "}",
657                AllowSimpleBracedStatements);
658   verifyFormat("if (true)\n"
659                "{\n"
660                "  f();\n"
661                "} else\n"
662                "{\n"
663                "  f();\n"
664                "}",
665                AllowSimpleBracedStatements);
666 
667   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
668       FormatStyle::SIS_Never;
669   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
670   verifyFormat("if (true)\n"
671                "{\n"
672                "  f();\n"
673                "}",
674                AllowSimpleBracedStatements);
675   verifyFormat("if (true)\n"
676                "{\n"
677                "  f();\n"
678                "} else\n"
679                "{\n"
680                "  f();\n"
681                "}",
682                AllowSimpleBracedStatements);
683 
684   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
685   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
686   verifyFormat("while (true)\n"
687                "{\n"
688                "  f();\n"
689                "}",
690                AllowSimpleBracedStatements);
691   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
692   verifyFormat("for (;;)\n"
693                "{\n"
694                "  f();\n"
695                "}",
696                AllowSimpleBracedStatements);
697 }
698 
699 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
700   FormatStyle Style = getLLVMStyleWithColumns(60);
701   Style.AllowShortBlocksOnASingleLine = true;
702   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
703   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
704   EXPECT_EQ("#define A                                                  \\\n"
705             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
706             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
707             "X;",
708             format("#define A \\\n"
709                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
710                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
711                    "   }\n"
712                    "X;",
713                    Style));
714 }
715 
716 TEST_F(FormatTest, ParseIfElse) {
717   verifyFormat("if (true)\n"
718                "  if (true)\n"
719                "    if (true)\n"
720                "      f();\n"
721                "    else\n"
722                "      g();\n"
723                "  else\n"
724                "    h();\n"
725                "else\n"
726                "  i();");
727   verifyFormat("if (true)\n"
728                "  if (true)\n"
729                "    if (true) {\n"
730                "      if (true)\n"
731                "        f();\n"
732                "    } else {\n"
733                "      g();\n"
734                "    }\n"
735                "  else\n"
736                "    h();\n"
737                "else {\n"
738                "  i();\n"
739                "}");
740   verifyFormat("if (true)\n"
741                "  if constexpr (true)\n"
742                "    if (true) {\n"
743                "      if constexpr (true)\n"
744                "        f();\n"
745                "    } else {\n"
746                "      g();\n"
747                "    }\n"
748                "  else\n"
749                "    h();\n"
750                "else {\n"
751                "  i();\n"
752                "}");
753   verifyFormat("void f() {\n"
754                "  if (a) {\n"
755                "  } else {\n"
756                "  }\n"
757                "}");
758 }
759 
760 TEST_F(FormatTest, ElseIf) {
761   verifyFormat("if (a) {\n} else if (b) {\n}");
762   verifyFormat("if (a)\n"
763                "  f();\n"
764                "else if (b)\n"
765                "  g();\n"
766                "else\n"
767                "  h();");
768   verifyFormat("if constexpr (a)\n"
769                "  f();\n"
770                "else if constexpr (b)\n"
771                "  g();\n"
772                "else\n"
773                "  h();");
774   verifyFormat("if (a) {\n"
775                "  f();\n"
776                "}\n"
777                "// or else ..\n"
778                "else {\n"
779                "  g()\n"
780                "}");
781 
782   verifyFormat("if (a) {\n"
783                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
784                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
785                "}");
786   verifyFormat("if (a) {\n"
787                "} else if (\n"
788                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
789                "}",
790                getLLVMStyleWithColumns(62));
791   verifyFormat("if (a) {\n"
792                "} else if constexpr (\n"
793                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
794                "}",
795                getLLVMStyleWithColumns(62));
796 }
797 
798 TEST_F(FormatTest, FormatsForLoop) {
799   verifyFormat(
800       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
801       "     ++VeryVeryLongLoopVariable)\n"
802       "  ;");
803   verifyFormat("for (;;)\n"
804                "  f();");
805   verifyFormat("for (;;) {\n}");
806   verifyFormat("for (;;) {\n"
807                "  f();\n"
808                "}");
809   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
810 
811   verifyFormat(
812       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
813       "                                          E = UnwrappedLines.end();\n"
814       "     I != E; ++I) {\n}");
815 
816   verifyFormat(
817       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
818       "     ++IIIII) {\n}");
819   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
820                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
821                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
822   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
823                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
824                "         E = FD->getDeclsInPrototypeScope().end();\n"
825                "     I != E; ++I) {\n}");
826   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
827                "         I = Container.begin(),\n"
828                "         E = Container.end();\n"
829                "     I != E; ++I) {\n}",
830                getLLVMStyleWithColumns(76));
831 
832   verifyFormat(
833       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
834       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
835       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
836       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
837       "     ++aaaaaaaaaaa) {\n}");
838   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
839                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
840                "     ++i) {\n}");
841   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
842                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
843                "}");
844   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
845                "         aaaaaaaaaa);\n"
846                "     iter; ++iter) {\n"
847                "}");
848   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
849                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
850                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
851                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
852 
853   // These should not be formatted as Objective-C for-in loops.
854   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
855   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
856   verifyFormat("Foo *x;\nfor (x in y) {\n}");
857   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
858 
859   FormatStyle NoBinPacking = getLLVMStyle();
860   NoBinPacking.BinPackParameters = false;
861   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
862                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
863                "                                           aaaaaaaaaaaaaaaa,\n"
864                "                                           aaaaaaaaaaaaaaaa,\n"
865                "                                           aaaaaaaaaaaaaaaa);\n"
866                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
867                "}",
868                NoBinPacking);
869   verifyFormat(
870       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
871       "                                          E = UnwrappedLines.end();\n"
872       "     I != E;\n"
873       "     ++I) {\n}",
874       NoBinPacking);
875 
876   FormatStyle AlignLeft = getLLVMStyle();
877   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
878   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
879 }
880 
881 TEST_F(FormatTest, RangeBasedForLoops) {
882   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
883                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
884   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
885                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
886   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
887                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
888   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
889                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
890 }
891 
892 TEST_F(FormatTest, ForEachLoops) {
893   verifyFormat("void f() {\n"
894                "  foreach (Item *item, itemlist) {}\n"
895                "  Q_FOREACH (Item *item, itemlist) {}\n"
896                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
897                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
898                "}");
899 
900   // As function-like macros.
901   verifyFormat("#define foreach(x, y)\n"
902                "#define Q_FOREACH(x, y)\n"
903                "#define BOOST_FOREACH(x, y)\n"
904                "#define UNKNOWN_FOREACH(x, y)\n");
905 
906   // Not as function-like macros.
907   verifyFormat("#define foreach (x, y)\n"
908                "#define Q_FOREACH (x, y)\n"
909                "#define BOOST_FOREACH (x, y)\n"
910                "#define UNKNOWN_FOREACH (x, y)\n");
911 }
912 
913 TEST_F(FormatTest, FormatsWhileLoop) {
914   verifyFormat("while (true) {\n}");
915   verifyFormat("while (true)\n"
916                "  f();");
917   verifyFormat("while () {\n}");
918   verifyFormat("while () {\n"
919                "  f();\n"
920                "}");
921 }
922 
923 TEST_F(FormatTest, FormatsDoWhile) {
924   verifyFormat("do {\n"
925                "  do_something();\n"
926                "} while (something());");
927   verifyFormat("do\n"
928                "  do_something();\n"
929                "while (something());");
930 }
931 
932 TEST_F(FormatTest, FormatsSwitchStatement) {
933   verifyFormat("switch (x) {\n"
934                "case 1:\n"
935                "  f();\n"
936                "  break;\n"
937                "case kFoo:\n"
938                "case ns::kBar:\n"
939                "case kBaz:\n"
940                "  break;\n"
941                "default:\n"
942                "  g();\n"
943                "  break;\n"
944                "}");
945   verifyFormat("switch (x) {\n"
946                "case 1: {\n"
947                "  f();\n"
948                "  break;\n"
949                "}\n"
950                "case 2: {\n"
951                "  break;\n"
952                "}\n"
953                "}");
954   verifyFormat("switch (x) {\n"
955                "case 1: {\n"
956                "  f();\n"
957                "  {\n"
958                "    g();\n"
959                "    h();\n"
960                "  }\n"
961                "  break;\n"
962                "}\n"
963                "}");
964   verifyFormat("switch (x) {\n"
965                "case 1: {\n"
966                "  f();\n"
967                "  if (foo) {\n"
968                "    g();\n"
969                "    h();\n"
970                "  }\n"
971                "  break;\n"
972                "}\n"
973                "}");
974   verifyFormat("switch (x) {\n"
975                "case 1: {\n"
976                "  f();\n"
977                "  g();\n"
978                "} break;\n"
979                "}");
980   verifyFormat("switch (test)\n"
981                "  ;");
982   verifyFormat("switch (x) {\n"
983                "default: {\n"
984                "  // Do nothing.\n"
985                "}\n"
986                "}");
987   verifyFormat("switch (x) {\n"
988                "// comment\n"
989                "// if 1, do f()\n"
990                "case 1:\n"
991                "  f();\n"
992                "}");
993   verifyFormat("switch (x) {\n"
994                "case 1:\n"
995                "  // Do amazing stuff\n"
996                "  {\n"
997                "    f();\n"
998                "    g();\n"
999                "  }\n"
1000                "  break;\n"
1001                "}");
1002   verifyFormat("#define A          \\\n"
1003                "  switch (x) {     \\\n"
1004                "  case a:          \\\n"
1005                "    foo = b;       \\\n"
1006                "  }",
1007                getLLVMStyleWithColumns(20));
1008   verifyFormat("#define OPERATION_CASE(name)           \\\n"
1009                "  case OP_name:                        \\\n"
1010                "    return operations::Operation##name\n",
1011                getLLVMStyleWithColumns(40));
1012   verifyFormat("switch (x) {\n"
1013                "case 1:;\n"
1014                "default:;\n"
1015                "  int i;\n"
1016                "}");
1017 
1018   verifyGoogleFormat("switch (x) {\n"
1019                      "  case 1:\n"
1020                      "    f();\n"
1021                      "    break;\n"
1022                      "  case kFoo:\n"
1023                      "  case ns::kBar:\n"
1024                      "  case kBaz:\n"
1025                      "    break;\n"
1026                      "  default:\n"
1027                      "    g();\n"
1028                      "    break;\n"
1029                      "}");
1030   verifyGoogleFormat("switch (x) {\n"
1031                      "  case 1: {\n"
1032                      "    f();\n"
1033                      "    break;\n"
1034                      "  }\n"
1035                      "}");
1036   verifyGoogleFormat("switch (test)\n"
1037                      "  ;");
1038 
1039   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1040                      "  case OP_name:              \\\n"
1041                      "    return operations::Operation##name\n");
1042   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1043                      "  // Get the correction operation class.\n"
1044                      "  switch (OpCode) {\n"
1045                      "    CASE(Add);\n"
1046                      "    CASE(Subtract);\n"
1047                      "    default:\n"
1048                      "      return operations::Unknown;\n"
1049                      "  }\n"
1050                      "#undef OPERATION_CASE\n"
1051                      "}");
1052   verifyFormat("DEBUG({\n"
1053                "  switch (x) {\n"
1054                "  case A:\n"
1055                "    f();\n"
1056                "    break;\n"
1057                "    // fallthrough\n"
1058                "  case B:\n"
1059                "    g();\n"
1060                "    break;\n"
1061                "  }\n"
1062                "});");
1063   EXPECT_EQ("DEBUG({\n"
1064             "  switch (x) {\n"
1065             "  case A:\n"
1066             "    f();\n"
1067             "    break;\n"
1068             "  // On B:\n"
1069             "  case B:\n"
1070             "    g();\n"
1071             "    break;\n"
1072             "  }\n"
1073             "});",
1074             format("DEBUG({\n"
1075                    "  switch (x) {\n"
1076                    "  case A:\n"
1077                    "    f();\n"
1078                    "    break;\n"
1079                    "  // On B:\n"
1080                    "  case B:\n"
1081                    "    g();\n"
1082                    "    break;\n"
1083                    "  }\n"
1084                    "});",
1085                    getLLVMStyle()));
1086   EXPECT_EQ("switch (n) {\n"
1087             "case 0: {\n"
1088             "  return false;\n"
1089             "}\n"
1090             "default: {\n"
1091             "  return true;\n"
1092             "}\n"
1093             "}",
1094             format("switch (n)\n"
1095                    "{\n"
1096                    "case 0: {\n"
1097                    "  return false;\n"
1098                    "}\n"
1099                    "default: {\n"
1100                    "  return true;\n"
1101                    "}\n"
1102                    "}",
1103                    getLLVMStyle()));
1104   verifyFormat("switch (a) {\n"
1105                "case (b):\n"
1106                "  return;\n"
1107                "}");
1108 
1109   verifyFormat("switch (a) {\n"
1110                "case some_namespace::\n"
1111                "    some_constant:\n"
1112                "  return;\n"
1113                "}",
1114                getLLVMStyleWithColumns(34));
1115 
1116   FormatStyle Style = getLLVMStyle();
1117   Style.IndentCaseLabels = true;
1118   Style.AllowShortBlocksOnASingleLine = false;
1119   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1120   Style.BraceWrapping.AfterControlStatement = true;
1121   EXPECT_EQ("switch (n)\n"
1122             "{\n"
1123             "  case 0:\n"
1124             "  {\n"
1125             "    return false;\n"
1126             "  }\n"
1127             "  default:\n"
1128             "  {\n"
1129             "    return true;\n"
1130             "  }\n"
1131             "}",
1132             format("switch (n) {\n"
1133                    "  case 0: {\n"
1134                    "    return false;\n"
1135                    "  }\n"
1136                    "  default: {\n"
1137                    "    return true;\n"
1138                    "  }\n"
1139                    "}",
1140                    Style));
1141 }
1142 
1143 TEST_F(FormatTest, CaseRanges) {
1144   verifyFormat("switch (x) {\n"
1145                "case 'A' ... 'Z':\n"
1146                "case 1 ... 5:\n"
1147                "case a ... b:\n"
1148                "  break;\n"
1149                "}");
1150 }
1151 
1152 TEST_F(FormatTest, ShortCaseLabels) {
1153   FormatStyle Style = getLLVMStyle();
1154   Style.AllowShortCaseLabelsOnASingleLine = true;
1155   verifyFormat("switch (a) {\n"
1156                "case 1: x = 1; break;\n"
1157                "case 2: return;\n"
1158                "case 3:\n"
1159                "case 4:\n"
1160                "case 5: return;\n"
1161                "case 6: // comment\n"
1162                "  return;\n"
1163                "case 7:\n"
1164                "  // comment\n"
1165                "  return;\n"
1166                "case 8:\n"
1167                "  x = 8; // comment\n"
1168                "  break;\n"
1169                "default: y = 1; break;\n"
1170                "}",
1171                Style);
1172   verifyFormat("switch (a) {\n"
1173                "case 0: return; // comment\n"
1174                "case 1: break;  // comment\n"
1175                "case 2: return;\n"
1176                "// comment\n"
1177                "case 3: return;\n"
1178                "// comment 1\n"
1179                "// comment 2\n"
1180                "// comment 3\n"
1181                "case 4: break; /* comment */\n"
1182                "case 5:\n"
1183                "  // comment\n"
1184                "  break;\n"
1185                "case 6: /* comment */ x = 1; break;\n"
1186                "case 7: x = /* comment */ 1; break;\n"
1187                "case 8:\n"
1188                "  x = 1; /* comment */\n"
1189                "  break;\n"
1190                "case 9:\n"
1191                "  break; // comment line 1\n"
1192                "         // comment line 2\n"
1193                "}",
1194                Style);
1195   EXPECT_EQ("switch (a) {\n"
1196             "case 1:\n"
1197             "  x = 8;\n"
1198             "  // fall through\n"
1199             "case 2: x = 8;\n"
1200             "// comment\n"
1201             "case 3:\n"
1202             "  return; /* comment line 1\n"
1203             "           * comment line 2 */\n"
1204             "case 4: i = 8;\n"
1205             "// something else\n"
1206             "#if FOO\n"
1207             "case 5: break;\n"
1208             "#endif\n"
1209             "}",
1210             format("switch (a) {\n"
1211                    "case 1: x = 8;\n"
1212                    "  // fall through\n"
1213                    "case 2:\n"
1214                    "  x = 8;\n"
1215                    "// comment\n"
1216                    "case 3:\n"
1217                    "  return; /* comment line 1\n"
1218                    "           * comment line 2 */\n"
1219                    "case 4:\n"
1220                    "  i = 8;\n"
1221                    "// something else\n"
1222                    "#if FOO\n"
1223                    "case 5: break;\n"
1224                    "#endif\n"
1225                    "}",
1226                    Style));
1227   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1228             "  return; // long long long long long long long long long long long long comment\n"
1229             "          // line\n" "}",
1230             format("switch (a) {\n"
1231                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1232                    "}",
1233                    Style));
1234   EXPECT_EQ("switch (a) {\n"
1235             "case 0:\n"
1236             "  return; /* long long long long long long long long long long long long comment\n"
1237             "             line */\n"
1238             "}",
1239             format("switch (a) {\n"
1240                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1241                    "}",
1242                    Style));
1243   verifyFormat("switch (a) {\n"
1244                "#if FOO\n"
1245                "case 0: return 0;\n"
1246                "#endif\n"
1247                "}",
1248                Style);
1249   verifyFormat("switch (a) {\n"
1250                "case 1: {\n"
1251                "}\n"
1252                "case 2: {\n"
1253                "  return;\n"
1254                "}\n"
1255                "case 3: {\n"
1256                "  x = 1;\n"
1257                "  return;\n"
1258                "}\n"
1259                "case 4:\n"
1260                "  if (x)\n"
1261                "    return;\n"
1262                "}",
1263                Style);
1264   Style.ColumnLimit = 21;
1265   verifyFormat("switch (a) {\n"
1266                "case 1: x = 1; break;\n"
1267                "case 2: return;\n"
1268                "case 3:\n"
1269                "case 4:\n"
1270                "case 5: return;\n"
1271                "default:\n"
1272                "  y = 1;\n"
1273                "  break;\n"
1274                "}",
1275                Style);
1276   Style.ColumnLimit = 80;
1277   Style.AllowShortCaseLabelsOnASingleLine = false;
1278   Style.IndentCaseLabels = true;
1279   EXPECT_EQ("switch (n) {\n"
1280             "  default /*comments*/:\n"
1281             "    return true;\n"
1282             "  case 0:\n"
1283             "    return false;\n"
1284             "}",
1285             format("switch (n) {\n"
1286                    "default/*comments*/:\n"
1287                    "  return true;\n"
1288                    "case 0:\n"
1289                    "  return false;\n"
1290                    "}",
1291                    Style));
1292   Style.AllowShortCaseLabelsOnASingleLine = true;
1293   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1294   Style.BraceWrapping.AfterControlStatement = true;
1295   EXPECT_EQ("switch (n)\n"
1296             "{\n"
1297             "  case 0:\n"
1298             "  {\n"
1299             "    return false;\n"
1300             "  }\n"
1301             "  default:\n"
1302             "  {\n"
1303             "    return true;\n"
1304             "  }\n"
1305             "}",
1306             format("switch (n) {\n"
1307                    "  case 0: {\n"
1308                    "    return false;\n"
1309                    "  }\n"
1310                    "  default:\n"
1311                    "  {\n"
1312                    "    return true;\n"
1313                    "  }\n"
1314                    "}",
1315                    Style));
1316 }
1317 
1318 TEST_F(FormatTest, FormatsLabels) {
1319   verifyFormat("void f() {\n"
1320                "  some_code();\n"
1321                "test_label:\n"
1322                "  some_other_code();\n"
1323                "  {\n"
1324                "    some_more_code();\n"
1325                "  another_label:\n"
1326                "    some_more_code();\n"
1327                "  }\n"
1328                "}");
1329   verifyFormat("{\n"
1330                "  some_code();\n"
1331                "test_label:\n"
1332                "  some_other_code();\n"
1333                "}");
1334   verifyFormat("{\n"
1335                "  some_code();\n"
1336                "test_label:;\n"
1337                "  int i = 0;\n"
1338                "}");
1339 }
1340 
1341 //===----------------------------------------------------------------------===//
1342 // Tests for classes, namespaces, etc.
1343 //===----------------------------------------------------------------------===//
1344 
1345 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1346   verifyFormat("class A {};");
1347 }
1348 
1349 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1350   verifyFormat("class A {\n"
1351                "public:\n"
1352                "public: // comment\n"
1353                "protected:\n"
1354                "private:\n"
1355                "  void f() {}\n"
1356                "};");
1357   verifyFormat("export class A {\n"
1358                "public:\n"
1359                "public: // comment\n"
1360                "protected:\n"
1361                "private:\n"
1362                "  void f() {}\n"
1363                "};");
1364   verifyGoogleFormat("class A {\n"
1365                      " public:\n"
1366                      " protected:\n"
1367                      " private:\n"
1368                      "  void f() {}\n"
1369                      "};");
1370   verifyGoogleFormat("export class A {\n"
1371                      " public:\n"
1372                      " protected:\n"
1373                      " private:\n"
1374                      "  void f() {}\n"
1375                      "};");
1376   verifyFormat("class A {\n"
1377                "public slots:\n"
1378                "  void f1() {}\n"
1379                "public Q_SLOTS:\n"
1380                "  void f2() {}\n"
1381                "protected slots:\n"
1382                "  void f3() {}\n"
1383                "protected Q_SLOTS:\n"
1384                "  void f4() {}\n"
1385                "private slots:\n"
1386                "  void f5() {}\n"
1387                "private Q_SLOTS:\n"
1388                "  void f6() {}\n"
1389                "signals:\n"
1390                "  void g1();\n"
1391                "Q_SIGNALS:\n"
1392                "  void g2();\n"
1393                "};");
1394 
1395   // Don't interpret 'signals' the wrong way.
1396   verifyFormat("signals.set();");
1397   verifyFormat("for (Signals signals : f()) {\n}");
1398   verifyFormat("{\n"
1399                "  signals.set(); // This needs indentation.\n"
1400                "}");
1401   verifyFormat("void f() {\n"
1402                "label:\n"
1403                "  signals.baz();\n"
1404                "}");
1405 }
1406 
1407 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1408   EXPECT_EQ("class A {\n"
1409             "public:\n"
1410             "  void f();\n"
1411             "\n"
1412             "private:\n"
1413             "  void g() {}\n"
1414             "  // test\n"
1415             "protected:\n"
1416             "  int h;\n"
1417             "};",
1418             format("class A {\n"
1419                    "public:\n"
1420                    "void f();\n"
1421                    "private:\n"
1422                    "void g() {}\n"
1423                    "// test\n"
1424                    "protected:\n"
1425                    "int h;\n"
1426                    "};"));
1427   EXPECT_EQ("class A {\n"
1428             "protected:\n"
1429             "public:\n"
1430             "  void f();\n"
1431             "};",
1432             format("class A {\n"
1433                    "protected:\n"
1434                    "\n"
1435                    "public:\n"
1436                    "\n"
1437                    "  void f();\n"
1438                    "};"));
1439 
1440   // Even ensure proper spacing inside macros.
1441   EXPECT_EQ("#define B     \\\n"
1442             "  class A {   \\\n"
1443             "   protected: \\\n"
1444             "   public:    \\\n"
1445             "    void f(); \\\n"
1446             "  };",
1447             format("#define B     \\\n"
1448                    "  class A {   \\\n"
1449                    "   protected: \\\n"
1450                    "              \\\n"
1451                    "   public:    \\\n"
1452                    "              \\\n"
1453                    "    void f(); \\\n"
1454                    "  };",
1455                    getGoogleStyle()));
1456   // But don't remove empty lines after macros ending in access specifiers.
1457   EXPECT_EQ("#define A private:\n"
1458             "\n"
1459             "int i;",
1460             format("#define A         private:\n"
1461                    "\n"
1462                    "int              i;"));
1463 }
1464 
1465 TEST_F(FormatTest, FormatsClasses) {
1466   verifyFormat("class A : public B {};");
1467   verifyFormat("class A : public ::B {};");
1468 
1469   verifyFormat(
1470       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1471       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1472   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1473                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1474                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1475   verifyFormat(
1476       "class A : public B, public C, public D, public E, public F {};");
1477   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1478                "                     public C,\n"
1479                "                     public D,\n"
1480                "                     public E,\n"
1481                "                     public F,\n"
1482                "                     public G {};");
1483 
1484   verifyFormat("class\n"
1485                "    ReallyReallyLongClassName {\n"
1486                "  int i;\n"
1487                "};",
1488                getLLVMStyleWithColumns(32));
1489   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1490                "                           aaaaaaaaaaaaaaaa> {};");
1491   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1492                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1493                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1494   verifyFormat("template <class R, class C>\n"
1495                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1496                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1497   verifyFormat("class ::A::B {};");
1498 }
1499 
1500 TEST_F(FormatTest, BreakInheritanceStyle) {
1501   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1502   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1503           FormatStyle::BILS_BeforeComma;
1504   verifyFormat("class MyClass : public X {};",
1505                StyleWithInheritanceBreakBeforeComma);
1506   verifyFormat("class MyClass\n"
1507                "    : public X\n"
1508                "    , public Y {};",
1509                StyleWithInheritanceBreakBeforeComma);
1510   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1511                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1512                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1513                StyleWithInheritanceBreakBeforeComma);
1514   verifyFormat("struct aaaaaaaaaaaaa\n"
1515                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1516                "          aaaaaaaaaaaaaaaa> {};",
1517                StyleWithInheritanceBreakBeforeComma);
1518 
1519   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1520   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1521           FormatStyle::BILS_AfterColon;
1522   verifyFormat("class MyClass : public X {};",
1523                StyleWithInheritanceBreakAfterColon);
1524   verifyFormat("class MyClass : public X, public Y {};",
1525                StyleWithInheritanceBreakAfterColon);
1526   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1527                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1528                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1529                StyleWithInheritanceBreakAfterColon);
1530   verifyFormat("struct aaaaaaaaaaaaa :\n"
1531                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1532                "        aaaaaaaaaaaaaaaa> {};",
1533                StyleWithInheritanceBreakAfterColon);
1534 }
1535 
1536 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1537   verifyFormat("class A {\n} a, b;");
1538   verifyFormat("struct A {\n} a, b;");
1539   verifyFormat("union A {\n} a;");
1540 }
1541 
1542 TEST_F(FormatTest, FormatsEnum) {
1543   verifyFormat("enum {\n"
1544                "  Zero,\n"
1545                "  One = 1,\n"
1546                "  Two = One + 1,\n"
1547                "  Three = (One + Two),\n"
1548                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1549                "  Five = (One, Two, Three, Four, 5)\n"
1550                "};");
1551   verifyGoogleFormat("enum {\n"
1552                      "  Zero,\n"
1553                      "  One = 1,\n"
1554                      "  Two = One + 1,\n"
1555                      "  Three = (One + Two),\n"
1556                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1557                      "  Five = (One, Two, Three, Four, 5)\n"
1558                      "};");
1559   verifyFormat("enum Enum {};");
1560   verifyFormat("enum {};");
1561   verifyFormat("enum X E {} d;");
1562   verifyFormat("enum __attribute__((...)) E {} d;");
1563   verifyFormat("enum __declspec__((...)) E {} d;");
1564   verifyFormat("enum {\n"
1565                "  Bar = Foo<int, int>::value\n"
1566                "};",
1567                getLLVMStyleWithColumns(30));
1568 
1569   verifyFormat("enum ShortEnum { A, B, C };");
1570   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1571 
1572   EXPECT_EQ("enum KeepEmptyLines {\n"
1573             "  ONE,\n"
1574             "\n"
1575             "  TWO,\n"
1576             "\n"
1577             "  THREE\n"
1578             "}",
1579             format("enum KeepEmptyLines {\n"
1580                    "  ONE,\n"
1581                    "\n"
1582                    "  TWO,\n"
1583                    "\n"
1584                    "\n"
1585                    "  THREE\n"
1586                    "}"));
1587   verifyFormat("enum E { // comment\n"
1588                "  ONE,\n"
1589                "  TWO\n"
1590                "};\n"
1591                "int i;");
1592   // Not enums.
1593   verifyFormat("enum X f() {\n"
1594                "  a();\n"
1595                "  return 42;\n"
1596                "}");
1597   verifyFormat("enum X Type::f() {\n"
1598                "  a();\n"
1599                "  return 42;\n"
1600                "}");
1601   verifyFormat("enum ::X f() {\n"
1602                "  a();\n"
1603                "  return 42;\n"
1604                "}");
1605   verifyFormat("enum ns::X f() {\n"
1606                "  a();\n"
1607                "  return 42;\n"
1608                "}");
1609 }
1610 
1611 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1612   verifyFormat("enum Type {\n"
1613                "  One = 0; // These semicolons should be commas.\n"
1614                "  Two = 1;\n"
1615                "};");
1616   verifyFormat("namespace n {\n"
1617                "enum Type {\n"
1618                "  One,\n"
1619                "  Two, // missing };\n"
1620                "  int i;\n"
1621                "}\n"
1622                "void g() {}");
1623 }
1624 
1625 TEST_F(FormatTest, FormatsEnumStruct) {
1626   verifyFormat("enum struct {\n"
1627                "  Zero,\n"
1628                "  One = 1,\n"
1629                "  Two = One + 1,\n"
1630                "  Three = (One + Two),\n"
1631                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1632                "  Five = (One, Two, Three, Four, 5)\n"
1633                "};");
1634   verifyFormat("enum struct Enum {};");
1635   verifyFormat("enum struct {};");
1636   verifyFormat("enum struct X E {} d;");
1637   verifyFormat("enum struct __attribute__((...)) E {} d;");
1638   verifyFormat("enum struct __declspec__((...)) E {} d;");
1639   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1640 }
1641 
1642 TEST_F(FormatTest, FormatsEnumClass) {
1643   verifyFormat("enum class {\n"
1644                "  Zero,\n"
1645                "  One = 1,\n"
1646                "  Two = One + 1,\n"
1647                "  Three = (One + Two),\n"
1648                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1649                "  Five = (One, Two, Three, Four, 5)\n"
1650                "};");
1651   verifyFormat("enum class Enum {};");
1652   verifyFormat("enum class {};");
1653   verifyFormat("enum class X E {} d;");
1654   verifyFormat("enum class __attribute__((...)) E {} d;");
1655   verifyFormat("enum class __declspec__((...)) E {} d;");
1656   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1657 }
1658 
1659 TEST_F(FormatTest, FormatsEnumTypes) {
1660   verifyFormat("enum X : int {\n"
1661                "  A, // Force multiple lines.\n"
1662                "  B\n"
1663                "};");
1664   verifyFormat("enum X : int { A, B };");
1665   verifyFormat("enum X : std::uint32_t { A, B };");
1666 }
1667 
1668 TEST_F(FormatTest, FormatsTypedefEnum) {
1669   FormatStyle Style = getLLVMStyle();
1670   Style.ColumnLimit = 40;
1671   verifyFormat("typedef enum {} EmptyEnum;");
1672   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1673   verifyFormat("typedef enum {\n"
1674                "  ZERO = 0,\n"
1675                "  ONE = 1,\n"
1676                "  TWO = 2,\n"
1677                "  THREE = 3\n"
1678                "} LongEnum;",
1679                Style);
1680   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1681   Style.BraceWrapping.AfterEnum = true;
1682   verifyFormat("typedef enum {} EmptyEnum;");
1683   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1684   verifyFormat("typedef enum\n"
1685                "{\n"
1686                "  ZERO = 0,\n"
1687                "  ONE = 1,\n"
1688                "  TWO = 2,\n"
1689                "  THREE = 3\n"
1690                "} LongEnum;",
1691                Style);
1692 }
1693 
1694 TEST_F(FormatTest, FormatsNSEnums) {
1695   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1696   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1697                      "  // Information about someDecentlyLongValue.\n"
1698                      "  someDecentlyLongValue,\n"
1699                      "  // Information about anotherDecentlyLongValue.\n"
1700                      "  anotherDecentlyLongValue,\n"
1701                      "  // Information about aThirdDecentlyLongValue.\n"
1702                      "  aThirdDecentlyLongValue\n"
1703                      "};");
1704   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1705                      "  a = 1,\n"
1706                      "  b = 2,\n"
1707                      "  c = 3,\n"
1708                      "};");
1709   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1710                      "  a = 1,\n"
1711                      "  b = 2,\n"
1712                      "  c = 3,\n"
1713                      "};");
1714   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1715                      "  a = 1,\n"
1716                      "  b = 2,\n"
1717                      "  c = 3,\n"
1718                      "};");
1719 }
1720 
1721 TEST_F(FormatTest, FormatsBitfields) {
1722   verifyFormat("struct Bitfields {\n"
1723                "  unsigned sClass : 8;\n"
1724                "  unsigned ValueKind : 2;\n"
1725                "};");
1726   verifyFormat("struct A {\n"
1727                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1728                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1729                "};");
1730   verifyFormat("struct MyStruct {\n"
1731                "  uchar data;\n"
1732                "  uchar : 8;\n"
1733                "  uchar : 8;\n"
1734                "  uchar other;\n"
1735                "};");
1736 }
1737 
1738 TEST_F(FormatTest, FormatsNamespaces) {
1739   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1740   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1741 
1742   verifyFormat("namespace some_namespace {\n"
1743                "class A {};\n"
1744                "void f() { f(); }\n"
1745                "}",
1746                LLVMWithNoNamespaceFix);
1747   verifyFormat("/* something */ namespace some_namespace {\n"
1748                "class A {};\n"
1749                "void f() { f(); }\n"
1750                "}",
1751                LLVMWithNoNamespaceFix);
1752   verifyFormat("namespace {\n"
1753                "class A {};\n"
1754                "void f() { f(); }\n"
1755                "}",
1756                LLVMWithNoNamespaceFix);
1757   verifyFormat("/* something */ namespace {\n"
1758                "class A {};\n"
1759                "void f() { f(); }\n"
1760                "}",
1761                LLVMWithNoNamespaceFix);
1762   verifyFormat("inline namespace X {\n"
1763                "class A {};\n"
1764                "void f() { f(); }\n"
1765                "}",
1766                LLVMWithNoNamespaceFix);
1767   verifyFormat("/* something */ inline namespace X {\n"
1768                "class A {};\n"
1769                "void f() { f(); }\n"
1770                "}",
1771                LLVMWithNoNamespaceFix);
1772   verifyFormat("export namespace X {\n"
1773                "class A {};\n"
1774                "void f() { f(); }\n"
1775                "}",
1776                LLVMWithNoNamespaceFix);
1777   verifyFormat("using namespace some_namespace;\n"
1778                "class A {};\n"
1779                "void f() { f(); }",
1780                LLVMWithNoNamespaceFix);
1781 
1782   // This code is more common than we thought; if we
1783   // layout this correctly the semicolon will go into
1784   // its own line, which is undesirable.
1785   verifyFormat("namespace {};",
1786                LLVMWithNoNamespaceFix);
1787   verifyFormat("namespace {\n"
1788                "class A {};\n"
1789                "};",
1790                LLVMWithNoNamespaceFix);
1791 
1792   verifyFormat("namespace {\n"
1793                "int SomeVariable = 0; // comment\n"
1794                "} // namespace",
1795                LLVMWithNoNamespaceFix);
1796   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1797             "#define HEADER_GUARD\n"
1798             "namespace my_namespace {\n"
1799             "int i;\n"
1800             "} // my_namespace\n"
1801             "#endif // HEADER_GUARD",
1802             format("#ifndef HEADER_GUARD\n"
1803                    " #define HEADER_GUARD\n"
1804                    "   namespace my_namespace {\n"
1805                    "int i;\n"
1806                    "}    // my_namespace\n"
1807                    "#endif    // HEADER_GUARD",
1808                    LLVMWithNoNamespaceFix));
1809 
1810   EXPECT_EQ("namespace A::B {\n"
1811             "class C {};\n"
1812             "}",
1813             format("namespace A::B {\n"
1814                    "class C {};\n"
1815                    "}",
1816                    LLVMWithNoNamespaceFix));
1817 
1818   FormatStyle Style = getLLVMStyle();
1819   Style.NamespaceIndentation = FormatStyle::NI_All;
1820   EXPECT_EQ("namespace out {\n"
1821             "  int i;\n"
1822             "  namespace in {\n"
1823             "    int i;\n"
1824             "  } // namespace in\n"
1825             "} // namespace out",
1826             format("namespace out {\n"
1827                    "int i;\n"
1828                    "namespace in {\n"
1829                    "int i;\n"
1830                    "} // namespace in\n"
1831                    "} // namespace out",
1832                    Style));
1833 
1834   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1835   EXPECT_EQ("namespace out {\n"
1836             "int i;\n"
1837             "namespace in {\n"
1838             "  int i;\n"
1839             "} // namespace in\n"
1840             "} // namespace out",
1841             format("namespace out {\n"
1842                    "int i;\n"
1843                    "namespace in {\n"
1844                    "int i;\n"
1845                    "} // namespace in\n"
1846                    "} // namespace out",
1847                    Style));
1848 }
1849 
1850 TEST_F(FormatTest, FormatsCompactNamespaces) {
1851   FormatStyle Style = getLLVMStyle();
1852   Style.CompactNamespaces = true;
1853 
1854   verifyFormat("namespace A { namespace B {\n"
1855 			   "}} // namespace A::B",
1856 			   Style);
1857 
1858   EXPECT_EQ("namespace out { namespace in {\n"
1859             "}} // namespace out::in",
1860             format("namespace out {\n"
1861                    "namespace in {\n"
1862                    "} // namespace in\n"
1863                    "} // namespace out",
1864                    Style));
1865 
1866   // Only namespaces which have both consecutive opening and end get compacted
1867   EXPECT_EQ("namespace out {\n"
1868             "namespace in1 {\n"
1869             "} // namespace in1\n"
1870             "namespace in2 {\n"
1871             "} // namespace in2\n"
1872             "} // namespace out",
1873             format("namespace out {\n"
1874                    "namespace in1 {\n"
1875                    "} // namespace in1\n"
1876                    "namespace in2 {\n"
1877                    "} // namespace in2\n"
1878                    "} // namespace out",
1879                    Style));
1880 
1881   EXPECT_EQ("namespace out {\n"
1882             "int i;\n"
1883             "namespace in {\n"
1884             "int j;\n"
1885             "} // namespace in\n"
1886             "int k;\n"
1887             "} // namespace out",
1888             format("namespace out { int i;\n"
1889                    "namespace in { int j; } // namespace in\n"
1890                    "int k; } // namespace out",
1891                    Style));
1892 
1893   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1894             "}}} // namespace A::B::C\n",
1895             format("namespace A { namespace B {\n"
1896                    "namespace C {\n"
1897                    "}} // namespace B::C\n"
1898                    "} // namespace A\n",
1899                    Style));
1900 
1901   Style.ColumnLimit = 40;
1902   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1903             "namespace bbbbbbbbbb {\n"
1904             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1905             format("namespace aaaaaaaaaa {\n"
1906                    "namespace bbbbbbbbbb {\n"
1907                    "} // namespace bbbbbbbbbb\n"
1908                    "} // namespace aaaaaaaaaa",
1909                    Style));
1910 
1911   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1912             "namespace cccccc {\n"
1913             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1914             format("namespace aaaaaa {\n"
1915                    "namespace bbbbbb {\n"
1916                    "namespace cccccc {\n"
1917                    "} // namespace cccccc\n"
1918                    "} // namespace bbbbbb\n"
1919                    "} // namespace aaaaaa",
1920                    Style));
1921   Style.ColumnLimit = 80;
1922 
1923   // Extra semicolon after 'inner' closing brace prevents merging
1924   EXPECT_EQ("namespace out { namespace in {\n"
1925             "}; } // namespace out::in",
1926             format("namespace out {\n"
1927                    "namespace in {\n"
1928                    "}; // namespace in\n"
1929                    "} // namespace out",
1930                    Style));
1931 
1932   // Extra semicolon after 'outer' closing brace is conserved
1933   EXPECT_EQ("namespace out { namespace in {\n"
1934             "}}; // namespace out::in",
1935             format("namespace out {\n"
1936                    "namespace in {\n"
1937                    "} // namespace in\n"
1938                    "}; // namespace out",
1939                    Style));
1940 
1941   Style.NamespaceIndentation = FormatStyle::NI_All;
1942   EXPECT_EQ("namespace out { namespace in {\n"
1943             "  int i;\n"
1944             "}} // namespace out::in",
1945             format("namespace out {\n"
1946                    "namespace in {\n"
1947                    "int i;\n"
1948                    "} // namespace in\n"
1949                    "} // namespace out",
1950                    Style));
1951   EXPECT_EQ("namespace out { namespace mid {\n"
1952             "  namespace in {\n"
1953             "    int j;\n"
1954             "  } // namespace in\n"
1955             "  int k;\n"
1956             "}} // namespace out::mid",
1957             format("namespace out { namespace mid {\n"
1958                    "namespace in { int j; } // namespace in\n"
1959                    "int k; }} // namespace out::mid",
1960                    Style));
1961 
1962   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1963   EXPECT_EQ("namespace out { namespace in {\n"
1964             "  int i;\n"
1965             "}} // namespace out::in",
1966             format("namespace out {\n"
1967                    "namespace in {\n"
1968                    "int i;\n"
1969                    "} // namespace in\n"
1970                    "} // namespace out",
1971                    Style));
1972   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1973             "  int i;\n"
1974             "}}} // namespace out::mid::in",
1975             format("namespace out {\n"
1976                    "namespace mid {\n"
1977                    "namespace in {\n"
1978                    "int i;\n"
1979                    "} // namespace in\n"
1980                    "} // namespace mid\n"
1981                    "} // namespace out",
1982                    Style));
1983 }
1984 
1985 TEST_F(FormatTest, FormatsExternC) {
1986   verifyFormat("extern \"C\" {\nint a;");
1987   verifyFormat("extern \"C\" {}");
1988   verifyFormat("extern \"C\" {\n"
1989                "int foo();\n"
1990                "}");
1991   verifyFormat("extern \"C\" int foo() {}");
1992   verifyFormat("extern \"C\" int foo();");
1993   verifyFormat("extern \"C\" int foo() {\n"
1994                "  int i = 42;\n"
1995                "  return i;\n"
1996                "}");
1997 
1998   FormatStyle Style = getLLVMStyle();
1999   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2000   Style.BraceWrapping.AfterFunction = true;
2001   verifyFormat("extern \"C\" int foo() {}", Style);
2002   verifyFormat("extern \"C\" int foo();", Style);
2003   verifyFormat("extern \"C\" int foo()\n"
2004                "{\n"
2005                "  int i = 42;\n"
2006                "  return i;\n"
2007                "}",
2008                Style);
2009 
2010   Style.BraceWrapping.AfterExternBlock = true;
2011   Style.BraceWrapping.SplitEmptyRecord = false;
2012   verifyFormat("extern \"C\"\n"
2013                "{}",
2014                Style);
2015   verifyFormat("extern \"C\"\n"
2016                "{\n"
2017                "  int foo();\n"
2018                "}",
2019                Style);
2020 }
2021 
2022 TEST_F(FormatTest, FormatsInlineASM) {
2023   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2024   verifyFormat("asm(\"nop\" ::: \"memory\");");
2025   verifyFormat(
2026       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2027       "    \"cpuid\\n\\t\"\n"
2028       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2029       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2030       "    : \"a\"(value));");
2031   EXPECT_EQ(
2032       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2033       "  __asm {\n"
2034       "        mov     edx,[that] // vtable in edx\n"
2035       "        mov     eax,methodIndex\n"
2036       "        call    [edx][eax*4] // stdcall\n"
2037       "  }\n"
2038       "}",
2039       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2040              "    __asm {\n"
2041              "        mov     edx,[that] // vtable in edx\n"
2042              "        mov     eax,methodIndex\n"
2043              "        call    [edx][eax*4] // stdcall\n"
2044              "    }\n"
2045              "}"));
2046   EXPECT_EQ("_asm {\n"
2047             "  xor eax, eax;\n"
2048             "  cpuid;\n"
2049             "}",
2050             format("_asm {\n"
2051                    "  xor eax, eax;\n"
2052                    "  cpuid;\n"
2053                    "}"));
2054   verifyFormat("void function() {\n"
2055                "  // comment\n"
2056                "  asm(\"\");\n"
2057                "}");
2058   EXPECT_EQ("__asm {\n"
2059             "}\n"
2060             "int i;",
2061             format("__asm   {\n"
2062                    "}\n"
2063                    "int   i;"));
2064 }
2065 
2066 TEST_F(FormatTest, FormatTryCatch) {
2067   verifyFormat("try {\n"
2068                "  throw a * b;\n"
2069                "} catch (int a) {\n"
2070                "  // Do nothing.\n"
2071                "} catch (...) {\n"
2072                "  exit(42);\n"
2073                "}");
2074 
2075   // Function-level try statements.
2076   verifyFormat("int f() try { return 4; } catch (...) {\n"
2077                "  return 5;\n"
2078                "}");
2079   verifyFormat("class A {\n"
2080                "  int a;\n"
2081                "  A() try : a(0) {\n"
2082                "  } catch (...) {\n"
2083                "    throw;\n"
2084                "  }\n"
2085                "};\n");
2086 
2087   // Incomplete try-catch blocks.
2088   verifyIncompleteFormat("try {} catch (");
2089 }
2090 
2091 TEST_F(FormatTest, FormatSEHTryCatch) {
2092   verifyFormat("__try {\n"
2093                "  int a = b * c;\n"
2094                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2095                "  // Do nothing.\n"
2096                "}");
2097 
2098   verifyFormat("__try {\n"
2099                "  int a = b * c;\n"
2100                "} __finally {\n"
2101                "  // Do nothing.\n"
2102                "}");
2103 
2104   verifyFormat("DEBUG({\n"
2105                "  __try {\n"
2106                "  } __finally {\n"
2107                "  }\n"
2108                "});\n");
2109 }
2110 
2111 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2112   verifyFormat("try {\n"
2113                "  f();\n"
2114                "} catch {\n"
2115                "  g();\n"
2116                "}");
2117   verifyFormat("try {\n"
2118                "  f();\n"
2119                "} catch (A a) MACRO(x) {\n"
2120                "  g();\n"
2121                "} catch (B b) MACRO(x) {\n"
2122                "  g();\n"
2123                "}");
2124 }
2125 
2126 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2127   FormatStyle Style = getLLVMStyle();
2128   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2129                           FormatStyle::BS_WebKit}) {
2130     Style.BreakBeforeBraces = BraceStyle;
2131     verifyFormat("try {\n"
2132                  "  // something\n"
2133                  "} catch (...) {\n"
2134                  "  // something\n"
2135                  "}",
2136                  Style);
2137   }
2138   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2139   verifyFormat("try {\n"
2140                "  // something\n"
2141                "}\n"
2142                "catch (...) {\n"
2143                "  // something\n"
2144                "}",
2145                Style);
2146   verifyFormat("__try {\n"
2147                "  // something\n"
2148                "}\n"
2149                "__finally {\n"
2150                "  // something\n"
2151                "}",
2152                Style);
2153   verifyFormat("@try {\n"
2154                "  // something\n"
2155                "}\n"
2156                "@finally {\n"
2157                "  // something\n"
2158                "}",
2159                Style);
2160   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2161   verifyFormat("try\n"
2162                "{\n"
2163                "  // something\n"
2164                "}\n"
2165                "catch (...)\n"
2166                "{\n"
2167                "  // something\n"
2168                "}",
2169                Style);
2170   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2171   verifyFormat("try\n"
2172                "  {\n"
2173                "    // something\n"
2174                "  }\n"
2175                "catch (...)\n"
2176                "  {\n"
2177                "    // something\n"
2178                "  }",
2179                Style);
2180   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2181   Style.BraceWrapping.BeforeCatch = true;
2182   verifyFormat("try {\n"
2183                "  // something\n"
2184                "}\n"
2185                "catch (...) {\n"
2186                "  // something\n"
2187                "}",
2188                Style);
2189 }
2190 
2191 TEST_F(FormatTest, StaticInitializers) {
2192   verifyFormat("static SomeClass SC = {1, 'a'};");
2193 
2194   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2195                "    100000000, "
2196                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2197 
2198   // Here, everything other than the "}" would fit on a line.
2199   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2200                "    10000000000000000000000000};");
2201   EXPECT_EQ("S s = {a,\n"
2202             "\n"
2203             "       b};",
2204             format("S s = {\n"
2205                    "  a,\n"
2206                    "\n"
2207                    "  b\n"
2208                    "};"));
2209 
2210   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2211   // line. However, the formatting looks a bit off and this probably doesn't
2212   // happen often in practice.
2213   verifyFormat("static int Variable[1] = {\n"
2214                "    {1000000000000000000000000000000000000}};",
2215                getLLVMStyleWithColumns(40));
2216 }
2217 
2218 TEST_F(FormatTest, DesignatedInitializers) {
2219   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2220   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2221                "                    .bbbbbbbbbb = 2,\n"
2222                "                    .cccccccccc = 3,\n"
2223                "                    .dddddddddd = 4,\n"
2224                "                    .eeeeeeeeee = 5};");
2225   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2226                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2227                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2228                "    .ccccccccccccccccccccccccccc = 3,\n"
2229                "    .ddddddddddddddddddddddddddd = 4,\n"
2230                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2231 
2232   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2233 
2234   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2235   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2236                "                    [2] = bbbbbbbbbb,\n"
2237                "                    [3] = cccccccccc,\n"
2238                "                    [4] = dddddddddd,\n"
2239                "                    [5] = eeeeeeeeee};");
2240   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2241                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2242                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2243                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2244                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2245                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2246 }
2247 
2248 TEST_F(FormatTest, NestedStaticInitializers) {
2249   verifyFormat("static A x = {{{}}};\n");
2250   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2251                "               {init1, init2, init3, init4}}};",
2252                getLLVMStyleWithColumns(50));
2253 
2254   verifyFormat("somes Status::global_reps[3] = {\n"
2255                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2256                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2257                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2258                getLLVMStyleWithColumns(60));
2259   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2260                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2261                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2262                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2263   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2264                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2265                "rect.fTop}};");
2266 
2267   verifyFormat(
2268       "SomeArrayOfSomeType a = {\n"
2269       "    {{1, 2, 3},\n"
2270       "     {1, 2, 3},\n"
2271       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2272       "      333333333333333333333333333333},\n"
2273       "     {1, 2, 3},\n"
2274       "     {1, 2, 3}}};");
2275   verifyFormat(
2276       "SomeArrayOfSomeType a = {\n"
2277       "    {{1, 2, 3}},\n"
2278       "    {{1, 2, 3}},\n"
2279       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2280       "      333333333333333333333333333333}},\n"
2281       "    {{1, 2, 3}},\n"
2282       "    {{1, 2, 3}}};");
2283 
2284   verifyFormat("struct {\n"
2285                "  unsigned bit;\n"
2286                "  const char *const name;\n"
2287                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2288                "                 {kOsWin, \"Windows\"},\n"
2289                "                 {kOsLinux, \"Linux\"},\n"
2290                "                 {kOsCrOS, \"Chrome OS\"}};");
2291   verifyFormat("struct {\n"
2292                "  unsigned bit;\n"
2293                "  const char *const name;\n"
2294                "} kBitsToOs[] = {\n"
2295                "    {kOsMac, \"Mac\"},\n"
2296                "    {kOsWin, \"Windows\"},\n"
2297                "    {kOsLinux, \"Linux\"},\n"
2298                "    {kOsCrOS, \"Chrome OS\"},\n"
2299                "};");
2300 }
2301 
2302 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2303   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2304                "                      \\\n"
2305                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2306 }
2307 
2308 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2309   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2310                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2311 
2312   // Do break defaulted and deleted functions.
2313   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2314                "    default;",
2315                getLLVMStyleWithColumns(40));
2316   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2317                "    delete;",
2318                getLLVMStyleWithColumns(40));
2319 }
2320 
2321 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2322   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2323                getLLVMStyleWithColumns(40));
2324   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2325                getLLVMStyleWithColumns(40));
2326   EXPECT_EQ("#define Q                              \\\n"
2327             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2328             "  \"aaaaaaaa.cpp\"",
2329             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2330                    getLLVMStyleWithColumns(40)));
2331 }
2332 
2333 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2334   EXPECT_EQ("# 123 \"A string literal\"",
2335             format("   #     123    \"A string literal\""));
2336 }
2337 
2338 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2339   EXPECT_EQ("#;", format("#;"));
2340   verifyFormat("#\n;\n;\n;");
2341 }
2342 
2343 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2344   EXPECT_EQ("#line 42 \"test\"\n",
2345             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2346   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2347                                     getLLVMStyleWithColumns(12)));
2348 }
2349 
2350 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2351   EXPECT_EQ("#line 42 \"test\"",
2352             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2353   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2354 }
2355 
2356 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2357   verifyFormat("#define A \\x20");
2358   verifyFormat("#define A \\ x20");
2359   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2360   verifyFormat("#define A ''");
2361   verifyFormat("#define A ''qqq");
2362   verifyFormat("#define A `qqq");
2363   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2364   EXPECT_EQ("const char *c = STRINGIFY(\n"
2365             "\\na : b);",
2366             format("const char * c = STRINGIFY(\n"
2367                    "\\na : b);"));
2368 
2369   verifyFormat("a\r\\");
2370   verifyFormat("a\v\\");
2371   verifyFormat("a\f\\");
2372 }
2373 
2374 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2375   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2376   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2377   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2378   // FIXME: We never break before the macro name.
2379   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2380 
2381   verifyFormat("#define A A\n#define A A");
2382   verifyFormat("#define A(X) A\n#define A A");
2383 
2384   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2385   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2386 }
2387 
2388 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2389   EXPECT_EQ("// somecomment\n"
2390             "#include \"a.h\"\n"
2391             "#define A(  \\\n"
2392             "    A, B)\n"
2393             "#include \"b.h\"\n"
2394             "// somecomment\n",
2395             format("  // somecomment\n"
2396                    "  #include \"a.h\"\n"
2397                    "#define A(A,\\\n"
2398                    "    B)\n"
2399                    "    #include \"b.h\"\n"
2400                    " // somecomment\n",
2401                    getLLVMStyleWithColumns(13)));
2402 }
2403 
2404 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2405 
2406 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2407   EXPECT_EQ("#define A    \\\n"
2408             "  c;         \\\n"
2409             "  e;\n"
2410             "f;",
2411             format("#define A c; e;\n"
2412                    "f;",
2413                    getLLVMStyleWithColumns(14)));
2414 }
2415 
2416 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2417 
2418 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2419   EXPECT_EQ("int x,\n"
2420             "#define A\n"
2421             "    y;",
2422             format("int x,\n#define A\ny;"));
2423 }
2424 
2425 TEST_F(FormatTest, HashInMacroDefinition) {
2426   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2427   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2428   verifyFormat("#define A  \\\n"
2429                "  {        \\\n"
2430                "    f(#c); \\\n"
2431                "  }",
2432                getLLVMStyleWithColumns(11));
2433 
2434   verifyFormat("#define A(X)         \\\n"
2435                "  void function##X()",
2436                getLLVMStyleWithColumns(22));
2437 
2438   verifyFormat("#define A(a, b, c)   \\\n"
2439                "  void a##b##c()",
2440                getLLVMStyleWithColumns(22));
2441 
2442   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2443 }
2444 
2445 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2446   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2447   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2448 }
2449 
2450 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2451   EXPECT_EQ("#define A b;", format("#define A \\\n"
2452                                    "          \\\n"
2453                                    "  b;",
2454                                    getLLVMStyleWithColumns(25)));
2455   EXPECT_EQ("#define A \\\n"
2456             "          \\\n"
2457             "  a;      \\\n"
2458             "  b;",
2459             format("#define A \\\n"
2460                    "          \\\n"
2461                    "  a;      \\\n"
2462                    "  b;",
2463                    getLLVMStyleWithColumns(11)));
2464   EXPECT_EQ("#define A \\\n"
2465             "  a;      \\\n"
2466             "          \\\n"
2467             "  b;",
2468             format("#define A \\\n"
2469                    "  a;      \\\n"
2470                    "          \\\n"
2471                    "  b;",
2472                    getLLVMStyleWithColumns(11)));
2473 }
2474 
2475 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2476   verifyIncompleteFormat("#define A :");
2477   verifyFormat("#define SOMECASES  \\\n"
2478                "  case 1:          \\\n"
2479                "  case 2\n",
2480                getLLVMStyleWithColumns(20));
2481   verifyFormat("#define MACRO(a) \\\n"
2482                "  if (a)         \\\n"
2483                "    f();         \\\n"
2484                "  else           \\\n"
2485                "    g()",
2486                getLLVMStyleWithColumns(18));
2487   verifyFormat("#define A template <typename T>");
2488   verifyIncompleteFormat("#define STR(x) #x\n"
2489                          "f(STR(this_is_a_string_literal{));");
2490   verifyFormat("#pragma omp threadprivate( \\\n"
2491                "    y)), // expected-warning",
2492                getLLVMStyleWithColumns(28));
2493   verifyFormat("#d, = };");
2494   verifyFormat("#if \"a");
2495   verifyIncompleteFormat("({\n"
2496                          "#define b     \\\n"
2497                          "  }           \\\n"
2498                          "  a\n"
2499                          "a",
2500                          getLLVMStyleWithColumns(15));
2501   verifyFormat("#define A     \\\n"
2502                "  {           \\\n"
2503                "    {\n"
2504                "#define B     \\\n"
2505                "  }           \\\n"
2506                "  }",
2507                getLLVMStyleWithColumns(15));
2508   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2509   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2510   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2511   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2512 }
2513 
2514 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2515   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2516   EXPECT_EQ("class A : public QObject {\n"
2517             "  Q_OBJECT\n"
2518             "\n"
2519             "  A() {}\n"
2520             "};",
2521             format("class A  :  public QObject {\n"
2522                    "     Q_OBJECT\n"
2523                    "\n"
2524                    "  A() {\n}\n"
2525                    "}  ;"));
2526   EXPECT_EQ("MACRO\n"
2527             "/*static*/ int i;",
2528             format("MACRO\n"
2529                    " /*static*/ int   i;"));
2530   EXPECT_EQ("SOME_MACRO\n"
2531             "namespace {\n"
2532             "void f();\n"
2533             "} // namespace",
2534             format("SOME_MACRO\n"
2535                    "  namespace    {\n"
2536                    "void   f(  );\n"
2537                    "} // namespace"));
2538   // Only if the identifier contains at least 5 characters.
2539   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2540   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2541   // Only if everything is upper case.
2542   EXPECT_EQ("class A : public QObject {\n"
2543             "  Q_Object A() {}\n"
2544             "};",
2545             format("class A  :  public QObject {\n"
2546                    "     Q_Object\n"
2547                    "  A() {\n}\n"
2548                    "}  ;"));
2549 
2550   // Only if the next line can actually start an unwrapped line.
2551   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2552             format("SOME_WEIRD_LOG_MACRO\n"
2553                    "<< SomeThing;"));
2554 
2555   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2556                "(n, buffers))\n",
2557                getChromiumStyle(FormatStyle::LK_Cpp));
2558 }
2559 
2560 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2561   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2562             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2563             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2564             "class X {};\n"
2565             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2566             "int *createScopDetectionPass() { return 0; }",
2567             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2568                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2569                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2570                    "  class X {};\n"
2571                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2572                    "  int *createScopDetectionPass() { return 0; }"));
2573   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2574   // braces, so that inner block is indented one level more.
2575   EXPECT_EQ("int q() {\n"
2576             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2577             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2578             "  IPC_END_MESSAGE_MAP()\n"
2579             "}",
2580             format("int q() {\n"
2581                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2582                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2583                    "  IPC_END_MESSAGE_MAP()\n"
2584                    "}"));
2585 
2586   // Same inside macros.
2587   EXPECT_EQ("#define LIST(L) \\\n"
2588             "  L(A)          \\\n"
2589             "  L(B)          \\\n"
2590             "  L(C)",
2591             format("#define LIST(L) \\\n"
2592                    "  L(A) \\\n"
2593                    "  L(B) \\\n"
2594                    "  L(C)",
2595                    getGoogleStyle()));
2596 
2597   // These must not be recognized as macros.
2598   EXPECT_EQ("int q() {\n"
2599             "  f(x);\n"
2600             "  f(x) {}\n"
2601             "  f(x)->g();\n"
2602             "  f(x)->*g();\n"
2603             "  f(x).g();\n"
2604             "  f(x) = x;\n"
2605             "  f(x) += x;\n"
2606             "  f(x) -= x;\n"
2607             "  f(x) *= x;\n"
2608             "  f(x) /= x;\n"
2609             "  f(x) %= x;\n"
2610             "  f(x) &= x;\n"
2611             "  f(x) |= x;\n"
2612             "  f(x) ^= x;\n"
2613             "  f(x) >>= x;\n"
2614             "  f(x) <<= x;\n"
2615             "  f(x)[y].z();\n"
2616             "  LOG(INFO) << x;\n"
2617             "  ifstream(x) >> x;\n"
2618             "}\n",
2619             format("int q() {\n"
2620                    "  f(x)\n;\n"
2621                    "  f(x)\n {}\n"
2622                    "  f(x)\n->g();\n"
2623                    "  f(x)\n->*g();\n"
2624                    "  f(x)\n.g();\n"
2625                    "  f(x)\n = x;\n"
2626                    "  f(x)\n += x;\n"
2627                    "  f(x)\n -= x;\n"
2628                    "  f(x)\n *= x;\n"
2629                    "  f(x)\n /= x;\n"
2630                    "  f(x)\n %= x;\n"
2631                    "  f(x)\n &= x;\n"
2632                    "  f(x)\n |= x;\n"
2633                    "  f(x)\n ^= x;\n"
2634                    "  f(x)\n >>= x;\n"
2635                    "  f(x)\n <<= x;\n"
2636                    "  f(x)\n[y].z();\n"
2637                    "  LOG(INFO)\n << x;\n"
2638                    "  ifstream(x)\n >> x;\n"
2639                    "}\n"));
2640   EXPECT_EQ("int q() {\n"
2641             "  F(x)\n"
2642             "  if (1) {\n"
2643             "  }\n"
2644             "  F(x)\n"
2645             "  while (1) {\n"
2646             "  }\n"
2647             "  F(x)\n"
2648             "  G(x);\n"
2649             "  F(x)\n"
2650             "  try {\n"
2651             "    Q();\n"
2652             "  } catch (...) {\n"
2653             "  }\n"
2654             "}\n",
2655             format("int q() {\n"
2656                    "F(x)\n"
2657                    "if (1) {}\n"
2658                    "F(x)\n"
2659                    "while (1) {}\n"
2660                    "F(x)\n"
2661                    "G(x);\n"
2662                    "F(x)\n"
2663                    "try { Q(); } catch (...) {}\n"
2664                    "}\n"));
2665   EXPECT_EQ("class A {\n"
2666             "  A() : t(0) {}\n"
2667             "  A(int i) noexcept() : {}\n"
2668             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2669             "  try : t(0) {\n"
2670             "  } catch (...) {\n"
2671             "  }\n"
2672             "};",
2673             format("class A {\n"
2674                    "  A()\n : t(0) {}\n"
2675                    "  A(int i)\n noexcept() : {}\n"
2676                    "  A(X x)\n"
2677                    "  try : t(0) {} catch (...) {}\n"
2678                    "};"));
2679   FormatStyle Style = getLLVMStyle();
2680   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2681   Style.BraceWrapping.AfterControlStatement = true;
2682   Style.BraceWrapping.AfterFunction = true;
2683   EXPECT_EQ("void f()\n"
2684             "try\n"
2685             "{\n"
2686             "}",
2687             format("void f() try {\n"
2688                    "}", Style));
2689   EXPECT_EQ("class SomeClass {\n"
2690             "public:\n"
2691             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2692             "};",
2693             format("class SomeClass {\n"
2694                    "public:\n"
2695                    "  SomeClass()\n"
2696                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2697                    "};"));
2698   EXPECT_EQ("class SomeClass {\n"
2699             "public:\n"
2700             "  SomeClass()\n"
2701             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2702             "};",
2703             format("class SomeClass {\n"
2704                    "public:\n"
2705                    "  SomeClass()\n"
2706                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2707                    "};",
2708                    getLLVMStyleWithColumns(40)));
2709 
2710   verifyFormat("MACRO(>)");
2711 
2712   // Some macros contain an implicit semicolon.
2713   Style = getLLVMStyle();
2714   Style.StatementMacros.push_back("FOO");
2715   verifyFormat("FOO(a) int b = 0;");
2716   verifyFormat("FOO(a)\n"
2717                "int b = 0;",
2718                Style);
2719   verifyFormat("FOO(a);\n"
2720                "int b = 0;",
2721                Style);
2722   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
2723                "int b = 0;",
2724                Style);
2725   verifyFormat("FOO()\n"
2726                "int b = 0;",
2727                Style);
2728   verifyFormat("FOO\n"
2729                "int b = 0;",
2730                Style);
2731   verifyFormat("void f() {\n"
2732                "  FOO(a)\n"
2733                "  return a;\n"
2734                "}",
2735                Style);
2736   verifyFormat("FOO(a)\n"
2737                "FOO(b)",
2738                Style);
2739   verifyFormat("int a = 0;\n"
2740                "FOO(b)\n"
2741                "int c = 0;",
2742                Style);
2743   verifyFormat("int a = 0;\n"
2744                "int x = FOO(a)\n"
2745                "int b = 0;",
2746                Style);
2747   verifyFormat("void foo(int a) { FOO(a) }\n"
2748                "uint32_t bar() {}",
2749                Style);
2750 }
2751 
2752 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2753   verifyFormat("#define A \\\n"
2754                "  f({     \\\n"
2755                "    g();  \\\n"
2756                "  });",
2757                getLLVMStyleWithColumns(11));
2758 }
2759 
2760 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2761   FormatStyle Style = getLLVMStyle();
2762   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2763   Style.ColumnLimit = 40;
2764   verifyFormat("#ifdef _WIN32\n"
2765                "#define A 0\n"
2766                "#ifdef VAR2\n"
2767                "#define B 1\n"
2768                "#include <someheader.h>\n"
2769                "#define MACRO                          \\\n"
2770                "  some_very_long_func_aaaaaaaaaa();\n"
2771                "#endif\n"
2772                "#else\n"
2773                "#define A 1\n"
2774                "#endif",
2775                Style);
2776   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2777   verifyFormat("#ifdef _WIN32\n"
2778                "#  define A 0\n"
2779                "#  ifdef VAR2\n"
2780                "#    define B 1\n"
2781                "#    include <someheader.h>\n"
2782                "#    define MACRO                      \\\n"
2783                "      some_very_long_func_aaaaaaaaaa();\n"
2784                "#  endif\n"
2785                "#else\n"
2786                "#  define A 1\n"
2787                "#endif",
2788                Style);
2789   verifyFormat("#if A\n"
2790                "#  define MACRO                        \\\n"
2791                "    void a(int x) {                    \\\n"
2792                "      b();                             \\\n"
2793                "      c();                             \\\n"
2794                "      d();                             \\\n"
2795                "      e();                             \\\n"
2796                "      f();                             \\\n"
2797                "    }\n"
2798                "#endif",
2799                Style);
2800   // Comments before include guard.
2801   verifyFormat("// file comment\n"
2802                "// file comment\n"
2803                "#ifndef HEADER_H\n"
2804                "#define HEADER_H\n"
2805                "code();\n"
2806                "#endif",
2807                Style);
2808   // Test with include guards.
2809   verifyFormat("#ifndef HEADER_H\n"
2810                "#define HEADER_H\n"
2811                "code();\n"
2812                "#endif",
2813                Style);
2814   // Include guards must have a #define with the same variable immediately
2815   // after #ifndef.
2816   verifyFormat("#ifndef NOT_GUARD\n"
2817                "#  define FOO\n"
2818                "code();\n"
2819                "#endif",
2820                Style);
2821 
2822   // Include guards must cover the entire file.
2823   verifyFormat("code();\n"
2824                "code();\n"
2825                "#ifndef NOT_GUARD\n"
2826                "#  define NOT_GUARD\n"
2827                "code();\n"
2828                "#endif",
2829                Style);
2830   verifyFormat("#ifndef NOT_GUARD\n"
2831                "#  define NOT_GUARD\n"
2832                "code();\n"
2833                "#endif\n"
2834                "code();",
2835                Style);
2836   // Test with trailing blank lines.
2837   verifyFormat("#ifndef HEADER_H\n"
2838                "#define HEADER_H\n"
2839                "code();\n"
2840                "#endif\n",
2841                Style);
2842   // Include guards don't have #else.
2843   verifyFormat("#ifndef NOT_GUARD\n"
2844                "#  define NOT_GUARD\n"
2845                "code();\n"
2846                "#else\n"
2847                "#endif",
2848                Style);
2849   verifyFormat("#ifndef NOT_GUARD\n"
2850                "#  define NOT_GUARD\n"
2851                "code();\n"
2852                "#elif FOO\n"
2853                "#endif",
2854                Style);
2855   // Non-identifier #define after potential include guard.
2856   verifyFormat("#ifndef FOO\n"
2857                "#  define 1\n"
2858                "#endif\n",
2859                Style);
2860   // #if closes past last non-preprocessor line.
2861   verifyFormat("#ifndef FOO\n"
2862                "#define FOO\n"
2863                "#if 1\n"
2864                "int i;\n"
2865                "#  define A 0\n"
2866                "#endif\n"
2867                "#endif\n",
2868                Style);
2869   // FIXME: This doesn't handle the case where there's code between the
2870   // #ifndef and #define but all other conditions hold. This is because when
2871   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2872   // previous code line yet, so we can't detect it.
2873   EXPECT_EQ("#ifndef NOT_GUARD\n"
2874             "code();\n"
2875             "#define NOT_GUARD\n"
2876             "code();\n"
2877             "#endif",
2878             format("#ifndef NOT_GUARD\n"
2879                    "code();\n"
2880                    "#  define NOT_GUARD\n"
2881                    "code();\n"
2882                    "#endif",
2883                    Style));
2884   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2885   // be outside an include guard. Examples are #pragma once and
2886   // #pragma GCC diagnostic, or anything else that does not change the meaning
2887   // of the file if it's included multiple times.
2888   EXPECT_EQ("#ifdef WIN32\n"
2889             "#  pragma once\n"
2890             "#endif\n"
2891             "#ifndef HEADER_H\n"
2892             "#  define HEADER_H\n"
2893             "code();\n"
2894             "#endif",
2895             format("#ifdef WIN32\n"
2896                    "#  pragma once\n"
2897                    "#endif\n"
2898                    "#ifndef HEADER_H\n"
2899                    "#define HEADER_H\n"
2900                    "code();\n"
2901                    "#endif",
2902                    Style));
2903   // FIXME: This does not detect when there is a single non-preprocessor line
2904   // in front of an include-guard-like structure where other conditions hold
2905   // because ScopedLineState hides the line.
2906   EXPECT_EQ("code();\n"
2907             "#ifndef HEADER_H\n"
2908             "#define HEADER_H\n"
2909             "code();\n"
2910             "#endif",
2911             format("code();\n"
2912                    "#ifndef HEADER_H\n"
2913                    "#  define HEADER_H\n"
2914                    "code();\n"
2915                    "#endif",
2916                    Style));
2917   // Keep comments aligned with #, otherwise indent comments normally. These
2918   // tests cannot use verifyFormat because messUp manipulates leading
2919   // whitespace.
2920   {
2921     const char *Expected = ""
2922                            "void f() {\n"
2923                            "#if 1\n"
2924                            "// Preprocessor aligned.\n"
2925                            "#  define A 0\n"
2926                            "  // Code. Separated by blank line.\n"
2927                            "\n"
2928                            "#  define B 0\n"
2929                            "  // Code. Not aligned with #\n"
2930                            "#  define C 0\n"
2931                            "#endif";
2932     const char *ToFormat = ""
2933                            "void f() {\n"
2934                            "#if 1\n"
2935                            "// Preprocessor aligned.\n"
2936                            "#  define A 0\n"
2937                            "// Code. Separated by blank line.\n"
2938                            "\n"
2939                            "#  define B 0\n"
2940                            "   // Code. Not aligned with #\n"
2941                            "#  define C 0\n"
2942                            "#endif";
2943     EXPECT_EQ(Expected, format(ToFormat, Style));
2944     EXPECT_EQ(Expected, format(Expected, Style));
2945   }
2946   // Keep block quotes aligned.
2947   {
2948     const char *Expected = ""
2949                            "void f() {\n"
2950                            "#if 1\n"
2951                            "/* Preprocessor aligned. */\n"
2952                            "#  define A 0\n"
2953                            "  /* Code. Separated by blank line. */\n"
2954                            "\n"
2955                            "#  define B 0\n"
2956                            "  /* Code. Not aligned with # */\n"
2957                            "#  define C 0\n"
2958                            "#endif";
2959     const char *ToFormat = ""
2960                            "void f() {\n"
2961                            "#if 1\n"
2962                            "/* Preprocessor aligned. */\n"
2963                            "#  define A 0\n"
2964                            "/* Code. Separated by blank line. */\n"
2965                            "\n"
2966                            "#  define B 0\n"
2967                            "   /* Code. Not aligned with # */\n"
2968                            "#  define C 0\n"
2969                            "#endif";
2970     EXPECT_EQ(Expected, format(ToFormat, Style));
2971     EXPECT_EQ(Expected, format(Expected, Style));
2972   }
2973   // Keep comments aligned with un-indented directives.
2974   {
2975     const char *Expected = ""
2976                            "void f() {\n"
2977                            "// Preprocessor aligned.\n"
2978                            "#define A 0\n"
2979                            "  // Code. Separated by blank line.\n"
2980                            "\n"
2981                            "#define B 0\n"
2982                            "  // Code. Not aligned with #\n"
2983                            "#define C 0\n";
2984     const char *ToFormat = ""
2985                            "void f() {\n"
2986                            "// Preprocessor aligned.\n"
2987                            "#define A 0\n"
2988                            "// Code. Separated by blank line.\n"
2989                            "\n"
2990                            "#define B 0\n"
2991                            "   // Code. Not aligned with #\n"
2992                            "#define C 0\n";
2993     EXPECT_EQ(Expected, format(ToFormat, Style));
2994     EXPECT_EQ(Expected, format(Expected, Style));
2995   }
2996   // Test with tabs.
2997   Style.UseTab = FormatStyle::UT_Always;
2998   Style.IndentWidth = 8;
2999   Style.TabWidth = 8;
3000   verifyFormat("#ifdef _WIN32\n"
3001                "#\tdefine A 0\n"
3002                "#\tifdef VAR2\n"
3003                "#\t\tdefine B 1\n"
3004                "#\t\tinclude <someheader.h>\n"
3005                "#\t\tdefine MACRO          \\\n"
3006                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
3007                "#\tendif\n"
3008                "#else\n"
3009                "#\tdefine A 1\n"
3010                "#endif",
3011                Style);
3012 
3013   // Regression test: Multiline-macro inside include guards.
3014   verifyFormat("#ifndef HEADER_H\n"
3015                "#define HEADER_H\n"
3016                "#define A()        \\\n"
3017                "  int i;           \\\n"
3018                "  int j;\n"
3019                "#endif // HEADER_H",
3020                getLLVMStyleWithColumns(20));
3021 }
3022 
3023 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3024   verifyFormat("{\n  { a #c; }\n}");
3025 }
3026 
3027 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3028   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3029             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3030   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3031             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3032 }
3033 
3034 TEST_F(FormatTest, EscapedNewlines) {
3035   FormatStyle Narrow = getLLVMStyleWithColumns(11);
3036   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
3037             format("#define A \\\nint i;\\\n  int j;", Narrow));
3038   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3039   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3040   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
3041   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3042 
3043   FormatStyle AlignLeft = getLLVMStyle();
3044   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3045   EXPECT_EQ("#define MACRO(x) \\\n"
3046             "private:         \\\n"
3047             "  int x(int a);\n",
3048             format("#define MACRO(x) \\\n"
3049                    "private:         \\\n"
3050                    "  int x(int a);\n",
3051                    AlignLeft));
3052 
3053   // CRLF line endings
3054   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
3055             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
3056   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3057   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3058   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
3059   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3060   EXPECT_EQ("#define MACRO(x) \\\r\n"
3061             "private:         \\\r\n"
3062             "  int x(int a);\r\n",
3063             format("#define MACRO(x) \\\r\n"
3064                    "private:         \\\r\n"
3065                    "  int x(int a);\r\n",
3066                    AlignLeft));
3067 
3068   FormatStyle DontAlign = getLLVMStyle();
3069   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3070   DontAlign.MaxEmptyLinesToKeep = 3;
3071   // FIXME: can't use verifyFormat here because the newline before
3072   // "public:" is not inserted the first time it's reformatted
3073   EXPECT_EQ("#define A \\\n"
3074             "  class Foo { \\\n"
3075             "    void bar(); \\\n"
3076             "\\\n"
3077             "\\\n"
3078             "\\\n"
3079             "  public: \\\n"
3080             "    void baz(); \\\n"
3081             "  };",
3082             format("#define A \\\n"
3083                    "  class Foo { \\\n"
3084                    "    void bar(); \\\n"
3085                    "\\\n"
3086                    "\\\n"
3087                    "\\\n"
3088                    "  public: \\\n"
3089                    "    void baz(); \\\n"
3090                    "  };",
3091                    DontAlign));
3092 }
3093 
3094 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3095   verifyFormat("#define A \\\n"
3096                "  int v(  \\\n"
3097                "      a); \\\n"
3098                "  int i;",
3099                getLLVMStyleWithColumns(11));
3100 }
3101 
3102 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3103   EXPECT_EQ(
3104       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3105       "                      \\\n"
3106       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3107       "\n"
3108       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3109       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3110       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3111              "\\\n"
3112              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3113              "  \n"
3114              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3115              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3116 }
3117 
3118 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3119   EXPECT_EQ("int\n"
3120             "#define A\n"
3121             "    a;",
3122             format("int\n#define A\na;"));
3123   verifyFormat("functionCallTo(\n"
3124                "    someOtherFunction(\n"
3125                "        withSomeParameters, whichInSequence,\n"
3126                "        areLongerThanALine(andAnotherCall,\n"
3127                "#define A B\n"
3128                "                           withMoreParamters,\n"
3129                "                           whichStronglyInfluenceTheLayout),\n"
3130                "        andMoreParameters),\n"
3131                "    trailing);",
3132                getLLVMStyleWithColumns(69));
3133   verifyFormat("Foo::Foo()\n"
3134                "#ifdef BAR\n"
3135                "    : baz(0)\n"
3136                "#endif\n"
3137                "{\n"
3138                "}");
3139   verifyFormat("void f() {\n"
3140                "  if (true)\n"
3141                "#ifdef A\n"
3142                "    f(42);\n"
3143                "  x();\n"
3144                "#else\n"
3145                "    g();\n"
3146                "  x();\n"
3147                "#endif\n"
3148                "}");
3149   verifyFormat("void f(param1, param2,\n"
3150                "       param3,\n"
3151                "#ifdef A\n"
3152                "       param4(param5,\n"
3153                "#ifdef A1\n"
3154                "              param6,\n"
3155                "#ifdef A2\n"
3156                "              param7),\n"
3157                "#else\n"
3158                "              param8),\n"
3159                "       param9,\n"
3160                "#endif\n"
3161                "       param10,\n"
3162                "#endif\n"
3163                "       param11)\n"
3164                "#else\n"
3165                "       param12)\n"
3166                "#endif\n"
3167                "{\n"
3168                "  x();\n"
3169                "}",
3170                getLLVMStyleWithColumns(28));
3171   verifyFormat("#if 1\n"
3172                "int i;");
3173   verifyFormat("#if 1\n"
3174                "#endif\n"
3175                "#if 1\n"
3176                "#else\n"
3177                "#endif\n");
3178   verifyFormat("DEBUG({\n"
3179                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3180                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3181                "});\n"
3182                "#if a\n"
3183                "#else\n"
3184                "#endif");
3185 
3186   verifyIncompleteFormat("void f(\n"
3187                          "#if A\n"
3188                          ");\n"
3189                          "#else\n"
3190                          "#endif");
3191 }
3192 
3193 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3194   verifyFormat("#endif\n"
3195                "#if B");
3196 }
3197 
3198 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3199   FormatStyle SingleLine = getLLVMStyle();
3200   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3201   verifyFormat("#if 0\n"
3202                "#elif 1\n"
3203                "#endif\n"
3204                "void foo() {\n"
3205                "  if (test) foo2();\n"
3206                "}",
3207                SingleLine);
3208 }
3209 
3210 TEST_F(FormatTest, LayoutBlockInsideParens) {
3211   verifyFormat("functionCall({ int i; });");
3212   verifyFormat("functionCall({\n"
3213                "  int i;\n"
3214                "  int j;\n"
3215                "});");
3216   verifyFormat("functionCall(\n"
3217                "    {\n"
3218                "      int i;\n"
3219                "      int j;\n"
3220                "    },\n"
3221                "    aaaa, bbbb, cccc);");
3222   verifyFormat("functionA(functionB({\n"
3223                "            int i;\n"
3224                "            int j;\n"
3225                "          }),\n"
3226                "          aaaa, bbbb, cccc);");
3227   verifyFormat("functionCall(\n"
3228                "    {\n"
3229                "      int i;\n"
3230                "      int j;\n"
3231                "    },\n"
3232                "    aaaa, bbbb, // comment\n"
3233                "    cccc);");
3234   verifyFormat("functionA(functionB({\n"
3235                "            int i;\n"
3236                "            int j;\n"
3237                "          }),\n"
3238                "          aaaa, bbbb, // comment\n"
3239                "          cccc);");
3240   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3241   verifyFormat("functionCall(aaaa, bbbb, {\n"
3242                "  int i;\n"
3243                "  int j;\n"
3244                "});");
3245   verifyFormat(
3246       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3247       "    {\n"
3248       "      int i; // break\n"
3249       "    },\n"
3250       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3251       "                                     ccccccccccccccccc));");
3252   verifyFormat("DEBUG({\n"
3253                "  if (a)\n"
3254                "    f();\n"
3255                "});");
3256 }
3257 
3258 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3259   EXPECT_EQ("SOME_MACRO { int i; }\n"
3260             "int i;",
3261             format("  SOME_MACRO  {int i;}  int i;"));
3262 }
3263 
3264 TEST_F(FormatTest, LayoutNestedBlocks) {
3265   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3266                "  struct s {\n"
3267                "    int i;\n"
3268                "  };\n"
3269                "  s kBitsToOs[] = {{10}};\n"
3270                "  for (int i = 0; i < 10; ++i)\n"
3271                "    return;\n"
3272                "}");
3273   verifyFormat("call(parameter, {\n"
3274                "  something();\n"
3275                "  // Comment using all columns.\n"
3276                "  somethingelse();\n"
3277                "});",
3278                getLLVMStyleWithColumns(40));
3279   verifyFormat("DEBUG( //\n"
3280                "    { f(); }, a);");
3281   verifyFormat("DEBUG( //\n"
3282                "    {\n"
3283                "      f(); //\n"
3284                "    },\n"
3285                "    a);");
3286 
3287   EXPECT_EQ("call(parameter, {\n"
3288             "  something();\n"
3289             "  // Comment too\n"
3290             "  // looooooooooong.\n"
3291             "  somethingElse();\n"
3292             "});",
3293             format("call(parameter, {\n"
3294                    "  something();\n"
3295                    "  // Comment too looooooooooong.\n"
3296                    "  somethingElse();\n"
3297                    "});",
3298                    getLLVMStyleWithColumns(29)));
3299   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3300   EXPECT_EQ("DEBUG({ // comment\n"
3301             "  int i;\n"
3302             "});",
3303             format("DEBUG({ // comment\n"
3304                    "int  i;\n"
3305                    "});"));
3306   EXPECT_EQ("DEBUG({\n"
3307             "  int i;\n"
3308             "\n"
3309             "  // comment\n"
3310             "  int j;\n"
3311             "});",
3312             format("DEBUG({\n"
3313                    "  int  i;\n"
3314                    "\n"
3315                    "  // comment\n"
3316                    "  int  j;\n"
3317                    "});"));
3318 
3319   verifyFormat("DEBUG({\n"
3320                "  if (a)\n"
3321                "    return;\n"
3322                "});");
3323   verifyGoogleFormat("DEBUG({\n"
3324                      "  if (a) return;\n"
3325                      "});");
3326   FormatStyle Style = getGoogleStyle();
3327   Style.ColumnLimit = 45;
3328   verifyFormat("Debug(\n"
3329                "    aaaaa,\n"
3330                "    {\n"
3331                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3332                "    },\n"
3333                "    a);",
3334                Style);
3335 
3336   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3337 
3338   verifyNoCrash("^{v^{a}}");
3339 }
3340 
3341 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3342   EXPECT_EQ("#define MACRO()                     \\\n"
3343             "  Debug(aaa, /* force line break */ \\\n"
3344             "        {                           \\\n"
3345             "          int i;                    \\\n"
3346             "          int j;                    \\\n"
3347             "        })",
3348             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3349                    "          {  int   i;  int  j;   })",
3350                    getGoogleStyle()));
3351 
3352   EXPECT_EQ("#define A                                       \\\n"
3353             "  [] {                                          \\\n"
3354             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3355             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3356             "  }",
3357             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3358                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3359                    getGoogleStyle()));
3360 }
3361 
3362 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3363   EXPECT_EQ("{}", format("{}"));
3364   verifyFormat("enum E {};");
3365   verifyFormat("enum E {}");
3366 }
3367 
3368 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3369   FormatStyle Style = getLLVMStyle();
3370   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3371   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3372   verifyFormat("FOO_BEGIN\n"
3373                "  FOO_ENTRY\n"
3374                "FOO_END", Style);
3375   verifyFormat("FOO_BEGIN\n"
3376                "  NESTED_FOO_BEGIN\n"
3377                "    NESTED_FOO_ENTRY\n"
3378                "  NESTED_FOO_END\n"
3379                "FOO_END", Style);
3380   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3381                "  int x;\n"
3382                "  x = 1;\n"
3383                "FOO_END(Baz)", Style);
3384 }
3385 
3386 //===----------------------------------------------------------------------===//
3387 // Line break tests.
3388 //===----------------------------------------------------------------------===//
3389 
3390 TEST_F(FormatTest, PreventConfusingIndents) {
3391   verifyFormat(
3392       "void f() {\n"
3393       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3394       "                         parameter, parameter, parameter)),\n"
3395       "                     SecondLongCall(parameter));\n"
3396       "}");
3397   verifyFormat(
3398       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3399       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3400       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3401       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3402   verifyFormat(
3403       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3404       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3405       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3406       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3407   verifyFormat(
3408       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3409       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3410       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3411       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3412   verifyFormat("int a = bbbb && ccc &&\n"
3413                "        fffff(\n"
3414                "#define A Just forcing a new line\n"
3415                "            ddd);");
3416 }
3417 
3418 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3419   verifyFormat(
3420       "bool aaaaaaa =\n"
3421       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3422       "    bbbbbbbb();");
3423   verifyFormat(
3424       "bool aaaaaaa =\n"
3425       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3426       "    bbbbbbbb();");
3427 
3428   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3429                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3430                "    ccccccccc == ddddddddddd;");
3431   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3432                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3433                "    ccccccccc == ddddddddddd;");
3434   verifyFormat(
3435       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3436       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3437       "    ccccccccc == ddddddddddd;");
3438 
3439   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3440                "                 aaaaaa) &&\n"
3441                "         bbbbbb && cccccc;");
3442   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3443                "                 aaaaaa) >>\n"
3444                "         bbbbbb;");
3445   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3446                "    SourceMgr.getSpellingColumnNumber(\n"
3447                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3448                "    1);");
3449 
3450   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3451                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3452                "    cccccc) {\n}");
3453   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3454                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3455                "              cccccc) {\n}");
3456   verifyFormat("b = a &&\n"
3457                "    // Comment\n"
3458                "    b.c && d;");
3459 
3460   // If the LHS of a comparison is not a binary expression itself, the
3461   // additional linebreak confuses many people.
3462   verifyFormat(
3463       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3464       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3465       "}");
3466   verifyFormat(
3467       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3468       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3469       "}");
3470   verifyFormat(
3471       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3472       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3473       "}");
3474   verifyFormat(
3475       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3476       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3477       "}");
3478   // Even explicit parentheses stress the precedence enough to make the
3479   // additional break unnecessary.
3480   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3481                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3482                "}");
3483   // This cases is borderline, but with the indentation it is still readable.
3484   verifyFormat(
3485       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3486       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3487       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3488       "}",
3489       getLLVMStyleWithColumns(75));
3490 
3491   // If the LHS is a binary expression, we should still use the additional break
3492   // as otherwise the formatting hides the operator precedence.
3493   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3494                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3495                "    5) {\n"
3496                "}");
3497   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3498                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3499                "    5) {\n"
3500                "}");
3501 
3502   FormatStyle OnePerLine = getLLVMStyle();
3503   OnePerLine.BinPackParameters = false;
3504   verifyFormat(
3505       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3506       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3507       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3508       OnePerLine);
3509 
3510   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3511                "                .aaa(aaaaaaaaaaaaa) *\n"
3512                "            aaaaaaa +\n"
3513                "        aaaaaaa;",
3514                getLLVMStyleWithColumns(40));
3515 }
3516 
3517 TEST_F(FormatTest, ExpressionIndentation) {
3518   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3519                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3520                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3521                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3522                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3523                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3524                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3525                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3526                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3527   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3528                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3529                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3530                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3531   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3532                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3533                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3534                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3535   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3536                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3537                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3538                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3539   verifyFormat("if () {\n"
3540                "} else if (aaaaa && bbbbb > // break\n"
3541                "                        ccccc) {\n"
3542                "}");
3543   verifyFormat("if () {\n"
3544                "} else if (aaaaa &&\n"
3545                "           bbbbb > // break\n"
3546                "               ccccc &&\n"
3547                "           ddddd) {\n"
3548                "}");
3549 
3550   // Presence of a trailing comment used to change indentation of b.
3551   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3552                "       b;\n"
3553                "return aaaaaaaaaaaaaaaaaaa +\n"
3554                "       b; //",
3555                getLLVMStyleWithColumns(30));
3556 }
3557 
3558 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3559   // Not sure what the best system is here. Like this, the LHS can be found
3560   // immediately above an operator (everything with the same or a higher
3561   // indent). The RHS is aligned right of the operator and so compasses
3562   // everything until something with the same indent as the operator is found.
3563   // FIXME: Is this a good system?
3564   FormatStyle Style = getLLVMStyle();
3565   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3566   verifyFormat(
3567       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3568       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3569       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3570       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3571       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3572       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3573       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3574       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3575       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3576       Style);
3577   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3578                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3579                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3580                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3581                Style);
3582   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3583                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3584                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3585                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3586                Style);
3587   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3588                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3589                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3590                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3591                Style);
3592   verifyFormat("if () {\n"
3593                "} else if (aaaaa\n"
3594                "           && bbbbb // break\n"
3595                "                  > ccccc) {\n"
3596                "}",
3597                Style);
3598   verifyFormat("return (a)\n"
3599                "       // comment\n"
3600                "       + b;",
3601                Style);
3602   verifyFormat(
3603       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3604       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3605       "             + cc;",
3606       Style);
3607 
3608   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3609                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3610                Style);
3611 
3612   // Forced by comments.
3613   verifyFormat(
3614       "unsigned ContentSize =\n"
3615       "    sizeof(int16_t)   // DWARF ARange version number\n"
3616       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3617       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3618       "    + sizeof(int8_t); // Segment Size (in bytes)");
3619 
3620   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3621                "       == boost::fusion::at_c<1>(iiii).second;",
3622                Style);
3623 
3624   Style.ColumnLimit = 60;
3625   verifyFormat("zzzzzzzzzz\n"
3626                "    = bbbbbbbbbbbbbbbbb\n"
3627                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3628                Style);
3629 
3630   Style.ColumnLimit = 80;
3631   Style.IndentWidth = 4;
3632   Style.TabWidth = 4;
3633   Style.UseTab = FormatStyle::UT_Always;
3634   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3635   Style.AlignOperands = false;
3636   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3637             "\t&& (someOtherLongishConditionPart1\n"
3638             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3639             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3640                    Style));
3641 }
3642 
3643 TEST_F(FormatTest, EnforcedOperatorWraps) {
3644   // Here we'd like to wrap after the || operators, but a comment is forcing an
3645   // earlier wrap.
3646   verifyFormat("bool x = aaaaa //\n"
3647                "         || bbbbb\n"
3648                "         //\n"
3649                "         || cccc;");
3650 }
3651 
3652 TEST_F(FormatTest, NoOperandAlignment) {
3653   FormatStyle Style = getLLVMStyle();
3654   Style.AlignOperands = false;
3655   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3656                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3657                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3658                Style);
3659   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3660   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3661                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3662                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3663                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3664                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3665                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3666                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3667                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3668                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3669                Style);
3670 
3671   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3672                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3673                "    + cc;",
3674                Style);
3675   verifyFormat("int a = aa\n"
3676                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3677                "        * cccccccccccccccccccccccccccccccccccc;\n",
3678                Style);
3679 
3680   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3681   verifyFormat("return (a > b\n"
3682                "    // comment1\n"
3683                "    // comment2\n"
3684                "    || c);",
3685                Style);
3686 }
3687 
3688 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3689   FormatStyle Style = getLLVMStyle();
3690   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3691   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3692                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3693                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3694                Style);
3695 }
3696 
3697 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3698   FormatStyle Style = getLLVMStyle();
3699   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3700   Style.BinPackArguments = false;
3701   Style.ColumnLimit = 40;
3702   verifyFormat("void test() {\n"
3703                "  someFunction(\n"
3704                "      this + argument + is + quite\n"
3705                "      + long + so + it + gets + wrapped\n"
3706                "      + but + remains + bin - packed);\n"
3707                "}",
3708                Style);
3709   verifyFormat("void test() {\n"
3710                "  someFunction(arg1,\n"
3711                "               this + argument + is\n"
3712                "                   + quite + long + so\n"
3713                "                   + it + gets + wrapped\n"
3714                "                   + but + remains + bin\n"
3715                "                   - packed,\n"
3716                "               arg3);\n"
3717                "}",
3718                Style);
3719   verifyFormat("void test() {\n"
3720                "  someFunction(\n"
3721                "      arg1,\n"
3722                "      this + argument + has\n"
3723                "          + anotherFunc(nested,\n"
3724                "                        calls + whose\n"
3725                "                            + arguments\n"
3726                "                            + are + also\n"
3727                "                            + wrapped,\n"
3728                "                        in + addition)\n"
3729                "          + to + being + bin - packed,\n"
3730                "      arg3);\n"
3731                "}",
3732                Style);
3733 
3734   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3735   verifyFormat("void test() {\n"
3736                "  someFunction(\n"
3737                "      arg1,\n"
3738                "      this + argument + has +\n"
3739                "          anotherFunc(nested,\n"
3740                "                      calls + whose +\n"
3741                "                          arguments +\n"
3742                "                          are + also +\n"
3743                "                          wrapped,\n"
3744                "                      in + addition) +\n"
3745                "          to + being + bin - packed,\n"
3746                "      arg3);\n"
3747                "}",
3748                Style);
3749 }
3750 
3751 TEST_F(FormatTest, ConstructorInitializers) {
3752   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3753   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3754                getLLVMStyleWithColumns(45));
3755   verifyFormat("Constructor()\n"
3756                "    : Inttializer(FitsOnTheLine) {}",
3757                getLLVMStyleWithColumns(44));
3758   verifyFormat("Constructor()\n"
3759                "    : Inttializer(FitsOnTheLine) {}",
3760                getLLVMStyleWithColumns(43));
3761 
3762   verifyFormat("template <typename T>\n"
3763                "Constructor() : Initializer(FitsOnTheLine) {}",
3764                getLLVMStyleWithColumns(45));
3765 
3766   verifyFormat(
3767       "SomeClass::Constructor()\n"
3768       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3769 
3770   verifyFormat(
3771       "SomeClass::Constructor()\n"
3772       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3773       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3774   verifyFormat(
3775       "SomeClass::Constructor()\n"
3776       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3777       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3778   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3779                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3780                "    : aaaaaaaaaa(aaaaaa) {}");
3781 
3782   verifyFormat("Constructor()\n"
3783                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3784                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3785                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3786                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3787 
3788   verifyFormat("Constructor()\n"
3789                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3790                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3791 
3792   verifyFormat("Constructor(int Parameter = 0)\n"
3793                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3794                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3795   verifyFormat("Constructor()\n"
3796                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3797                "}",
3798                getLLVMStyleWithColumns(60));
3799   verifyFormat("Constructor()\n"
3800                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3801                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3802 
3803   // Here a line could be saved by splitting the second initializer onto two
3804   // lines, but that is not desirable.
3805   verifyFormat("Constructor()\n"
3806                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3807                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3808                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3809 
3810   FormatStyle OnePerLine = getLLVMStyle();
3811   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3812   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3813   verifyFormat("SomeClass::Constructor()\n"
3814                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3815                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3816                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3817                OnePerLine);
3818   verifyFormat("SomeClass::Constructor()\n"
3819                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3820                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3821                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3822                OnePerLine);
3823   verifyFormat("MyClass::MyClass(int var)\n"
3824                "    : some_var_(var),            // 4 space indent\n"
3825                "      some_other_var_(var + 1) { // lined up\n"
3826                "}",
3827                OnePerLine);
3828   verifyFormat("Constructor()\n"
3829                "    : aaaaa(aaaaaa),\n"
3830                "      aaaaa(aaaaaa),\n"
3831                "      aaaaa(aaaaaa),\n"
3832                "      aaaaa(aaaaaa),\n"
3833                "      aaaaa(aaaaaa) {}",
3834                OnePerLine);
3835   verifyFormat("Constructor()\n"
3836                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3837                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3838                OnePerLine);
3839   OnePerLine.BinPackParameters = false;
3840   verifyFormat(
3841       "Constructor()\n"
3842       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3843       "          aaaaaaaaaaa().aaa(),\n"
3844       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3845       OnePerLine);
3846   OnePerLine.ColumnLimit = 60;
3847   verifyFormat("Constructor()\n"
3848                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3849                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3850                OnePerLine);
3851 
3852   EXPECT_EQ("Constructor()\n"
3853             "    : // Comment forcing unwanted break.\n"
3854             "      aaaa(aaaa) {}",
3855             format("Constructor() :\n"
3856                    "    // Comment forcing unwanted break.\n"
3857                    "    aaaa(aaaa) {}"));
3858 }
3859 
3860 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3861   FormatStyle Style = getLLVMStyle();
3862   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3863 
3864   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3865   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3866                getStyleWithColumns(Style, 45));
3867   verifyFormat("Constructor() :\n"
3868                "    Initializer(FitsOnTheLine) {}",
3869                getStyleWithColumns(Style, 44));
3870   verifyFormat("Constructor() :\n"
3871                "    Initializer(FitsOnTheLine) {}",
3872                getStyleWithColumns(Style, 43));
3873 
3874   verifyFormat("template <typename T>\n"
3875                "Constructor() : Initializer(FitsOnTheLine) {}",
3876                getStyleWithColumns(Style, 50));
3877 
3878   verifyFormat(
3879       "SomeClass::Constructor() :\n"
3880       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3881 	  Style);
3882 
3883   verifyFormat(
3884       "SomeClass::Constructor() :\n"
3885       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3886       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3887 	  Style);
3888   verifyFormat(
3889       "SomeClass::Constructor() :\n"
3890       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3891       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3892 	  Style);
3893   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3894                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3895                "    aaaaaaaaaa(aaaaaa) {}",
3896 			   Style);
3897 
3898   verifyFormat("Constructor() :\n"
3899                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3900                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3901                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3902                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3903 			   Style);
3904 
3905   verifyFormat("Constructor() :\n"
3906                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3907                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3908 			   Style);
3909 
3910   verifyFormat("Constructor(int Parameter = 0) :\n"
3911                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3912                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3913 			   Style);
3914   verifyFormat("Constructor() :\n"
3915                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3916                "}",
3917                getStyleWithColumns(Style, 60));
3918   verifyFormat("Constructor() :\n"
3919                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3920                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3921 			   Style);
3922 
3923   // Here a line could be saved by splitting the second initializer onto two
3924   // lines, but that is not desirable.
3925   verifyFormat("Constructor() :\n"
3926                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3927                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3928                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3929 			   Style);
3930 
3931   FormatStyle OnePerLine = Style;
3932   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3933   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3934   verifyFormat("SomeClass::Constructor() :\n"
3935                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3936                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3937                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3938                OnePerLine);
3939   verifyFormat("SomeClass::Constructor() :\n"
3940                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3941                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3942                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3943                OnePerLine);
3944   verifyFormat("MyClass::MyClass(int var) :\n"
3945                "    some_var_(var),            // 4 space indent\n"
3946                "    some_other_var_(var + 1) { // lined up\n"
3947                "}",
3948                OnePerLine);
3949   verifyFormat("Constructor() :\n"
3950                "    aaaaa(aaaaaa),\n"
3951                "    aaaaa(aaaaaa),\n"
3952                "    aaaaa(aaaaaa),\n"
3953                "    aaaaa(aaaaaa),\n"
3954                "    aaaaa(aaaaaa) {}",
3955                OnePerLine);
3956   verifyFormat("Constructor() :\n"
3957                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3958                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3959                OnePerLine);
3960   OnePerLine.BinPackParameters = false;
3961   verifyFormat(
3962       "Constructor() :\n"
3963       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3964       "        aaaaaaaaaaa().aaa(),\n"
3965       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3966       OnePerLine);
3967   OnePerLine.ColumnLimit = 60;
3968   verifyFormat("Constructor() :\n"
3969                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3970                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3971                OnePerLine);
3972 
3973   EXPECT_EQ("Constructor() :\n"
3974             "    // Comment forcing unwanted break.\n"
3975             "    aaaa(aaaa) {}",
3976             format("Constructor() :\n"
3977                    "    // Comment forcing unwanted break.\n"
3978                    "    aaaa(aaaa) {}",
3979 				   Style));
3980 
3981   Style.ColumnLimit = 0;
3982   verifyFormat("SomeClass::Constructor() :\n"
3983                "    a(a) {}",
3984                Style);
3985   verifyFormat("SomeClass::Constructor() noexcept :\n"
3986                "    a(a) {}",
3987                Style);
3988   verifyFormat("SomeClass::Constructor() :\n"
3989 			   "    a(a), b(b), c(c) {}",
3990                Style);
3991   verifyFormat("SomeClass::Constructor() :\n"
3992                "    a(a) {\n"
3993                "  foo();\n"
3994                "  bar();\n"
3995                "}",
3996                Style);
3997 
3998   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3999   verifyFormat("SomeClass::Constructor() :\n"
4000 			   "    a(a), b(b), c(c) {\n"
4001 			   "}",
4002                Style);
4003   verifyFormat("SomeClass::Constructor() :\n"
4004                "    a(a) {\n"
4005 			   "}",
4006                Style);
4007 
4008   Style.ColumnLimit = 80;
4009   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
4010   Style.ConstructorInitializerIndentWidth = 2;
4011   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
4012                Style);
4013   verifyFormat("SomeClass::Constructor() :\n"
4014                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4015                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
4016                Style);
4017 
4018   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
4019   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
4020   verifyFormat("class SomeClass\n"
4021                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4022                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4023                Style);
4024   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
4025   verifyFormat("class SomeClass\n"
4026                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4027                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4028                Style);
4029   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
4030   verifyFormat("class SomeClass :\n"
4031                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4032                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4033                Style);
4034 }
4035 
4036 #ifndef EXPENSIVE_CHECKS
4037 // Expensive checks enables libstdc++ checking which includes validating the
4038 // state of ranges used in std::priority_queue - this blows out the
4039 // runtime/scalability of the function and makes this test unacceptably slow.
4040 TEST_F(FormatTest, MemoizationTests) {
4041   // This breaks if the memoization lookup does not take \c Indent and
4042   // \c LastSpace into account.
4043   verifyFormat(
4044       "extern CFRunLoopTimerRef\n"
4045       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
4046       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
4047       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
4048       "                     CFRunLoopTimerContext *context) {}");
4049 
4050   // Deep nesting somewhat works around our memoization.
4051   verifyFormat(
4052       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4053       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4054       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4055       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4056       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
4057       getLLVMStyleWithColumns(65));
4058   verifyFormat(
4059       "aaaaa(\n"
4060       "    aaaaa,\n"
4061       "    aaaaa(\n"
4062       "        aaaaa,\n"
4063       "        aaaaa(\n"
4064       "            aaaaa,\n"
4065       "            aaaaa(\n"
4066       "                aaaaa,\n"
4067       "                aaaaa(\n"
4068       "                    aaaaa,\n"
4069       "                    aaaaa(\n"
4070       "                        aaaaa,\n"
4071       "                        aaaaa(\n"
4072       "                            aaaaa,\n"
4073       "                            aaaaa(\n"
4074       "                                aaaaa,\n"
4075       "                                aaaaa(\n"
4076       "                                    aaaaa,\n"
4077       "                                    aaaaa(\n"
4078       "                                        aaaaa,\n"
4079       "                                        aaaaa(\n"
4080       "                                            aaaaa,\n"
4081       "                                            aaaaa(\n"
4082       "                                                aaaaa,\n"
4083       "                                                aaaaa))))))))))));",
4084       getLLVMStyleWithColumns(65));
4085   verifyFormat(
4086       "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"
4087       "                                  a),\n"
4088       "                                a),\n"
4089       "                              a),\n"
4090       "                            a),\n"
4091       "                          a),\n"
4092       "                        a),\n"
4093       "                      a),\n"
4094       "                    a),\n"
4095       "                  a),\n"
4096       "                a),\n"
4097       "              a),\n"
4098       "            a),\n"
4099       "          a),\n"
4100       "        a),\n"
4101       "      a),\n"
4102       "    a),\n"
4103       "  a)",
4104       getLLVMStyleWithColumns(65));
4105 
4106   // This test takes VERY long when memoization is broken.
4107   FormatStyle OnePerLine = getLLVMStyle();
4108   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4109   OnePerLine.BinPackParameters = false;
4110   std::string input = "Constructor()\n"
4111                       "    : aaaa(a,\n";
4112   for (unsigned i = 0, e = 80; i != e; ++i) {
4113     input += "           a,\n";
4114   }
4115   input += "           a) {}";
4116   verifyFormat(input, OnePerLine);
4117 }
4118 #endif
4119 
4120 TEST_F(FormatTest, BreaksAsHighAsPossible) {
4121   verifyFormat(
4122       "void f() {\n"
4123       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
4124       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
4125       "    f();\n"
4126       "}");
4127   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
4128                "    Intervals[i - 1].getRange().getLast()) {\n}");
4129 }
4130 
4131 TEST_F(FormatTest, BreaksFunctionDeclarations) {
4132   // Principially, we break function declarations in a certain order:
4133   // 1) break amongst arguments.
4134   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
4135                "                              Cccccccccccccc cccccccccccccc);");
4136   verifyFormat("template <class TemplateIt>\n"
4137                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
4138                "                            TemplateIt *stop) {}");
4139 
4140   // 2) break after return type.
4141   verifyFormat(
4142       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4143       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
4144       getGoogleStyle());
4145 
4146   // 3) break after (.
4147   verifyFormat(
4148       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
4149       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
4150       getGoogleStyle());
4151 
4152   // 4) break before after nested name specifiers.
4153   verifyFormat(
4154       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4155       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
4156       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
4157       getGoogleStyle());
4158 
4159   // However, there are exceptions, if a sufficient amount of lines can be
4160   // saved.
4161   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
4162   // more adjusting.
4163   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4164                "                                  Cccccccccccccc cccccccccc,\n"
4165                "                                  Cccccccccccccc cccccccccc,\n"
4166                "                                  Cccccccccccccc cccccccccc,\n"
4167                "                                  Cccccccccccccc cccccccccc);");
4168   verifyFormat(
4169       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4170       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4171       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4172       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
4173       getGoogleStyle());
4174   verifyFormat(
4175       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4176       "                                          Cccccccccccccc cccccccccc,\n"
4177       "                                          Cccccccccccccc cccccccccc,\n"
4178       "                                          Cccccccccccccc cccccccccc,\n"
4179       "                                          Cccccccccccccc cccccccccc,\n"
4180       "                                          Cccccccccccccc cccccccccc,\n"
4181       "                                          Cccccccccccccc cccccccccc);");
4182   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4183                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4184                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4185                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4186                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
4187 
4188   // Break after multi-line parameters.
4189   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4190                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4191                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4192                "    bbbb bbbb);");
4193   verifyFormat("void SomeLoooooooooooongFunction(\n"
4194                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4195                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4196                "    int bbbbbbbbbbbbb);");
4197 
4198   // Treat overloaded operators like other functions.
4199   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4200                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
4201   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4202                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
4203   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4204                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
4205   verifyGoogleFormat(
4206       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
4207       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4208   verifyGoogleFormat(
4209       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
4210       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4211   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4212                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4213   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
4214                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4215   verifyGoogleFormat(
4216       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
4217       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4218       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
4219   verifyGoogleFormat(
4220       "template <typename T>\n"
4221       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4222       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
4223       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
4224 
4225   FormatStyle Style = getLLVMStyle();
4226   Style.PointerAlignment = FormatStyle::PAS_Left;
4227   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4228                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
4229                Style);
4230   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4231                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4232                Style);
4233 }
4234 
4235 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
4236   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
4237   // Prefer keeping `::` followed by `operator` together.
4238   EXPECT_EQ("const aaaa::bbbbbbb &\n"
4239             "ccccccccc::operator++() {\n"
4240             "  stuff();\n"
4241             "}",
4242             format("const aaaa::bbbbbbb\n"
4243                    "&ccccccccc::operator++() { stuff(); }",
4244                    getLLVMStyleWithColumns(40)));
4245 }
4246 
4247 TEST_F(FormatTest, TrailingReturnType) {
4248   verifyFormat("auto foo() -> int;\n");
4249   verifyFormat("struct S {\n"
4250                "  auto bar() const -> int;\n"
4251                "};");
4252   verifyFormat("template <size_t Order, typename T>\n"
4253                "auto load_img(const std::string &filename)\n"
4254                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4255   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4256                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4257   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4258   verifyFormat("template <typename T>\n"
4259                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4260                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4261 
4262   // Not trailing return types.
4263   verifyFormat("void f() { auto a = b->c(); }");
4264 }
4265 
4266 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4267   // Avoid breaking before trailing 'const' or other trailing annotations, if
4268   // they are not function-like.
4269   FormatStyle Style = getGoogleStyle();
4270   Style.ColumnLimit = 47;
4271   verifyFormat("void someLongFunction(\n"
4272                "    int someLoooooooooooooongParameter) const {\n}",
4273                getLLVMStyleWithColumns(47));
4274   verifyFormat("LoooooongReturnType\n"
4275                "someLoooooooongFunction() const {}",
4276                getLLVMStyleWithColumns(47));
4277   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4278                "    const {}",
4279                Style);
4280   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4281                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4282   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4283                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4284   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4285                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4286   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4287                "                   aaaaaaaaaaa aaaaa) const override;");
4288   verifyGoogleFormat(
4289       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4290       "    const override;");
4291 
4292   // Even if the first parameter has to be wrapped.
4293   verifyFormat("void someLongFunction(\n"
4294                "    int someLongParameter) const {}",
4295                getLLVMStyleWithColumns(46));
4296   verifyFormat("void someLongFunction(\n"
4297                "    int someLongParameter) const {}",
4298                Style);
4299   verifyFormat("void someLongFunction(\n"
4300                "    int someLongParameter) override {}",
4301                Style);
4302   verifyFormat("void someLongFunction(\n"
4303                "    int someLongParameter) OVERRIDE {}",
4304                Style);
4305   verifyFormat("void someLongFunction(\n"
4306                "    int someLongParameter) final {}",
4307                Style);
4308   verifyFormat("void someLongFunction(\n"
4309                "    int someLongParameter) FINAL {}",
4310                Style);
4311   verifyFormat("void someLongFunction(\n"
4312                "    int parameter) const override {}",
4313                Style);
4314 
4315   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4316   verifyFormat("void someLongFunction(\n"
4317                "    int someLongParameter) const\n"
4318                "{\n"
4319                "}",
4320                Style);
4321 
4322   // Unless these are unknown annotations.
4323   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4324                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4325                "    LONG_AND_UGLY_ANNOTATION;");
4326 
4327   // Breaking before function-like trailing annotations is fine to keep them
4328   // close to their arguments.
4329   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4330                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4331   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4332                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4333   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4334                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4335   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4336                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4337   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4338 
4339   verifyFormat(
4340       "void aaaaaaaaaaaaaaaaaa()\n"
4341       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4342       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4343   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4344                "    __attribute__((unused));");
4345   verifyGoogleFormat(
4346       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4347       "    GUARDED_BY(aaaaaaaaaaaa);");
4348   verifyGoogleFormat(
4349       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4350       "    GUARDED_BY(aaaaaaaaaaaa);");
4351   verifyGoogleFormat(
4352       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4353       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4354   verifyGoogleFormat(
4355       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4356       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4357 }
4358 
4359 TEST_F(FormatTest, FunctionAnnotations) {
4360   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4361                "int OldFunction(const string &parameter) {}");
4362   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4363                "string OldFunction(const string &parameter) {}");
4364   verifyFormat("template <typename T>\n"
4365                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4366                "string OldFunction(const string &parameter) {}");
4367 
4368   // Not function annotations.
4369   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4370                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4371   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4372                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4373   verifyFormat("MACRO(abc).function() // wrap\n"
4374                "    << abc;");
4375   verifyFormat("MACRO(abc)->function() // wrap\n"
4376                "    << abc;");
4377   verifyFormat("MACRO(abc)::function() // wrap\n"
4378                "    << abc;");
4379 }
4380 
4381 TEST_F(FormatTest, BreaksDesireably) {
4382   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4383                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4384                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4385   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4386                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4387                "}");
4388 
4389   verifyFormat(
4390       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4391       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4392 
4393   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4394                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4395                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4396 
4397   verifyFormat(
4398       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4399       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4400       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4401       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4402       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4403 
4404   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4405                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4406 
4407   verifyFormat(
4408       "void f() {\n"
4409       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4410       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4411       "}");
4412   verifyFormat(
4413       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4414       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4415   verifyFormat(
4416       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4417       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4418   verifyFormat(
4419       "aaaaaa(aaa,\n"
4420       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4421       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4422       "       aaaa);");
4423   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4424                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4425                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4426 
4427   // Indent consistently independent of call expression and unary operator.
4428   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4429                "    dddddddddddddddddddddddddddddd));");
4430   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4431                "    dddddddddddddddddddddddddddddd));");
4432   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4433                "    dddddddddddddddddddddddddddddd));");
4434 
4435   // This test case breaks on an incorrect memoization, i.e. an optimization not
4436   // taking into account the StopAt value.
4437   verifyFormat(
4438       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4439       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4440       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4441       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4442 
4443   verifyFormat("{\n  {\n    {\n"
4444                "      Annotation.SpaceRequiredBefore =\n"
4445                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4446                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4447                "    }\n  }\n}");
4448 
4449   // Break on an outer level if there was a break on an inner level.
4450   EXPECT_EQ("f(g(h(a, // comment\n"
4451             "      b, c),\n"
4452             "    d, e),\n"
4453             "  x, y);",
4454             format("f(g(h(a, // comment\n"
4455                    "    b, c), d, e), x, y);"));
4456 
4457   // Prefer breaking similar line breaks.
4458   verifyFormat(
4459       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4460       "                             NSTrackingMouseEnteredAndExited |\n"
4461       "                             NSTrackingActiveAlways;");
4462 }
4463 
4464 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4465   FormatStyle NoBinPacking = getGoogleStyle();
4466   NoBinPacking.BinPackParameters = false;
4467   NoBinPacking.BinPackArguments = true;
4468   verifyFormat("void f() {\n"
4469                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4470                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4471                "}",
4472                NoBinPacking);
4473   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4474                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4475                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4476                NoBinPacking);
4477 
4478   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4479   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4480                "                        vector<int> bbbbbbbbbbbbbbb);",
4481                NoBinPacking);
4482   // FIXME: This behavior difference is probably not wanted. However, currently
4483   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4484   // template arguments from BreakBeforeParameter being set because of the
4485   // one-per-line formatting.
4486   verifyFormat(
4487       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4488       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4489       NoBinPacking);
4490   verifyFormat(
4491       "void fffffffffff(\n"
4492       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4493       "        aaaaaaaaaa);");
4494 }
4495 
4496 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4497   FormatStyle NoBinPacking = getGoogleStyle();
4498   NoBinPacking.BinPackParameters = false;
4499   NoBinPacking.BinPackArguments = false;
4500   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4501                "  aaaaaaaaaaaaaaaaaaaa,\n"
4502                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4503                NoBinPacking);
4504   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4505                "        aaaaaaaaaaaaa,\n"
4506                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4507                NoBinPacking);
4508   verifyFormat(
4509       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4510       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4511       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4512       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4513       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4514       NoBinPacking);
4515   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4516                "    .aaaaaaaaaaaaaaaaaa();",
4517                NoBinPacking);
4518   verifyFormat("void f() {\n"
4519                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4520                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4521                "}",
4522                NoBinPacking);
4523 
4524   verifyFormat(
4525       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4526       "             aaaaaaaaaaaa,\n"
4527       "             aaaaaaaaaaaa);",
4528       NoBinPacking);
4529   verifyFormat(
4530       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4531       "                               ddddddddddddddddddddddddddddd),\n"
4532       "             test);",
4533       NoBinPacking);
4534 
4535   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4536                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4537                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4538                "    aaaaaaaaaaaaaaaaaa;",
4539                NoBinPacking);
4540   verifyFormat("a(\"a\"\n"
4541                "  \"a\",\n"
4542                "  a);");
4543 
4544   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4545   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4546                "                aaaaaaaaa,\n"
4547                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4548                NoBinPacking);
4549   verifyFormat(
4550       "void f() {\n"
4551       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4552       "      .aaaaaaa();\n"
4553       "}",
4554       NoBinPacking);
4555   verifyFormat(
4556       "template <class SomeType, class SomeOtherType>\n"
4557       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4558       NoBinPacking);
4559 }
4560 
4561 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4562   FormatStyle Style = getLLVMStyleWithColumns(15);
4563   Style.ExperimentalAutoDetectBinPacking = true;
4564   EXPECT_EQ("aaa(aaaa,\n"
4565             "    aaaa,\n"
4566             "    aaaa);\n"
4567             "aaa(aaaa,\n"
4568             "    aaaa,\n"
4569             "    aaaa);",
4570             format("aaa(aaaa,\n" // one-per-line
4571                    "  aaaa,\n"
4572                    "    aaaa  );\n"
4573                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4574                    Style));
4575   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4576             "    aaaa);\n"
4577             "aaa(aaaa, aaaa,\n"
4578             "    aaaa);",
4579             format("aaa(aaaa,  aaaa,\n" // bin-packed
4580                    "    aaaa  );\n"
4581                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4582                    Style));
4583 }
4584 
4585 TEST_F(FormatTest, FormatsBuilderPattern) {
4586   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4587                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4588                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4589                "    .StartsWith(\".init\", ORDER_INIT)\n"
4590                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4591                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4592                "    .Default(ORDER_TEXT);\n");
4593 
4594   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4595                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4596   verifyFormat(
4597       "aaaaaaa->aaaaaaa\n"
4598       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4599       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4600       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4601   verifyFormat(
4602       "aaaaaaa->aaaaaaa\n"
4603       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4604       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4605   verifyFormat(
4606       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4607       "    aaaaaaaaaaaaaa);");
4608   verifyFormat(
4609       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4610       "    aaaaaa->aaaaaaaaaaaa()\n"
4611       "        ->aaaaaaaaaaaaaaaa(\n"
4612       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4613       "        ->aaaaaaaaaaaaaaaaa();");
4614   verifyGoogleFormat(
4615       "void f() {\n"
4616       "  someo->Add((new util::filetools::Handler(dir))\n"
4617       "                 ->OnEvent1(NewPermanentCallback(\n"
4618       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4619       "                 ->OnEvent2(NewPermanentCallback(\n"
4620       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4621       "                 ->OnEvent3(NewPermanentCallback(\n"
4622       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4623       "                 ->OnEvent5(NewPermanentCallback(\n"
4624       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4625       "                 ->OnEvent6(NewPermanentCallback(\n"
4626       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4627       "}");
4628 
4629   verifyFormat(
4630       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4631   verifyFormat("aaaaaaaaaaaaaaa()\n"
4632                "    .aaaaaaaaaaaaaaa()\n"
4633                "    .aaaaaaaaaaaaaaa()\n"
4634                "    .aaaaaaaaaaaaaaa()\n"
4635                "    .aaaaaaaaaaaaaaa();");
4636   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4637                "    .aaaaaaaaaaaaaaa()\n"
4638                "    .aaaaaaaaaaaaaaa()\n"
4639                "    .aaaaaaaaaaaaaaa();");
4640   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4641                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4642                "    .aaaaaaaaaaaaaaa();");
4643   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4644                "    ->aaaaaaaaaaaaaae(0)\n"
4645                "    ->aaaaaaaaaaaaaaa();");
4646 
4647   // Don't linewrap after very short segments.
4648   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4649                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4650                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4651   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4652                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4653                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4654   verifyFormat("aaa()\n"
4655                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4656                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4657                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4658 
4659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4660                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4661                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4662   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4663                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4664                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4665 
4666   // Prefer not to break after empty parentheses.
4667   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4668                "    First->LastNewlineOffset);");
4669 
4670   // Prefer not to create "hanging" indents.
4671   verifyFormat(
4672       "return !soooooooooooooome_map\n"
4673       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4674       "            .second;");
4675   verifyFormat(
4676       "return aaaaaaaaaaaaaaaa\n"
4677       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4678       "    .aaaa(aaaaaaaaaaaaaa);");
4679   // No hanging indent here.
4680   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4681                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4682   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4683                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4684   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4685                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4686                getLLVMStyleWithColumns(60));
4687   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4688                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4689                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4690                getLLVMStyleWithColumns(59));
4691   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4692                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4693                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4694 
4695   // Dont break if only closing statements before member call
4696   verifyFormat("test() {\n"
4697                "  ([]() -> {\n"
4698                "    int b = 32;\n"
4699                "    return 3;\n"
4700                "  }).foo();\n"
4701                "}");
4702   verifyFormat("test() {\n"
4703                "  (\n"
4704                "      []() -> {\n"
4705                "        int b = 32;\n"
4706                "        return 3;\n"
4707                "      },\n"
4708                "      foo, bar)\n"
4709                "      .foo();\n"
4710                "}");
4711   verifyFormat("test() {\n"
4712                "  ([]() -> {\n"
4713                "    int b = 32;\n"
4714                "    return 3;\n"
4715                "  })\n"
4716                "      .foo()\n"
4717                "      .bar();\n"
4718                "}");
4719   verifyFormat("test() {\n"
4720                "  ([]() -> {\n"
4721                "    int b = 32;\n"
4722                "    return 3;\n"
4723                "  })\n"
4724                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
4725                "           \"bbbb\");\n"
4726                "}",
4727                getLLVMStyleWithColumns(30));
4728 }
4729 
4730 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4731   verifyFormat(
4732       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4733       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4734   verifyFormat(
4735       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4736       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4737 
4738   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4739                "    ccccccccccccccccccccccccc) {\n}");
4740   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4741                "    ccccccccccccccccccccccccc) {\n}");
4742 
4743   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4744                "    ccccccccccccccccccccccccc) {\n}");
4745   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4746                "    ccccccccccccccccccccccccc) {\n}");
4747 
4748   verifyFormat(
4749       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4750       "    ccccccccccccccccccccccccc) {\n}");
4751   verifyFormat(
4752       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4753       "    ccccccccccccccccccccccccc) {\n}");
4754 
4755   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4756                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4757                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4758                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4759   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4760                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4761                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4762                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4763 
4764   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4765                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4766                "    aaaaaaaaaaaaaaa != aa) {\n}");
4767   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4768                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4769                "    aaaaaaaaaaaaaaa != aa) {\n}");
4770 }
4771 
4772 TEST_F(FormatTest, BreaksAfterAssignments) {
4773   verifyFormat(
4774       "unsigned Cost =\n"
4775       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4776       "                        SI->getPointerAddressSpaceee());\n");
4777   verifyFormat(
4778       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4779       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4780 
4781   verifyFormat(
4782       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4783       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4784   verifyFormat("unsigned OriginalStartColumn =\n"
4785                "    SourceMgr.getSpellingColumnNumber(\n"
4786                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4787                "    1;");
4788 }
4789 
4790 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4791   FormatStyle Style = getLLVMStyle();
4792   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4793                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4794                Style);
4795 
4796   Style.PenaltyBreakAssignment = 20;
4797   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4798                "                                 cccccccccccccccccccccccccc;",
4799                Style);
4800 }
4801 
4802 TEST_F(FormatTest, AlignsAfterAssignments) {
4803   verifyFormat(
4804       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4805       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4806   verifyFormat(
4807       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4808       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4809   verifyFormat(
4810       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4811       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4812   verifyFormat(
4813       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4814       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4815   verifyFormat(
4816       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4817       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4818       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4819 }
4820 
4821 TEST_F(FormatTest, AlignsAfterReturn) {
4822   verifyFormat(
4823       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4824       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4825   verifyFormat(
4826       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4827       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4828   verifyFormat(
4829       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4830       "       aaaaaaaaaaaaaaaaaaaaaa();");
4831   verifyFormat(
4832       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4833       "        aaaaaaaaaaaaaaaaaaaaaa());");
4834   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4835                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4836   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4837                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4838                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4839   verifyFormat("return\n"
4840                "    // true if code is one of a or b.\n"
4841                "    code == a || code == b;");
4842 }
4843 
4844 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4845   verifyFormat(
4846       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4847       "                                                aaaaaaaaa aaaaaaa) {}");
4848   verifyFormat(
4849       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4850       "                                               aaaaaaaaaaa aaaaaaaaa);");
4851   verifyFormat(
4852       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4853       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4854   FormatStyle Style = getLLVMStyle();
4855   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4856   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4857                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4858                Style);
4859   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4860                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4861                Style);
4862   verifyFormat("SomeLongVariableName->someFunction(\n"
4863                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4864                Style);
4865   verifyFormat(
4866       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4867       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4868       Style);
4869   verifyFormat(
4870       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4871       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4872       Style);
4873   verifyFormat(
4874       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4875       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4876       Style);
4877 
4878   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4879                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4880                "        b));",
4881                Style);
4882 
4883   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4884   Style.BinPackArguments = false;
4885   Style.BinPackParameters = false;
4886   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4887                "    aaaaaaaaaaa aaaaaaaa,\n"
4888                "    aaaaaaaaa aaaaaaa,\n"
4889                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4890                Style);
4891   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4892                "    aaaaaaaaaaa aaaaaaaaa,\n"
4893                "    aaaaaaaaaaa aaaaaaaaa,\n"
4894                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4895                Style);
4896   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4897                "    aaaaaaaaaaaaaaa,\n"
4898                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4899                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4900                Style);
4901   verifyFormat(
4902       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4903       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4904       Style);
4905   verifyFormat(
4906       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4907       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4908       Style);
4909   verifyFormat(
4910       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4911       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4912       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4913       "    aaaaaaaaaaaaaaaa);",
4914       Style);
4915   verifyFormat(
4916       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4917       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4918       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4919       "    aaaaaaaaaaaaaaaa);",
4920       Style);
4921 }
4922 
4923 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4924   FormatStyle Style = getLLVMStyleWithColumns(40);
4925   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4926                "          bbbbbbbbbbbbbbbbbbbbbb);",
4927                Style);
4928   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4929   Style.AlignOperands = false;
4930   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4931                "          bbbbbbbbbbbbbbbbbbbbbb);",
4932                Style);
4933   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4934   Style.AlignOperands = true;
4935   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4936                "          bbbbbbbbbbbbbbbbbbbbbb);",
4937                Style);
4938   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4939   Style.AlignOperands = false;
4940   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4941                "    bbbbbbbbbbbbbbbbbbbbbb);",
4942                Style);
4943 }
4944 
4945 TEST_F(FormatTest, BreaksConditionalExpressions) {
4946   verifyFormat(
4947       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4948       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4949       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4950   verifyFormat(
4951       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4952       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4953       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4954   verifyFormat(
4955       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4956       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4957   verifyFormat(
4958       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4959       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4960       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4961   verifyFormat(
4962       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4963       "                                                    : aaaaaaaaaaaaa);");
4964   verifyFormat(
4965       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4966       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4967       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4968       "                   aaaaaaaaaaaaa);");
4969   verifyFormat(
4970       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4971       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4972       "                   aaaaaaaaaaaaa);");
4973   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4974                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4975                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4976                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4977                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4978   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4979                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4980                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4981                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4982                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4983                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4984                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4985   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4986                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4987                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4988                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4989                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4990   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4991                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4992                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4993   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4994                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4995                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4996                "        : aaaaaaaaaaaaaaaa;");
4997   verifyFormat(
4998       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4999       "    ? aaaaaaaaaaaaaaa\n"
5000       "    : aaaaaaaaaaaaaaa;");
5001   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5002                "          aaaaaaaaa\n"
5003                "      ? b\n"
5004                "      : c);");
5005   verifyFormat("return aaaa == bbbb\n"
5006                "           // comment\n"
5007                "           ? aaaa\n"
5008                "           : bbbb;");
5009   verifyFormat("unsigned Indent =\n"
5010                "    format(TheLine.First,\n"
5011                "           IndentForLevel[TheLine.Level] >= 0\n"
5012                "               ? IndentForLevel[TheLine.Level]\n"
5013                "               : TheLine * 2,\n"
5014                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5015                getLLVMStyleWithColumns(60));
5016   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5017                "                  ? aaaaaaaaaaaaaaa\n"
5018                "                  : bbbbbbbbbbbbbbb //\n"
5019                "                        ? ccccccccccccccc\n"
5020                "                        : ddddddddddddddd;");
5021   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5022                "                  ? aaaaaaaaaaaaaaa\n"
5023                "                  : (bbbbbbbbbbbbbbb //\n"
5024                "                         ? ccccccccccccccc\n"
5025                "                         : ddddddddddddddd);");
5026   verifyFormat(
5027       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5028       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5029       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
5030       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
5031       "                                      : aaaaaaaaaa;");
5032   verifyFormat(
5033       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5034       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
5035       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5036 
5037   FormatStyle NoBinPacking = getLLVMStyle();
5038   NoBinPacking.BinPackArguments = false;
5039   verifyFormat(
5040       "void f() {\n"
5041       "  g(aaa,\n"
5042       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5043       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5044       "        ? aaaaaaaaaaaaaaa\n"
5045       "        : aaaaaaaaaaaaaaa);\n"
5046       "}",
5047       NoBinPacking);
5048   verifyFormat(
5049       "void f() {\n"
5050       "  g(aaa,\n"
5051       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5052       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5053       "        ?: aaaaaaaaaaaaaaa);\n"
5054       "}",
5055       NoBinPacking);
5056 
5057   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
5058                "             // comment.\n"
5059                "             ccccccccccccccccccccccccccccccccccccccc\n"
5060                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5061                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
5062 
5063   // Assignments in conditional expressions. Apparently not uncommon :-(.
5064   verifyFormat("return a != b\n"
5065                "           // comment\n"
5066                "           ? a = b\n"
5067                "           : a = b;");
5068   verifyFormat("return a != b\n"
5069                "           // comment\n"
5070                "           ? a = a != b\n"
5071                "                     // comment\n"
5072                "                     ? a = b\n"
5073                "                     : a\n"
5074                "           : a;\n");
5075   verifyFormat("return a != b\n"
5076                "           // comment\n"
5077                "           ? a\n"
5078                "           : a = a != b\n"
5079                "                     // comment\n"
5080                "                     ? a = b\n"
5081                "                     : a;");
5082 }
5083 
5084 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
5085   FormatStyle Style = getLLVMStyle();
5086   Style.BreakBeforeTernaryOperators = false;
5087   Style.ColumnLimit = 70;
5088   verifyFormat(
5089       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5090       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5091       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5092       Style);
5093   verifyFormat(
5094       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5095       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5096       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5097       Style);
5098   verifyFormat(
5099       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5100       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5101       Style);
5102   verifyFormat(
5103       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
5104       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5105       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5106       Style);
5107   verifyFormat(
5108       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
5109       "                                                      aaaaaaaaaaaaa);",
5110       Style);
5111   verifyFormat(
5112       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5113       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5114       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5115       "                   aaaaaaaaaaaaa);",
5116       Style);
5117   verifyFormat(
5118       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5119       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5120       "                   aaaaaaaaaaaaa);",
5121       Style);
5122   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5123                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5124                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5125                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5126                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5127                Style);
5128   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5129                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5130                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5131                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5132                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5133                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5134                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5135                Style);
5136   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5137                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
5138                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5139                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5140                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5141                Style);
5142   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5143                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5144                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5145                Style);
5146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5147                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5148                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5149                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5150                Style);
5151   verifyFormat(
5152       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5153       "    aaaaaaaaaaaaaaa :\n"
5154       "    aaaaaaaaaaaaaaa;",
5155       Style);
5156   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5157                "          aaaaaaaaa ?\n"
5158                "      b :\n"
5159                "      c);",
5160                Style);
5161   verifyFormat("unsigned Indent =\n"
5162                "    format(TheLine.First,\n"
5163                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
5164                "               IndentForLevel[TheLine.Level] :\n"
5165                "               TheLine * 2,\n"
5166                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5167                Style);
5168   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5169                "                  aaaaaaaaaaaaaaa :\n"
5170                "                  bbbbbbbbbbbbbbb ? //\n"
5171                "                      ccccccccccccccc :\n"
5172                "                      ddddddddddddddd;",
5173                Style);
5174   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5175                "                  aaaaaaaaaaaaaaa :\n"
5176                "                  (bbbbbbbbbbbbbbb ? //\n"
5177                "                       ccccccccccccccc :\n"
5178                "                       ddddddddddddddd);",
5179                Style);
5180   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5181                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
5182                "            ccccccccccccccccccccccccccc;",
5183                Style);
5184   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5185                "           aaaaa :\n"
5186                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
5187                Style);
5188 }
5189 
5190 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
5191   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
5192                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
5193   verifyFormat("bool a = true, b = false;");
5194 
5195   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5196                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
5197                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
5198                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
5199   verifyFormat(
5200       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5201       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
5202       "     d = e && f;");
5203   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
5204                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
5205   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5206                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
5207   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
5208                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
5209 
5210   FormatStyle Style = getGoogleStyle();
5211   Style.PointerAlignment = FormatStyle::PAS_Left;
5212   Style.DerivePointerAlignment = false;
5213   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5214                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
5215                "    *b = bbbbbbbbbbbbbbbbbbb;",
5216                Style);
5217   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5218                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
5219                Style);
5220   verifyFormat("vector<int*> a, b;", Style);
5221   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
5222 }
5223 
5224 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
5225   verifyFormat("arr[foo ? bar : baz];");
5226   verifyFormat("f()[foo ? bar : baz];");
5227   verifyFormat("(a + b)[foo ? bar : baz];");
5228   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
5229 }
5230 
5231 TEST_F(FormatTest, AlignsStringLiterals) {
5232   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
5233                "                                      \"short literal\");");
5234   verifyFormat(
5235       "looooooooooooooooooooooooongFunction(\n"
5236       "    \"short literal\"\n"
5237       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
5238   verifyFormat("someFunction(\"Always break between multi-line\"\n"
5239                "             \" string literals\",\n"
5240                "             and, other, parameters);");
5241   EXPECT_EQ("fun + \"1243\" /* comment */\n"
5242             "      \"5678\";",
5243             format("fun + \"1243\" /* comment */\n"
5244                    "    \"5678\";",
5245                    getLLVMStyleWithColumns(28)));
5246   EXPECT_EQ(
5247       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
5248       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
5249       "         \"aaaaaaaaaaaaaaaa\";",
5250       format("aaaaaa ="
5251              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
5252              "aaaaaaaaaaaaaaaaaaaaa\" "
5253              "\"aaaaaaaaaaaaaaaa\";"));
5254   verifyFormat("a = a + \"a\"\n"
5255                "        \"a\"\n"
5256                "        \"a\";");
5257   verifyFormat("f(\"a\", \"b\"\n"
5258                "       \"c\");");
5259 
5260   verifyFormat(
5261       "#define LL_FORMAT \"ll\"\n"
5262       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
5263       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
5264 
5265   verifyFormat("#define A(X)          \\\n"
5266                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
5267                "  \"ccccc\"",
5268                getLLVMStyleWithColumns(23));
5269   verifyFormat("#define A \"def\"\n"
5270                "f(\"abc\" A \"ghi\"\n"
5271                "  \"jkl\");");
5272 
5273   verifyFormat("f(L\"a\"\n"
5274                "  L\"b\");");
5275   verifyFormat("#define A(X)            \\\n"
5276                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
5277                "  L\"ccccc\"",
5278                getLLVMStyleWithColumns(25));
5279 
5280   verifyFormat("f(@\"a\"\n"
5281                "  @\"b\");");
5282   verifyFormat("NSString s = @\"a\"\n"
5283                "             @\"b\"\n"
5284                "             @\"c\";");
5285   verifyFormat("NSString s = @\"a\"\n"
5286                "              \"b\"\n"
5287                "              \"c\";");
5288 }
5289 
5290 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5291   FormatStyle Style = getLLVMStyle();
5292   // No declarations or definitions should be moved to own line.
5293   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5294   verifyFormat("class A {\n"
5295                "  int f() { return 1; }\n"
5296                "  int g();\n"
5297                "};\n"
5298                "int f() { return 1; }\n"
5299                "int g();\n",
5300                Style);
5301 
5302   // All declarations and definitions should have the return type moved to its
5303   // own
5304   // line.
5305   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5306   verifyFormat("class E {\n"
5307                "  int\n"
5308                "  f() {\n"
5309                "    return 1;\n"
5310                "  }\n"
5311                "  int\n"
5312                "  g();\n"
5313                "};\n"
5314                "int\n"
5315                "f() {\n"
5316                "  return 1;\n"
5317                "}\n"
5318                "int\n"
5319                "g();\n",
5320                Style);
5321 
5322   // Top-level definitions, and no kinds of declarations should have the
5323   // return type moved to its own line.
5324   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5325   verifyFormat("class B {\n"
5326                "  int f() { return 1; }\n"
5327                "  int g();\n"
5328                "};\n"
5329                "int\n"
5330                "f() {\n"
5331                "  return 1;\n"
5332                "}\n"
5333                "int g();\n",
5334                Style);
5335 
5336   // Top-level definitions and declarations should have the return type moved
5337   // to its own line.
5338   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5339   verifyFormat("class C {\n"
5340                "  int f() { return 1; }\n"
5341                "  int g();\n"
5342                "};\n"
5343                "int\n"
5344                "f() {\n"
5345                "  return 1;\n"
5346                "}\n"
5347                "int\n"
5348                "g();\n",
5349                Style);
5350 
5351   // All definitions should have the return type moved to its own line, but no
5352   // kinds of declarations.
5353   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5354   verifyFormat("class D {\n"
5355                "  int\n"
5356                "  f() {\n"
5357                "    return 1;\n"
5358                "  }\n"
5359                "  int g();\n"
5360                "};\n"
5361                "int\n"
5362                "f() {\n"
5363                "  return 1;\n"
5364                "}\n"
5365                "int g();\n",
5366                Style);
5367   verifyFormat("const char *\n"
5368                "f(void) {\n" // Break here.
5369                "  return \"\";\n"
5370                "}\n"
5371                "const char *bar(void);\n", // No break here.
5372                Style);
5373   verifyFormat("template <class T>\n"
5374                "T *\n"
5375                "f(T &c) {\n" // Break here.
5376                "  return NULL;\n"
5377                "}\n"
5378                "template <class T> T *f(T &c);\n", // No break here.
5379                Style);
5380   verifyFormat("class C {\n"
5381                "  int\n"
5382                "  operator+() {\n"
5383                "    return 1;\n"
5384                "  }\n"
5385                "  int\n"
5386                "  operator()() {\n"
5387                "    return 1;\n"
5388                "  }\n"
5389                "};\n",
5390                Style);
5391   verifyFormat("void\n"
5392                "A::operator()() {}\n"
5393                "void\n"
5394                "A::operator>>() {}\n"
5395                "void\n"
5396                "A::operator+() {}\n",
5397                Style);
5398   verifyFormat("void *operator new(std::size_t s);", // No break here.
5399                Style);
5400   verifyFormat("void *\n"
5401                "operator new(std::size_t s) {}",
5402                Style);
5403   verifyFormat("void *\n"
5404                "operator delete[](void *ptr) {}",
5405                Style);
5406   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5407   verifyFormat("const char *\n"
5408                "f(void)\n" // Break here.
5409                "{\n"
5410                "  return \"\";\n"
5411                "}\n"
5412                "const char *bar(void);\n", // No break here.
5413                Style);
5414   verifyFormat("template <class T>\n"
5415                "T *\n"     // Problem here: no line break
5416                "f(T &c)\n" // Break here.
5417                "{\n"
5418                "  return NULL;\n"
5419                "}\n"
5420                "template <class T> T *f(T &c);\n", // No break here.
5421                Style);
5422 }
5423 
5424 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5425   FormatStyle NoBreak = getLLVMStyle();
5426   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5427   FormatStyle Break = getLLVMStyle();
5428   Break.AlwaysBreakBeforeMultilineStrings = true;
5429   verifyFormat("aaaa = \"bbbb\"\n"
5430                "       \"cccc\";",
5431                NoBreak);
5432   verifyFormat("aaaa =\n"
5433                "    \"bbbb\"\n"
5434                "    \"cccc\";",
5435                Break);
5436   verifyFormat("aaaa(\"bbbb\"\n"
5437                "     \"cccc\");",
5438                NoBreak);
5439   verifyFormat("aaaa(\n"
5440                "    \"bbbb\"\n"
5441                "    \"cccc\");",
5442                Break);
5443   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5444                "          \"cccc\");",
5445                NoBreak);
5446   verifyFormat("aaaa(qqq,\n"
5447                "     \"bbbb\"\n"
5448                "     \"cccc\");",
5449                Break);
5450   verifyFormat("aaaa(qqq,\n"
5451                "     L\"bbbb\"\n"
5452                "     L\"cccc\");",
5453                Break);
5454   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5455                "                      \"bbbb\"));",
5456                Break);
5457   verifyFormat("string s = someFunction(\n"
5458                "    \"abc\"\n"
5459                "    \"abc\");",
5460                Break);
5461 
5462   // As we break before unary operators, breaking right after them is bad.
5463   verifyFormat("string foo = abc ? \"x\"\n"
5464                "                   \"blah blah blah blah blah blah\"\n"
5465                "                 : \"y\";",
5466                Break);
5467 
5468   // Don't break if there is no column gain.
5469   verifyFormat("f(\"aaaa\"\n"
5470                "  \"bbbb\");",
5471                Break);
5472 
5473   // Treat literals with escaped newlines like multi-line string literals.
5474   EXPECT_EQ("x = \"a\\\n"
5475             "b\\\n"
5476             "c\";",
5477             format("x = \"a\\\n"
5478                    "b\\\n"
5479                    "c\";",
5480                    NoBreak));
5481   EXPECT_EQ("xxxx =\n"
5482             "    \"a\\\n"
5483             "b\\\n"
5484             "c\";",
5485             format("xxxx = \"a\\\n"
5486                    "b\\\n"
5487                    "c\";",
5488                    Break));
5489 
5490   EXPECT_EQ("NSString *const kString =\n"
5491             "    @\"aaaa\"\n"
5492             "    @\"bbbb\";",
5493             format("NSString *const kString = @\"aaaa\"\n"
5494                    "@\"bbbb\";",
5495                    Break));
5496 
5497   Break.ColumnLimit = 0;
5498   verifyFormat("const char *hello = \"hello llvm\";", Break);
5499 }
5500 
5501 TEST_F(FormatTest, AlignsPipes) {
5502   verifyFormat(
5503       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5504       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5505       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5506   verifyFormat(
5507       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5508       "                     << aaaaaaaaaaaaaaaaaaaa;");
5509   verifyFormat(
5510       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5511       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5512   verifyFormat(
5513       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5514       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5515   verifyFormat(
5516       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5517       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5518       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5519   verifyFormat(
5520       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5521       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5522       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5523   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5524                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5525                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5526                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5527   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5528                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5529   verifyFormat(
5530       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5531       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5532   verifyFormat(
5533       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5534       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5535 
5536   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5537                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5538   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5539                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5540                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5541                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5542   verifyFormat("LOG_IF(aaa == //\n"
5543                "       bbb)\n"
5544                "    << a << b;");
5545 
5546   // But sometimes, breaking before the first "<<" is desirable.
5547   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5548                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5549   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5550                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5551                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5552   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5553                "    << BEF << IsTemplate << Description << E->getType();");
5554   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5555                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5556                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5557   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5558                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5559                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5560                "    << aaa;");
5561 
5562   verifyFormat(
5563       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5564       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5565 
5566   // Incomplete string literal.
5567   EXPECT_EQ("llvm::errs() << \"\n"
5568             "             << a;",
5569             format("llvm::errs() << \"\n<<a;"));
5570 
5571   verifyFormat("void f() {\n"
5572                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5573                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5574                "}");
5575 
5576   // Handle 'endl'.
5577   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5578                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5579   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5580 
5581   // Handle '\n'.
5582   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5583                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5584   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5585                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5586   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5587                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5588   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5589 }
5590 
5591 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5592   verifyFormat("return out << \"somepacket = {\\n\"\n"
5593                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5594                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5595                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5596                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5597                "           << \"}\";");
5598 
5599   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5600                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5601                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5602   verifyFormat(
5603       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5604       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5605       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5606       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5607       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5608   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5609                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5610   verifyFormat(
5611       "void f() {\n"
5612       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5613       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5614       "}");
5615 
5616   // Breaking before the first "<<" is generally not desirable.
5617   verifyFormat(
5618       "llvm::errs()\n"
5619       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5620       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5621       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5622       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5623       getLLVMStyleWithColumns(70));
5624   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5625                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5626                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5627                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5628                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5629                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5630                getLLVMStyleWithColumns(70));
5631 
5632   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5633                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5634                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5635   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5636                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5637                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5638   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5639                "           (aaaa + aaaa);",
5640                getLLVMStyleWithColumns(40));
5641   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5642                "                  (aaaaaaa + aaaaa));",
5643                getLLVMStyleWithColumns(40));
5644   verifyFormat(
5645       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5646       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5647       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5648 }
5649 
5650 TEST_F(FormatTest, UnderstandsEquals) {
5651   verifyFormat(
5652       "aaaaaaaaaaaaaaaaa =\n"
5653       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5654   verifyFormat(
5655       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5656       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5657   verifyFormat(
5658       "if (a) {\n"
5659       "  f();\n"
5660       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5661       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5662       "}");
5663 
5664   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5665                "        100000000 + 10000000) {\n}");
5666 }
5667 
5668 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5669   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5670                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5671 
5672   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5673                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5674 
5675   verifyFormat(
5676       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5677       "                                                          Parameter2);");
5678 
5679   verifyFormat(
5680       "ShortObject->shortFunction(\n"
5681       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5682       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5683 
5684   verifyFormat("loooooooooooooongFunction(\n"
5685                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5686 
5687   verifyFormat(
5688       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5689       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5690 
5691   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5692                "    .WillRepeatedly(Return(SomeValue));");
5693   verifyFormat("void f() {\n"
5694                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5695                "      .Times(2)\n"
5696                "      .WillRepeatedly(Return(SomeValue));\n"
5697                "}");
5698   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5699                "    ccccccccccccccccccccccc);");
5700   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5701                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5702                "          .aaaaa(aaaaa),\n"
5703                "      aaaaaaaaaaaaaaaaaaaaa);");
5704   verifyFormat("void f() {\n"
5705                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5706                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5707                "}");
5708   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5709                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5710                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5711                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5712                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5713   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5714                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5715                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5716                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5717                "}");
5718 
5719   // Here, it is not necessary to wrap at "." or "->".
5720   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5721                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5722   verifyFormat(
5723       "aaaaaaaaaaa->aaaaaaaaa(\n"
5724       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5725       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5726 
5727   verifyFormat(
5728       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5729       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5730   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5731                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5732   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5733                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5734 
5735   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5736                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5737                "    .a();");
5738 
5739   FormatStyle NoBinPacking = getLLVMStyle();
5740   NoBinPacking.BinPackParameters = false;
5741   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5742                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5743                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5744                "                         aaaaaaaaaaaaaaaaaaa,\n"
5745                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5746                NoBinPacking);
5747 
5748   // If there is a subsequent call, change to hanging indentation.
5749   verifyFormat(
5750       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5751       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5752       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5753   verifyFormat(
5754       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5755       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5756   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5757                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5758                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5759   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5760                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5761                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5762 }
5763 
5764 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5765   verifyFormat("template <typename T>\n"
5766                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5767   verifyFormat("template <typename T>\n"
5768                "// T should be one of {A, B}.\n"
5769                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5770   verifyFormat(
5771       "template <typename T>\n"
5772       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5773   verifyFormat("template <typename T>\n"
5774                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5775                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5776   verifyFormat(
5777       "template <typename T>\n"
5778       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5779       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5780   verifyFormat(
5781       "template <typename T>\n"
5782       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5783       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5784       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5785   verifyFormat("template <typename T>\n"
5786                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5787                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5788   verifyFormat(
5789       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5790       "          typename T4 = char>\n"
5791       "void f();");
5792   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5793                "          template <typename> class cccccccccccccccccccccc,\n"
5794                "          typename ddddddddddddd>\n"
5795                "class C {};");
5796   verifyFormat(
5797       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5798       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5799 
5800   verifyFormat("void f() {\n"
5801                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5802                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5803                "}");
5804 
5805   verifyFormat("template <typename T> class C {};");
5806   verifyFormat("template <typename T> void f();");
5807   verifyFormat("template <typename T> void f() {}");
5808   verifyFormat(
5809       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5810       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5811       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5812       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5813       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5814       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5815       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5816       getLLVMStyleWithColumns(72));
5817   EXPECT_EQ("static_cast<A< //\n"
5818             "    B> *>(\n"
5819             "\n"
5820             ");",
5821             format("static_cast<A<//\n"
5822                    "    B>*>(\n"
5823                    "\n"
5824                    "    );"));
5825   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5826                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5827 
5828   FormatStyle AlwaysBreak = getLLVMStyle();
5829   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
5830   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5831   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5832   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5833   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5834                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5835                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5836   verifyFormat("template <template <typename> class Fooooooo,\n"
5837                "          template <typename> class Baaaaaaar>\n"
5838                "struct C {};",
5839                AlwaysBreak);
5840   verifyFormat("template <typename T> // T can be A, B or C.\n"
5841                "struct C {};",
5842                AlwaysBreak);
5843   verifyFormat("template <enum E> class A {\n"
5844                "public:\n"
5845                "  E *f();\n"
5846                "};");
5847 
5848   FormatStyle NeverBreak = getLLVMStyle();
5849   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
5850   verifyFormat("template <typename T> class C {};", NeverBreak);
5851   verifyFormat("template <typename T> void f();", NeverBreak);
5852   verifyFormat("template <typename T> void f() {}", NeverBreak);
5853   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5854                NeverBreak);
5855   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5856                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5857                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
5858                NeverBreak);
5859   verifyFormat("template <template <typename> class Fooooooo,\n"
5860                "          template <typename> class Baaaaaaar>\n"
5861                "struct C {};",
5862                NeverBreak);
5863   verifyFormat("template <typename T> // T can be A, B or C.\n"
5864                "struct C {};",
5865                NeverBreak);
5866   verifyFormat("template <enum E> class A {\n"
5867                "public:\n"
5868                "  E *f();\n"
5869                "};", NeverBreak);
5870   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
5871   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5872                NeverBreak);
5873 }
5874 
5875 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
5876   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
5877   Style.ColumnLimit = 60;
5878   EXPECT_EQ("// Baseline - no comments.\n"
5879             "template <\n"
5880             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5881             "void f() {}",
5882             format("// Baseline - no comments.\n"
5883                    "template <\n"
5884                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5885                    "void f() {}",
5886                    Style));
5887 
5888   EXPECT_EQ("template <\n"
5889             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5890             "void f() {}",
5891             format("template <\n"
5892                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5893                    "void f() {}",
5894                    Style));
5895 
5896   EXPECT_EQ(
5897       "template <\n"
5898       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
5899       "void f() {}",
5900       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
5901              "void f() {}",
5902              Style));
5903 
5904   EXPECT_EQ(
5905       "template <\n"
5906       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5907       "                                               // multiline\n"
5908       "void f() {}",
5909       format("template <\n"
5910              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5911              "                                              // multiline\n"
5912              "void f() {}",
5913              Style));
5914 
5915   EXPECT_EQ(
5916       "template <typename aaaaaaaaaa<\n"
5917       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
5918       "void f() {}",
5919       format(
5920           "template <\n"
5921           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
5922           "void f() {}",
5923           Style));
5924 }
5925 
5926 TEST_F(FormatTest, WrapsTemplateParameters) {
5927   FormatStyle Style = getLLVMStyle();
5928   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5929   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5930   verifyFormat(
5931       "template <typename... a> struct q {};\n"
5932       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5933       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5934       "    y;",
5935       Style);
5936   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5937   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5938   verifyFormat(
5939       "template <typename... a> struct r {};\n"
5940       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5941       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5942       "    y;",
5943       Style);
5944   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5945   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5946   verifyFormat(
5947       "template <typename... a> struct s {};\n"
5948       "extern s<\n"
5949       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5950       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5951       "    y;",
5952       Style);
5953   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5954   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5955   verifyFormat(
5956       "template <typename... a> struct t {};\n"
5957       "extern t<\n"
5958       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5959       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5960       "    y;",
5961       Style);
5962 }
5963 
5964 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5965   verifyFormat(
5966       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5967       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5968   verifyFormat(
5969       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5970       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5971       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5972 
5973   // FIXME: Should we have the extra indent after the second break?
5974   verifyFormat(
5975       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5976       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5977       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5978 
5979   verifyFormat(
5980       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5981       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5982 
5983   // Breaking at nested name specifiers is generally not desirable.
5984   verifyFormat(
5985       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5986       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5987 
5988   verifyFormat(
5989       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5990       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5991       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5992       "                   aaaaaaaaaaaaaaaaaaaaa);",
5993       getLLVMStyleWithColumns(74));
5994 
5995   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5996                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5997                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5998 }
5999 
6000 TEST_F(FormatTest, UnderstandsTemplateParameters) {
6001   verifyFormat("A<int> a;");
6002   verifyFormat("A<A<A<int>>> a;");
6003   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
6004   verifyFormat("bool x = a < 1 || 2 > a;");
6005   verifyFormat("bool x = 5 < f<int>();");
6006   verifyFormat("bool x = f<int>() > 5;");
6007   verifyFormat("bool x = 5 < a<int>::x;");
6008   verifyFormat("bool x = a < 4 ? a > 2 : false;");
6009   verifyFormat("bool x = f() ? a < 2 : a > 2;");
6010 
6011   verifyGoogleFormat("A<A<int>> a;");
6012   verifyGoogleFormat("A<A<A<int>>> a;");
6013   verifyGoogleFormat("A<A<A<A<int>>>> a;");
6014   verifyGoogleFormat("A<A<int> > a;");
6015   verifyGoogleFormat("A<A<A<int> > > a;");
6016   verifyGoogleFormat("A<A<A<A<int> > > > a;");
6017   verifyGoogleFormat("A<::A<int>> a;");
6018   verifyGoogleFormat("A<::A> a;");
6019   verifyGoogleFormat("A< ::A> a;");
6020   verifyGoogleFormat("A< ::A<int> > a;");
6021   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
6022   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
6023   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
6024   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
6025   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
6026             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
6027 
6028   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
6029 
6030   verifyFormat("test >> a >> b;");
6031   verifyFormat("test << a >> b;");
6032 
6033   verifyFormat("f<int>();");
6034   verifyFormat("template <typename T> void f() {}");
6035   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
6036   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
6037                "sizeof(char)>::type>;");
6038   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
6039   verifyFormat("f(a.operator()<A>());");
6040   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6041                "      .template operator()<A>());",
6042                getLLVMStyleWithColumns(35));
6043 
6044   // Not template parameters.
6045   verifyFormat("return a < b && c > d;");
6046   verifyFormat("void f() {\n"
6047                "  while (a < b && c > d) {\n"
6048                "  }\n"
6049                "}");
6050   verifyFormat("template <typename... Types>\n"
6051                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
6052 
6053   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6054                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
6055                getLLVMStyleWithColumns(60));
6056   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
6057   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
6058   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
6059 }
6060 
6061 TEST_F(FormatTest, BitshiftOperatorWidth) {
6062   EXPECT_EQ("int a = 1 << 2; /* foo\n"
6063             "                   bar */",
6064             format("int    a=1<<2;  /* foo\n"
6065                    "                   bar */"));
6066 
6067   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
6068             "                     bar */",
6069             format("int  b  =256>>1 ;  /* foo\n"
6070                    "                      bar */"));
6071 }
6072 
6073 TEST_F(FormatTest, UnderstandsBinaryOperators) {
6074   verifyFormat("COMPARE(a, ==, b);");
6075   verifyFormat("auto s = sizeof...(Ts) - 1;");
6076 }
6077 
6078 TEST_F(FormatTest, UnderstandsPointersToMembers) {
6079   verifyFormat("int A::*x;");
6080   verifyFormat("int (S::*func)(void *);");
6081   verifyFormat("void f() { int (S::*func)(void *); }");
6082   verifyFormat("typedef bool *(Class::*Member)() const;");
6083   verifyFormat("void f() {\n"
6084                "  (a->*f)();\n"
6085                "  a->*x;\n"
6086                "  (a.*f)();\n"
6087                "  ((*a).*f)();\n"
6088                "  a.*x;\n"
6089                "}");
6090   verifyFormat("void f() {\n"
6091                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6092                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
6093                "}");
6094   verifyFormat(
6095       "(aaaaaaaaaa->*bbbbbbb)(\n"
6096       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6097   FormatStyle Style = getLLVMStyle();
6098   Style.PointerAlignment = FormatStyle::PAS_Left;
6099   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
6100 }
6101 
6102 TEST_F(FormatTest, UnderstandsUnaryOperators) {
6103   verifyFormat("int a = -2;");
6104   verifyFormat("f(-1, -2, -3);");
6105   verifyFormat("a[-1] = 5;");
6106   verifyFormat("int a = 5 + -2;");
6107   verifyFormat("if (i == -1) {\n}");
6108   verifyFormat("if (i != -1) {\n}");
6109   verifyFormat("if (i > -1) {\n}");
6110   verifyFormat("if (i < -1) {\n}");
6111   verifyFormat("++(a->f());");
6112   verifyFormat("--(a->f());");
6113   verifyFormat("(a->f())++;");
6114   verifyFormat("a[42]++;");
6115   verifyFormat("if (!(a->f())) {\n}");
6116   verifyFormat("if (!+i) {\n}");
6117   verifyFormat("~&a;");
6118 
6119   verifyFormat("a-- > b;");
6120   verifyFormat("b ? -a : c;");
6121   verifyFormat("n * sizeof char16;");
6122   verifyFormat("n * alignof char16;", getGoogleStyle());
6123   verifyFormat("sizeof(char);");
6124   verifyFormat("alignof(char);", getGoogleStyle());
6125 
6126   verifyFormat("return -1;");
6127   verifyFormat("switch (a) {\n"
6128                "case -1:\n"
6129                "  break;\n"
6130                "}");
6131   verifyFormat("#define X -1");
6132   verifyFormat("#define X -kConstant");
6133 
6134   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
6135   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
6136 
6137   verifyFormat("int a = /* confusing comment */ -1;");
6138   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
6139   verifyFormat("int a = i /* confusing comment */++;");
6140 }
6141 
6142 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
6143   verifyFormat("if (!aaaaaaaaaa( // break\n"
6144                "        aaaaa)) {\n"
6145                "}");
6146   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
6147                "    aaaaa));");
6148   verifyFormat("*aaa = aaaaaaa( // break\n"
6149                "    bbbbbb);");
6150 }
6151 
6152 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
6153   verifyFormat("bool operator<();");
6154   verifyFormat("bool operator>();");
6155   verifyFormat("bool operator=();");
6156   verifyFormat("bool operator==();");
6157   verifyFormat("bool operator!=();");
6158   verifyFormat("int operator+();");
6159   verifyFormat("int operator++();");
6160   verifyFormat("int operator++(int) volatile noexcept;");
6161   verifyFormat("bool operator,();");
6162   verifyFormat("bool operator();");
6163   verifyFormat("bool operator()();");
6164   verifyFormat("bool operator[]();");
6165   verifyFormat("operator bool();");
6166   verifyFormat("operator int();");
6167   verifyFormat("operator void *();");
6168   verifyFormat("operator SomeType<int>();");
6169   verifyFormat("operator SomeType<int, int>();");
6170   verifyFormat("operator SomeType<SomeType<int>>();");
6171   verifyFormat("void *operator new(std::size_t size);");
6172   verifyFormat("void *operator new[](std::size_t size);");
6173   verifyFormat("void operator delete(void *ptr);");
6174   verifyFormat("void operator delete[](void *ptr);");
6175   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
6176                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
6177   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
6178                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
6179 
6180   verifyFormat(
6181       "ostream &operator<<(ostream &OutputStream,\n"
6182       "                    SomeReallyLongType WithSomeReallyLongValue);");
6183   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
6184                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
6185                "  return left.group < right.group;\n"
6186                "}");
6187   verifyFormat("SomeType &operator=(const SomeType &S);");
6188   verifyFormat("f.template operator()<int>();");
6189 
6190   verifyGoogleFormat("operator void*();");
6191   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
6192   verifyGoogleFormat("operator ::A();");
6193 
6194   verifyFormat("using A::operator+;");
6195   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
6196                "int i;");
6197 }
6198 
6199 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
6200   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
6201   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
6202   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
6203   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
6204   verifyFormat("Deleted &operator=(const Deleted &) &;");
6205   verifyFormat("Deleted &operator=(const Deleted &) &&;");
6206   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
6207   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
6208   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
6209   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
6210   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
6211   verifyFormat("void Fn(T const &) const &;");
6212   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
6213   verifyFormat("template <typename T>\n"
6214                "void F(T) && = delete;",
6215                getGoogleStyle());
6216 
6217   FormatStyle AlignLeft = getLLVMStyle();
6218   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
6219   verifyFormat("void A::b() && {}", AlignLeft);
6220   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
6221   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
6222                AlignLeft);
6223   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
6224   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
6225   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
6226   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
6227   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
6228   verifyFormat("auto Function(T) & -> void;", AlignLeft);
6229   verifyFormat("void Fn(T const&) const&;", AlignLeft);
6230   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
6231 
6232   FormatStyle Spaces = getLLVMStyle();
6233   Spaces.SpacesInCStyleCastParentheses = true;
6234   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
6235   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
6236   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
6237   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
6238 
6239   Spaces.SpacesInCStyleCastParentheses = false;
6240   Spaces.SpacesInParentheses = true;
6241   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
6242   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
6243   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
6244   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
6245 }
6246 
6247 TEST_F(FormatTest, UnderstandsNewAndDelete) {
6248   verifyFormat("void f() {\n"
6249                "  A *a = new A;\n"
6250                "  A *a = new (placement) A;\n"
6251                "  delete a;\n"
6252                "  delete (A *)a;\n"
6253                "}");
6254   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6255                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6256   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6257                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6258                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6259   verifyFormat("delete[] h->p;");
6260 }
6261 
6262 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
6263   verifyFormat("int *f(int *a) {}");
6264   verifyFormat("int main(int argc, char **argv) {}");
6265   verifyFormat("Test::Test(int b) : a(b * b) {}");
6266   verifyIndependentOfContext("f(a, *a);");
6267   verifyFormat("void g() { f(*a); }");
6268   verifyIndependentOfContext("int a = b * 10;");
6269   verifyIndependentOfContext("int a = 10 * b;");
6270   verifyIndependentOfContext("int a = b * c;");
6271   verifyIndependentOfContext("int a += b * c;");
6272   verifyIndependentOfContext("int a -= b * c;");
6273   verifyIndependentOfContext("int a *= b * c;");
6274   verifyIndependentOfContext("int a /= b * c;");
6275   verifyIndependentOfContext("int a = *b;");
6276   verifyIndependentOfContext("int a = *b * c;");
6277   verifyIndependentOfContext("int a = b * *c;");
6278   verifyIndependentOfContext("int a = b * (10);");
6279   verifyIndependentOfContext("S << b * (10);");
6280   verifyIndependentOfContext("return 10 * b;");
6281   verifyIndependentOfContext("return *b * *c;");
6282   verifyIndependentOfContext("return a & ~b;");
6283   verifyIndependentOfContext("f(b ? *c : *d);");
6284   verifyIndependentOfContext("int a = b ? *c : *d;");
6285   verifyIndependentOfContext("*b = a;");
6286   verifyIndependentOfContext("a * ~b;");
6287   verifyIndependentOfContext("a * !b;");
6288   verifyIndependentOfContext("a * +b;");
6289   verifyIndependentOfContext("a * -b;");
6290   verifyIndependentOfContext("a * ++b;");
6291   verifyIndependentOfContext("a * --b;");
6292   verifyIndependentOfContext("a[4] * b;");
6293   verifyIndependentOfContext("a[a * a] = 1;");
6294   verifyIndependentOfContext("f() * b;");
6295   verifyIndependentOfContext("a * [self dostuff];");
6296   verifyIndependentOfContext("int x = a * (a + b);");
6297   verifyIndependentOfContext("(a *)(a + b);");
6298   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6299   verifyIndependentOfContext("int *pa = (int *)&a;");
6300   verifyIndependentOfContext("return sizeof(int **);");
6301   verifyIndependentOfContext("return sizeof(int ******);");
6302   verifyIndependentOfContext("return (int **&)a;");
6303   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6304   verifyFormat("void f(Type (*parameter)[10]) {}");
6305   verifyFormat("void f(Type (&parameter)[10]) {}");
6306   verifyGoogleFormat("return sizeof(int**);");
6307   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6308   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6309   verifyFormat("auto a = [](int **&, int ***) {};");
6310   verifyFormat("auto PointerBinding = [](const char *S) {};");
6311   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6312   verifyFormat("[](const decltype(*a) &value) {}");
6313   verifyFormat("decltype(a * b) F();");
6314   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6315   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6316   verifyIndependentOfContext("typedef void (*f)(int *a);");
6317   verifyIndependentOfContext("int i{a * b};");
6318   verifyIndependentOfContext("aaa && aaa->f();");
6319   verifyIndependentOfContext("int x = ~*p;");
6320   verifyFormat("Constructor() : a(a), area(width * height) {}");
6321   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6322   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6323   verifyFormat("void f() { f(a, c * d); }");
6324   verifyFormat("void f() { f(new a(), c * d); }");
6325   verifyFormat("void f(const MyOverride &override);");
6326   verifyFormat("void f(const MyFinal &final);");
6327   verifyIndependentOfContext("bool a = f() && override.f();");
6328   verifyIndependentOfContext("bool a = f() && final.f();");
6329 
6330   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6331 
6332   verifyIndependentOfContext("A<int *> a;");
6333   verifyIndependentOfContext("A<int **> a;");
6334   verifyIndependentOfContext("A<int *, int *> a;");
6335   verifyIndependentOfContext("A<int *[]> a;");
6336   verifyIndependentOfContext(
6337       "const char *const p = reinterpret_cast<const char *const>(q);");
6338   verifyIndependentOfContext("A<int **, int **> a;");
6339   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6340   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6341   verifyFormat("for (; a && b;) {\n}");
6342   verifyFormat("bool foo = true && [] { return false; }();");
6343 
6344   verifyFormat(
6345       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6346       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6347 
6348   verifyGoogleFormat("int const* a = &b;");
6349   verifyGoogleFormat("**outparam = 1;");
6350   verifyGoogleFormat("*outparam = a * b;");
6351   verifyGoogleFormat("int main(int argc, char** argv) {}");
6352   verifyGoogleFormat("A<int*> a;");
6353   verifyGoogleFormat("A<int**> a;");
6354   verifyGoogleFormat("A<int*, int*> a;");
6355   verifyGoogleFormat("A<int**, int**> a;");
6356   verifyGoogleFormat("f(b ? *c : *d);");
6357   verifyGoogleFormat("int a = b ? *c : *d;");
6358   verifyGoogleFormat("Type* t = **x;");
6359   verifyGoogleFormat("Type* t = *++*x;");
6360   verifyGoogleFormat("*++*x;");
6361   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6362   verifyGoogleFormat("Type* t = x++ * y;");
6363   verifyGoogleFormat(
6364       "const char* const p = reinterpret_cast<const char* const>(q);");
6365   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6366   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6367   verifyGoogleFormat("template <typename T>\n"
6368                      "void f(int i = 0, SomeType** temps = NULL);");
6369 
6370   FormatStyle Left = getLLVMStyle();
6371   Left.PointerAlignment = FormatStyle::PAS_Left;
6372   verifyFormat("x = *a(x) = *a(y);", Left);
6373   verifyFormat("for (;; *a = b) {\n}", Left);
6374   verifyFormat("return *this += 1;", Left);
6375   verifyFormat("throw *x;", Left);
6376   verifyFormat("delete *x;", Left);
6377   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6378   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6379   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6380 
6381   verifyIndependentOfContext("a = *(x + y);");
6382   verifyIndependentOfContext("a = &(x + y);");
6383   verifyIndependentOfContext("*(x + y).call();");
6384   verifyIndependentOfContext("&(x + y)->call();");
6385   verifyFormat("void f() { &(*I).first; }");
6386 
6387   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6388   verifyFormat(
6389       "int *MyValues = {\n"
6390       "    *A, // Operator detection might be confused by the '{'\n"
6391       "    *BB // Operator detection might be confused by previous comment\n"
6392       "};");
6393 
6394   verifyIndependentOfContext("if (int *a = &b)");
6395   verifyIndependentOfContext("if (int &a = *b)");
6396   verifyIndependentOfContext("if (a & b[i])");
6397   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6398   verifyIndependentOfContext("if (*b[i])");
6399   verifyIndependentOfContext("if (int *a = (&b))");
6400   verifyIndependentOfContext("while (int *a = &b)");
6401   verifyIndependentOfContext("size = sizeof *a;");
6402   verifyIndependentOfContext("if (a && (b = c))");
6403   verifyFormat("void f() {\n"
6404                "  for (const int &v : Values) {\n"
6405                "  }\n"
6406                "}");
6407   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6408   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6409   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6410 
6411   verifyFormat("#define A (!a * b)");
6412   verifyFormat("#define MACRO     \\\n"
6413                "  int *i = a * b; \\\n"
6414                "  void f(a *b);",
6415                getLLVMStyleWithColumns(19));
6416 
6417   verifyIndependentOfContext("A = new SomeType *[Length];");
6418   verifyIndependentOfContext("A = new SomeType *[Length]();");
6419   verifyIndependentOfContext("T **t = new T *;");
6420   verifyIndependentOfContext("T **t = new T *();");
6421   verifyGoogleFormat("A = new SomeType*[Length]();");
6422   verifyGoogleFormat("A = new SomeType*[Length];");
6423   verifyGoogleFormat("T** t = new T*;");
6424   verifyGoogleFormat("T** t = new T*();");
6425 
6426   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6427   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6428   verifyFormat("template <bool a, bool b> "
6429                "typename t::if<x && y>::type f() {}");
6430   verifyFormat("template <int *y> f() {}");
6431   verifyFormat("vector<int *> v;");
6432   verifyFormat("vector<int *const> v;");
6433   verifyFormat("vector<int *const **const *> v;");
6434   verifyFormat("vector<int *volatile> v;");
6435   verifyFormat("vector<a * b> v;");
6436   verifyFormat("foo<b && false>();");
6437   verifyFormat("foo<b & 1>();");
6438   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6439   verifyFormat(
6440       "template <class T, class = typename std::enable_if<\n"
6441       "                       std::is_integral<T>::value &&\n"
6442       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6443       "void F();",
6444       getLLVMStyleWithColumns(70));
6445   verifyFormat(
6446       "template <class T,\n"
6447       "          class = typename std::enable_if<\n"
6448       "              std::is_integral<T>::value &&\n"
6449       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6450       "          class U>\n"
6451       "void F();",
6452       getLLVMStyleWithColumns(70));
6453   verifyFormat(
6454       "template <class T,\n"
6455       "          class = typename ::std::enable_if<\n"
6456       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6457       "void F();",
6458       getGoogleStyleWithColumns(68));
6459 
6460   verifyIndependentOfContext("MACRO(int *i);");
6461   verifyIndependentOfContext("MACRO(auto *a);");
6462   verifyIndependentOfContext("MACRO(const A *a);");
6463   verifyIndependentOfContext("MACRO(A *const a);");
6464   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6465   verifyFormat("void f() { f(float{1}, a * a); }");
6466   // FIXME: Is there a way to make this work?
6467   // verifyIndependentOfContext("MACRO(A *a);");
6468 
6469   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6470   verifyFormat("return options != nullptr && operator==(*options);");
6471 
6472   EXPECT_EQ("#define OP(x)                                    \\\n"
6473             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6474             "    return s << a.DebugString();                 \\\n"
6475             "  }",
6476             format("#define OP(x) \\\n"
6477                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6478                    "    return s << a.DebugString(); \\\n"
6479                    "  }",
6480                    getLLVMStyleWithColumns(50)));
6481 
6482   // FIXME: We cannot handle this case yet; we might be able to figure out that
6483   // foo<x> d > v; doesn't make sense.
6484   verifyFormat("foo<a<b && c> d> v;");
6485 
6486   FormatStyle PointerMiddle = getLLVMStyle();
6487   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6488   verifyFormat("delete *x;", PointerMiddle);
6489   verifyFormat("int * x;", PointerMiddle);
6490   verifyFormat("int *[] x;", PointerMiddle);
6491   verifyFormat("template <int * y> f() {}", PointerMiddle);
6492   verifyFormat("int * f(int * a) {}", PointerMiddle);
6493   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6494   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6495   verifyFormat("A<int *> a;", PointerMiddle);
6496   verifyFormat("A<int **> a;", PointerMiddle);
6497   verifyFormat("A<int *, int *> a;", PointerMiddle);
6498   verifyFormat("A<int *[]> a;", PointerMiddle);
6499   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6500   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6501   verifyFormat("T ** t = new T *;", PointerMiddle);
6502 
6503   // Member function reference qualifiers aren't binary operators.
6504   verifyFormat("string // break\n"
6505                "operator()() & {}");
6506   verifyFormat("string // break\n"
6507                "operator()() && {}");
6508   verifyGoogleFormat("template <typename T>\n"
6509                      "auto x() & -> int {}");
6510 }
6511 
6512 TEST_F(FormatTest, UnderstandsAttributes) {
6513   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6514   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6515                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6516   FormatStyle AfterType = getLLVMStyle();
6517   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6518   verifyFormat("__attribute__((nodebug)) void\n"
6519                "foo() {}\n",
6520                AfterType);
6521 }
6522 
6523 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6524   verifyFormat("SomeType s [[unused]] (InitValue);");
6525   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6526   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6527   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6528   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6529   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6530                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6531 
6532   // Make sure we do not mistake attributes for array subscripts.
6533   verifyFormat("int a() {}\n"
6534                "[[unused]] int b() {}\n");
6535   verifyFormat("NSArray *arr;\n"
6536                "arr[[Foo() bar]];");
6537 
6538   // On the other hand, we still need to correctly find array subscripts.
6539   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6540 
6541   // Make sure we do not parse attributes as lambda introducers.
6542   FormatStyle MultiLineFunctions = getLLVMStyle();
6543   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6544   verifyFormat("[[unused]] int b() {\n"
6545                "  return 42;\n"
6546                "}\n",
6547                MultiLineFunctions);
6548 }
6549 
6550 TEST_F(FormatTest, UnderstandsEllipsis) {
6551   verifyFormat("int printf(const char *fmt, ...);");
6552   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6553   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6554 
6555   FormatStyle PointersLeft = getLLVMStyle();
6556   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6557   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6558 }
6559 
6560 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6561   EXPECT_EQ("int *a;\n"
6562             "int *a;\n"
6563             "int *a;",
6564             format("int *a;\n"
6565                    "int* a;\n"
6566                    "int *a;",
6567                    getGoogleStyle()));
6568   EXPECT_EQ("int* a;\n"
6569             "int* a;\n"
6570             "int* a;",
6571             format("int* a;\n"
6572                    "int* a;\n"
6573                    "int *a;",
6574                    getGoogleStyle()));
6575   EXPECT_EQ("int *a;\n"
6576             "int *a;\n"
6577             "int *a;",
6578             format("int *a;\n"
6579                    "int * a;\n"
6580                    "int *  a;",
6581                    getGoogleStyle()));
6582   EXPECT_EQ("auto x = [] {\n"
6583             "  int *a;\n"
6584             "  int *a;\n"
6585             "  int *a;\n"
6586             "};",
6587             format("auto x=[]{int *a;\n"
6588                    "int * a;\n"
6589                    "int *  a;};",
6590                    getGoogleStyle()));
6591 }
6592 
6593 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6594   verifyFormat("int f(int &&a) {}");
6595   verifyFormat("int f(int a, char &&b) {}");
6596   verifyFormat("void f() { int &&a = b; }");
6597   verifyGoogleFormat("int f(int a, char&& b) {}");
6598   verifyGoogleFormat("void f() { int&& a = b; }");
6599 
6600   verifyIndependentOfContext("A<int &&> a;");
6601   verifyIndependentOfContext("A<int &&, int &&> a;");
6602   verifyGoogleFormat("A<int&&> a;");
6603   verifyGoogleFormat("A<int&&, int&&> a;");
6604 
6605   // Not rvalue references:
6606   verifyFormat("template <bool B, bool C> class A {\n"
6607                "  static_assert(B && C, \"Something is wrong\");\n"
6608                "};");
6609   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6610   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6611   verifyFormat("#define A(a, b) (a && b)");
6612 }
6613 
6614 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6615   verifyFormat("void f() {\n"
6616                "  x[aaaaaaaaa -\n"
6617                "    b] = 23;\n"
6618                "}",
6619                getLLVMStyleWithColumns(15));
6620 }
6621 
6622 TEST_F(FormatTest, FormatsCasts) {
6623   verifyFormat("Type *A = static_cast<Type *>(P);");
6624   verifyFormat("Type *A = (Type *)P;");
6625   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6626   verifyFormat("int a = (int)(2.0f);");
6627   verifyFormat("int a = (int)2.0f;");
6628   verifyFormat("x[(int32)y];");
6629   verifyFormat("x = (int32)y;");
6630   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6631   verifyFormat("int a = (int)*b;");
6632   verifyFormat("int a = (int)2.0f;");
6633   verifyFormat("int a = (int)~0;");
6634   verifyFormat("int a = (int)++a;");
6635   verifyFormat("int a = (int)sizeof(int);");
6636   verifyFormat("int a = (int)+2;");
6637   verifyFormat("my_int a = (my_int)2.0f;");
6638   verifyFormat("my_int a = (my_int)sizeof(int);");
6639   verifyFormat("return (my_int)aaa;");
6640   verifyFormat("#define x ((int)-1)");
6641   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6642   verifyFormat("#define p(q) ((int *)&q)");
6643   verifyFormat("fn(a)(b) + 1;");
6644 
6645   verifyFormat("void f() { my_int a = (my_int)*b; }");
6646   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6647   verifyFormat("my_int a = (my_int)~0;");
6648   verifyFormat("my_int a = (my_int)++a;");
6649   verifyFormat("my_int a = (my_int)-2;");
6650   verifyFormat("my_int a = (my_int)1;");
6651   verifyFormat("my_int a = (my_int *)1;");
6652   verifyFormat("my_int a = (const my_int)-1;");
6653   verifyFormat("my_int a = (const my_int *)-1;");
6654   verifyFormat("my_int a = (my_int)(my_int)-1;");
6655   verifyFormat("my_int a = (ns::my_int)-2;");
6656   verifyFormat("case (my_int)ONE:");
6657   verifyFormat("auto x = (X)this;");
6658 
6659   // FIXME: single value wrapped with paren will be treated as cast.
6660   verifyFormat("void f(int i = (kValue)*kMask) {}");
6661 
6662   verifyFormat("{ (void)F; }");
6663 
6664   // Don't break after a cast's
6665   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6666                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6667                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6668 
6669   // These are not casts.
6670   verifyFormat("void f(int *) {}");
6671   verifyFormat("f(foo)->b;");
6672   verifyFormat("f(foo).b;");
6673   verifyFormat("f(foo)(b);");
6674   verifyFormat("f(foo)[b];");
6675   verifyFormat("[](foo) { return 4; }(bar);");
6676   verifyFormat("(*funptr)(foo)[4];");
6677   verifyFormat("funptrs[4](foo)[4];");
6678   verifyFormat("void f(int *);");
6679   verifyFormat("void f(int *) = 0;");
6680   verifyFormat("void f(SmallVector<int>) {}");
6681   verifyFormat("void f(SmallVector<int>);");
6682   verifyFormat("void f(SmallVector<int>) = 0;");
6683   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6684   verifyFormat("int a = sizeof(int) * b;");
6685   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6686   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6687   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6688   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6689 
6690   // These are not casts, but at some point were confused with casts.
6691   verifyFormat("virtual void foo(int *) override;");
6692   verifyFormat("virtual void foo(char &) const;");
6693   verifyFormat("virtual void foo(int *a, char *) const;");
6694   verifyFormat("int a = sizeof(int *) + b;");
6695   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6696   verifyFormat("bool b = f(g<int>) && c;");
6697   verifyFormat("typedef void (*f)(int i) func;");
6698 
6699   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6700                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6701   // FIXME: The indentation here is not ideal.
6702   verifyFormat(
6703       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6704       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6705       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6706 }
6707 
6708 TEST_F(FormatTest, FormatsFunctionTypes) {
6709   verifyFormat("A<bool()> a;");
6710   verifyFormat("A<SomeType()> a;");
6711   verifyFormat("A<void (*)(int, std::string)> a;");
6712   verifyFormat("A<void *(int)>;");
6713   verifyFormat("void *(*a)(int *, SomeType *);");
6714   verifyFormat("int (*func)(void *);");
6715   verifyFormat("void f() { int (*func)(void *); }");
6716   verifyFormat("template <class CallbackClass>\n"
6717                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6718 
6719   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6720   verifyGoogleFormat("void* (*a)(int);");
6721   verifyGoogleFormat(
6722       "template <class CallbackClass>\n"
6723       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6724 
6725   // Other constructs can look somewhat like function types:
6726   verifyFormat("A<sizeof(*x)> a;");
6727   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6728   verifyFormat("some_var = function(*some_pointer_var)[0];");
6729   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6730   verifyFormat("int x = f(&h)();");
6731   verifyFormat("returnsFunction(&param1, &param2)(param);");
6732   verifyFormat("std::function<\n"
6733                "    LooooooooooongTemplatedType<\n"
6734                "        SomeType>*(\n"
6735                "        LooooooooooooooooongType type)>\n"
6736                "    function;",
6737                getGoogleStyleWithColumns(40));
6738 }
6739 
6740 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6741   verifyFormat("A (*foo_)[6];");
6742   verifyFormat("vector<int> (*foo_)[6];");
6743 }
6744 
6745 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6746   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6747                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6748   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6749                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6750   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6751                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6752 
6753   // Different ways of ()-initializiation.
6754   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6755                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6756   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6757                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6758   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6759                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6760   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6761                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6762 
6763   // Lambdas should not confuse the variable declaration heuristic.
6764   verifyFormat("LooooooooooooooooongType\n"
6765                "    variable(nullptr, [](A *a) {});",
6766                getLLVMStyleWithColumns(40));
6767 }
6768 
6769 TEST_F(FormatTest, BreaksLongDeclarations) {
6770   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6771                "    AnotherNameForTheLongType;");
6772   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6773                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6774   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6775                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6776   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6777                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6778   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6779                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6780   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6781                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6782   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6783                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6784   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6785                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6786   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6787                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6788   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6789                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6790   FormatStyle Indented = getLLVMStyle();
6791   Indented.IndentWrappedFunctionNames = true;
6792   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6793                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6794                Indented);
6795   verifyFormat(
6796       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6797       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6798       Indented);
6799   verifyFormat(
6800       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6801       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6802       Indented);
6803   verifyFormat(
6804       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6805       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6806       Indented);
6807 
6808   // FIXME: Without the comment, this breaks after "(".
6809   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6810                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6811                getGoogleStyle());
6812 
6813   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6814                "                  int LoooooooooooooooooooongParam2) {}");
6815   verifyFormat(
6816       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6817       "                                   SourceLocation L, IdentifierIn *II,\n"
6818       "                                   Type *T) {}");
6819   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6820                "ReallyReaaallyLongFunctionName(\n"
6821                "    const std::string &SomeParameter,\n"
6822                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6823                "        &ReallyReallyLongParameterName,\n"
6824                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6825                "        &AnotherLongParameterName) {}");
6826   verifyFormat("template <typename A>\n"
6827                "SomeLoooooooooooooooooooooongType<\n"
6828                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6829                "Function() {}");
6830 
6831   verifyGoogleFormat(
6832       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6833       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6834   verifyGoogleFormat(
6835       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6836       "                                   SourceLocation L) {}");
6837   verifyGoogleFormat(
6838       "some_namespace::LongReturnType\n"
6839       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6840       "    int first_long_parameter, int second_parameter) {}");
6841 
6842   verifyGoogleFormat("template <typename T>\n"
6843                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6844                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6845   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6846                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6847 
6848   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6849                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6850                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6851   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6852                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6853                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6854   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6855                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6856                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6857                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6858 
6859   verifyFormat("template <typename T> // Templates on own line.\n"
6860                "static int            // Some comment.\n"
6861                "MyFunction(int a);",
6862                getLLVMStyle());
6863 }
6864 
6865 TEST_F(FormatTest, FormatsArrays) {
6866   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6867                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6868   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6869                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6870   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6871                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6872   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6873                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6874   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6875                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6876   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6877                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6878                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6879   verifyFormat(
6880       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6881       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6882       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6883   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6884                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6885 
6886   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6887                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6888   verifyFormat(
6889       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6890       "                                  .aaaaaaa[0]\n"
6891       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6892   verifyFormat("a[::b::c];");
6893 
6894   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6895 
6896   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6897   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6898 }
6899 
6900 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6901   verifyFormat("(a)->b();");
6902   verifyFormat("--a;");
6903 }
6904 
6905 TEST_F(FormatTest, HandlesIncludeDirectives) {
6906   verifyFormat("#include <string>\n"
6907                "#include <a/b/c.h>\n"
6908                "#include \"a/b/string\"\n"
6909                "#include \"string.h\"\n"
6910                "#include \"string.h\"\n"
6911                "#include <a-a>\n"
6912                "#include < path with space >\n"
6913                "#include_next <test.h>"
6914                "#include \"abc.h\" // this is included for ABC\n"
6915                "#include \"some long include\" // with a comment\n"
6916                "#include \"some very long include path\"\n"
6917                "#include <some/very/long/include/path>\n",
6918                getLLVMStyleWithColumns(35));
6919   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6920   EXPECT_EQ("#include <a>", format("#include<a>"));
6921 
6922   verifyFormat("#import <string>");
6923   verifyFormat("#import <a/b/c.h>");
6924   verifyFormat("#import \"a/b/string\"");
6925   verifyFormat("#import \"string.h\"");
6926   verifyFormat("#import \"string.h\"");
6927   verifyFormat("#if __has_include(<strstream>)\n"
6928                "#include <strstream>\n"
6929                "#endif");
6930 
6931   verifyFormat("#define MY_IMPORT <a/b>");
6932 
6933   verifyFormat("#if __has_include(<a/b>)");
6934   verifyFormat("#if __has_include_next(<a/b>)");
6935   verifyFormat("#define F __has_include(<a/b>)");
6936   verifyFormat("#define F __has_include_next(<a/b>)");
6937 
6938   // Protocol buffer definition or missing "#".
6939   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6940                getLLVMStyleWithColumns(30));
6941 
6942   FormatStyle Style = getLLVMStyle();
6943   Style.AlwaysBreakBeforeMultilineStrings = true;
6944   Style.ColumnLimit = 0;
6945   verifyFormat("#import \"abc.h\"", Style);
6946 
6947   // But 'import' might also be a regular C++ namespace.
6948   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6949                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6950 }
6951 
6952 //===----------------------------------------------------------------------===//
6953 // Error recovery tests.
6954 //===----------------------------------------------------------------------===//
6955 
6956 TEST_F(FormatTest, IncompleteParameterLists) {
6957   FormatStyle NoBinPacking = getLLVMStyle();
6958   NoBinPacking.BinPackParameters = false;
6959   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6960                "                        double *min_x,\n"
6961                "                        double *max_x,\n"
6962                "                        double *min_y,\n"
6963                "                        double *max_y,\n"
6964                "                        double *min_z,\n"
6965                "                        double *max_z, ) {}",
6966                NoBinPacking);
6967 }
6968 
6969 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6970   verifyFormat("void f() { return; }\n42");
6971   verifyFormat("void f() {\n"
6972                "  if (0)\n"
6973                "    return;\n"
6974                "}\n"
6975                "42");
6976   verifyFormat("void f() { return }\n42");
6977   verifyFormat("void f() {\n"
6978                "  if (0)\n"
6979                "    return\n"
6980                "}\n"
6981                "42");
6982 }
6983 
6984 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6985   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6986   EXPECT_EQ("void f() {\n"
6987             "  if (a)\n"
6988             "    return\n"
6989             "}",
6990             format("void  f  (  )  {  if  ( a )  return  }"));
6991   EXPECT_EQ("namespace N {\n"
6992             "void f()\n"
6993             "}",
6994             format("namespace  N  {  void f()  }"));
6995   EXPECT_EQ("namespace N {\n"
6996             "void f() {}\n"
6997             "void g()\n"
6998             "} // namespace N",
6999             format("namespace N  { void f( ) { } void g( ) }"));
7000 }
7001 
7002 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
7003   verifyFormat("int aaaaaaaa =\n"
7004                "    // Overlylongcomment\n"
7005                "    b;",
7006                getLLVMStyleWithColumns(20));
7007   verifyFormat("function(\n"
7008                "    ShortArgument,\n"
7009                "    LoooooooooooongArgument);\n",
7010                getLLVMStyleWithColumns(20));
7011 }
7012 
7013 TEST_F(FormatTest, IncorrectAccessSpecifier) {
7014   verifyFormat("public:");
7015   verifyFormat("class A {\n"
7016                "public\n"
7017                "  void f() {}\n"
7018                "};");
7019   verifyFormat("public\n"
7020                "int qwerty;");
7021   verifyFormat("public\n"
7022                "B {}");
7023   verifyFormat("public\n"
7024                "{}");
7025   verifyFormat("public\n"
7026                "B { int x; }");
7027 }
7028 
7029 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
7030   verifyFormat("{");
7031   verifyFormat("#})");
7032   verifyNoCrash("(/**/[:!] ?[).");
7033 }
7034 
7035 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
7036   // Found by oss-fuzz:
7037   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
7038   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7039   Style.ColumnLimit = 60;
7040   verifyNoCrash(
7041       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
7042       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
7043       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
7044       Style);
7045 }
7046 
7047 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
7048   verifyFormat("do {\n}");
7049   verifyFormat("do {\n}\n"
7050                "f();");
7051   verifyFormat("do {\n}\n"
7052                "wheeee(fun);");
7053   verifyFormat("do {\n"
7054                "  f();\n"
7055                "}");
7056 }
7057 
7058 TEST_F(FormatTest, IncorrectCodeMissingParens) {
7059   verifyFormat("if {\n  foo;\n  foo();\n}");
7060   verifyFormat("switch {\n  foo;\n  foo();\n}");
7061   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
7062   verifyFormat("while {\n  foo;\n  foo();\n}");
7063   verifyFormat("do {\n  foo;\n  foo();\n} while;");
7064 }
7065 
7066 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
7067   verifyIncompleteFormat("namespace {\n"
7068                          "class Foo { Foo (\n"
7069                          "};\n"
7070                          "} // namespace");
7071 }
7072 
7073 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
7074   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
7075   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
7076   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
7077   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
7078 
7079   EXPECT_EQ("{\n"
7080             "  {\n"
7081             "    breakme(\n"
7082             "        qwe);\n"
7083             "  }\n",
7084             format("{\n"
7085                    "    {\n"
7086                    " breakme(qwe);\n"
7087                    "}\n",
7088                    getLLVMStyleWithColumns(10)));
7089 }
7090 
7091 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
7092   verifyFormat("int x = {\n"
7093                "    avariable,\n"
7094                "    b(alongervariable)};",
7095                getLLVMStyleWithColumns(25));
7096 }
7097 
7098 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
7099   verifyFormat("return (a)(b){1, 2, 3};");
7100 }
7101 
7102 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
7103   verifyFormat("vector<int> x{1, 2, 3, 4};");
7104   verifyFormat("vector<int> x{\n"
7105                "    1,\n"
7106                "    2,\n"
7107                "    3,\n"
7108                "    4,\n"
7109                "};");
7110   verifyFormat("vector<T> x{{}, {}, {}, {}};");
7111   verifyFormat("f({1, 2});");
7112   verifyFormat("auto v = Foo{-1};");
7113   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
7114   verifyFormat("Class::Class : member{1, 2, 3} {}");
7115   verifyFormat("new vector<int>{1, 2, 3};");
7116   verifyFormat("new int[3]{1, 2, 3};");
7117   verifyFormat("new int{1};");
7118   verifyFormat("return {arg1, arg2};");
7119   verifyFormat("return {arg1, SomeType{parameter}};");
7120   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
7121   verifyFormat("new T{arg1, arg2};");
7122   verifyFormat("f(MyMap[{composite, key}]);");
7123   verifyFormat("class Class {\n"
7124                "  T member = {arg1, arg2};\n"
7125                "};");
7126   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
7127   verifyFormat("const struct A a = {.a = 1, .b = 2};");
7128   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
7129   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
7130   verifyFormat("int a = std::is_integral<int>{} + 0;");
7131 
7132   verifyFormat("int foo(int i) { return fo1{}(i); }");
7133   verifyFormat("int foo(int i) { return fo1{}(i); }");
7134   verifyFormat("auto i = decltype(x){};");
7135   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
7136   verifyFormat("Node n{1, Node{1000}, //\n"
7137                "       2};");
7138   verifyFormat("Aaaa aaaaaaa{\n"
7139                "    {\n"
7140                "        aaaa,\n"
7141                "    },\n"
7142                "};");
7143   verifyFormat("class C : public D {\n"
7144                "  SomeClass SC{2};\n"
7145                "};");
7146   verifyFormat("class C : public A {\n"
7147                "  class D : public B {\n"
7148                "    void f() { int i{2}; }\n"
7149                "  };\n"
7150                "};");
7151   verifyFormat("#define A {a, a},");
7152 
7153   // Avoid breaking between equal sign and opening brace
7154   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
7155   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
7156   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
7157                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
7158                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
7159                "     {\"ccccccccccccccccccccc\", 2}};",
7160                AvoidBreakingFirstArgument);
7161 
7162   // Binpacking only if there is no trailing comma
7163   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
7164                "                      cccccccccc, dddddddddd};",
7165 			   getLLVMStyleWithColumns(50));
7166   verifyFormat("const Aaaaaa aaaaa = {\n"
7167                "    aaaaaaaaaaa,\n"
7168                "    bbbbbbbbbbb,\n"
7169                "    ccccccccccc,\n"
7170                "    ddddddddddd,\n"
7171                "};", getLLVMStyleWithColumns(50));
7172 
7173   // Cases where distinguising braced lists and blocks is hard.
7174   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
7175   verifyFormat("void f() {\n"
7176                "  return; // comment\n"
7177                "}\n"
7178                "SomeType t;");
7179   verifyFormat("void f() {\n"
7180                "  if (a) {\n"
7181                "    f();\n"
7182                "  }\n"
7183                "}\n"
7184                "SomeType t;");
7185 
7186   // In combination with BinPackArguments = false.
7187   FormatStyle NoBinPacking = getLLVMStyle();
7188   NoBinPacking.BinPackArguments = false;
7189   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
7190                "                      bbbbb,\n"
7191                "                      ccccc,\n"
7192                "                      ddddd,\n"
7193                "                      eeeee,\n"
7194                "                      ffffff,\n"
7195                "                      ggggg,\n"
7196                "                      hhhhhh,\n"
7197                "                      iiiiii,\n"
7198                "                      jjjjjj,\n"
7199                "                      kkkkkk};",
7200                NoBinPacking);
7201   verifyFormat("const Aaaaaa aaaaa = {\n"
7202                "    aaaaa,\n"
7203                "    bbbbb,\n"
7204                "    ccccc,\n"
7205                "    ddddd,\n"
7206                "    eeeee,\n"
7207                "    ffffff,\n"
7208                "    ggggg,\n"
7209                "    hhhhhh,\n"
7210                "    iiiiii,\n"
7211                "    jjjjjj,\n"
7212                "    kkkkkk,\n"
7213                "};",
7214                NoBinPacking);
7215   verifyFormat(
7216       "const Aaaaaa aaaaa = {\n"
7217       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
7218       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
7219       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
7220       "};",
7221       NoBinPacking);
7222 
7223   // FIXME: The alignment of these trailing comments might be bad. Then again,
7224   // this might be utterly useless in real code.
7225   verifyFormat("Constructor::Constructor()\n"
7226                "    : some_value{         //\n"
7227                "                 aaaaaaa, //\n"
7228                "                 bbbbbbb} {}");
7229 
7230   // In braced lists, the first comment is always assumed to belong to the
7231   // first element. Thus, it can be moved to the next or previous line as
7232   // appropriate.
7233   EXPECT_EQ("function({// First element:\n"
7234             "          1,\n"
7235             "          // Second element:\n"
7236             "          2});",
7237             format("function({\n"
7238                    "    // First element:\n"
7239                    "    1,\n"
7240                    "    // Second element:\n"
7241                    "    2});"));
7242   EXPECT_EQ("std::vector<int> MyNumbers{\n"
7243             "    // First element:\n"
7244             "    1,\n"
7245             "    // Second element:\n"
7246             "    2};",
7247             format("std::vector<int> MyNumbers{// First element:\n"
7248                    "                           1,\n"
7249                    "                           // Second element:\n"
7250                    "                           2};",
7251                    getLLVMStyleWithColumns(30)));
7252   // A trailing comma should still lead to an enforced line break and no
7253   // binpacking.
7254   EXPECT_EQ("vector<int> SomeVector = {\n"
7255             "    // aaa\n"
7256             "    1,\n"
7257             "    2,\n"
7258             "};",
7259             format("vector<int> SomeVector = { // aaa\n"
7260                    "    1, 2, };"));
7261 
7262   FormatStyle ExtraSpaces = getLLVMStyle();
7263   ExtraSpaces.Cpp11BracedListStyle = false;
7264   ExtraSpaces.ColumnLimit = 75;
7265   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
7266   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
7267   verifyFormat("f({ 1, 2 });", ExtraSpaces);
7268   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
7269   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
7270   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
7271   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
7272   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
7273   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
7274   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
7275   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
7276   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
7277   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
7278   verifyFormat("class Class {\n"
7279                "  T member = { arg1, arg2 };\n"
7280                "};",
7281                ExtraSpaces);
7282   verifyFormat(
7283       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7284       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7285       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7286       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7287       ExtraSpaces);
7288   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7289   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7290                ExtraSpaces);
7291   verifyFormat(
7292       "someFunction(OtherParam,\n"
7293       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7294       "                         param1, param2,\n"
7295       "                         // comment 2\n"
7296       "                         param3, param4 });",
7297       ExtraSpaces);
7298   verifyFormat(
7299       "std::this_thread::sleep_for(\n"
7300       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7301       ExtraSpaces);
7302   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7303                "    aaaaaaa,\n"
7304                "    aaaaaaaaaa,\n"
7305                "    aaaaa,\n"
7306                "    aaaaaaaaaaaaaaa,\n"
7307                "    aaa,\n"
7308                "    aaaaaaaaaa,\n"
7309                "    a,\n"
7310                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7311                "    aaaaaaaaaaaa,\n"
7312                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7313                "    aaaaaaa,\n"
7314                "    a};");
7315   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7316   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7317   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7318 
7319   // Avoid breaking between initializer/equal sign and opening brace
7320   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7321   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7322                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7323                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7324                "  { \"ccccccccccccccccccccc\", 2 }\n"
7325                "};",
7326                ExtraSpaces);
7327   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7328                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7329                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7330                "  { \"ccccccccccccccccccccc\", 2 }\n"
7331                "};",
7332                ExtraSpaces);
7333 
7334   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7335   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7336   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7337   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7338 }
7339 
7340 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7341   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7342                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7343                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7344                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7345                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7346                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7347   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7348                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7349                "                 1, 22, 333, 4444, 55555, //\n"
7350                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7351                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7352   verifyFormat(
7353       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7354       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7355       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7356       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7357       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7358       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7359       "                 7777777};");
7360   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7361                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7362                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7363   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7364                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7365                "    // Separating comment.\n"
7366                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7367   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7368                "    // Leading comment\n"
7369                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7370                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7371   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7372                "                 1, 1, 1, 1};",
7373                getLLVMStyleWithColumns(39));
7374   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7375                "                 1, 1, 1, 1};",
7376                getLLVMStyleWithColumns(38));
7377   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7378                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7379                getLLVMStyleWithColumns(43));
7380   verifyFormat(
7381       "static unsigned SomeValues[10][3] = {\n"
7382       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7383       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7384   verifyFormat("static auto fields = new vector<string>{\n"
7385                "    \"aaaaaaaaaaaaa\",\n"
7386                "    \"aaaaaaaaaaaaa\",\n"
7387                "    \"aaaaaaaaaaaa\",\n"
7388                "    \"aaaaaaaaaaaaaa\",\n"
7389                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7390                "    \"aaaaaaaaaaaa\",\n"
7391                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7392                "};");
7393   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7394   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7395                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7396                "                 3, cccccccccccccccccccccc};",
7397                getLLVMStyleWithColumns(60));
7398 
7399   // Trailing commas.
7400   verifyFormat("vector<int> x = {\n"
7401                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7402                "};",
7403                getLLVMStyleWithColumns(39));
7404   verifyFormat("vector<int> x = {\n"
7405                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7406                "};",
7407                getLLVMStyleWithColumns(39));
7408   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7409                "                 1, 1, 1, 1,\n"
7410                "                 /**/ /**/};",
7411                getLLVMStyleWithColumns(39));
7412 
7413   // Trailing comment in the first line.
7414   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7415                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7416                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7417                "    11111111,   22222222,   333333333,   44444444};");
7418   // Trailing comment in the last line.
7419   verifyFormat("int aaaaa[] = {\n"
7420                "    1, 2, 3, // comment\n"
7421                "    4, 5, 6  // comment\n"
7422                "};");
7423 
7424   // With nested lists, we should either format one item per line or all nested
7425   // lists one on line.
7426   // FIXME: For some nested lists, we can do better.
7427   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7428                "        {aaaaaaaaaaaaaaaaaaa},\n"
7429                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7430                "        {aaaaaaaaaaaaaaaaa}};",
7431                getLLVMStyleWithColumns(60));
7432   verifyFormat(
7433       "SomeStruct my_struct_array = {\n"
7434       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7435       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7436       "    {aaa, aaa},\n"
7437       "    {aaa, aaa},\n"
7438       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7439       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7440       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7441 
7442   // No column layout should be used here.
7443   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7444                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7445 
7446   verifyNoCrash("a<,");
7447 
7448   // No braced initializer here.
7449   verifyFormat("void f() {\n"
7450                "  struct Dummy {};\n"
7451                "  f(v);\n"
7452                "}");
7453 
7454   // Long lists should be formatted in columns even if they are nested.
7455   verifyFormat(
7456       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7457       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7458       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7459       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7460       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7461       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7462 
7463   // Allow "single-column" layout even if that violates the column limit. There
7464   // isn't going to be a better way.
7465   verifyFormat("std::vector<int> a = {\n"
7466                "    aaaaaaaa,\n"
7467                "    aaaaaaaa,\n"
7468                "    aaaaaaaa,\n"
7469                "    aaaaaaaa,\n"
7470                "    aaaaaaaaaa,\n"
7471                "    aaaaaaaa,\n"
7472                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7473                getLLVMStyleWithColumns(30));
7474   verifyFormat("vector<int> aaaa = {\n"
7475                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7476                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7477                "    aaaaaa.aaaaaaa,\n"
7478                "    aaaaaa.aaaaaaa,\n"
7479                "    aaaaaa.aaaaaaa,\n"
7480                "    aaaaaa.aaaaaaa,\n"
7481                "};");
7482 
7483   // Don't create hanging lists.
7484   verifyFormat("someFunction(Param, {List1, List2,\n"
7485                "                     List3});",
7486                getLLVMStyleWithColumns(35));
7487   verifyFormat("someFunction(Param, Param,\n"
7488                "             {List1, List2,\n"
7489                "              List3});",
7490                getLLVMStyleWithColumns(35));
7491   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7492                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7493 }
7494 
7495 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7496   FormatStyle DoNotMerge = getLLVMStyle();
7497   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7498 
7499   verifyFormat("void f() { return 42; }");
7500   verifyFormat("void f() {\n"
7501                "  return 42;\n"
7502                "}",
7503                DoNotMerge);
7504   verifyFormat("void f() {\n"
7505                "  // Comment\n"
7506                "}");
7507   verifyFormat("{\n"
7508                "#error {\n"
7509                "  int a;\n"
7510                "}");
7511   verifyFormat("{\n"
7512                "  int a;\n"
7513                "#error {\n"
7514                "}");
7515   verifyFormat("void f() {} // comment");
7516   verifyFormat("void f() { int a; } // comment");
7517   verifyFormat("void f() {\n"
7518                "} // comment",
7519                DoNotMerge);
7520   verifyFormat("void f() {\n"
7521                "  int a;\n"
7522                "} // comment",
7523                DoNotMerge);
7524   verifyFormat("void f() {\n"
7525                "} // comment",
7526                getLLVMStyleWithColumns(15));
7527 
7528   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7529   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7530 
7531   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7532   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7533   verifyFormat("class C {\n"
7534                "  C()\n"
7535                "      : iiiiiiii(nullptr),\n"
7536                "        kkkkkkk(nullptr),\n"
7537                "        mmmmmmm(nullptr),\n"
7538                "        nnnnnnn(nullptr) {}\n"
7539                "};",
7540                getGoogleStyle());
7541 
7542   FormatStyle NoColumnLimit = getLLVMStyle();
7543   NoColumnLimit.ColumnLimit = 0;
7544   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7545   EXPECT_EQ("class C {\n"
7546             "  A() : b(0) {}\n"
7547             "};",
7548             format("class C{A():b(0){}};", NoColumnLimit));
7549   EXPECT_EQ("A()\n"
7550             "    : b(0) {\n"
7551             "}",
7552             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7553 
7554   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7555   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7556       FormatStyle::SFS_None;
7557   EXPECT_EQ("A()\n"
7558             "    : b(0) {\n"
7559             "}",
7560             format("A():b(0){}", DoNotMergeNoColumnLimit));
7561   EXPECT_EQ("A()\n"
7562             "    : b(0) {\n"
7563             "}",
7564             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7565 
7566   verifyFormat("#define A          \\\n"
7567                "  void f() {       \\\n"
7568                "    int i;         \\\n"
7569                "  }",
7570                getLLVMStyleWithColumns(20));
7571   verifyFormat("#define A           \\\n"
7572                "  void f() { int i; }",
7573                getLLVMStyleWithColumns(21));
7574   verifyFormat("#define A            \\\n"
7575                "  void f() {         \\\n"
7576                "    int i;           \\\n"
7577                "  }                  \\\n"
7578                "  int j;",
7579                getLLVMStyleWithColumns(22));
7580   verifyFormat("#define A             \\\n"
7581                "  void f() { int i; } \\\n"
7582                "  int j;",
7583                getLLVMStyleWithColumns(23));
7584 }
7585 
7586 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7587   FormatStyle MergeEmptyOnly = getLLVMStyle();
7588   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7589   verifyFormat("class C {\n"
7590                "  int f() {}\n"
7591                "};",
7592                MergeEmptyOnly);
7593   verifyFormat("class C {\n"
7594                "  int f() {\n"
7595                "    return 42;\n"
7596                "  }\n"
7597                "};",
7598                MergeEmptyOnly);
7599   verifyFormat("int f() {}", MergeEmptyOnly);
7600   verifyFormat("int f() {\n"
7601                "  return 42;\n"
7602                "}",
7603                MergeEmptyOnly);
7604 
7605   // Also verify behavior when BraceWrapping.AfterFunction = true
7606   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7607   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7608   verifyFormat("int f() {}", MergeEmptyOnly);
7609   verifyFormat("class C {\n"
7610                "  int f() {}\n"
7611                "};",
7612                MergeEmptyOnly);
7613 }
7614 
7615 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7616   FormatStyle MergeInlineOnly = getLLVMStyle();
7617   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7618   verifyFormat("class C {\n"
7619                "  int f() { return 42; }\n"
7620                "};",
7621                MergeInlineOnly);
7622   verifyFormat("int f() {\n"
7623                "  return 42;\n"
7624                "}",
7625                MergeInlineOnly);
7626 
7627   // SFS_Inline implies SFS_Empty
7628   verifyFormat("class C {\n"
7629                "  int f() {}\n"
7630                "};",
7631                MergeInlineOnly);
7632   verifyFormat("int f() {}", MergeInlineOnly);
7633 
7634   // Also verify behavior when BraceWrapping.AfterFunction = true
7635   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7636   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7637   verifyFormat("class C {\n"
7638                "  int f() { return 42; }\n"
7639                "};",
7640                MergeInlineOnly);
7641   verifyFormat("int f()\n"
7642                "{\n"
7643                "  return 42;\n"
7644                "}",
7645                MergeInlineOnly);
7646 
7647   // SFS_Inline implies SFS_Empty
7648   verifyFormat("int f() {}", MergeInlineOnly);
7649   verifyFormat("class C {\n"
7650                "  int f() {}\n"
7651                "};",
7652                MergeInlineOnly);
7653 }
7654 
7655 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7656   FormatStyle MergeInlineOnly = getLLVMStyle();
7657   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7658       FormatStyle::SFS_InlineOnly;
7659   verifyFormat("class C {\n"
7660                "  int f() { return 42; }\n"
7661                "};",
7662                MergeInlineOnly);
7663   verifyFormat("int f() {\n"
7664                "  return 42;\n"
7665                "}",
7666                MergeInlineOnly);
7667 
7668   // SFS_InlineOnly does not imply SFS_Empty
7669   verifyFormat("class C {\n"
7670                "  int f() {}\n"
7671                "};",
7672                MergeInlineOnly);
7673   verifyFormat("int f() {\n"
7674                "}",
7675                MergeInlineOnly);
7676 
7677   // Also verify behavior when BraceWrapping.AfterFunction = true
7678   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7679   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7680   verifyFormat("class C {\n"
7681                "  int f() { return 42; }\n"
7682                "};",
7683                MergeInlineOnly);
7684   verifyFormat("int f()\n"
7685                "{\n"
7686                "  return 42;\n"
7687                "}",
7688                MergeInlineOnly);
7689 
7690   // SFS_InlineOnly does not imply SFS_Empty
7691   verifyFormat("int f()\n"
7692                "{\n"
7693                "}",
7694                MergeInlineOnly);
7695   verifyFormat("class C {\n"
7696                "  int f() {}\n"
7697                "};",
7698                MergeInlineOnly);
7699 }
7700 
7701 TEST_F(FormatTest, SplitEmptyFunction) {
7702   FormatStyle Style = getLLVMStyle();
7703   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7704   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7705   Style.BraceWrapping.AfterFunction = true;
7706   Style.BraceWrapping.SplitEmptyFunction = false;
7707   Style.ColumnLimit = 40;
7708 
7709   verifyFormat("int f()\n"
7710                "{}",
7711                Style);
7712   verifyFormat("int f()\n"
7713                "{\n"
7714                "  return 42;\n"
7715                "}",
7716                Style);
7717   verifyFormat("int f()\n"
7718                "{\n"
7719                "  // some comment\n"
7720                "}",
7721                Style);
7722 
7723   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7724   verifyFormat("int f() {}", Style);
7725   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7726                "{}",
7727                Style);
7728   verifyFormat("int f()\n"
7729                "{\n"
7730                "  return 0;\n"
7731                "}",
7732                Style);
7733 
7734   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7735   verifyFormat("class Foo {\n"
7736                "  int f() {}\n"
7737                "};\n",
7738                Style);
7739   verifyFormat("class Foo {\n"
7740                "  int f() { return 0; }\n"
7741                "};\n",
7742                Style);
7743   verifyFormat("class Foo {\n"
7744                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7745                "  {}\n"
7746                "};\n",
7747                Style);
7748   verifyFormat("class Foo {\n"
7749                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7750                "  {\n"
7751                "    return 0;\n"
7752                "  }\n"
7753                "};\n",
7754                Style);
7755 
7756   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7757   verifyFormat("int f() {}", Style);
7758   verifyFormat("int f() { return 0; }", Style);
7759   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7760                "{}",
7761                Style);
7762   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7763                "{\n"
7764                "  return 0;\n"
7765                "}",
7766                Style);
7767 }
7768 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7769   FormatStyle Style = getLLVMStyle();
7770   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7771   verifyFormat("#ifdef A\n"
7772                "int f() {}\n"
7773                "#else\n"
7774                "int g() {}\n"
7775                "#endif",
7776                Style);
7777 }
7778 
7779 TEST_F(FormatTest, SplitEmptyClass) {
7780   FormatStyle Style = getLLVMStyle();
7781   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7782   Style.BraceWrapping.AfterClass = true;
7783   Style.BraceWrapping.SplitEmptyRecord = false;
7784 
7785   verifyFormat("class Foo\n"
7786                "{};",
7787                Style);
7788   verifyFormat("/* something */ class Foo\n"
7789                "{};",
7790                Style);
7791   verifyFormat("template <typename X> class Foo\n"
7792                "{};",
7793                Style);
7794   verifyFormat("class Foo\n"
7795                "{\n"
7796                "  Foo();\n"
7797                "};",
7798                Style);
7799   verifyFormat("typedef class Foo\n"
7800                "{\n"
7801                "} Foo_t;",
7802                Style);
7803 }
7804 
7805 TEST_F(FormatTest, SplitEmptyStruct) {
7806   FormatStyle Style = getLLVMStyle();
7807   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7808   Style.BraceWrapping.AfterStruct = true;
7809   Style.BraceWrapping.SplitEmptyRecord = false;
7810 
7811   verifyFormat("struct Foo\n"
7812                "{};",
7813                Style);
7814   verifyFormat("/* something */ struct Foo\n"
7815                "{};",
7816                Style);
7817   verifyFormat("template <typename X> struct Foo\n"
7818                "{};",
7819                Style);
7820   verifyFormat("struct Foo\n"
7821                "{\n"
7822                "  Foo();\n"
7823                "};",
7824                Style);
7825   verifyFormat("typedef struct Foo\n"
7826                "{\n"
7827                "} Foo_t;",
7828                Style);
7829   //typedef struct Bar {} Bar_t;
7830 }
7831 
7832 TEST_F(FormatTest, SplitEmptyUnion) {
7833   FormatStyle Style = getLLVMStyle();
7834   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7835   Style.BraceWrapping.AfterUnion = true;
7836   Style.BraceWrapping.SplitEmptyRecord = false;
7837 
7838   verifyFormat("union Foo\n"
7839                "{};",
7840                Style);
7841   verifyFormat("/* something */ union Foo\n"
7842                "{};",
7843                Style);
7844   verifyFormat("union Foo\n"
7845                "{\n"
7846                "  A,\n"
7847                "};",
7848                Style);
7849   verifyFormat("typedef union Foo\n"
7850                "{\n"
7851                "} Foo_t;",
7852                Style);
7853 }
7854 
7855 TEST_F(FormatTest, SplitEmptyNamespace) {
7856   FormatStyle Style = getLLVMStyle();
7857   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7858   Style.BraceWrapping.AfterNamespace = true;
7859   Style.BraceWrapping.SplitEmptyNamespace = false;
7860 
7861   verifyFormat("namespace Foo\n"
7862                "{};",
7863                Style);
7864   verifyFormat("/* something */ namespace Foo\n"
7865                "{};",
7866                Style);
7867   verifyFormat("inline namespace Foo\n"
7868                "{};",
7869                Style);
7870   verifyFormat("/* something */ inline namespace Foo\n"
7871                "{};",
7872                Style);
7873   verifyFormat("export namespace Foo\n"
7874                "{};",
7875                Style);
7876   verifyFormat("namespace Foo\n"
7877                "{\n"
7878                "void Bar();\n"
7879                "};",
7880                Style);
7881 }
7882 
7883 TEST_F(FormatTest, NeverMergeShortRecords) {
7884   FormatStyle Style = getLLVMStyle();
7885 
7886   verifyFormat("class Foo {\n"
7887                "  Foo();\n"
7888                "};",
7889                Style);
7890   verifyFormat("typedef class Foo {\n"
7891                "  Foo();\n"
7892                "} Foo_t;",
7893                Style);
7894   verifyFormat("struct Foo {\n"
7895                "  Foo();\n"
7896                "};",
7897                Style);
7898   verifyFormat("typedef struct Foo {\n"
7899                "  Foo();\n"
7900                "} Foo_t;",
7901                Style);
7902   verifyFormat("union Foo {\n"
7903                "  A,\n"
7904                "};",
7905                Style);
7906   verifyFormat("typedef union Foo {\n"
7907                "  A,\n"
7908                "} Foo_t;",
7909                Style);
7910   verifyFormat("namespace Foo {\n"
7911                "void Bar();\n"
7912                "};",
7913                Style);
7914 
7915   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7916   Style.BraceWrapping.AfterClass = true;
7917   Style.BraceWrapping.AfterStruct = true;
7918   Style.BraceWrapping.AfterUnion = true;
7919   Style.BraceWrapping.AfterNamespace = true;
7920   verifyFormat("class Foo\n"
7921                "{\n"
7922                "  Foo();\n"
7923                "};",
7924                Style);
7925   verifyFormat("typedef class Foo\n"
7926                "{\n"
7927                "  Foo();\n"
7928                "} Foo_t;",
7929                Style);
7930   verifyFormat("struct Foo\n"
7931                "{\n"
7932                "  Foo();\n"
7933                "};",
7934                Style);
7935   verifyFormat("typedef struct Foo\n"
7936                "{\n"
7937                "  Foo();\n"
7938                "} Foo_t;",
7939                Style);
7940   verifyFormat("union Foo\n"
7941                "{\n"
7942                "  A,\n"
7943                "};",
7944                Style);
7945   verifyFormat("typedef union Foo\n"
7946                "{\n"
7947                "  A,\n"
7948                "} Foo_t;",
7949                Style);
7950   verifyFormat("namespace Foo\n"
7951                "{\n"
7952                "void Bar();\n"
7953                "};",
7954                Style);
7955 }
7956 
7957 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7958   // Elaborate type variable declarations.
7959   verifyFormat("struct foo a = {bar};\nint n;");
7960   verifyFormat("class foo a = {bar};\nint n;");
7961   verifyFormat("union foo a = {bar};\nint n;");
7962 
7963   // Elaborate types inside function definitions.
7964   verifyFormat("struct foo f() {}\nint n;");
7965   verifyFormat("class foo f() {}\nint n;");
7966   verifyFormat("union foo f() {}\nint n;");
7967 
7968   // Templates.
7969   verifyFormat("template <class X> void f() {}\nint n;");
7970   verifyFormat("template <struct X> void f() {}\nint n;");
7971   verifyFormat("template <union X> void f() {}\nint n;");
7972 
7973   // Actual definitions...
7974   verifyFormat("struct {\n} n;");
7975   verifyFormat(
7976       "template <template <class T, class Y>, class Z> class X {\n} n;");
7977   verifyFormat("union Z {\n  int n;\n} x;");
7978   verifyFormat("class MACRO Z {\n} n;");
7979   verifyFormat("class MACRO(X) Z {\n} n;");
7980   verifyFormat("class __attribute__(X) Z {\n} n;");
7981   verifyFormat("class __declspec(X) Z {\n} n;");
7982   verifyFormat("class A##B##C {\n} n;");
7983   verifyFormat("class alignas(16) Z {\n} n;");
7984   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7985   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7986 
7987   // Redefinition from nested context:
7988   verifyFormat("class A::B::C {\n} n;");
7989 
7990   // Template definitions.
7991   verifyFormat(
7992       "template <typename F>\n"
7993       "Matcher(const Matcher<F> &Other,\n"
7994       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7995       "                             !is_same<F, T>::value>::type * = 0)\n"
7996       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7997 
7998   // FIXME: This is still incorrectly handled at the formatter side.
7999   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
8000   verifyFormat("int i = SomeFunction(a<b, a> b);");
8001 
8002   // FIXME:
8003   // This now gets parsed incorrectly as class definition.
8004   // verifyFormat("class A<int> f() {\n}\nint n;");
8005 
8006   // Elaborate types where incorrectly parsing the structural element would
8007   // break the indent.
8008   verifyFormat("if (true)\n"
8009                "  class X x;\n"
8010                "else\n"
8011                "  f();\n");
8012 
8013   // This is simply incomplete. Formatting is not important, but must not crash.
8014   verifyFormat("class A:");
8015 }
8016 
8017 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
8018   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
8019             format("#error Leave     all         white!!!!! space* alone!\n"));
8020   EXPECT_EQ(
8021       "#warning Leave     all         white!!!!! space* alone!\n",
8022       format("#warning Leave     all         white!!!!! space* alone!\n"));
8023   EXPECT_EQ("#error 1", format("  #  error   1"));
8024   EXPECT_EQ("#warning 1", format("  #  warning 1"));
8025 }
8026 
8027 TEST_F(FormatTest, FormatHashIfExpressions) {
8028   verifyFormat("#if AAAA && BBBB");
8029   verifyFormat("#if (AAAA && BBBB)");
8030   verifyFormat("#elif (AAAA && BBBB)");
8031   // FIXME: Come up with a better indentation for #elif.
8032   verifyFormat(
8033       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
8034       "    defined(BBBBBBBB)\n"
8035       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
8036       "    defined(BBBBBBBB)\n"
8037       "#endif",
8038       getLLVMStyleWithColumns(65));
8039 }
8040 
8041 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
8042   FormatStyle AllowsMergedIf = getGoogleStyle();
8043   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
8044       FormatStyle::SIS_WithoutElse;
8045   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
8046   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
8047   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
8048   EXPECT_EQ("if (true) return 42;",
8049             format("if (true)\nreturn 42;", AllowsMergedIf));
8050   FormatStyle ShortMergedIf = AllowsMergedIf;
8051   ShortMergedIf.ColumnLimit = 25;
8052   verifyFormat("#define A \\\n"
8053                "  if (true) return 42;",
8054                ShortMergedIf);
8055   verifyFormat("#define A \\\n"
8056                "  f();    \\\n"
8057                "  if (true)\n"
8058                "#define B",
8059                ShortMergedIf);
8060   verifyFormat("#define A \\\n"
8061                "  f();    \\\n"
8062                "  if (true)\n"
8063                "g();",
8064                ShortMergedIf);
8065   verifyFormat("{\n"
8066                "#ifdef A\n"
8067                "  // Comment\n"
8068                "  if (true) continue;\n"
8069                "#endif\n"
8070                "  // Comment\n"
8071                "  if (true) continue;\n"
8072                "}",
8073                ShortMergedIf);
8074   ShortMergedIf.ColumnLimit = 33;
8075   verifyFormat("#define A \\\n"
8076                "  if constexpr (true) return 42;",
8077                ShortMergedIf);
8078   ShortMergedIf.ColumnLimit = 29;
8079   verifyFormat("#define A                   \\\n"
8080                "  if (aaaaaaaaaa) return 1; \\\n"
8081                "  return 2;",
8082                ShortMergedIf);
8083   ShortMergedIf.ColumnLimit = 28;
8084   verifyFormat("#define A         \\\n"
8085                "  if (aaaaaaaaaa) \\\n"
8086                "    return 1;     \\\n"
8087                "  return 2;",
8088                ShortMergedIf);
8089   verifyFormat("#define A                \\\n"
8090                "  if constexpr (aaaaaaa) \\\n"
8091                "    return 1;            \\\n"
8092                "  return 2;",
8093                ShortMergedIf);
8094 }
8095 
8096 TEST_F(FormatTest, FormatStarDependingOnContext) {
8097   verifyFormat("void f(int *a);");
8098   verifyFormat("void f() { f(fint * b); }");
8099   verifyFormat("class A {\n  void f(int *a);\n};");
8100   verifyFormat("class A {\n  int *a;\n};");
8101   verifyFormat("namespace a {\n"
8102                "namespace b {\n"
8103                "class A {\n"
8104                "  void f() {}\n"
8105                "  int *a;\n"
8106                "};\n"
8107                "} // namespace b\n"
8108                "} // namespace a");
8109 }
8110 
8111 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
8112   verifyFormat("while");
8113   verifyFormat("operator");
8114 }
8115 
8116 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
8117   // This code would be painfully slow to format if we didn't skip it.
8118   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
8119                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8120                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8121                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8122                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8123                    "A(1, 1)\n"
8124                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
8125                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8126                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8127                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8128                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8129                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8130                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8131                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8132                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8133                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
8134   // Deeply nested part is untouched, rest is formatted.
8135   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
8136             format(std::string("int    i;\n") + Code + "int    j;\n",
8137                    getLLVMStyle(), SC_ExpectIncomplete));
8138 }
8139 
8140 //===----------------------------------------------------------------------===//
8141 // Objective-C tests.
8142 //===----------------------------------------------------------------------===//
8143 
8144 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
8145   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
8146   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
8147             format("-(NSUInteger)indexOfObject:(id)anObject;"));
8148   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
8149   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
8150   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
8151             format("-(NSInteger)Method3:(id)anObject;"));
8152   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
8153             format("-(NSInteger)Method4:(id)anObject;"));
8154   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
8155             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
8156   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
8157             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
8158   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8159             "forAllCells:(BOOL)flag;",
8160             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8161                    "forAllCells:(BOOL)flag;"));
8162 
8163   // Very long objectiveC method declaration.
8164   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
8165                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
8166   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
8167                "                    inRange:(NSRange)range\n"
8168                "                   outRange:(NSRange)out_range\n"
8169                "                  outRange1:(NSRange)out_range1\n"
8170                "                  outRange2:(NSRange)out_range2\n"
8171                "                  outRange3:(NSRange)out_range3\n"
8172                "                  outRange4:(NSRange)out_range4\n"
8173                "                  outRange5:(NSRange)out_range5\n"
8174                "                  outRange6:(NSRange)out_range6\n"
8175                "                  outRange7:(NSRange)out_range7\n"
8176                "                  outRange8:(NSRange)out_range8\n"
8177                "                  outRange9:(NSRange)out_range9;");
8178 
8179   // When the function name has to be wrapped.
8180   FormatStyle Style = getLLVMStyle();
8181   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
8182   // and always indents instead.
8183   Style.IndentWrappedFunctionNames = false;
8184   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8185                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
8186                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
8187                "}",
8188                Style);
8189   Style.IndentWrappedFunctionNames = true;
8190   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8191                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
8192                "               anotherName:(NSString)dddddddddddddd {\n"
8193                "}",
8194                Style);
8195 
8196   verifyFormat("- (int)sum:(vector<int>)numbers;");
8197   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
8198   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
8199   // protocol lists (but not for template classes):
8200   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
8201 
8202   verifyFormat("- (int (*)())foo:(int (*)())f;");
8203   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
8204 
8205   // If there's no return type (very rare in practice!), LLVM and Google style
8206   // agree.
8207   verifyFormat("- foo;");
8208   verifyFormat("- foo:(int)f;");
8209   verifyGoogleFormat("- foo:(int)foo;");
8210 }
8211 
8212 
8213 TEST_F(FormatTest, BreaksStringLiterals) {
8214   EXPECT_EQ("\"some text \"\n"
8215             "\"other\";",
8216             format("\"some text other\";", getLLVMStyleWithColumns(12)));
8217   EXPECT_EQ("\"some text \"\n"
8218             "\"other\";",
8219             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
8220   EXPECT_EQ(
8221       "#define A  \\\n"
8222       "  \"some \"  \\\n"
8223       "  \"text \"  \\\n"
8224       "  \"other\";",
8225       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
8226   EXPECT_EQ(
8227       "#define A  \\\n"
8228       "  \"so \"    \\\n"
8229       "  \"text \"  \\\n"
8230       "  \"other\";",
8231       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
8232 
8233   EXPECT_EQ("\"some text\"",
8234             format("\"some text\"", getLLVMStyleWithColumns(1)));
8235   EXPECT_EQ("\"some text\"",
8236             format("\"some text\"", getLLVMStyleWithColumns(11)));
8237   EXPECT_EQ("\"some \"\n"
8238             "\"text\"",
8239             format("\"some text\"", getLLVMStyleWithColumns(10)));
8240   EXPECT_EQ("\"some \"\n"
8241             "\"text\"",
8242             format("\"some text\"", getLLVMStyleWithColumns(7)));
8243   EXPECT_EQ("\"some\"\n"
8244             "\" tex\"\n"
8245             "\"t\"",
8246             format("\"some text\"", getLLVMStyleWithColumns(6)));
8247   EXPECT_EQ("\"some\"\n"
8248             "\" tex\"\n"
8249             "\" and\"",
8250             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
8251   EXPECT_EQ("\"some\"\n"
8252             "\"/tex\"\n"
8253             "\"/and\"",
8254             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
8255 
8256   EXPECT_EQ("variable =\n"
8257             "    \"long string \"\n"
8258             "    \"literal\";",
8259             format("variable = \"long string literal\";",
8260                    getLLVMStyleWithColumns(20)));
8261 
8262   EXPECT_EQ("variable = f(\n"
8263             "    \"long string \"\n"
8264             "    \"literal\",\n"
8265             "    short,\n"
8266             "    loooooooooooooooooooong);",
8267             format("variable = f(\"long string literal\", short, "
8268                    "loooooooooooooooooooong);",
8269                    getLLVMStyleWithColumns(20)));
8270 
8271   EXPECT_EQ(
8272       "f(g(\"long string \"\n"
8273       "    \"literal\"),\n"
8274       "  b);",
8275       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
8276   EXPECT_EQ("f(g(\"long string \"\n"
8277             "    \"literal\",\n"
8278             "    a),\n"
8279             "  b);",
8280             format("f(g(\"long string literal\", a), b);",
8281                    getLLVMStyleWithColumns(20)));
8282   EXPECT_EQ(
8283       "f(\"one two\".split(\n"
8284       "    variable));",
8285       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8286   EXPECT_EQ("f(\"one two three four five six \"\n"
8287             "  \"seven\".split(\n"
8288             "      really_looooong_variable));",
8289             format("f(\"one two three four five six seven\"."
8290                    "split(really_looooong_variable));",
8291                    getLLVMStyleWithColumns(33)));
8292 
8293   EXPECT_EQ("f(\"some \"\n"
8294             "  \"text\",\n"
8295             "  other);",
8296             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8297 
8298   // Only break as a last resort.
8299   verifyFormat(
8300       "aaaaaaaaaaaaaaaaaaaa(\n"
8301       "    aaaaaaaaaaaaaaaaaaaa,\n"
8302       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8303 
8304   EXPECT_EQ("\"splitmea\"\n"
8305             "\"trandomp\"\n"
8306             "\"oint\"",
8307             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8308 
8309   EXPECT_EQ("\"split/\"\n"
8310             "\"pathat/\"\n"
8311             "\"slashes\"",
8312             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8313 
8314   EXPECT_EQ("\"split/\"\n"
8315             "\"pathat/\"\n"
8316             "\"slashes\"",
8317             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8318   EXPECT_EQ("\"split at \"\n"
8319             "\"spaces/at/\"\n"
8320             "\"slashes.at.any$\"\n"
8321             "\"non-alphanumeric%\"\n"
8322             "\"1111111111characte\"\n"
8323             "\"rs\"",
8324             format("\"split at "
8325                    "spaces/at/"
8326                    "slashes.at."
8327                    "any$non-"
8328                    "alphanumeric%"
8329                    "1111111111characte"
8330                    "rs\"",
8331                    getLLVMStyleWithColumns(20)));
8332 
8333   // Verify that splitting the strings understands
8334   // Style::AlwaysBreakBeforeMultilineStrings.
8335   EXPECT_EQ(
8336       "aaaaaaaaaaaa(\n"
8337       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8338       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8339       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8340              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8341              "aaaaaaaaaaaaaaaaaaaaaa\");",
8342              getGoogleStyle()));
8343   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8344             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8345             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8346                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8347                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8348                    getGoogleStyle()));
8349   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8350             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8351             format("llvm::outs() << "
8352                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8353                    "aaaaaaaaaaaaaaaaaaa\";"));
8354   EXPECT_EQ("ffff(\n"
8355             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8356             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8357             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8358                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8359                    getGoogleStyle()));
8360 
8361   FormatStyle Style = getLLVMStyleWithColumns(12);
8362   Style.BreakStringLiterals = false;
8363   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8364 
8365   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8366   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8367   EXPECT_EQ("#define A \\\n"
8368             "  \"some \" \\\n"
8369             "  \"text \" \\\n"
8370             "  \"other\";",
8371             format("#define A \"some text other\";", AlignLeft));
8372 }
8373 
8374 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8375   EXPECT_EQ("C a = \"some more \"\n"
8376             "      \"text\";",
8377             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8378 }
8379 
8380 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8381   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8382   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8383   EXPECT_EQ("int i = a(b());",
8384             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8385 }
8386 
8387 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8388   EXPECT_EQ(
8389       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8390       "(\n"
8391       "    \"x\t\");",
8392       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8393              "aaaaaaa("
8394              "\"x\t\");"));
8395 }
8396 
8397 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8398   EXPECT_EQ(
8399       "u8\"utf8 string \"\n"
8400       "u8\"literal\";",
8401       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8402   EXPECT_EQ(
8403       "u\"utf16 string \"\n"
8404       "u\"literal\";",
8405       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8406   EXPECT_EQ(
8407       "U\"utf32 string \"\n"
8408       "U\"literal\";",
8409       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8410   EXPECT_EQ("L\"wide string \"\n"
8411             "L\"literal\";",
8412             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8413   EXPECT_EQ("@\"NSString \"\n"
8414             "@\"literal\";",
8415             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8416   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8417 
8418   // This input makes clang-format try to split the incomplete unicode escape
8419   // sequence, which used to lead to a crasher.
8420   verifyNoCrash(
8421       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8422       getLLVMStyleWithColumns(60));
8423 }
8424 
8425 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8426   FormatStyle Style = getGoogleStyleWithColumns(15);
8427   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8428   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8429   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8430   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8431   EXPECT_EQ("u8R\"x(raw literal)x\";",
8432             format("u8R\"x(raw literal)x\";", Style));
8433 }
8434 
8435 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8436   FormatStyle Style = getLLVMStyleWithColumns(20);
8437   EXPECT_EQ(
8438       "_T(\"aaaaaaaaaaaaaa\")\n"
8439       "_T(\"aaaaaaaaaaaaaa\")\n"
8440       "_T(\"aaaaaaaaaaaa\")",
8441       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8442   EXPECT_EQ("f(x,\n"
8443             "  _T(\"aaaaaaaaaaaa\")\n"
8444             "  _T(\"aaa\"),\n"
8445             "  z);",
8446             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8447 
8448   // FIXME: Handle embedded spaces in one iteration.
8449   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8450   //            "_T(\"aaaaaaaaaaaaa\")\n"
8451   //            "_T(\"aaaaaaaaaaaaa\")\n"
8452   //            "_T(\"a\")",
8453   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8454   //                   getLLVMStyleWithColumns(20)));
8455   EXPECT_EQ(
8456       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8457       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8458   EXPECT_EQ("f(\n"
8459             "#if !TEST\n"
8460             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8461             "#endif\n"
8462             ");",
8463             format("f(\n"
8464                    "#if !TEST\n"
8465                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8466                    "#endif\n"
8467                    ");"));
8468   EXPECT_EQ("f(\n"
8469             "\n"
8470             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8471             format("f(\n"
8472                    "\n"
8473                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8474 }
8475 
8476 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8477   // In a function call with two operands, the second can be broken with no line
8478   // break before it.
8479   EXPECT_EQ("func(a, \"long long \"\n"
8480             "        \"long long\");",
8481             format("func(a, \"long long long long\");",
8482                    getLLVMStyleWithColumns(24)));
8483   // In a function call with three operands, the second must be broken with a
8484   // line break before it.
8485   EXPECT_EQ("func(a,\n"
8486             "     \"long long long \"\n"
8487             "     \"long\",\n"
8488             "     c);",
8489             format("func(a, \"long long long long\", c);",
8490                    getLLVMStyleWithColumns(24)));
8491   // In a function call with three operands, the third must be broken with a
8492   // line break before it.
8493   EXPECT_EQ("func(a, b,\n"
8494             "     \"long long long \"\n"
8495             "     \"long\");",
8496             format("func(a, b, \"long long long long\");",
8497                    getLLVMStyleWithColumns(24)));
8498   // In a function call with three operands, both the second and the third must
8499   // be broken with a line break before them.
8500   EXPECT_EQ("func(a,\n"
8501             "     \"long long long \"\n"
8502             "     \"long\",\n"
8503             "     \"long long long \"\n"
8504             "     \"long\");",
8505             format("func(a, \"long long long long\", \"long long long long\");",
8506                    getLLVMStyleWithColumns(24)));
8507   // In a chain of << with two operands, the second can be broken with no line
8508   // break before it.
8509   EXPECT_EQ("a << \"line line \"\n"
8510             "     \"line\";",
8511             format("a << \"line line line\";",
8512                    getLLVMStyleWithColumns(20)));
8513   // In a chain of << with three operands, the second can be broken with no line
8514   // break before it.
8515   EXPECT_EQ("abcde << \"line \"\n"
8516             "         \"line line\"\n"
8517             "      << c;",
8518             format("abcde << \"line line line\" << c;",
8519                    getLLVMStyleWithColumns(20)));
8520   // In a chain of << with three operands, the third must be broken with a line
8521   // break before it.
8522   EXPECT_EQ("a << b\n"
8523             "  << \"line line \"\n"
8524             "     \"line\";",
8525             format("a << b << \"line line line\";",
8526                    getLLVMStyleWithColumns(20)));
8527   // In a chain of << with three operands, the second can be broken with no line
8528   // break before it and the third must be broken with a line break before it.
8529   EXPECT_EQ("abcd << \"line line \"\n"
8530             "        \"line\"\n"
8531             "     << \"line line \"\n"
8532             "        \"line\";",
8533             format("abcd << \"line line line\" << \"line line line\";",
8534                    getLLVMStyleWithColumns(20)));
8535   // In a chain of binary operators with two operands, the second can be broken
8536   // with no line break before it.
8537   EXPECT_EQ("abcd + \"line line \"\n"
8538             "       \"line line\";",
8539             format("abcd + \"line line line line\";",
8540                    getLLVMStyleWithColumns(20)));
8541   // In a chain of binary operators with three operands, the second must be
8542   // broken with a line break before it.
8543   EXPECT_EQ("abcd +\n"
8544             "    \"line line \"\n"
8545             "    \"line line\" +\n"
8546             "    e;",
8547             format("abcd + \"line line line line\" + e;",
8548                    getLLVMStyleWithColumns(20)));
8549   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8550   // the first must be broken with a line break before it.
8551   FormatStyle Style = getLLVMStyleWithColumns(25);
8552   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8553   EXPECT_EQ("someFunction(\n"
8554             "    \"long long long \"\n"
8555             "    \"long\",\n"
8556             "    a);",
8557             format("someFunction(\"long long long long\", a);", Style));
8558 }
8559 
8560 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8561   EXPECT_EQ(
8562       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8563       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8564       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8565       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8566              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8567              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8568 }
8569 
8570 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8571   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8572             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8573   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8574             "multiline raw string literal xxxxxxxxxxxxxx\n"
8575             ")x\",\n"
8576             "              a),\n"
8577             "            b);",
8578             format("fffffffffff(g(R\"x(\n"
8579                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8580                    ")x\", a), b);",
8581                    getGoogleStyleWithColumns(20)));
8582   EXPECT_EQ("fffffffffff(\n"
8583             "    g(R\"x(qqq\n"
8584             "multiline raw string literal xxxxxxxxxxxxxx\n"
8585             ")x\",\n"
8586             "      a),\n"
8587             "    b);",
8588             format("fffffffffff(g(R\"x(qqq\n"
8589                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8590                    ")x\", a), b);",
8591                    getGoogleStyleWithColumns(20)));
8592 
8593   EXPECT_EQ("fffffffffff(R\"x(\n"
8594             "multiline raw string literal xxxxxxxxxxxxxx\n"
8595             ")x\");",
8596             format("fffffffffff(R\"x(\n"
8597                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8598                    ")x\");",
8599                    getGoogleStyleWithColumns(20)));
8600   EXPECT_EQ("fffffffffff(R\"x(\n"
8601             "multiline raw string literal xxxxxxxxxxxxxx\n"
8602             ")x\" + bbbbbb);",
8603             format("fffffffffff(R\"x(\n"
8604                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8605                    ")x\" +   bbbbbb);",
8606                    getGoogleStyleWithColumns(20)));
8607   EXPECT_EQ("fffffffffff(\n"
8608             "    R\"x(\n"
8609             "multiline raw string literal xxxxxxxxxxxxxx\n"
8610             ")x\" +\n"
8611             "    bbbbbb);",
8612             format("fffffffffff(\n"
8613                    " R\"x(\n"
8614                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8615                    ")x\" + bbbbbb);",
8616                    getGoogleStyleWithColumns(20)));
8617   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8618             format("fffffffffff(\n"
8619                    " R\"(single line raw string)\" + bbbbbb);"));
8620 }
8621 
8622 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8623   verifyFormat("string a = \"unterminated;");
8624   EXPECT_EQ("function(\"unterminated,\n"
8625             "         OtherParameter);",
8626             format("function(  \"unterminated,\n"
8627                    "    OtherParameter);"));
8628 }
8629 
8630 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8631   FormatStyle Style = getLLVMStyle();
8632   Style.Standard = FormatStyle::LS_Cpp03;
8633   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8634             format("#define x(_a) printf(\"foo\"_a);", Style));
8635 }
8636 
8637 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8638 
8639 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8640   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8641             "             \"ddeeefff\");",
8642             format("someFunction(\"aaabbbcccdddeeefff\");",
8643                    getLLVMStyleWithColumns(25)));
8644   EXPECT_EQ("someFunction1234567890(\n"
8645             "    \"aaabbbcccdddeeefff\");",
8646             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8647                    getLLVMStyleWithColumns(26)));
8648   EXPECT_EQ("someFunction1234567890(\n"
8649             "    \"aaabbbcccdddeeeff\"\n"
8650             "    \"f\");",
8651             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8652                    getLLVMStyleWithColumns(25)));
8653   EXPECT_EQ("someFunction1234567890(\n"
8654             "    \"aaabbbcccdddeeeff\"\n"
8655             "    \"f\");",
8656             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8657                    getLLVMStyleWithColumns(24)));
8658   EXPECT_EQ("someFunction(\n"
8659             "    \"aaabbbcc ddde \"\n"
8660             "    \"efff\");",
8661             format("someFunction(\"aaabbbcc ddde efff\");",
8662                    getLLVMStyleWithColumns(25)));
8663   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8664             "             \"ddeeefff\");",
8665             format("someFunction(\"aaabbbccc ddeeefff\");",
8666                    getLLVMStyleWithColumns(25)));
8667   EXPECT_EQ("someFunction1234567890(\n"
8668             "    \"aaabb \"\n"
8669             "    \"cccdddeeefff\");",
8670             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8671                    getLLVMStyleWithColumns(25)));
8672   EXPECT_EQ("#define A          \\\n"
8673             "  string s =       \\\n"
8674             "      \"123456789\"  \\\n"
8675             "      \"0\";         \\\n"
8676             "  int i;",
8677             format("#define A string s = \"1234567890\"; int i;",
8678                    getLLVMStyleWithColumns(20)));
8679   EXPECT_EQ("someFunction(\n"
8680             "    \"aaabbbcc \"\n"
8681             "    \"dddeeefff\");",
8682             format("someFunction(\"aaabbbcc dddeeefff\");",
8683                    getLLVMStyleWithColumns(25)));
8684 }
8685 
8686 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8687   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8688   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8689   EXPECT_EQ("\"test\"\n"
8690             "\"\\n\"",
8691             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8692   EXPECT_EQ("\"tes\\\\\"\n"
8693             "\"n\"",
8694             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8695   EXPECT_EQ("\"\\\\\\\\\"\n"
8696             "\"\\n\"",
8697             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8698   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8699   EXPECT_EQ("\"\\uff01\"\n"
8700             "\"test\"",
8701             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8702   EXPECT_EQ("\"\\Uff01ff02\"",
8703             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8704   EXPECT_EQ("\"\\x000000000001\"\n"
8705             "\"next\"",
8706             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8707   EXPECT_EQ("\"\\x000000000001next\"",
8708             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8709   EXPECT_EQ("\"\\x000000000001\"",
8710             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8711   EXPECT_EQ("\"test\"\n"
8712             "\"\\000000\"\n"
8713             "\"000001\"",
8714             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8715   EXPECT_EQ("\"test\\000\"\n"
8716             "\"00000000\"\n"
8717             "\"1\"",
8718             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8719 }
8720 
8721 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8722   verifyFormat("void f() {\n"
8723                "  return g() {}\n"
8724                "  void h() {}");
8725   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8726                "g();\n"
8727                "}");
8728 }
8729 
8730 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8731   verifyFormat(
8732       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8733 }
8734 
8735 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8736   verifyFormat("class X {\n"
8737                "  void f() {\n"
8738                "  }\n"
8739                "};",
8740                getLLVMStyleWithColumns(12));
8741 }
8742 
8743 TEST_F(FormatTest, ConfigurableIndentWidth) {
8744   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8745   EightIndent.IndentWidth = 8;
8746   EightIndent.ContinuationIndentWidth = 8;
8747   verifyFormat("void f() {\n"
8748                "        someFunction();\n"
8749                "        if (true) {\n"
8750                "                f();\n"
8751                "        }\n"
8752                "}",
8753                EightIndent);
8754   verifyFormat("class X {\n"
8755                "        void f() {\n"
8756                "        }\n"
8757                "};",
8758                EightIndent);
8759   verifyFormat("int x[] = {\n"
8760                "        call(),\n"
8761                "        call()};",
8762                EightIndent);
8763 }
8764 
8765 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8766   verifyFormat("double\n"
8767                "f();",
8768                getLLVMStyleWithColumns(8));
8769 }
8770 
8771 TEST_F(FormatTest, ConfigurableUseOfTab) {
8772   FormatStyle Tab = getLLVMStyleWithColumns(42);
8773   Tab.IndentWidth = 8;
8774   Tab.UseTab = FormatStyle::UT_Always;
8775   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8776 
8777   EXPECT_EQ("if (aaaaaaaa && // q\n"
8778             "    bb)\t\t// w\n"
8779             "\t;",
8780             format("if (aaaaaaaa &&// q\n"
8781                    "bb)// w\n"
8782                    ";",
8783                    Tab));
8784   EXPECT_EQ("if (aaa && bbb) // w\n"
8785             "\t;",
8786             format("if(aaa&&bbb)// w\n"
8787                    ";",
8788                    Tab));
8789 
8790   verifyFormat("class X {\n"
8791                "\tvoid f() {\n"
8792                "\t\tsomeFunction(parameter1,\n"
8793                "\t\t\t     parameter2);\n"
8794                "\t}\n"
8795                "};",
8796                Tab);
8797   verifyFormat("#define A                        \\\n"
8798                "\tvoid f() {               \\\n"
8799                "\t\tsomeFunction(    \\\n"
8800                "\t\t    parameter1,  \\\n"
8801                "\t\t    parameter2); \\\n"
8802                "\t}",
8803                Tab);
8804   verifyFormat("int a;\t      // x\n"
8805                "int bbbbbbbb; // x\n",
8806                Tab);
8807 
8808   Tab.TabWidth = 4;
8809   Tab.IndentWidth = 8;
8810   verifyFormat("class TabWidth4Indent8 {\n"
8811                "\t\tvoid f() {\n"
8812                "\t\t\t\tsomeFunction(parameter1,\n"
8813                "\t\t\t\t\t\t\t parameter2);\n"
8814                "\t\t}\n"
8815                "};",
8816                Tab);
8817 
8818   Tab.TabWidth = 4;
8819   Tab.IndentWidth = 4;
8820   verifyFormat("class TabWidth4Indent4 {\n"
8821                "\tvoid f() {\n"
8822                "\t\tsomeFunction(parameter1,\n"
8823                "\t\t\t\t\t parameter2);\n"
8824                "\t}\n"
8825                "};",
8826                Tab);
8827 
8828   Tab.TabWidth = 8;
8829   Tab.IndentWidth = 4;
8830   verifyFormat("class TabWidth8Indent4 {\n"
8831                "    void f() {\n"
8832                "\tsomeFunction(parameter1,\n"
8833                "\t\t     parameter2);\n"
8834                "    }\n"
8835                "};",
8836                Tab);
8837 
8838   Tab.TabWidth = 8;
8839   Tab.IndentWidth = 8;
8840   EXPECT_EQ("/*\n"
8841             "\t      a\t\tcomment\n"
8842             "\t      in multiple lines\n"
8843             "       */",
8844             format("   /*\t \t \n"
8845                    " \t \t a\t\tcomment\t \t\n"
8846                    " \t \t in multiple lines\t\n"
8847                    " \t  */",
8848                    Tab));
8849 
8850   Tab.UseTab = FormatStyle::UT_ForIndentation;
8851   verifyFormat("{\n"
8852                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8853                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8854                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8855                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8856                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8857                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8858                "};",
8859                Tab);
8860   verifyFormat("enum AA {\n"
8861                "\ta1, // Force multiple lines\n"
8862                "\ta2,\n"
8863                "\ta3\n"
8864                "};",
8865                Tab);
8866   EXPECT_EQ("if (aaaaaaaa && // q\n"
8867             "    bb)         // w\n"
8868             "\t;",
8869             format("if (aaaaaaaa &&// q\n"
8870                    "bb)// w\n"
8871                    ";",
8872                    Tab));
8873   verifyFormat("class X {\n"
8874                "\tvoid f() {\n"
8875                "\t\tsomeFunction(parameter1,\n"
8876                "\t\t             parameter2);\n"
8877                "\t}\n"
8878                "};",
8879                Tab);
8880   verifyFormat("{\n"
8881                "\tQ(\n"
8882                "\t    {\n"
8883                "\t\t    int a;\n"
8884                "\t\t    someFunction(aaaaaaaa,\n"
8885                "\t\t                 bbbbbbb);\n"
8886                "\t    },\n"
8887                "\t    p);\n"
8888                "}",
8889                Tab);
8890   EXPECT_EQ("{\n"
8891             "\t/* aaaa\n"
8892             "\t   bbbb */\n"
8893             "}",
8894             format("{\n"
8895                    "/* aaaa\n"
8896                    "   bbbb */\n"
8897                    "}",
8898                    Tab));
8899   EXPECT_EQ("{\n"
8900             "\t/*\n"
8901             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8902             "\t  bbbbbbbbbbbbb\n"
8903             "\t*/\n"
8904             "}",
8905             format("{\n"
8906                    "/*\n"
8907                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8908                    "*/\n"
8909                    "}",
8910                    Tab));
8911   EXPECT_EQ("{\n"
8912             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8913             "\t// bbbbbbbbbbbbb\n"
8914             "}",
8915             format("{\n"
8916                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8917                    "}",
8918                    Tab));
8919   EXPECT_EQ("{\n"
8920             "\t/*\n"
8921             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8922             "\t  bbbbbbbbbbbbb\n"
8923             "\t*/\n"
8924             "}",
8925             format("{\n"
8926                    "\t/*\n"
8927                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8928                    "\t*/\n"
8929                    "}",
8930                    Tab));
8931   EXPECT_EQ("{\n"
8932             "\t/*\n"
8933             "\n"
8934             "\t*/\n"
8935             "}",
8936             format("{\n"
8937                    "\t/*\n"
8938                    "\n"
8939                    "\t*/\n"
8940                    "}",
8941                    Tab));
8942   EXPECT_EQ("{\n"
8943             "\t/*\n"
8944             " asdf\n"
8945             "\t*/\n"
8946             "}",
8947             format("{\n"
8948                    "\t/*\n"
8949                    " asdf\n"
8950                    "\t*/\n"
8951                    "}",
8952                    Tab));
8953 
8954   Tab.UseTab = FormatStyle::UT_Never;
8955   EXPECT_EQ("/*\n"
8956             "              a\t\tcomment\n"
8957             "              in multiple lines\n"
8958             "       */",
8959             format("   /*\t \t \n"
8960                    " \t \t a\t\tcomment\t \t\n"
8961                    " \t \t in multiple lines\t\n"
8962                    " \t  */",
8963                    Tab));
8964   EXPECT_EQ("/* some\n"
8965             "   comment */",
8966             format(" \t \t /* some\n"
8967                    " \t \t    comment */",
8968                    Tab));
8969   EXPECT_EQ("int a; /* some\n"
8970             "   comment */",
8971             format(" \t \t int a; /* some\n"
8972                    " \t \t    comment */",
8973                    Tab));
8974 
8975   EXPECT_EQ("int a; /* some\n"
8976             "comment */",
8977             format(" \t \t int\ta; /* some\n"
8978                    " \t \t    comment */",
8979                    Tab));
8980   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8981             "    comment */",
8982             format(" \t \t f(\"\t\t\"); /* some\n"
8983                    " \t \t    comment */",
8984                    Tab));
8985   EXPECT_EQ("{\n"
8986             "  /*\n"
8987             "   * Comment\n"
8988             "   */\n"
8989             "  int i;\n"
8990             "}",
8991             format("{\n"
8992                    "\t/*\n"
8993                    "\t * Comment\n"
8994                    "\t */\n"
8995                    "\t int i;\n"
8996                    "}"));
8997 
8998   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8999   Tab.TabWidth = 8;
9000   Tab.IndentWidth = 8;
9001   EXPECT_EQ("if (aaaaaaaa && // q\n"
9002             "    bb)         // w\n"
9003             "\t;",
9004             format("if (aaaaaaaa &&// q\n"
9005                    "bb)// w\n"
9006                    ";",
9007                    Tab));
9008   EXPECT_EQ("if (aaa && bbb) // w\n"
9009             "\t;",
9010             format("if(aaa&&bbb)// w\n"
9011                    ";",
9012                    Tab));
9013   verifyFormat("class X {\n"
9014                "\tvoid f() {\n"
9015                "\t\tsomeFunction(parameter1,\n"
9016                "\t\t\t     parameter2);\n"
9017                "\t}\n"
9018                "};",
9019                Tab);
9020   verifyFormat("#define A                        \\\n"
9021                "\tvoid f() {               \\\n"
9022                "\t\tsomeFunction(    \\\n"
9023                "\t\t    parameter1,  \\\n"
9024                "\t\t    parameter2); \\\n"
9025                "\t}",
9026                Tab);
9027   Tab.TabWidth = 4;
9028   Tab.IndentWidth = 8;
9029   verifyFormat("class TabWidth4Indent8 {\n"
9030                "\t\tvoid f() {\n"
9031                "\t\t\t\tsomeFunction(parameter1,\n"
9032                "\t\t\t\t\t\t\t parameter2);\n"
9033                "\t\t}\n"
9034                "};",
9035                Tab);
9036   Tab.TabWidth = 4;
9037   Tab.IndentWidth = 4;
9038   verifyFormat("class TabWidth4Indent4 {\n"
9039                "\tvoid f() {\n"
9040                "\t\tsomeFunction(parameter1,\n"
9041                "\t\t\t\t\t parameter2);\n"
9042                "\t}\n"
9043                "};",
9044                Tab);
9045   Tab.TabWidth = 8;
9046   Tab.IndentWidth = 4;
9047   verifyFormat("class TabWidth8Indent4 {\n"
9048                "    void f() {\n"
9049                "\tsomeFunction(parameter1,\n"
9050                "\t\t     parameter2);\n"
9051                "    }\n"
9052                "};",
9053                Tab);
9054   Tab.TabWidth = 8;
9055   Tab.IndentWidth = 8;
9056   EXPECT_EQ("/*\n"
9057             "\t      a\t\tcomment\n"
9058             "\t      in multiple lines\n"
9059             "       */",
9060             format("   /*\t \t \n"
9061                    " \t \t a\t\tcomment\t \t\n"
9062                    " \t \t in multiple lines\t\n"
9063                    " \t  */",
9064                    Tab));
9065   verifyFormat("{\n"
9066                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9067                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9068                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9069                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9070                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9071                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9072                "};",
9073                Tab);
9074   verifyFormat("enum AA {\n"
9075                "\ta1, // Force multiple lines\n"
9076                "\ta2,\n"
9077                "\ta3\n"
9078                "};",
9079                Tab);
9080   EXPECT_EQ("if (aaaaaaaa && // q\n"
9081             "    bb)         // w\n"
9082             "\t;",
9083             format("if (aaaaaaaa &&// q\n"
9084                    "bb)// w\n"
9085                    ";",
9086                    Tab));
9087   verifyFormat("class X {\n"
9088                "\tvoid f() {\n"
9089                "\t\tsomeFunction(parameter1,\n"
9090                "\t\t\t     parameter2);\n"
9091                "\t}\n"
9092                "};",
9093                Tab);
9094   verifyFormat("{\n"
9095                "\tQ(\n"
9096                "\t    {\n"
9097                "\t\t    int a;\n"
9098                "\t\t    someFunction(aaaaaaaa,\n"
9099                "\t\t\t\t bbbbbbb);\n"
9100                "\t    },\n"
9101                "\t    p);\n"
9102                "}",
9103                Tab);
9104   EXPECT_EQ("{\n"
9105             "\t/* aaaa\n"
9106             "\t   bbbb */\n"
9107             "}",
9108             format("{\n"
9109                    "/* aaaa\n"
9110                    "   bbbb */\n"
9111                    "}",
9112                    Tab));
9113   EXPECT_EQ("{\n"
9114             "\t/*\n"
9115             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9116             "\t  bbbbbbbbbbbbb\n"
9117             "\t*/\n"
9118             "}",
9119             format("{\n"
9120                    "/*\n"
9121                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9122                    "*/\n"
9123                    "}",
9124                    Tab));
9125   EXPECT_EQ("{\n"
9126             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9127             "\t// bbbbbbbbbbbbb\n"
9128             "}",
9129             format("{\n"
9130                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9131                    "}",
9132                    Tab));
9133   EXPECT_EQ("{\n"
9134             "\t/*\n"
9135             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9136             "\t  bbbbbbbbbbbbb\n"
9137             "\t*/\n"
9138             "}",
9139             format("{\n"
9140                    "\t/*\n"
9141                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9142                    "\t*/\n"
9143                    "}",
9144                    Tab));
9145   EXPECT_EQ("{\n"
9146             "\t/*\n"
9147             "\n"
9148             "\t*/\n"
9149             "}",
9150             format("{\n"
9151                    "\t/*\n"
9152                    "\n"
9153                    "\t*/\n"
9154                    "}",
9155                    Tab));
9156   EXPECT_EQ("{\n"
9157             "\t/*\n"
9158             " asdf\n"
9159             "\t*/\n"
9160             "}",
9161             format("{\n"
9162                    "\t/*\n"
9163                    " asdf\n"
9164                    "\t*/\n"
9165                    "}",
9166                    Tab));
9167   EXPECT_EQ("/*\n"
9168             "\t      a\t\tcomment\n"
9169             "\t      in multiple lines\n"
9170             "       */",
9171             format("   /*\t \t \n"
9172                    " \t \t a\t\tcomment\t \t\n"
9173                    " \t \t in multiple lines\t\n"
9174                    " \t  */",
9175                    Tab));
9176   EXPECT_EQ("/* some\n"
9177             "   comment */",
9178             format(" \t \t /* some\n"
9179                    " \t \t    comment */",
9180                    Tab));
9181   EXPECT_EQ("int a; /* some\n"
9182             "   comment */",
9183             format(" \t \t int a; /* some\n"
9184                    " \t \t    comment */",
9185                    Tab));
9186   EXPECT_EQ("int a; /* some\n"
9187             "comment */",
9188             format(" \t \t int\ta; /* some\n"
9189                    " \t \t    comment */",
9190                    Tab));
9191   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9192             "    comment */",
9193             format(" \t \t f(\"\t\t\"); /* some\n"
9194                    " \t \t    comment */",
9195                    Tab));
9196   EXPECT_EQ("{\n"
9197             "  /*\n"
9198             "   * Comment\n"
9199             "   */\n"
9200             "  int i;\n"
9201             "}",
9202             format("{\n"
9203                    "\t/*\n"
9204                    "\t * Comment\n"
9205                    "\t */\n"
9206                    "\t int i;\n"
9207                    "}"));
9208   Tab.AlignConsecutiveAssignments = true;
9209   Tab.AlignConsecutiveDeclarations = true;
9210   Tab.TabWidth = 4;
9211   Tab.IndentWidth = 4;
9212   verifyFormat("class Assign {\n"
9213                "\tvoid f() {\n"
9214                "\t\tint         x      = 123;\n"
9215                "\t\tint         random = 4;\n"
9216                "\t\tstd::string alphabet =\n"
9217                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
9218                "\t}\n"
9219                "};",
9220                Tab);
9221 }
9222 
9223 TEST_F(FormatTest, CalculatesOriginalColumn) {
9224   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9225             "q\"; /* some\n"
9226             "       comment */",
9227             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9228                    "q\"; /* some\n"
9229                    "       comment */",
9230                    getLLVMStyle()));
9231   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9232             "/* some\n"
9233             "   comment */",
9234             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9235                    " /* some\n"
9236                    "    comment */",
9237                    getLLVMStyle()));
9238   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9239             "qqq\n"
9240             "/* some\n"
9241             "   comment */",
9242             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9243                    "qqq\n"
9244                    " /* some\n"
9245                    "    comment */",
9246                    getLLVMStyle()));
9247   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9248             "wwww; /* some\n"
9249             "         comment */",
9250             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9251                    "wwww; /* some\n"
9252                    "         comment */",
9253                    getLLVMStyle()));
9254 }
9255 
9256 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
9257   FormatStyle NoSpace = getLLVMStyle();
9258   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
9259 
9260   verifyFormat("while(true)\n"
9261                "  continue;",
9262                NoSpace);
9263   verifyFormat("for(;;)\n"
9264                "  continue;",
9265                NoSpace);
9266   verifyFormat("if(true)\n"
9267                "  f();\n"
9268                "else if(true)\n"
9269                "  f();",
9270                NoSpace);
9271   verifyFormat("do {\n"
9272                "  do_something();\n"
9273                "} while(something());",
9274                NoSpace);
9275   verifyFormat("switch(x) {\n"
9276                "default:\n"
9277                "  break;\n"
9278                "}",
9279                NoSpace);
9280   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
9281   verifyFormat("size_t x = sizeof(x);", NoSpace);
9282   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
9283   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
9284   verifyFormat("alignas(128) char a[128];", NoSpace);
9285   verifyFormat("size_t x = alignof(MyType);", NoSpace);
9286   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
9287   verifyFormat("int f() throw(Deprecated);", NoSpace);
9288   verifyFormat("typedef void (*cb)(int);", NoSpace);
9289   verifyFormat("T A::operator()();", NoSpace);
9290   verifyFormat("X A::operator++(T);", NoSpace);
9291   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
9292 
9293   FormatStyle Space = getLLVMStyle();
9294   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9295 
9296   verifyFormat("int f ();", Space);
9297   verifyFormat("void f (int a, T b) {\n"
9298                "  while (true)\n"
9299                "    continue;\n"
9300                "}",
9301                Space);
9302   verifyFormat("if (true)\n"
9303                "  f ();\n"
9304                "else if (true)\n"
9305                "  f ();",
9306                Space);
9307   verifyFormat("do {\n"
9308                "  do_something ();\n"
9309                "} while (something ());",
9310                Space);
9311   verifyFormat("switch (x) {\n"
9312                "default:\n"
9313                "  break;\n"
9314                "}",
9315                Space);
9316   verifyFormat("A::A () : a (1) {}", Space);
9317   verifyFormat("void f () __attribute__ ((asdf));", Space);
9318   verifyFormat("*(&a + 1);\n"
9319                "&((&a)[1]);\n"
9320                "a[(b + c) * d];\n"
9321                "(((a + 1) * 2) + 3) * 4;",
9322                Space);
9323   verifyFormat("#define A(x) x", Space);
9324   verifyFormat("#define A (x) x", Space);
9325   verifyFormat("#if defined(x)\n"
9326                "#endif",
9327                Space);
9328   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9329   verifyFormat("size_t x = sizeof (x);", Space);
9330   verifyFormat("auto f (int x) -> decltype (x);", Space);
9331   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9332   verifyFormat("alignas (128) char a[128];", Space);
9333   verifyFormat("size_t x = alignof (MyType);", Space);
9334   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9335   verifyFormat("int f () throw (Deprecated);", Space);
9336   verifyFormat("typedef void (*cb) (int);", Space);
9337   verifyFormat("T A::operator() ();", Space);
9338   verifyFormat("X A::operator++ (T);", Space);
9339   verifyFormat("auto lambda = [] () { return 0; };", Space);
9340 }
9341 
9342 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9343   FormatStyle Spaces = getLLVMStyle();
9344 
9345   Spaces.SpacesInParentheses = true;
9346   verifyFormat("do_something( ::globalVar );", Spaces);
9347   verifyFormat("call( x, y, z );", Spaces);
9348   verifyFormat("call();", Spaces);
9349   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9350   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9351                Spaces);
9352   verifyFormat("while ( (bool)1 )\n"
9353                "  continue;",
9354                Spaces);
9355   verifyFormat("for ( ;; )\n"
9356                "  continue;",
9357                Spaces);
9358   verifyFormat("if ( true )\n"
9359                "  f();\n"
9360                "else if ( true )\n"
9361                "  f();",
9362                Spaces);
9363   verifyFormat("do {\n"
9364                "  do_something( (int)i );\n"
9365                "} while ( something() );",
9366                Spaces);
9367   verifyFormat("switch ( x ) {\n"
9368                "default:\n"
9369                "  break;\n"
9370                "}",
9371                Spaces);
9372 
9373   Spaces.SpacesInParentheses = false;
9374   Spaces.SpacesInCStyleCastParentheses = true;
9375   verifyFormat("Type *A = ( Type * )P;", Spaces);
9376   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9377   verifyFormat("x = ( int32 )y;", Spaces);
9378   verifyFormat("int a = ( int )(2.0f);", Spaces);
9379   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9380   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9381   verifyFormat("#define x (( int )-1)", Spaces);
9382 
9383   // Run the first set of tests again with:
9384   Spaces.SpacesInParentheses = false;
9385   Spaces.SpaceInEmptyParentheses = true;
9386   Spaces.SpacesInCStyleCastParentheses = true;
9387   verifyFormat("call(x, y, z);", Spaces);
9388   verifyFormat("call( );", Spaces);
9389   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9390   verifyFormat("while (( bool )1)\n"
9391                "  continue;",
9392                Spaces);
9393   verifyFormat("for (;;)\n"
9394                "  continue;",
9395                Spaces);
9396   verifyFormat("if (true)\n"
9397                "  f( );\n"
9398                "else if (true)\n"
9399                "  f( );",
9400                Spaces);
9401   verifyFormat("do {\n"
9402                "  do_something(( int )i);\n"
9403                "} while (something( ));",
9404                Spaces);
9405   verifyFormat("switch (x) {\n"
9406                "default:\n"
9407                "  break;\n"
9408                "}",
9409                Spaces);
9410 
9411   // Run the first set of tests again with:
9412   Spaces.SpaceAfterCStyleCast = true;
9413   verifyFormat("call(x, y, z);", Spaces);
9414   verifyFormat("call( );", Spaces);
9415   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9416   verifyFormat("while (( bool ) 1)\n"
9417                "  continue;",
9418                Spaces);
9419   verifyFormat("for (;;)\n"
9420                "  continue;",
9421                Spaces);
9422   verifyFormat("if (true)\n"
9423                "  f( );\n"
9424                "else if (true)\n"
9425                "  f( );",
9426                Spaces);
9427   verifyFormat("do {\n"
9428                "  do_something(( int ) i);\n"
9429                "} while (something( ));",
9430                Spaces);
9431   verifyFormat("switch (x) {\n"
9432                "default:\n"
9433                "  break;\n"
9434                "}",
9435                Spaces);
9436 
9437   // Run subset of tests again with:
9438   Spaces.SpacesInCStyleCastParentheses = false;
9439   Spaces.SpaceAfterCStyleCast = true;
9440   verifyFormat("while ((bool) 1)\n"
9441                "  continue;",
9442                Spaces);
9443   verifyFormat("do {\n"
9444                "  do_something((int) i);\n"
9445                "} while (something( ));",
9446                Spaces);
9447 }
9448 
9449 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9450   verifyFormat("int a[5];");
9451   verifyFormat("a[3] += 42;");
9452 
9453   FormatStyle Spaces = getLLVMStyle();
9454   Spaces.SpacesInSquareBrackets = true;
9455   // Lambdas unchanged.
9456   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9457   verifyFormat("return [i, args...] {};", Spaces);
9458 
9459   // Not lambdas.
9460   verifyFormat("int a[ 5 ];", Spaces);
9461   verifyFormat("a[ 3 ] += 42;", Spaces);
9462   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9463   verifyFormat("double &operator[](int i) { return 0; }\n"
9464                "int i;",
9465                Spaces);
9466   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9467   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9468   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9469 }
9470 
9471 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9472   verifyFormat("int a = 5;");
9473   verifyFormat("a += 42;");
9474   verifyFormat("a or_eq 8;");
9475 
9476   FormatStyle Spaces = getLLVMStyle();
9477   Spaces.SpaceBeforeAssignmentOperators = false;
9478   verifyFormat("int a= 5;", Spaces);
9479   verifyFormat("a+= 42;", Spaces);
9480   verifyFormat("a or_eq 8;", Spaces);
9481 }
9482 
9483 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9484   verifyFormat("class Foo : public Bar {};");
9485   verifyFormat("Foo::Foo() : foo(1) {}");
9486   verifyFormat("for (auto a : b) {\n}");
9487   verifyFormat("int x = a ? b : c;");
9488   verifyFormat("{\n"
9489                "label0:\n"
9490                "  int x = 0;\n"
9491                "}");
9492   verifyFormat("switch (x) {\n"
9493                "case 1:\n"
9494                "default:\n"
9495                "}");
9496 
9497   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9498   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9499   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9500   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9501   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9502   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9503   verifyFormat("{\n"
9504                "label1:\n"
9505                "  int x = 0;\n"
9506                "}",
9507                CtorInitializerStyle);
9508   verifyFormat("switch (x) {\n"
9509                "case 1:\n"
9510                "default:\n"
9511                "}",
9512                CtorInitializerStyle);
9513   CtorInitializerStyle.BreakConstructorInitializers =
9514       FormatStyle::BCIS_AfterColon;
9515   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9516                "    aaaaaaaaaaaaaaaa(1),\n"
9517                "    bbbbbbbbbbbbbbbb(2) {}",
9518                CtorInitializerStyle);
9519   CtorInitializerStyle.BreakConstructorInitializers =
9520       FormatStyle::BCIS_BeforeComma;
9521   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9522                "    : aaaaaaaaaaaaaaaa(1)\n"
9523                "    , bbbbbbbbbbbbbbbb(2) {}",
9524                CtorInitializerStyle);
9525   CtorInitializerStyle.BreakConstructorInitializers =
9526       FormatStyle::BCIS_BeforeColon;
9527   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9528                "    : aaaaaaaaaaaaaaaa(1),\n"
9529                "      bbbbbbbbbbbbbbbb(2) {}",
9530                CtorInitializerStyle);
9531   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9532   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9533                ": aaaaaaaaaaaaaaaa(1),\n"
9534                "  bbbbbbbbbbbbbbbb(2) {}",
9535                CtorInitializerStyle);
9536 
9537   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9538   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9539   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9540   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9541   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9542   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9543   verifyFormat("{\n"
9544                "label2:\n"
9545                "  int x = 0;\n"
9546                "}",
9547                InheritanceStyle);
9548   verifyFormat("switch (x) {\n"
9549                "case 1:\n"
9550                "default:\n"
9551                "}",
9552                InheritanceStyle);
9553   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9554   verifyFormat("class Foooooooooooooooooooooo:\n"
9555                "    public aaaaaaaaaaaaaaaaaa,\n"
9556                "    public bbbbbbbbbbbbbbbbbb {\n"
9557                "}",
9558                InheritanceStyle);
9559   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9560   verifyFormat("class Foooooooooooooooooooooo\n"
9561                "    : public aaaaaaaaaaaaaaaaaa\n"
9562                "    , public bbbbbbbbbbbbbbbbbb {\n"
9563                "}",
9564                InheritanceStyle);
9565   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9566   verifyFormat("class Foooooooooooooooooooooo\n"
9567                "    : public aaaaaaaaaaaaaaaaaa,\n"
9568                "      public bbbbbbbbbbbbbbbbbb {\n"
9569                "}",
9570                InheritanceStyle);
9571   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9572   verifyFormat("class Foooooooooooooooooooooo\n"
9573                ": public aaaaaaaaaaaaaaaaaa,\n"
9574                "  public bbbbbbbbbbbbbbbbbb {}",
9575                InheritanceStyle);
9576 
9577   FormatStyle ForLoopStyle = getLLVMStyle();
9578   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9579   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9580   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9581   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9582   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9583   verifyFormat("{\n"
9584                "label2:\n"
9585                "  int x = 0;\n"
9586                "}",
9587                ForLoopStyle);
9588   verifyFormat("switch (x) {\n"
9589                "case 1:\n"
9590                "default:\n"
9591                "}",
9592                ForLoopStyle);
9593 
9594   FormatStyle NoSpaceStyle = getLLVMStyle();
9595   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9596   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9597   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9598   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9599   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9600   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9601   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9602   verifyFormat("{\n"
9603                "label3:\n"
9604                "  int x = 0;\n"
9605                "}",
9606                NoSpaceStyle);
9607   verifyFormat("switch (x) {\n"
9608                "case 1:\n"
9609                "default:\n"
9610                "}",
9611                NoSpaceStyle);
9612 }
9613 
9614 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9615   FormatStyle Alignment = getLLVMStyle();
9616   Alignment.AlignConsecutiveAssignments = false;
9617   verifyFormat("int a = 5;\n"
9618                "int oneTwoThree = 123;",
9619                Alignment);
9620   verifyFormat("int a = 5;\n"
9621                "int oneTwoThree = 123;",
9622                Alignment);
9623 
9624   Alignment.AlignConsecutiveAssignments = true;
9625   verifyFormat("int a           = 5;\n"
9626                "int oneTwoThree = 123;",
9627                Alignment);
9628   verifyFormat("int a           = method();\n"
9629                "int oneTwoThree = 133;",
9630                Alignment);
9631   verifyFormat("a &= 5;\n"
9632                "bcd *= 5;\n"
9633                "ghtyf += 5;\n"
9634                "dvfvdb -= 5;\n"
9635                "a /= 5;\n"
9636                "vdsvsv %= 5;\n"
9637                "sfdbddfbdfbb ^= 5;\n"
9638                "dvsdsv |= 5;\n"
9639                "int dsvvdvsdvvv = 123;",
9640                Alignment);
9641   verifyFormat("int i = 1, j = 10;\n"
9642                "something = 2000;",
9643                Alignment);
9644   verifyFormat("something = 2000;\n"
9645                "int i = 1, j = 10;\n",
9646                Alignment);
9647   verifyFormat("something = 2000;\n"
9648                "another   = 911;\n"
9649                "int i = 1, j = 10;\n"
9650                "oneMore = 1;\n"
9651                "i       = 2;",
9652                Alignment);
9653   verifyFormat("int a   = 5;\n"
9654                "int one = 1;\n"
9655                "method();\n"
9656                "int oneTwoThree = 123;\n"
9657                "int oneTwo      = 12;",
9658                Alignment);
9659   verifyFormat("int oneTwoThree = 123;\n"
9660                "int oneTwo      = 12;\n"
9661                "method();\n",
9662                Alignment);
9663   verifyFormat("int oneTwoThree = 123; // comment\n"
9664                "int oneTwo      = 12;  // comment",
9665                Alignment);
9666   EXPECT_EQ("int a = 5;\n"
9667             "\n"
9668             "int oneTwoThree = 123;",
9669             format("int a       = 5;\n"
9670                    "\n"
9671                    "int oneTwoThree= 123;",
9672                    Alignment));
9673   EXPECT_EQ("int a   = 5;\n"
9674             "int one = 1;\n"
9675             "\n"
9676             "int oneTwoThree = 123;",
9677             format("int a = 5;\n"
9678                    "int one = 1;\n"
9679                    "\n"
9680                    "int oneTwoThree = 123;",
9681                    Alignment));
9682   EXPECT_EQ("int a   = 5;\n"
9683             "int one = 1;\n"
9684             "\n"
9685             "int oneTwoThree = 123;\n"
9686             "int oneTwo      = 12;",
9687             format("int a = 5;\n"
9688                    "int one = 1;\n"
9689                    "\n"
9690                    "int oneTwoThree = 123;\n"
9691                    "int oneTwo = 12;",
9692                    Alignment));
9693   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9694   verifyFormat("#define A \\\n"
9695                "  int aaaa       = 12; \\\n"
9696                "  int b          = 23; \\\n"
9697                "  int ccc        = 234; \\\n"
9698                "  int dddddddddd = 2345;",
9699                Alignment);
9700   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9701   verifyFormat("#define A               \\\n"
9702                "  int aaaa       = 12;  \\\n"
9703                "  int b          = 23;  \\\n"
9704                "  int ccc        = 234; \\\n"
9705                "  int dddddddddd = 2345;",
9706                Alignment);
9707   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9708   verifyFormat("#define A                                                      "
9709                "                \\\n"
9710                "  int aaaa       = 12;                                         "
9711                "                \\\n"
9712                "  int b          = 23;                                         "
9713                "                \\\n"
9714                "  int ccc        = 234;                                        "
9715                "                \\\n"
9716                "  int dddddddddd = 2345;",
9717                Alignment);
9718   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9719                "k = 4, int l = 5,\n"
9720                "                  int m = 6) {\n"
9721                "  int j      = 10;\n"
9722                "  otherThing = 1;\n"
9723                "}",
9724                Alignment);
9725   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9726                "  int i   = 1;\n"
9727                "  int j   = 2;\n"
9728                "  int big = 10000;\n"
9729                "}",
9730                Alignment);
9731   verifyFormat("class C {\n"
9732                "public:\n"
9733                "  int i            = 1;\n"
9734                "  virtual void f() = 0;\n"
9735                "};",
9736                Alignment);
9737   verifyFormat("int i = 1;\n"
9738                "if (SomeType t = getSomething()) {\n"
9739                "}\n"
9740                "int j   = 2;\n"
9741                "int big = 10000;",
9742                Alignment);
9743   verifyFormat("int j = 7;\n"
9744                "for (int k = 0; k < N; ++k) {\n"
9745                "}\n"
9746                "int j   = 2;\n"
9747                "int big = 10000;\n"
9748                "}",
9749                Alignment);
9750   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9751   verifyFormat("int i = 1;\n"
9752                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9753                "    = someLooooooooooooooooongFunction();\n"
9754                "int j = 2;",
9755                Alignment);
9756   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9757   verifyFormat("int i = 1;\n"
9758                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9759                "    someLooooooooooooooooongFunction();\n"
9760                "int j = 2;",
9761                Alignment);
9762 
9763   verifyFormat("auto lambda = []() {\n"
9764                "  auto i = 0;\n"
9765                "  return 0;\n"
9766                "};\n"
9767                "int i  = 0;\n"
9768                "auto v = type{\n"
9769                "    i = 1,   //\n"
9770                "    (i = 2), //\n"
9771                "    i = 3    //\n"
9772                "};",
9773                Alignment);
9774 
9775   verifyFormat(
9776       "int i      = 1;\n"
9777       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9778       "                          loooooooooooooooooooooongParameterB);\n"
9779       "int j      = 2;",
9780       Alignment);
9781 
9782   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9783                "          typename B   = very_long_type_name_1,\n"
9784                "          typename T_2 = very_long_type_name_2>\n"
9785                "auto foo() {}\n",
9786                Alignment);
9787   verifyFormat("int a, b = 1;\n"
9788                "int c  = 2;\n"
9789                "int dd = 3;\n",
9790                Alignment);
9791   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9792                "float b[1][] = {{3.f}};\n",
9793                Alignment);
9794   verifyFormat("for (int i = 0; i < 1; i++)\n"
9795                "  int x = 1;\n",
9796                Alignment);
9797   verifyFormat("for (i = 0; i < 1; i++)\n"
9798                "  x = 1;\n"
9799                "y = 1;\n",
9800                Alignment);
9801 }
9802 
9803 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9804   FormatStyle Alignment = getLLVMStyle();
9805   Alignment.AlignConsecutiveDeclarations = false;
9806   verifyFormat("float const a = 5;\n"
9807                "int oneTwoThree = 123;",
9808                Alignment);
9809   verifyFormat("int a = 5;\n"
9810                "float const oneTwoThree = 123;",
9811                Alignment);
9812 
9813   Alignment.AlignConsecutiveDeclarations = true;
9814   verifyFormat("float const a = 5;\n"
9815                "int         oneTwoThree = 123;",
9816                Alignment);
9817   verifyFormat("int         a = method();\n"
9818                "float const oneTwoThree = 133;",
9819                Alignment);
9820   verifyFormat("int i = 1, j = 10;\n"
9821                "something = 2000;",
9822                Alignment);
9823   verifyFormat("something = 2000;\n"
9824                "int i = 1, j = 10;\n",
9825                Alignment);
9826   verifyFormat("float      something = 2000;\n"
9827                "double     another = 911;\n"
9828                "int        i = 1, j = 10;\n"
9829                "const int *oneMore = 1;\n"
9830                "unsigned   i = 2;",
9831                Alignment);
9832   verifyFormat("float a = 5;\n"
9833                "int   one = 1;\n"
9834                "method();\n"
9835                "const double       oneTwoThree = 123;\n"
9836                "const unsigned int oneTwo = 12;",
9837                Alignment);
9838   verifyFormat("int      oneTwoThree{0}; // comment\n"
9839                "unsigned oneTwo;         // comment",
9840                Alignment);
9841   EXPECT_EQ("float const a = 5;\n"
9842             "\n"
9843             "int oneTwoThree = 123;",
9844             format("float const   a = 5;\n"
9845                    "\n"
9846                    "int           oneTwoThree= 123;",
9847                    Alignment));
9848   EXPECT_EQ("float a = 5;\n"
9849             "int   one = 1;\n"
9850             "\n"
9851             "unsigned oneTwoThree = 123;",
9852             format("float    a = 5;\n"
9853                    "int      one = 1;\n"
9854                    "\n"
9855                    "unsigned oneTwoThree = 123;",
9856                    Alignment));
9857   EXPECT_EQ("float a = 5;\n"
9858             "int   one = 1;\n"
9859             "\n"
9860             "unsigned oneTwoThree = 123;\n"
9861             "int      oneTwo = 12;",
9862             format("float    a = 5;\n"
9863                    "int one = 1;\n"
9864                    "\n"
9865                    "unsigned oneTwoThree = 123;\n"
9866                    "int oneTwo = 12;",
9867                    Alignment));
9868   // Function prototype alignment
9869   verifyFormat("int    a();\n"
9870                "double b();",
9871                Alignment);
9872   verifyFormat("int    a(int x);\n"
9873                "double b();",
9874                Alignment);
9875   unsigned OldColumnLimit = Alignment.ColumnLimit;
9876   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9877   // otherwise the function parameters will be re-flowed onto a single line.
9878   Alignment.ColumnLimit = 0;
9879   EXPECT_EQ("int    a(int   x,\n"
9880             "         float y);\n"
9881             "double b(int    x,\n"
9882             "         double y);",
9883             format("int a(int x,\n"
9884                    " float y);\n"
9885                    "double b(int x,\n"
9886                    " double y);",
9887                    Alignment));
9888   // This ensures that function parameters of function declarations are
9889   // correctly indented when their owning functions are indented.
9890   // The failure case here is for 'double y' to not be indented enough.
9891   EXPECT_EQ("double a(int x);\n"
9892             "int    b(int    y,\n"
9893             "         double z);",
9894             format("double a(int x);\n"
9895                    "int b(int y,\n"
9896                    " double z);",
9897                    Alignment));
9898   // Set ColumnLimit low so that we induce wrapping immediately after
9899   // the function name and opening paren.
9900   Alignment.ColumnLimit = 13;
9901   verifyFormat("int function(\n"
9902                "    int  x,\n"
9903                "    bool y);",
9904                Alignment);
9905   Alignment.ColumnLimit = OldColumnLimit;
9906   // Ensure function pointers don't screw up recursive alignment
9907   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9908                "double b();",
9909                Alignment);
9910   Alignment.AlignConsecutiveAssignments = true;
9911   // Ensure recursive alignment is broken by function braces, so that the
9912   // "a = 1" does not align with subsequent assignments inside the function
9913   // body.
9914   verifyFormat("int func(int a = 1) {\n"
9915                "  int b  = 2;\n"
9916                "  int cc = 3;\n"
9917                "}",
9918                Alignment);
9919   verifyFormat("float      something = 2000;\n"
9920                "double     another   = 911;\n"
9921                "int        i = 1, j = 10;\n"
9922                "const int *oneMore = 1;\n"
9923                "unsigned   i       = 2;",
9924                Alignment);
9925   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9926                "unsigned oneTwo      = 0;   // comment",
9927                Alignment);
9928   // Make sure that scope is correctly tracked, in the absence of braces
9929   verifyFormat("for (int i = 0; i < n; i++)\n"
9930                "  j = i;\n"
9931                "double x = 1;\n",
9932                Alignment);
9933   verifyFormat("if (int i = 0)\n"
9934                "  j = i;\n"
9935                "double x = 1;\n",
9936                Alignment);
9937   // Ensure operator[] and operator() are comprehended
9938   verifyFormat("struct test {\n"
9939                "  long long int foo();\n"
9940                "  int           operator[](int a);\n"
9941                "  double        bar();\n"
9942                "};\n",
9943                Alignment);
9944   verifyFormat("struct test {\n"
9945                "  long long int foo();\n"
9946                "  int           operator()(int a);\n"
9947                "  double        bar();\n"
9948                "};\n",
9949                Alignment);
9950   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9951             "  int const i   = 1;\n"
9952             "  int *     j   = 2;\n"
9953             "  int       big = 10000;\n"
9954             "\n"
9955             "  unsigned oneTwoThree = 123;\n"
9956             "  int      oneTwo      = 12;\n"
9957             "  method();\n"
9958             "  float k  = 2;\n"
9959             "  int   ll = 10000;\n"
9960             "}",
9961             format("void SomeFunction(int parameter= 0) {\n"
9962                    " int const  i= 1;\n"
9963                    "  int *j=2;\n"
9964                    " int big  =  10000;\n"
9965                    "\n"
9966                    "unsigned oneTwoThree  =123;\n"
9967                    "int oneTwo = 12;\n"
9968                    "  method();\n"
9969                    "float k= 2;\n"
9970                    "int ll=10000;\n"
9971                    "}",
9972                    Alignment));
9973   Alignment.AlignConsecutiveAssignments = false;
9974   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9975   verifyFormat("#define A \\\n"
9976                "  int       aaaa = 12; \\\n"
9977                "  float     b = 23; \\\n"
9978                "  const int ccc = 234; \\\n"
9979                "  unsigned  dddddddddd = 2345;",
9980                Alignment);
9981   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9982   verifyFormat("#define A              \\\n"
9983                "  int       aaaa = 12; \\\n"
9984                "  float     b = 23;    \\\n"
9985                "  const int ccc = 234; \\\n"
9986                "  unsigned  dddddddddd = 2345;",
9987                Alignment);
9988   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9989   Alignment.ColumnLimit = 30;
9990   verifyFormat("#define A                    \\\n"
9991                "  int       aaaa = 12;       \\\n"
9992                "  float     b = 23;          \\\n"
9993                "  const int ccc = 234;       \\\n"
9994                "  int       dddddddddd = 2345;",
9995                Alignment);
9996   Alignment.ColumnLimit = 80;
9997   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9998                "k = 4, int l = 5,\n"
9999                "                  int m = 6) {\n"
10000                "  const int j = 10;\n"
10001                "  otherThing = 1;\n"
10002                "}",
10003                Alignment);
10004   verifyFormat("void SomeFunction(int parameter = 0) {\n"
10005                "  int const i = 1;\n"
10006                "  int *     j = 2;\n"
10007                "  int       big = 10000;\n"
10008                "}",
10009                Alignment);
10010   verifyFormat("class C {\n"
10011                "public:\n"
10012                "  int          i = 1;\n"
10013                "  virtual void f() = 0;\n"
10014                "};",
10015                Alignment);
10016   verifyFormat("float i = 1;\n"
10017                "if (SomeType t = getSomething()) {\n"
10018                "}\n"
10019                "const unsigned j = 2;\n"
10020                "int            big = 10000;",
10021                Alignment);
10022   verifyFormat("float j = 7;\n"
10023                "for (int k = 0; k < N; ++k) {\n"
10024                "}\n"
10025                "unsigned j = 2;\n"
10026                "int      big = 10000;\n"
10027                "}",
10028                Alignment);
10029   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10030   verifyFormat("float              i = 1;\n"
10031                "LooooooooooongType loooooooooooooooooooooongVariable\n"
10032                "    = someLooooooooooooooooongFunction();\n"
10033                "int j = 2;",
10034                Alignment);
10035   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10036   verifyFormat("int                i = 1;\n"
10037                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
10038                "    someLooooooooooooooooongFunction();\n"
10039                "int j = 2;",
10040                Alignment);
10041 
10042   Alignment.AlignConsecutiveAssignments = true;
10043   verifyFormat("auto lambda = []() {\n"
10044                "  auto  ii = 0;\n"
10045                "  float j  = 0;\n"
10046                "  return 0;\n"
10047                "};\n"
10048                "int   i  = 0;\n"
10049                "float i2 = 0;\n"
10050                "auto  v  = type{\n"
10051                "    i = 1,   //\n"
10052                "    (i = 2), //\n"
10053                "    i = 3    //\n"
10054                "};",
10055                Alignment);
10056   Alignment.AlignConsecutiveAssignments = false;
10057 
10058   verifyFormat(
10059       "int      i = 1;\n"
10060       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10061       "                          loooooooooooooooooooooongParameterB);\n"
10062       "int      j = 2;",
10063       Alignment);
10064 
10065   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
10066   // We expect declarations and assignments to align, as long as it doesn't
10067   // exceed the column limit, starting a new alignment sequence whenever it
10068   // happens.
10069   Alignment.AlignConsecutiveAssignments = true;
10070   Alignment.ColumnLimit = 30;
10071   verifyFormat("float    ii              = 1;\n"
10072                "unsigned j               = 2;\n"
10073                "int someVerylongVariable = 1;\n"
10074                "AnotherLongType  ll = 123456;\n"
10075                "VeryVeryLongType k  = 2;\n"
10076                "int              myvar = 1;",
10077                Alignment);
10078   Alignment.ColumnLimit = 80;
10079   Alignment.AlignConsecutiveAssignments = false;
10080 
10081   verifyFormat(
10082       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
10083       "          typename LongType, typename B>\n"
10084       "auto foo() {}\n",
10085       Alignment);
10086   verifyFormat("float a, b = 1;\n"
10087                "int   c = 2;\n"
10088                "int   dd = 3;\n",
10089                Alignment);
10090   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
10091                "float b[1][] = {{3.f}};\n",
10092                Alignment);
10093   Alignment.AlignConsecutiveAssignments = true;
10094   verifyFormat("float a, b = 1;\n"
10095                "int   c  = 2;\n"
10096                "int   dd = 3;\n",
10097                Alignment);
10098   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
10099                "float b[1][] = {{3.f}};\n",
10100                Alignment);
10101   Alignment.AlignConsecutiveAssignments = false;
10102 
10103   Alignment.ColumnLimit = 30;
10104   Alignment.BinPackParameters = false;
10105   verifyFormat("void foo(float     a,\n"
10106                "         float     b,\n"
10107                "         int       c,\n"
10108                "         uint32_t *d) {\n"
10109                "  int *  e = 0;\n"
10110                "  float  f = 0;\n"
10111                "  double g = 0;\n"
10112                "}\n"
10113                "void bar(ino_t     a,\n"
10114                "         int       b,\n"
10115                "         uint32_t *c,\n"
10116                "         bool      d) {}\n",
10117                Alignment);
10118   Alignment.BinPackParameters = true;
10119   Alignment.ColumnLimit = 80;
10120 
10121   // Bug 33507
10122   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
10123   verifyFormat(
10124       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
10125       "  static const Version verVs2017;\n"
10126       "  return true;\n"
10127       "});\n",
10128       Alignment);
10129   Alignment.PointerAlignment = FormatStyle::PAS_Right;
10130 
10131   // See llvm.org/PR35641
10132   Alignment.AlignConsecutiveDeclarations = true;
10133   verifyFormat("int func() { //\n"
10134                "  int      b;\n"
10135                "  unsigned c;\n"
10136                "}",
10137                Alignment);
10138 }
10139 
10140 TEST_F(FormatTest, LinuxBraceBreaking) {
10141   FormatStyle LinuxBraceStyle = getLLVMStyle();
10142   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
10143   verifyFormat("namespace a\n"
10144                "{\n"
10145                "class A\n"
10146                "{\n"
10147                "  void f()\n"
10148                "  {\n"
10149                "    if (true) {\n"
10150                "      a();\n"
10151                "      b();\n"
10152                "    } else {\n"
10153                "      a();\n"
10154                "    }\n"
10155                "  }\n"
10156                "  void g() { return; }\n"
10157                "};\n"
10158                "struct B {\n"
10159                "  int x;\n"
10160                "};\n"
10161                "} // namespace a\n",
10162                LinuxBraceStyle);
10163   verifyFormat("enum X {\n"
10164                "  Y = 0,\n"
10165                "}\n",
10166                LinuxBraceStyle);
10167   verifyFormat("struct S {\n"
10168                "  int Type;\n"
10169                "  union {\n"
10170                "    int x;\n"
10171                "    double y;\n"
10172                "  } Value;\n"
10173                "  class C\n"
10174                "  {\n"
10175                "    MyFavoriteType Value;\n"
10176                "  } Class;\n"
10177                "}\n",
10178                LinuxBraceStyle);
10179 }
10180 
10181 TEST_F(FormatTest, MozillaBraceBreaking) {
10182   FormatStyle MozillaBraceStyle = getLLVMStyle();
10183   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
10184   MozillaBraceStyle.FixNamespaceComments = false;
10185   verifyFormat("namespace a {\n"
10186                "class A\n"
10187                "{\n"
10188                "  void f()\n"
10189                "  {\n"
10190                "    if (true) {\n"
10191                "      a();\n"
10192                "      b();\n"
10193                "    }\n"
10194                "  }\n"
10195                "  void g() { return; }\n"
10196                "};\n"
10197                "enum E\n"
10198                "{\n"
10199                "  A,\n"
10200                "  // foo\n"
10201                "  B,\n"
10202                "  C\n"
10203                "};\n"
10204                "struct B\n"
10205                "{\n"
10206                "  int x;\n"
10207                "};\n"
10208                "}\n",
10209                MozillaBraceStyle);
10210   verifyFormat("struct S\n"
10211                "{\n"
10212                "  int Type;\n"
10213                "  union\n"
10214                "  {\n"
10215                "    int x;\n"
10216                "    double y;\n"
10217                "  } Value;\n"
10218                "  class C\n"
10219                "  {\n"
10220                "    MyFavoriteType Value;\n"
10221                "  } Class;\n"
10222                "}\n",
10223                MozillaBraceStyle);
10224 }
10225 
10226 TEST_F(FormatTest, StroustrupBraceBreaking) {
10227   FormatStyle StroustrupBraceStyle = getLLVMStyle();
10228   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10229   verifyFormat("namespace a {\n"
10230                "class A {\n"
10231                "  void f()\n"
10232                "  {\n"
10233                "    if (true) {\n"
10234                "      a();\n"
10235                "      b();\n"
10236                "    }\n"
10237                "  }\n"
10238                "  void g() { return; }\n"
10239                "};\n"
10240                "struct B {\n"
10241                "  int x;\n"
10242                "};\n"
10243                "} // namespace a\n",
10244                StroustrupBraceStyle);
10245 
10246   verifyFormat("void foo()\n"
10247                "{\n"
10248                "  if (a) {\n"
10249                "    a();\n"
10250                "  }\n"
10251                "  else {\n"
10252                "    b();\n"
10253                "  }\n"
10254                "}\n",
10255                StroustrupBraceStyle);
10256 
10257   verifyFormat("#ifdef _DEBUG\n"
10258                "int foo(int i = 0)\n"
10259                "#else\n"
10260                "int foo(int i = 5)\n"
10261                "#endif\n"
10262                "{\n"
10263                "  return i;\n"
10264                "}",
10265                StroustrupBraceStyle);
10266 
10267   verifyFormat("void foo() {}\n"
10268                "void bar()\n"
10269                "#ifdef _DEBUG\n"
10270                "{\n"
10271                "  foo();\n"
10272                "}\n"
10273                "#else\n"
10274                "{\n"
10275                "}\n"
10276                "#endif",
10277                StroustrupBraceStyle);
10278 
10279   verifyFormat("void foobar() { int i = 5; }\n"
10280                "#ifdef _DEBUG\n"
10281                "void bar() {}\n"
10282                "#else\n"
10283                "void bar() { foobar(); }\n"
10284                "#endif",
10285                StroustrupBraceStyle);
10286 }
10287 
10288 TEST_F(FormatTest, AllmanBraceBreaking) {
10289   FormatStyle AllmanBraceStyle = getLLVMStyle();
10290   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
10291 
10292   EXPECT_EQ("namespace a\n"
10293             "{\n"
10294             "void f();\n"
10295             "void g();\n"
10296             "} // namespace a\n",
10297             format("namespace a\n"
10298                    "{\n"
10299                    "void f();\n"
10300                    "void g();\n"
10301                    "}\n",
10302                    AllmanBraceStyle));
10303 
10304   verifyFormat("namespace a\n"
10305                "{\n"
10306                "class A\n"
10307                "{\n"
10308                "  void f()\n"
10309                "  {\n"
10310                "    if (true)\n"
10311                "    {\n"
10312                "      a();\n"
10313                "      b();\n"
10314                "    }\n"
10315                "  }\n"
10316                "  void g() { return; }\n"
10317                "};\n"
10318                "struct B\n"
10319                "{\n"
10320                "  int x;\n"
10321                "};\n"
10322                "} // namespace a",
10323                AllmanBraceStyle);
10324 
10325   verifyFormat("void f()\n"
10326                "{\n"
10327                "  if (true)\n"
10328                "  {\n"
10329                "    a();\n"
10330                "  }\n"
10331                "  else if (false)\n"
10332                "  {\n"
10333                "    b();\n"
10334                "  }\n"
10335                "  else\n"
10336                "  {\n"
10337                "    c();\n"
10338                "  }\n"
10339                "}\n",
10340                AllmanBraceStyle);
10341 
10342   verifyFormat("void f()\n"
10343                "{\n"
10344                "  for (int i = 0; i < 10; ++i)\n"
10345                "  {\n"
10346                "    a();\n"
10347                "  }\n"
10348                "  while (false)\n"
10349                "  {\n"
10350                "    b();\n"
10351                "  }\n"
10352                "  do\n"
10353                "  {\n"
10354                "    c();\n"
10355                "  } while (false)\n"
10356                "}\n",
10357                AllmanBraceStyle);
10358 
10359   verifyFormat("void f(int a)\n"
10360                "{\n"
10361                "  switch (a)\n"
10362                "  {\n"
10363                "  case 0:\n"
10364                "    break;\n"
10365                "  case 1:\n"
10366                "  {\n"
10367                "    break;\n"
10368                "  }\n"
10369                "  case 2:\n"
10370                "  {\n"
10371                "  }\n"
10372                "  break;\n"
10373                "  default:\n"
10374                "    break;\n"
10375                "  }\n"
10376                "}\n",
10377                AllmanBraceStyle);
10378 
10379   verifyFormat("enum X\n"
10380                "{\n"
10381                "  Y = 0,\n"
10382                "}\n",
10383                AllmanBraceStyle);
10384   verifyFormat("enum X\n"
10385                "{\n"
10386                "  Y = 0\n"
10387                "}\n",
10388                AllmanBraceStyle);
10389 
10390   verifyFormat("@interface BSApplicationController ()\n"
10391                "{\n"
10392                "@private\n"
10393                "  id _extraIvar;\n"
10394                "}\n"
10395                "@end\n",
10396                AllmanBraceStyle);
10397 
10398   verifyFormat("#ifdef _DEBUG\n"
10399                "int foo(int i = 0)\n"
10400                "#else\n"
10401                "int foo(int i = 5)\n"
10402                "#endif\n"
10403                "{\n"
10404                "  return i;\n"
10405                "}",
10406                AllmanBraceStyle);
10407 
10408   verifyFormat("void foo() {}\n"
10409                "void bar()\n"
10410                "#ifdef _DEBUG\n"
10411                "{\n"
10412                "  foo();\n"
10413                "}\n"
10414                "#else\n"
10415                "{\n"
10416                "}\n"
10417                "#endif",
10418                AllmanBraceStyle);
10419 
10420   verifyFormat("void foobar() { int i = 5; }\n"
10421                "#ifdef _DEBUG\n"
10422                "void bar() {}\n"
10423                "#else\n"
10424                "void bar() { foobar(); }\n"
10425                "#endif",
10426                AllmanBraceStyle);
10427 
10428   // This shouldn't affect ObjC blocks..
10429   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10430                "  // ...\n"
10431                "  int i;\n"
10432                "}];",
10433                AllmanBraceStyle);
10434   verifyFormat("void (^block)(void) = ^{\n"
10435                "  // ...\n"
10436                "  int i;\n"
10437                "};",
10438                AllmanBraceStyle);
10439   // .. or dict literals.
10440   verifyFormat("void f()\n"
10441                "{\n"
10442                "  // ...\n"
10443                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10444                "}",
10445                AllmanBraceStyle);
10446   verifyFormat("void f()\n"
10447                "{\n"
10448                "  // ...\n"
10449                "  [object someMethod:@{a : @\"b\"}];\n"
10450                "}",
10451                AllmanBraceStyle);
10452   verifyFormat("int f()\n"
10453                "{ // comment\n"
10454                "  return 42;\n"
10455                "}",
10456                AllmanBraceStyle);
10457 
10458   AllmanBraceStyle.ColumnLimit = 19;
10459   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10460   AllmanBraceStyle.ColumnLimit = 18;
10461   verifyFormat("void f()\n"
10462                "{\n"
10463                "  int i;\n"
10464                "}",
10465                AllmanBraceStyle);
10466   AllmanBraceStyle.ColumnLimit = 80;
10467 
10468   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10469   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
10470       FormatStyle::SIS_WithoutElse;
10471   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10472   verifyFormat("void f(bool b)\n"
10473                "{\n"
10474                "  if (b)\n"
10475                "  {\n"
10476                "    return;\n"
10477                "  }\n"
10478                "}\n",
10479                BreakBeforeBraceShortIfs);
10480   verifyFormat("void f(bool b)\n"
10481                "{\n"
10482                "  if constexpr (b)\n"
10483                "  {\n"
10484                "    return;\n"
10485                "  }\n"
10486                "}\n",
10487                BreakBeforeBraceShortIfs);
10488   verifyFormat("void f(bool b)\n"
10489                "{\n"
10490                "  if (b) return;\n"
10491                "}\n",
10492                BreakBeforeBraceShortIfs);
10493   verifyFormat("void f(bool b)\n"
10494                "{\n"
10495                "  if constexpr (b) return;\n"
10496                "}\n",
10497                BreakBeforeBraceShortIfs);
10498   verifyFormat("void f(bool b)\n"
10499                "{\n"
10500                "  while (b)\n"
10501                "  {\n"
10502                "    return;\n"
10503                "  }\n"
10504                "}\n",
10505                BreakBeforeBraceShortIfs);
10506 }
10507 
10508 TEST_F(FormatTest, GNUBraceBreaking) {
10509   FormatStyle GNUBraceStyle = getLLVMStyle();
10510   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10511   verifyFormat("namespace a\n"
10512                "{\n"
10513                "class A\n"
10514                "{\n"
10515                "  void f()\n"
10516                "  {\n"
10517                "    int a;\n"
10518                "    {\n"
10519                "      int b;\n"
10520                "    }\n"
10521                "    if (true)\n"
10522                "      {\n"
10523                "        a();\n"
10524                "        b();\n"
10525                "      }\n"
10526                "  }\n"
10527                "  void g() { return; }\n"
10528                "}\n"
10529                "} // namespace a",
10530                GNUBraceStyle);
10531 
10532   verifyFormat("void f()\n"
10533                "{\n"
10534                "  if (true)\n"
10535                "    {\n"
10536                "      a();\n"
10537                "    }\n"
10538                "  else if (false)\n"
10539                "    {\n"
10540                "      b();\n"
10541                "    }\n"
10542                "  else\n"
10543                "    {\n"
10544                "      c();\n"
10545                "    }\n"
10546                "}\n",
10547                GNUBraceStyle);
10548 
10549   verifyFormat("void f()\n"
10550                "{\n"
10551                "  for (int i = 0; i < 10; ++i)\n"
10552                "    {\n"
10553                "      a();\n"
10554                "    }\n"
10555                "  while (false)\n"
10556                "    {\n"
10557                "      b();\n"
10558                "    }\n"
10559                "  do\n"
10560                "    {\n"
10561                "      c();\n"
10562                "    }\n"
10563                "  while (false);\n"
10564                "}\n",
10565                GNUBraceStyle);
10566 
10567   verifyFormat("void f(int a)\n"
10568                "{\n"
10569                "  switch (a)\n"
10570                "    {\n"
10571                "    case 0:\n"
10572                "      break;\n"
10573                "    case 1:\n"
10574                "      {\n"
10575                "        break;\n"
10576                "      }\n"
10577                "    case 2:\n"
10578                "      {\n"
10579                "      }\n"
10580                "      break;\n"
10581                "    default:\n"
10582                "      break;\n"
10583                "    }\n"
10584                "}\n",
10585                GNUBraceStyle);
10586 
10587   verifyFormat("enum X\n"
10588                "{\n"
10589                "  Y = 0,\n"
10590                "}\n",
10591                GNUBraceStyle);
10592 
10593   verifyFormat("@interface BSApplicationController ()\n"
10594                "{\n"
10595                "@private\n"
10596                "  id _extraIvar;\n"
10597                "}\n"
10598                "@end\n",
10599                GNUBraceStyle);
10600 
10601   verifyFormat("#ifdef _DEBUG\n"
10602                "int foo(int i = 0)\n"
10603                "#else\n"
10604                "int foo(int i = 5)\n"
10605                "#endif\n"
10606                "{\n"
10607                "  return i;\n"
10608                "}",
10609                GNUBraceStyle);
10610 
10611   verifyFormat("void foo() {}\n"
10612                "void bar()\n"
10613                "#ifdef _DEBUG\n"
10614                "{\n"
10615                "  foo();\n"
10616                "}\n"
10617                "#else\n"
10618                "{\n"
10619                "}\n"
10620                "#endif",
10621                GNUBraceStyle);
10622 
10623   verifyFormat("void foobar() { int i = 5; }\n"
10624                "#ifdef _DEBUG\n"
10625                "void bar() {}\n"
10626                "#else\n"
10627                "void bar() { foobar(); }\n"
10628                "#endif",
10629                GNUBraceStyle);
10630 }
10631 
10632 TEST_F(FormatTest, WebKitBraceBreaking) {
10633   FormatStyle WebKitBraceStyle = getLLVMStyle();
10634   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10635   WebKitBraceStyle.FixNamespaceComments = false;
10636   verifyFormat("namespace a {\n"
10637                "class A {\n"
10638                "  void f()\n"
10639                "  {\n"
10640                "    if (true) {\n"
10641                "      a();\n"
10642                "      b();\n"
10643                "    }\n"
10644                "  }\n"
10645                "  void g() { return; }\n"
10646                "};\n"
10647                "enum E {\n"
10648                "  A,\n"
10649                "  // foo\n"
10650                "  B,\n"
10651                "  C\n"
10652                "};\n"
10653                "struct B {\n"
10654                "  int x;\n"
10655                "};\n"
10656                "}\n",
10657                WebKitBraceStyle);
10658   verifyFormat("struct S {\n"
10659                "  int Type;\n"
10660                "  union {\n"
10661                "    int x;\n"
10662                "    double y;\n"
10663                "  } Value;\n"
10664                "  class C {\n"
10665                "    MyFavoriteType Value;\n"
10666                "  } Class;\n"
10667                "};\n",
10668                WebKitBraceStyle);
10669 }
10670 
10671 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10672   verifyFormat("void f() {\n"
10673                "  try {\n"
10674                "  } catch (const Exception &e) {\n"
10675                "  }\n"
10676                "}\n",
10677                getLLVMStyle());
10678 }
10679 
10680 TEST_F(FormatTest, UnderstandsPragmas) {
10681   verifyFormat("#pragma omp reduction(| : var)");
10682   verifyFormat("#pragma omp reduction(+ : var)");
10683 
10684   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10685             "(including parentheses).",
10686             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10687                    "(including parentheses)."));
10688 }
10689 
10690 TEST_F(FormatTest, UnderstandPragmaOption) {
10691   verifyFormat("#pragma option -C -A");
10692 
10693   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10694 }
10695 
10696 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10697   FormatStyle Style = getLLVMStyle();
10698   Style.ColumnLimit = 20;
10699 
10700   verifyFormat("int a; // the\n"
10701                "       // comment", Style);
10702   EXPECT_EQ("int a; /* first line\n"
10703             "        * second\n"
10704             "        * line third\n"
10705             "        * line\n"
10706             "        */",
10707             format("int a; /* first line\n"
10708                    "        * second\n"
10709                    "        * line third\n"
10710                    "        * line\n"
10711                    "        */",
10712                    Style));
10713   EXPECT_EQ("int a; // first line\n"
10714             "       // second\n"
10715             "       // line third\n"
10716             "       // line",
10717             format("int a; // first line\n"
10718                    "       // second line\n"
10719                    "       // third line",
10720                    Style));
10721 
10722   Style.PenaltyExcessCharacter = 90;
10723   verifyFormat("int a; // the comment", Style);
10724   EXPECT_EQ("int a; // the comment\n"
10725             "       // aaa",
10726             format("int a; // the comment aaa", Style));
10727   EXPECT_EQ("int a; /* first line\n"
10728             "        * second line\n"
10729             "        * third line\n"
10730             "        */",
10731             format("int a; /* first line\n"
10732                    "        * second line\n"
10733                    "        * third line\n"
10734                    "        */",
10735                    Style));
10736   EXPECT_EQ("int a; // first line\n"
10737             "       // second line\n"
10738             "       // third line",
10739             format("int a; // first line\n"
10740                    "       // second line\n"
10741                    "       // third line",
10742                    Style));
10743   // FIXME: Investigate why this is not getting the same layout as the test
10744   // above.
10745   EXPECT_EQ("int a; /* first line\n"
10746             "        * second line\n"
10747             "        * third line\n"
10748             "        */",
10749             format("int a; /* first line second line third line"
10750                    "\n*/",
10751                    Style));
10752 
10753   EXPECT_EQ("// foo bar baz bazfoo\n"
10754             "// foo bar foo bar\n",
10755             format("// foo bar baz bazfoo\n"
10756                    "// foo bar foo           bar\n",
10757                    Style));
10758   EXPECT_EQ("// foo bar baz bazfoo\n"
10759             "// foo bar foo bar\n",
10760             format("// foo bar baz      bazfoo\n"
10761                    "// foo            bar foo bar\n",
10762                    Style));
10763 
10764   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10765   // next one.
10766   EXPECT_EQ("// foo bar baz bazfoo\n"
10767             "// bar foo bar\n",
10768             format("// foo bar baz      bazfoo bar\n"
10769                    "// foo            bar\n",
10770                    Style));
10771 
10772   EXPECT_EQ("// foo bar baz bazfoo\n"
10773             "// foo bar baz bazfoo\n"
10774             "// bar foo bar\n",
10775             format("// foo bar baz      bazfoo\n"
10776                    "// foo bar baz      bazfoo bar\n"
10777                    "// foo bar\n",
10778                    Style));
10779 
10780   EXPECT_EQ("// foo bar baz bazfoo\n"
10781             "// foo bar baz bazfoo\n"
10782             "// bar foo bar\n",
10783             format("// foo bar baz      bazfoo\n"
10784                    "// foo bar baz      bazfoo bar\n"
10785                    "// foo           bar\n",
10786                    Style));
10787 
10788   // Make sure we do not keep protruding characters if strict mode reflow is
10789   // cheaper than keeping protruding characters.
10790   Style.ColumnLimit = 21;
10791   EXPECT_EQ("// foo foo foo foo\n"
10792             "// foo foo foo foo\n"
10793             "// foo foo foo foo\n",
10794             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10795                            Style));
10796 
10797   EXPECT_EQ("int a = /* long block\n"
10798             "           comment */\n"
10799             "    42;",
10800             format("int a = /* long block comment */ 42;", Style));
10801 }
10802 
10803 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10804   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10805   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10806                                   << " differs from Style #0"
10807 
10808 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10809   SmallVector<FormatStyle, 3> Styles;
10810   Styles.resize(3);
10811 
10812   Styles[0] = getLLVMStyle();
10813   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10814   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10815   EXPECT_ALL_STYLES_EQUAL(Styles);
10816 
10817   Styles[0] = getGoogleStyle();
10818   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10819   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10820   EXPECT_ALL_STYLES_EQUAL(Styles);
10821 
10822   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10823   EXPECT_TRUE(
10824       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10825   EXPECT_TRUE(
10826       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10827   EXPECT_ALL_STYLES_EQUAL(Styles);
10828 
10829   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10830   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10831   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10832   EXPECT_ALL_STYLES_EQUAL(Styles);
10833 
10834   Styles[0] = getMozillaStyle();
10835   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10836   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10837   EXPECT_ALL_STYLES_EQUAL(Styles);
10838 
10839   Styles[0] = getWebKitStyle();
10840   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10841   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10842   EXPECT_ALL_STYLES_EQUAL(Styles);
10843 
10844   Styles[0] = getGNUStyle();
10845   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10846   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10847   EXPECT_ALL_STYLES_EQUAL(Styles);
10848 
10849   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10850 }
10851 
10852 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10853   SmallVector<FormatStyle, 8> Styles;
10854   Styles.resize(2);
10855 
10856   Styles[0] = getGoogleStyle();
10857   Styles[1] = getLLVMStyle();
10858   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10859   EXPECT_ALL_STYLES_EQUAL(Styles);
10860 
10861   Styles.resize(5);
10862   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10863   Styles[1] = getLLVMStyle();
10864   Styles[1].Language = FormatStyle::LK_JavaScript;
10865   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10866 
10867   Styles[2] = getLLVMStyle();
10868   Styles[2].Language = FormatStyle::LK_JavaScript;
10869   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10870                                   "BasedOnStyle: Google",
10871                                   &Styles[2])
10872                    .value());
10873 
10874   Styles[3] = getLLVMStyle();
10875   Styles[3].Language = FormatStyle::LK_JavaScript;
10876   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10877                                   "Language: JavaScript",
10878                                   &Styles[3])
10879                    .value());
10880 
10881   Styles[4] = getLLVMStyle();
10882   Styles[4].Language = FormatStyle::LK_JavaScript;
10883   EXPECT_EQ(0, parseConfiguration("---\n"
10884                                   "BasedOnStyle: LLVM\n"
10885                                   "IndentWidth: 123\n"
10886                                   "---\n"
10887                                   "BasedOnStyle: Google\n"
10888                                   "Language: JavaScript",
10889                                   &Styles[4])
10890                    .value());
10891   EXPECT_ALL_STYLES_EQUAL(Styles);
10892 }
10893 
10894 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10895   Style.FIELD = false;                                                         \
10896   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10897   EXPECT_TRUE(Style.FIELD);                                                    \
10898   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10899   EXPECT_FALSE(Style.FIELD);
10900 
10901 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10902 
10903 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10904   Style.STRUCT.FIELD = false;                                                  \
10905   EXPECT_EQ(0,                                                                 \
10906             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10907                 .value());                                                     \
10908   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10909   EXPECT_EQ(0,                                                                 \
10910             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10911                 .value());                                                     \
10912   EXPECT_FALSE(Style.STRUCT.FIELD);
10913 
10914 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10915   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10916 
10917 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10918   EXPECT_NE(VALUE, Style.FIELD);                                               \
10919   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10920   EXPECT_EQ(VALUE, Style.FIELD)
10921 
10922 TEST_F(FormatTest, ParsesConfigurationBools) {
10923   FormatStyle Style = {};
10924   Style.Language = FormatStyle::LK_Cpp;
10925   CHECK_PARSE_BOOL(AlignOperands);
10926   CHECK_PARSE_BOOL(AlignTrailingComments);
10927   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10928   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10929   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10930   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10931   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10932   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10933   CHECK_PARSE_BOOL(BinPackArguments);
10934   CHECK_PARSE_BOOL(BinPackParameters);
10935   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10936   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10937   CHECK_PARSE_BOOL(BreakStringLiterals);
10938   CHECK_PARSE_BOOL(CompactNamespaces);
10939   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10940   CHECK_PARSE_BOOL(DerivePointerAlignment);
10941   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10942   CHECK_PARSE_BOOL(DisableFormat);
10943   CHECK_PARSE_BOOL(IndentCaseLabels);
10944   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10945   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10946   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10947   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10948   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10949   CHECK_PARSE_BOOL(ReflowComments);
10950   CHECK_PARSE_BOOL(SortIncludes);
10951   CHECK_PARSE_BOOL(SortUsingDeclarations);
10952   CHECK_PARSE_BOOL(SpacesInParentheses);
10953   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10954   CHECK_PARSE_BOOL(SpacesInAngles);
10955   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10956   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10957   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10958   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10959   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10960   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10961   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
10962   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10963   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10964   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10965 
10966   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10967   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10968   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10969   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10970   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10971   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10972   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10973   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10974   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10975   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10976   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10977   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10978   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10979   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10980   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10981 }
10982 
10983 #undef CHECK_PARSE_BOOL
10984 
10985 TEST_F(FormatTest, ParsesConfiguration) {
10986   FormatStyle Style = {};
10987   Style.Language = FormatStyle::LK_Cpp;
10988   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10989   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10990               ConstructorInitializerIndentWidth, 1234u);
10991   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10992   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10993   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10994   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10995               PenaltyBreakAssignment, 1234u);
10996   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10997               PenaltyBreakBeforeFirstCallParameter, 1234u);
10998   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
10999               PenaltyBreakTemplateDeclaration, 1234u);
11000   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
11001   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
11002               PenaltyReturnTypeOnItsOwnLine, 1234u);
11003   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
11004               SpacesBeforeTrailingComments, 1234u);
11005   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
11006   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
11007   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
11008 
11009   Style.PointerAlignment = FormatStyle::PAS_Middle;
11010   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
11011               FormatStyle::PAS_Left);
11012   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
11013               FormatStyle::PAS_Right);
11014   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
11015               FormatStyle::PAS_Middle);
11016   // For backward compatibility:
11017   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
11018               FormatStyle::PAS_Left);
11019   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
11020               FormatStyle::PAS_Right);
11021   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
11022               FormatStyle::PAS_Middle);
11023 
11024   Style.Standard = FormatStyle::LS_Auto;
11025   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
11026   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
11027   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
11028   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
11029   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
11030 
11031   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11032   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
11033               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
11034   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
11035               FormatStyle::BOS_None);
11036   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
11037               FormatStyle::BOS_All);
11038   // For backward compatibility:
11039   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
11040               FormatStyle::BOS_None);
11041   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
11042               FormatStyle::BOS_All);
11043 
11044   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
11045   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
11046               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11047   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
11048               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
11049   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
11050               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
11051   // For backward compatibility:
11052   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
11053               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11054 
11055   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
11056   CHECK_PARSE("BreakInheritanceList: BeforeComma",
11057               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11058   CHECK_PARSE("BreakInheritanceList: AfterColon",
11059               BreakInheritanceList, FormatStyle::BILS_AfterColon);
11060   CHECK_PARSE("BreakInheritanceList: BeforeColon",
11061               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
11062   // For backward compatibility:
11063   CHECK_PARSE("BreakBeforeInheritanceComma: true",
11064               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11065 
11066   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11067   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
11068               FormatStyle::BAS_Align);
11069   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
11070               FormatStyle::BAS_DontAlign);
11071   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
11072               FormatStyle::BAS_AlwaysBreak);
11073   // For backward compatibility:
11074   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
11075               FormatStyle::BAS_DontAlign);
11076   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
11077               FormatStyle::BAS_Align);
11078 
11079   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11080   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
11081               FormatStyle::ENAS_DontAlign);
11082   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
11083               FormatStyle::ENAS_Left);
11084   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
11085               FormatStyle::ENAS_Right);
11086   // For backward compatibility:
11087   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
11088               FormatStyle::ENAS_Left);
11089   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
11090               FormatStyle::ENAS_Right);
11091 
11092   Style.UseTab = FormatStyle::UT_ForIndentation;
11093   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
11094   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
11095   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
11096   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
11097               FormatStyle::UT_ForContinuationAndIndentation);
11098   // For backward compatibility:
11099   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
11100   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
11101 
11102   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11103   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
11104               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11105   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
11106               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
11107   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
11108               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
11109   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
11110               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11111   // For backward compatibility:
11112   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
11113               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11114   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
11115               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11116 
11117   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
11118   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
11119               FormatStyle::SBPO_Never);
11120   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
11121               FormatStyle::SBPO_Always);
11122   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
11123               FormatStyle::SBPO_ControlStatements);
11124   // For backward compatibility:
11125   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
11126               FormatStyle::SBPO_Never);
11127   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
11128               FormatStyle::SBPO_ControlStatements);
11129 
11130   Style.ColumnLimit = 123;
11131   FormatStyle BaseStyle = getLLVMStyle();
11132   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
11133   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
11134 
11135   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
11136   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
11137               FormatStyle::BS_Attach);
11138   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
11139               FormatStyle::BS_Linux);
11140   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
11141               FormatStyle::BS_Mozilla);
11142   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
11143               FormatStyle::BS_Stroustrup);
11144   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
11145               FormatStyle::BS_Allman);
11146   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
11147   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
11148               FormatStyle::BS_WebKit);
11149   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
11150               FormatStyle::BS_Custom);
11151 
11152   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11153   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
11154               FormatStyle::RTBS_None);
11155   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
11156               FormatStyle::RTBS_All);
11157   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
11158               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
11159   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
11160               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
11161   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
11162               AlwaysBreakAfterReturnType,
11163               FormatStyle::RTBS_TopLevelDefinitions);
11164 
11165   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11166   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
11167               FormatStyle::BTDS_No);
11168   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
11169               FormatStyle::BTDS_MultiLine);
11170   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
11171               FormatStyle::BTDS_Yes);
11172   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
11173               FormatStyle::BTDS_MultiLine);
11174   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
11175               FormatStyle::BTDS_Yes);
11176 
11177   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
11178   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
11179               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
11180   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
11181               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
11182   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
11183               AlwaysBreakAfterDefinitionReturnType,
11184               FormatStyle::DRTBS_TopLevel);
11185 
11186   Style.NamespaceIndentation = FormatStyle::NI_All;
11187   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
11188               FormatStyle::NI_None);
11189   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
11190               FormatStyle::NI_Inner);
11191   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
11192               FormatStyle::NI_All);
11193 
11194   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
11195   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
11196               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
11197   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
11198               AllowShortIfStatementsOnASingleLine,
11199               FormatStyle::SIS_WithoutElse);
11200   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
11201               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always);
11202   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
11203               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
11204   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
11205               AllowShortIfStatementsOnASingleLine,
11206               FormatStyle::SIS_WithoutElse);
11207 
11208   // FIXME: This is required because parsing a configuration simply overwrites
11209   // the first N elements of the list instead of resetting it.
11210   Style.ForEachMacros.clear();
11211   std::vector<std::string> BoostForeach;
11212   BoostForeach.push_back("BOOST_FOREACH");
11213   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
11214   std::vector<std::string> BoostAndQForeach;
11215   BoostAndQForeach.push_back("BOOST_FOREACH");
11216   BoostAndQForeach.push_back("Q_FOREACH");
11217   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
11218               BoostAndQForeach);
11219 
11220   Style.StatementMacros.clear();
11221   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
11222               std::vector<std::string>{"QUNUSED"});
11223   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
11224               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
11225 
11226   Style.IncludeStyle.IncludeCategories.clear();
11227   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
11228       {"abc/.*", 2}, {".*", 1}};
11229   CHECK_PARSE("IncludeCategories:\n"
11230               "  - Regex: abc/.*\n"
11231               "    Priority: 2\n"
11232               "  - Regex: .*\n"
11233               "    Priority: 1",
11234               IncludeStyle.IncludeCategories, ExpectedCategories);
11235   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
11236               "abc$");
11237 
11238   Style.RawStringFormats.clear();
11239   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
11240       {
11241           FormatStyle::LK_TextProto,
11242           {"pb", "proto"},
11243           {"PARSE_TEXT_PROTO"},
11244           /*CanonicalDelimiter=*/"",
11245           "llvm",
11246       },
11247       {
11248           FormatStyle::LK_Cpp,
11249           {"cc", "cpp"},
11250           {"C_CODEBLOCK", "CPPEVAL"},
11251           /*CanonicalDelimiter=*/"cc",
11252           /*BasedOnStyle=*/"",
11253       },
11254   };
11255 
11256   CHECK_PARSE("RawStringFormats:\n"
11257               "  - Language: TextProto\n"
11258               "    Delimiters:\n"
11259               "      - 'pb'\n"
11260               "      - 'proto'\n"
11261               "    EnclosingFunctions:\n"
11262               "      - 'PARSE_TEXT_PROTO'\n"
11263               "    BasedOnStyle: llvm\n"
11264               "  - Language: Cpp\n"
11265               "    Delimiters:\n"
11266               "      - 'cc'\n"
11267               "      - 'cpp'\n"
11268               "    EnclosingFunctions:\n"
11269               "      - 'C_CODEBLOCK'\n"
11270               "      - 'CPPEVAL'\n"
11271               "    CanonicalDelimiter: 'cc'",
11272               RawStringFormats, ExpectedRawStringFormats);
11273 }
11274 
11275 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
11276   FormatStyle Style = {};
11277   Style.Language = FormatStyle::LK_Cpp;
11278   CHECK_PARSE("Language: Cpp\n"
11279               "IndentWidth: 12",
11280               IndentWidth, 12u);
11281   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
11282                                "IndentWidth: 34",
11283                                &Style),
11284             ParseError::Unsuitable);
11285   EXPECT_EQ(12u, Style.IndentWidth);
11286   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11287   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11288 
11289   Style.Language = FormatStyle::LK_JavaScript;
11290   CHECK_PARSE("Language: JavaScript\n"
11291               "IndentWidth: 12",
11292               IndentWidth, 12u);
11293   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
11294   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
11295                                "IndentWidth: 34",
11296                                &Style),
11297             ParseError::Unsuitable);
11298   EXPECT_EQ(23u, Style.IndentWidth);
11299   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11300   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11301 
11302   CHECK_PARSE("BasedOnStyle: LLVM\n"
11303               "IndentWidth: 67",
11304               IndentWidth, 67u);
11305 
11306   CHECK_PARSE("---\n"
11307               "Language: JavaScript\n"
11308               "IndentWidth: 12\n"
11309               "---\n"
11310               "Language: Cpp\n"
11311               "IndentWidth: 34\n"
11312               "...\n",
11313               IndentWidth, 12u);
11314 
11315   Style.Language = FormatStyle::LK_Cpp;
11316   CHECK_PARSE("---\n"
11317               "Language: JavaScript\n"
11318               "IndentWidth: 12\n"
11319               "---\n"
11320               "Language: Cpp\n"
11321               "IndentWidth: 34\n"
11322               "...\n",
11323               IndentWidth, 34u);
11324   CHECK_PARSE("---\n"
11325               "IndentWidth: 78\n"
11326               "---\n"
11327               "Language: JavaScript\n"
11328               "IndentWidth: 56\n"
11329               "...\n",
11330               IndentWidth, 78u);
11331 
11332   Style.ColumnLimit = 123;
11333   Style.IndentWidth = 234;
11334   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
11335   Style.TabWidth = 345;
11336   EXPECT_FALSE(parseConfiguration("---\n"
11337                                   "IndentWidth: 456\n"
11338                                   "BreakBeforeBraces: Allman\n"
11339                                   "---\n"
11340                                   "Language: JavaScript\n"
11341                                   "IndentWidth: 111\n"
11342                                   "TabWidth: 111\n"
11343                                   "---\n"
11344                                   "Language: Cpp\n"
11345                                   "BreakBeforeBraces: Stroustrup\n"
11346                                   "TabWidth: 789\n"
11347                                   "...\n",
11348                                   &Style));
11349   EXPECT_EQ(123u, Style.ColumnLimit);
11350   EXPECT_EQ(456u, Style.IndentWidth);
11351   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11352   EXPECT_EQ(789u, Style.TabWidth);
11353 
11354   EXPECT_EQ(parseConfiguration("---\n"
11355                                "Language: JavaScript\n"
11356                                "IndentWidth: 56\n"
11357                                "---\n"
11358                                "IndentWidth: 78\n"
11359                                "...\n",
11360                                &Style),
11361             ParseError::Error);
11362   EXPECT_EQ(parseConfiguration("---\n"
11363                                "Language: JavaScript\n"
11364                                "IndentWidth: 56\n"
11365                                "---\n"
11366                                "Language: JavaScript\n"
11367                                "IndentWidth: 78\n"
11368                                "...\n",
11369                                &Style),
11370             ParseError::Error);
11371 
11372   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11373 }
11374 
11375 #undef CHECK_PARSE
11376 
11377 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11378   FormatStyle Style = {};
11379   Style.Language = FormatStyle::LK_JavaScript;
11380   Style.BreakBeforeTernaryOperators = true;
11381   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11382   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11383 
11384   Style.BreakBeforeTernaryOperators = true;
11385   EXPECT_EQ(0, parseConfiguration("---\n"
11386                                   "BasedOnStyle: Google\n"
11387                                   "---\n"
11388                                   "Language: JavaScript\n"
11389                                   "IndentWidth: 76\n"
11390                                   "...\n",
11391                                   &Style)
11392                    .value());
11393   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11394   EXPECT_EQ(76u, Style.IndentWidth);
11395   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11396 }
11397 
11398 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11399   FormatStyle Style = getLLVMStyle();
11400   std::string YAML = configurationAsText(Style);
11401   FormatStyle ParsedStyle = {};
11402   ParsedStyle.Language = FormatStyle::LK_Cpp;
11403   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11404   EXPECT_EQ(Style, ParsedStyle);
11405 }
11406 
11407 TEST_F(FormatTest, WorksFor8bitEncodings) {
11408   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11409             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11410             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11411             "\"\xef\xee\xf0\xf3...\"",
11412             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11413                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11414                    "\xef\xee\xf0\xf3...\"",
11415                    getLLVMStyleWithColumns(12)));
11416 }
11417 
11418 TEST_F(FormatTest, HandlesUTF8BOM) {
11419   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11420   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11421             format("\xef\xbb\xbf#include <iostream>"));
11422   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11423             format("\xef\xbb\xbf\n#include <iostream>"));
11424 }
11425 
11426 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11427 #if !defined(_MSC_VER)
11428 
11429 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11430   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11431                getLLVMStyleWithColumns(35));
11432   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11433                getLLVMStyleWithColumns(31));
11434   verifyFormat("// Однажды в студёную зимнюю пору...",
11435                getLLVMStyleWithColumns(36));
11436   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11437   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11438                getLLVMStyleWithColumns(39));
11439   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11440                getLLVMStyleWithColumns(35));
11441 }
11442 
11443 TEST_F(FormatTest, SplitsUTF8Strings) {
11444   // Non-printable characters' width is currently considered to be the length in
11445   // bytes in UTF8. The characters can be displayed in very different manner
11446   // (zero-width, single width with a substitution glyph, expanded to their code
11447   // (e.g. "<8d>"), so there's no single correct way to handle them.
11448   EXPECT_EQ("\"aaaaÄ\"\n"
11449             "\"\xc2\x8d\";",
11450             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11451   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11452             "\"\xc2\x8d\";",
11453             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11454   EXPECT_EQ("\"Однажды, в \"\n"
11455             "\"студёную \"\n"
11456             "\"зимнюю \"\n"
11457             "\"пору,\"",
11458             format("\"Однажды, в студёную зимнюю пору,\"",
11459                    getLLVMStyleWithColumns(13)));
11460   EXPECT_EQ(
11461       "\"一 二 三 \"\n"
11462       "\"四 五六 \"\n"
11463       "\"七 八 九 \"\n"
11464       "\"十\"",
11465       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11466   EXPECT_EQ("\"一\t\"\n"
11467             "\"二 \t\"\n"
11468             "\"三 四 \"\n"
11469             "\"五\t\"\n"
11470             "\"六 \t\"\n"
11471             "\"七 \"\n"
11472             "\"八九十\tqq\"",
11473             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11474                    getLLVMStyleWithColumns(11)));
11475 
11476   // UTF8 character in an escape sequence.
11477   EXPECT_EQ("\"aaaaaa\"\n"
11478             "\"\\\xC2\x8D\"",
11479             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11480 }
11481 
11482 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11483   EXPECT_EQ("const char *sssss =\n"
11484             "    \"一二三四五六七八\\\n"
11485             " 九 十\";",
11486             format("const char *sssss = \"一二三四五六七八\\\n"
11487                    " 九 十\";",
11488                    getLLVMStyleWithColumns(30)));
11489 }
11490 
11491 TEST_F(FormatTest, SplitsUTF8LineComments) {
11492   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11493             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11494   EXPECT_EQ("// Я из лесу\n"
11495             "// вышел; был\n"
11496             "// сильный\n"
11497             "// мороз.",
11498             format("// Я из лесу вышел; был сильный мороз.",
11499                    getLLVMStyleWithColumns(13)));
11500   EXPECT_EQ("// 一二三\n"
11501             "// 四五六七\n"
11502             "// 八  九\n"
11503             "// 十",
11504             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11505 }
11506 
11507 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11508   EXPECT_EQ("/* Гляжу,\n"
11509             " * поднимается\n"
11510             " * медленно в\n"
11511             " * гору\n"
11512             " * Лошадка,\n"
11513             " * везущая\n"
11514             " * хворосту\n"
11515             " * воз. */",
11516             format("/* Гляжу, поднимается медленно в гору\n"
11517                    " * Лошадка, везущая хворосту воз. */",
11518                    getLLVMStyleWithColumns(13)));
11519   EXPECT_EQ(
11520       "/* 一二三\n"
11521       " * 四五六七\n"
11522       " * 八  九\n"
11523       " * 十  */",
11524       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11525   EXPECT_EQ("/* �������� ��������\n"
11526             " * ��������\n"
11527             " * ������-�� */",
11528             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11529 }
11530 
11531 #endif // _MSC_VER
11532 
11533 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11534   FormatStyle Style = getLLVMStyle();
11535 
11536   Style.ConstructorInitializerIndentWidth = 4;
11537   verifyFormat(
11538       "SomeClass::Constructor()\n"
11539       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11540       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11541       Style);
11542 
11543   Style.ConstructorInitializerIndentWidth = 2;
11544   verifyFormat(
11545       "SomeClass::Constructor()\n"
11546       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11547       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11548       Style);
11549 
11550   Style.ConstructorInitializerIndentWidth = 0;
11551   verifyFormat(
11552       "SomeClass::Constructor()\n"
11553       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11554       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11555       Style);
11556   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11557   verifyFormat(
11558       "SomeLongTemplateVariableName<\n"
11559       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11560       Style);
11561   verifyFormat(
11562       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11563       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11564       Style);
11565 }
11566 
11567 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11568   FormatStyle Style = getLLVMStyle();
11569   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11570   Style.ConstructorInitializerIndentWidth = 4;
11571   verifyFormat("SomeClass::Constructor()\n"
11572                "    : a(a)\n"
11573                "    , b(b)\n"
11574                "    , c(c) {}",
11575                Style);
11576   verifyFormat("SomeClass::Constructor()\n"
11577                "    : a(a) {}",
11578                Style);
11579 
11580   Style.ColumnLimit = 0;
11581   verifyFormat("SomeClass::Constructor()\n"
11582                "    : a(a) {}",
11583                Style);
11584   verifyFormat("SomeClass::Constructor() noexcept\n"
11585                "    : a(a) {}",
11586                Style);
11587   verifyFormat("SomeClass::Constructor()\n"
11588                "    : a(a)\n"
11589                "    , b(b)\n"
11590                "    , c(c) {}",
11591                Style);
11592   verifyFormat("SomeClass::Constructor()\n"
11593                "    : a(a) {\n"
11594                "  foo();\n"
11595                "  bar();\n"
11596                "}",
11597                Style);
11598 
11599   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11600   verifyFormat("SomeClass::Constructor()\n"
11601                "    : a(a)\n"
11602                "    , b(b)\n"
11603                "    , c(c) {\n}",
11604                Style);
11605   verifyFormat("SomeClass::Constructor()\n"
11606                "    : a(a) {\n}",
11607                Style);
11608 
11609   Style.ColumnLimit = 80;
11610   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11611   Style.ConstructorInitializerIndentWidth = 2;
11612   verifyFormat("SomeClass::Constructor()\n"
11613                "  : a(a)\n"
11614                "  , b(b)\n"
11615                "  , c(c) {}",
11616                Style);
11617 
11618   Style.ConstructorInitializerIndentWidth = 0;
11619   verifyFormat("SomeClass::Constructor()\n"
11620                ": a(a)\n"
11621                ", b(b)\n"
11622                ", c(c) {}",
11623                Style);
11624 
11625   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11626   Style.ConstructorInitializerIndentWidth = 4;
11627   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11628   verifyFormat(
11629       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11630       Style);
11631   verifyFormat(
11632       "SomeClass::Constructor()\n"
11633       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11634       Style);
11635   Style.ConstructorInitializerIndentWidth = 4;
11636   Style.ColumnLimit = 60;
11637   verifyFormat("SomeClass::Constructor()\n"
11638                "    : aaaaaaaa(aaaaaaaa)\n"
11639                "    , aaaaaaaa(aaaaaaaa)\n"
11640                "    , aaaaaaaa(aaaaaaaa) {}",
11641                Style);
11642 }
11643 
11644 TEST_F(FormatTest, Destructors) {
11645   verifyFormat("void F(int &i) { i.~int(); }");
11646   verifyFormat("void F(int &i) { i->~int(); }");
11647 }
11648 
11649 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11650   FormatStyle Style = getWebKitStyle();
11651 
11652   // Don't indent in outer namespaces.
11653   verifyFormat("namespace outer {\n"
11654                "int i;\n"
11655                "namespace inner {\n"
11656                "    int i;\n"
11657                "} // namespace inner\n"
11658                "} // namespace outer\n"
11659                "namespace other_outer {\n"
11660                "int i;\n"
11661                "}",
11662                Style);
11663 
11664   // Don't indent case labels.
11665   verifyFormat("switch (variable) {\n"
11666                "case 1:\n"
11667                "case 2:\n"
11668                "    doSomething();\n"
11669                "    break;\n"
11670                "default:\n"
11671                "    ++variable;\n"
11672                "}",
11673                Style);
11674 
11675   // Wrap before binary operators.
11676   EXPECT_EQ("void f()\n"
11677             "{\n"
11678             "    if (aaaaaaaaaaaaaaaa\n"
11679             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11680             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11681             "        return;\n"
11682             "}",
11683             format("void f() {\n"
11684                    "if (aaaaaaaaaaaaaaaa\n"
11685                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11686                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11687                    "return;\n"
11688                    "}",
11689                    Style));
11690 
11691   // Allow functions on a single line.
11692   verifyFormat("void f() { return; }", Style);
11693 
11694   // Constructor initializers are formatted one per line with the "," on the
11695   // new line.
11696   verifyFormat("Constructor()\n"
11697                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11698                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11699                "          aaaaaaaaaaaaaa)\n"
11700                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11701                "{\n"
11702                "}",
11703                Style);
11704   verifyFormat("SomeClass::Constructor()\n"
11705                "    : a(a)\n"
11706                "{\n"
11707                "}",
11708                Style);
11709   EXPECT_EQ("SomeClass::Constructor()\n"
11710             "    : a(a)\n"
11711             "{\n"
11712             "}",
11713             format("SomeClass::Constructor():a(a){}", Style));
11714   verifyFormat("SomeClass::Constructor()\n"
11715                "    : a(a)\n"
11716                "    , b(b)\n"
11717                "    , c(c)\n"
11718                "{\n"
11719                "}",
11720                Style);
11721   verifyFormat("SomeClass::Constructor()\n"
11722                "    : a(a)\n"
11723                "{\n"
11724                "    foo();\n"
11725                "    bar();\n"
11726                "}",
11727                Style);
11728 
11729   // Access specifiers should be aligned left.
11730   verifyFormat("class C {\n"
11731                "public:\n"
11732                "    int i;\n"
11733                "};",
11734                Style);
11735 
11736   // Do not align comments.
11737   verifyFormat("int a; // Do not\n"
11738                "double b; // align comments.",
11739                Style);
11740 
11741   // Do not align operands.
11742   EXPECT_EQ("ASSERT(aaaa\n"
11743             "    || bbbb);",
11744             format("ASSERT ( aaaa\n||bbbb);", Style));
11745 
11746   // Accept input's line breaks.
11747   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11748             "    || bbbbbbbbbbbbbbb) {\n"
11749             "    i++;\n"
11750             "}",
11751             format("if (aaaaaaaaaaaaaaa\n"
11752                    "|| bbbbbbbbbbbbbbb) { i++; }",
11753                    Style));
11754   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11755             "    i++;\n"
11756             "}",
11757             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11758 
11759   // Don't automatically break all macro definitions (llvm.org/PR17842).
11760   verifyFormat("#define aNumber 10", Style);
11761   // However, generally keep the line breaks that the user authored.
11762   EXPECT_EQ("#define aNumber \\\n"
11763             "    10",
11764             format("#define aNumber \\\n"
11765                    " 10",
11766                    Style));
11767 
11768   // Keep empty and one-element array literals on a single line.
11769   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11770             "                                  copyItems:YES];",
11771             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11772                    "copyItems:YES];",
11773                    Style));
11774   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11775             "                                  copyItems:YES];",
11776             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11777                    "             copyItems:YES];",
11778                    Style));
11779   // FIXME: This does not seem right, there should be more indentation before
11780   // the array literal's entries. Nested blocks have the same problem.
11781   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11782             "    @\"a\",\n"
11783             "    @\"a\"\n"
11784             "]\n"
11785             "                                  copyItems:YES];",
11786             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11787                    "     @\"a\",\n"
11788                    "     @\"a\"\n"
11789                    "     ]\n"
11790                    "       copyItems:YES];",
11791                    Style));
11792   EXPECT_EQ(
11793       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11794       "                                  copyItems:YES];",
11795       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11796              "   copyItems:YES];",
11797              Style));
11798 
11799   verifyFormat("[self.a b:c c:d];", Style);
11800   EXPECT_EQ("[self.a b:c\n"
11801             "        c:d];",
11802             format("[self.a b:c\n"
11803                    "c:d];",
11804                    Style));
11805 }
11806 
11807 TEST_F(FormatTest, FormatsLambdas) {
11808   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11809   verifyFormat(
11810       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
11811   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11812   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11813   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11814   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11815   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11816   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11817   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11818   verifyFormat("int x = f(*+[] {});");
11819   verifyFormat("void f() {\n"
11820                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11821                "}\n");
11822   verifyFormat("void f() {\n"
11823                "  other(x.begin(), //\n"
11824                "        x.end(),   //\n"
11825                "        [&](int, int) { return 1; });\n"
11826                "}\n");
11827   verifyFormat("void f() {\n"
11828                "  other.other.other.other.other(\n"
11829                "      x.begin(), x.end(),\n"
11830                "      [something, rather](int, int, int, int, int, int, int) { return 1; });\n"
11831                "}\n");
11832   verifyFormat("void f() {\n"
11833                "  other.other.other.other.other(\n"
11834                "      x.begin(), x.end(),\n"
11835                "      [something, rather](int, int, int, int, int, int, int) {\n"
11836                "        //\n"
11837                "      });\n"
11838                "}\n");
11839   verifyFormat("SomeFunction([]() { // A cool function...\n"
11840                "  return 43;\n"
11841                "});");
11842   EXPECT_EQ("SomeFunction([]() {\n"
11843             "#define A a\n"
11844             "  return 43;\n"
11845             "});",
11846             format("SomeFunction([](){\n"
11847                    "#define A a\n"
11848                    "return 43;\n"
11849                    "});"));
11850   verifyFormat("void f() {\n"
11851                "  SomeFunction([](decltype(x), A *a) {});\n"
11852                "}");
11853   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11854                "    [](const aaaaaaaaaa &a) { return a; });");
11855   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11856                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11857                "});");
11858   verifyFormat("Constructor()\n"
11859                "    : Field([] { // comment\n"
11860                "        int i;\n"
11861                "      }) {}");
11862   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11863                "  return some_parameter.size();\n"
11864                "};");
11865   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11866                "    [](const string &s) { return s; };");
11867   verifyFormat("int i = aaaaaa ? 1 //\n"
11868                "               : [] {\n"
11869                "                   return 2; //\n"
11870                "                 }();");
11871   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11872                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11873                "                  return x == 2; // force break\n"
11874                "                });");
11875   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11876                "    [=](int iiiiiiiiiiii) {\n"
11877                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11878                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11879                "    });",
11880                getLLVMStyleWithColumns(60));
11881   verifyFormat("SomeFunction({[&] {\n"
11882                "                // comment\n"
11883                "              },\n"
11884                "              [&] {\n"
11885                "                // comment\n"
11886                "              }});");
11887   verifyFormat("SomeFunction({[&] {\n"
11888                "  // comment\n"
11889                "}});");
11890   verifyFormat("virtual aaaaaaaaaaaaaaaa(\n"
11891                "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
11892                "    aaaaa aaaaaaaaa);");
11893 
11894   // Lambdas with return types.
11895   verifyFormat("int c = []() -> int { return 2; }();\n");
11896   verifyFormat("int c = []() -> int * { return 2; }();\n");
11897   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11898   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11899   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11900   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11901   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11902   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11903   verifyFormat("[a, a]() -> a<1> {};");
11904   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
11905   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
11906   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
11907   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
11908   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
11909   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
11910   verifyFormat("[]() -> foo<!5> { return {}; };");
11911   verifyFormat("[]() -> foo<~5> { return {}; };");
11912   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
11913   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
11914   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
11915   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
11916   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
11917   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
11918   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
11919   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
11920   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
11921   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
11922   verifyFormat("namespace bar {\n"
11923               "// broken:\n"
11924               "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
11925               "} // namespace bar");
11926   verifyFormat("namespace bar {\n"
11927               "// broken:\n"
11928               "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
11929               "} // namespace bar");
11930   verifyFormat("namespace bar {\n"
11931               "// broken:\n"
11932               "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
11933               "} // namespace bar");
11934   verifyFormat("namespace bar {\n"
11935               "// broken:\n"
11936               "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
11937               "} // namespace bar");
11938   verifyFormat("namespace bar {\n"
11939               "// broken:\n"
11940               "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
11941               "} // namespace bar");
11942   verifyFormat("namespace bar {\n"
11943               "// broken:\n"
11944               "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
11945               "} // namespace bar");
11946   verifyFormat("namespace bar {\n"
11947               "// broken:\n"
11948               "auto foo{[]() -> foo<!5> { return {}; }};\n"
11949               "} // namespace bar");
11950   verifyFormat("namespace bar {\n"
11951               "// broken:\n"
11952               "auto foo{[]() -> foo<~5> { return {}; }};\n"
11953               "} // namespace bar");
11954   verifyFormat("namespace bar {\n"
11955               "// broken:\n"
11956               "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
11957               "} // namespace bar");
11958   verifyFormat("namespace bar {\n"
11959               "// broken:\n"
11960               "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
11961               "} // namespace bar");
11962   verifyFormat("namespace bar {\n"
11963               "// broken:\n"
11964               "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
11965               "} // namespace bar");
11966   verifyFormat("namespace bar {\n"
11967               "// broken:\n"
11968               "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
11969               "} // namespace bar");
11970   verifyFormat("namespace bar {\n"
11971               "// broken:\n"
11972               "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
11973               "} // namespace bar");
11974   verifyFormat("namespace bar {\n"
11975               "// broken:\n"
11976               "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
11977               "} // namespace bar");
11978   verifyFormat("namespace bar {\n"
11979               "// broken:\n"
11980               "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
11981               "} // namespace bar");
11982   verifyFormat("namespace bar {\n"
11983               "// broken:\n"
11984               "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
11985               "} // namespace bar");
11986   verifyFormat("namespace bar {\n"
11987               "// broken:\n"
11988               "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
11989               "} // namespace bar");
11990   verifyFormat("namespace bar {\n"
11991               "// broken:\n"
11992               "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
11993               "} // namespace bar");
11994   verifyFormat("[]() -> a<1> {};");
11995   verifyFormat("[]() -> a<1> { ; };");
11996   verifyFormat("[]() -> a<1> { ; }();");
11997   verifyFormat("[a, a]() -> a<true> {};");
11998   verifyFormat("[]() -> a<true> {};");
11999   verifyFormat("[]() -> a<true> { ; };");
12000   verifyFormat("[]() -> a<true> { ; }();");
12001   verifyFormat("[a, a]() -> a<false> {};");
12002   verifyFormat("[]() -> a<false> {};");
12003   verifyFormat("[]() -> a<false> { ; };");
12004   verifyFormat("[]() -> a<false> { ; }();");
12005   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
12006   verifyFormat("namespace bar {\n"
12007                "auto foo{[]() -> foo<false> { ; }};\n"
12008                "} // namespace bar");
12009   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
12010                "                   int j) -> int {\n"
12011                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
12012                "};");
12013   verifyFormat(
12014       "aaaaaaaaaaaaaaaaaaaaaa(\n"
12015       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
12016       "      return aaaaaaaaaaaaaaaaa;\n"
12017       "    });",
12018       getLLVMStyleWithColumns(70));
12019   verifyFormat("[]() //\n"
12020                "    -> int {\n"
12021                "  return 1; //\n"
12022                "};");
12023 
12024   // Multiple lambdas in the same parentheses change indentation rules. These
12025   // lambdas are forced to start on new lines.
12026   verifyFormat("SomeFunction(\n"
12027                "    []() {\n"
12028                "      //\n"
12029                "    },\n"
12030                "    []() {\n"
12031                "      //\n"
12032                "    });");
12033 
12034   // A lambda passed as arg0 is always pushed to the next line.
12035   verifyFormat("SomeFunction(\n"
12036                "    [this] {\n"
12037                "      //\n"
12038                "    },\n"
12039                "    1);\n");
12040 
12041   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0
12042   // case above.
12043   auto Style = getGoogleStyle();
12044   Style.BinPackArguments = false;
12045   verifyFormat("SomeFunction(\n"
12046                "    a,\n"
12047                "    [this] {\n"
12048                "      //\n"
12049                "    },\n"
12050                "    b);\n",
12051                Style);
12052   verifyFormat("SomeFunction(\n"
12053                "    a,\n"
12054                "    [this] {\n"
12055                "      //\n"
12056                "    },\n"
12057                "    b);\n");
12058 
12059   // A lambda with a very long line forces arg0 to be pushed out irrespective of
12060   // the BinPackArguments value (as long as the code is wide enough).
12061   verifyFormat("something->SomeFunction(\n"
12062                "    a,\n"
12063                "    [this] {\n"
12064                "      D0000000000000000000000000000000000000000000000000000000000001();\n"
12065                "    },\n"
12066                "    b);\n");
12067 
12068   // A multi-line lambda is pulled up as long as the introducer fits on the previous
12069   // line and there are no further args.
12070   verifyFormat("function(1, [this, that] {\n"
12071                "  //\n"
12072                "});\n");
12073   verifyFormat("function([this, that] {\n"
12074                "  //\n"
12075                "});\n");
12076   // FIXME: this format is not ideal and we should consider forcing the first arg
12077   // onto its own line.
12078   verifyFormat("function(a, b, c, //\n"
12079                "         d, [this, that] {\n"
12080                "           //\n"
12081                "         });\n");
12082 
12083   // Multiple lambdas are treated correctly even when there is a short arg0.
12084   verifyFormat("SomeFunction(\n"
12085                "    1,\n"
12086                "    [this] {\n"
12087                "      //\n"
12088                "    },\n"
12089                "    [this] {\n"
12090                "      //\n"
12091                "    },\n"
12092                "    1);\n");
12093 
12094   // More complex introducers.
12095   verifyFormat("return [i, args...] {};");
12096 
12097   // Not lambdas.
12098   verifyFormat("constexpr char hello[]{\"hello\"};");
12099   verifyFormat("double &operator[](int i) { return 0; }\n"
12100                "int i;");
12101   verifyFormat("std::unique_ptr<int[]> foo() {}");
12102   verifyFormat("int i = a[a][a]->f();");
12103   verifyFormat("int i = (*b)[a]->f();");
12104 
12105   // Other corner cases.
12106   verifyFormat("void f() {\n"
12107                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
12108                "  );\n"
12109                "}");
12110 
12111   // Lambdas created through weird macros.
12112   verifyFormat("void f() {\n"
12113                "  MACRO((const AA &a) { return 1; });\n"
12114                "  MACRO((AA &a) { return 1; });\n"
12115                "}");
12116 
12117   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
12118                "      doo_dah();\n"
12119                "      doo_dah();\n"
12120                "    })) {\n"
12121                "}");
12122   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
12123                "                doo_dah();\n"
12124                "                doo_dah();\n"
12125                "              })) {\n"
12126                "}");
12127   verifyFormat("auto lambda = []() {\n"
12128                "  int a = 2\n"
12129                "#if A\n"
12130                "          + 2\n"
12131                "#endif\n"
12132                "      ;\n"
12133                "};");
12134 
12135   // Lambdas with complex multiline introducers.
12136   verifyFormat(
12137       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12138       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
12139       "        -> ::std::unordered_set<\n"
12140       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
12141       "      //\n"
12142       "    });");
12143 }
12144 
12145 TEST_F(FormatTest, EmptyLinesInLambdas) {
12146   verifyFormat("auto lambda = []() {\n"
12147                "  x(); //\n"
12148                "};",
12149                "auto lambda = []() {\n"
12150                "\n"
12151                "  x(); //\n"
12152                "\n"
12153                "};");
12154 }
12155 
12156 TEST_F(FormatTest, FormatsBlocks) {
12157   FormatStyle ShortBlocks = getLLVMStyle();
12158   ShortBlocks.AllowShortBlocksOnASingleLine = true;
12159   verifyFormat("int (^Block)(int, int);", ShortBlocks);
12160   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
12161   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
12162   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
12163   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
12164   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
12165 
12166   verifyFormat("foo(^{ bar(); });", ShortBlocks);
12167   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
12168   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
12169 
12170   verifyFormat("[operation setCompletionBlock:^{\n"
12171                "  [self onOperationDone];\n"
12172                "}];");
12173   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
12174                "  [self onOperationDone];\n"
12175                "}]};");
12176   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
12177                "  f();\n"
12178                "}];");
12179   verifyFormat("int a = [operation block:^int(int *i) {\n"
12180                "  return 1;\n"
12181                "}];");
12182   verifyFormat("[myObject doSomethingWith:arg1\n"
12183                "                      aaa:^int(int *a) {\n"
12184                "                        return 1;\n"
12185                "                      }\n"
12186                "                      bbb:f(a * bbbbbbbb)];");
12187 
12188   verifyFormat("[operation setCompletionBlock:^{\n"
12189                "  [self.delegate newDataAvailable];\n"
12190                "}];",
12191                getLLVMStyleWithColumns(60));
12192   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
12193                "  NSString *path = [self sessionFilePath];\n"
12194                "  if (path) {\n"
12195                "    // ...\n"
12196                "  }\n"
12197                "});");
12198   verifyFormat("[[SessionService sharedService]\n"
12199                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12200                "      if (window) {\n"
12201                "        [self windowDidLoad:window];\n"
12202                "      } else {\n"
12203                "        [self errorLoadingWindow];\n"
12204                "      }\n"
12205                "    }];");
12206   verifyFormat("void (^largeBlock)(void) = ^{\n"
12207                "  // ...\n"
12208                "};\n",
12209                getLLVMStyleWithColumns(40));
12210   verifyFormat("[[SessionService sharedService]\n"
12211                "    loadWindowWithCompletionBlock: //\n"
12212                "        ^(SessionWindow *window) {\n"
12213                "          if (window) {\n"
12214                "            [self windowDidLoad:window];\n"
12215                "          } else {\n"
12216                "            [self errorLoadingWindow];\n"
12217                "          }\n"
12218                "        }];",
12219                getLLVMStyleWithColumns(60));
12220   verifyFormat("[myObject doSomethingWith:arg1\n"
12221                "    firstBlock:^(Foo *a) {\n"
12222                "      // ...\n"
12223                "      int i;\n"
12224                "    }\n"
12225                "    secondBlock:^(Bar *b) {\n"
12226                "      // ...\n"
12227                "      int i;\n"
12228                "    }\n"
12229                "    thirdBlock:^Foo(Bar *b) {\n"
12230                "      // ...\n"
12231                "      int i;\n"
12232                "    }];");
12233   verifyFormat("[myObject doSomethingWith:arg1\n"
12234                "               firstBlock:-1\n"
12235                "              secondBlock:^(Bar *b) {\n"
12236                "                // ...\n"
12237                "                int i;\n"
12238                "              }];");
12239 
12240   verifyFormat("f(^{\n"
12241                "  @autoreleasepool {\n"
12242                "    if (a) {\n"
12243                "      g();\n"
12244                "    }\n"
12245                "  }\n"
12246                "});");
12247   verifyFormat("Block b = ^int *(A *a, B *b) {}");
12248   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
12249                "};");
12250 
12251   FormatStyle FourIndent = getLLVMStyle();
12252   FourIndent.ObjCBlockIndentWidth = 4;
12253   verifyFormat("[operation setCompletionBlock:^{\n"
12254                "    [self onOperationDone];\n"
12255                "}];",
12256                FourIndent);
12257 }
12258 
12259 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
12260   FormatStyle ZeroColumn = getLLVMStyle();
12261   ZeroColumn.ColumnLimit = 0;
12262 
12263   verifyFormat("[[SessionService sharedService] "
12264                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12265                "  if (window) {\n"
12266                "    [self windowDidLoad:window];\n"
12267                "  } else {\n"
12268                "    [self errorLoadingWindow];\n"
12269                "  }\n"
12270                "}];",
12271                ZeroColumn);
12272   EXPECT_EQ("[[SessionService sharedService]\n"
12273             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12274             "      if (window) {\n"
12275             "        [self windowDidLoad:window];\n"
12276             "      } else {\n"
12277             "        [self errorLoadingWindow];\n"
12278             "      }\n"
12279             "    }];",
12280             format("[[SessionService sharedService]\n"
12281                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12282                    "                if (window) {\n"
12283                    "    [self windowDidLoad:window];\n"
12284                    "  } else {\n"
12285                    "    [self errorLoadingWindow];\n"
12286                    "  }\n"
12287                    "}];",
12288                    ZeroColumn));
12289   verifyFormat("[myObject doSomethingWith:arg1\n"
12290                "    firstBlock:^(Foo *a) {\n"
12291                "      // ...\n"
12292                "      int i;\n"
12293                "    }\n"
12294                "    secondBlock:^(Bar *b) {\n"
12295                "      // ...\n"
12296                "      int i;\n"
12297                "    }\n"
12298                "    thirdBlock:^Foo(Bar *b) {\n"
12299                "      // ...\n"
12300                "      int i;\n"
12301                "    }];",
12302                ZeroColumn);
12303   verifyFormat("f(^{\n"
12304                "  @autoreleasepool {\n"
12305                "    if (a) {\n"
12306                "      g();\n"
12307                "    }\n"
12308                "  }\n"
12309                "});",
12310                ZeroColumn);
12311   verifyFormat("void (^largeBlock)(void) = ^{\n"
12312                "  // ...\n"
12313                "};",
12314                ZeroColumn);
12315 
12316   ZeroColumn.AllowShortBlocksOnASingleLine = true;
12317   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
12318             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12319   ZeroColumn.AllowShortBlocksOnASingleLine = false;
12320   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
12321             "  int i;\n"
12322             "};",
12323             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12324 }
12325 
12326 TEST_F(FormatTest, SupportsCRLF) {
12327   EXPECT_EQ("int a;\r\n"
12328             "int b;\r\n"
12329             "int c;\r\n",
12330             format("int a;\r\n"
12331                    "  int b;\r\n"
12332                    "    int c;\r\n",
12333                    getLLVMStyle()));
12334   EXPECT_EQ("int a;\r\n"
12335             "int b;\r\n"
12336             "int c;\r\n",
12337             format("int a;\r\n"
12338                    "  int b;\n"
12339                    "    int c;\r\n",
12340                    getLLVMStyle()));
12341   EXPECT_EQ("int a;\n"
12342             "int b;\n"
12343             "int c;\n",
12344             format("int a;\r\n"
12345                    "  int b;\n"
12346                    "    int c;\n",
12347                    getLLVMStyle()));
12348   EXPECT_EQ("\"aaaaaaa \"\r\n"
12349             "\"bbbbbbb\";\r\n",
12350             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
12351   EXPECT_EQ("#define A \\\r\n"
12352             "  b;      \\\r\n"
12353             "  c;      \\\r\n"
12354             "  d;\r\n",
12355             format("#define A \\\r\n"
12356                    "  b; \\\r\n"
12357                    "  c; d; \r\n",
12358                    getGoogleStyle()));
12359 
12360   EXPECT_EQ("/*\r\n"
12361             "multi line block comments\r\n"
12362             "should not introduce\r\n"
12363             "an extra carriage return\r\n"
12364             "*/\r\n",
12365             format("/*\r\n"
12366                    "multi line block comments\r\n"
12367                    "should not introduce\r\n"
12368                    "an extra carriage return\r\n"
12369                    "*/\r\n"));
12370 }
12371 
12372 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
12373   verifyFormat("MY_CLASS(C) {\n"
12374                "  int i;\n"
12375                "  int j;\n"
12376                "};");
12377 }
12378 
12379 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
12380   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
12381   TwoIndent.ContinuationIndentWidth = 2;
12382 
12383   EXPECT_EQ("int i =\n"
12384             "  longFunction(\n"
12385             "    arg);",
12386             format("int i = longFunction(arg);", TwoIndent));
12387 
12388   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
12389   SixIndent.ContinuationIndentWidth = 6;
12390 
12391   EXPECT_EQ("int i =\n"
12392             "      longFunction(\n"
12393             "            arg);",
12394             format("int i = longFunction(arg);", SixIndent));
12395 }
12396 
12397 TEST_F(FormatTest, SpacesInAngles) {
12398   FormatStyle Spaces = getLLVMStyle();
12399   Spaces.SpacesInAngles = true;
12400 
12401   verifyFormat("static_cast< int >(arg);", Spaces);
12402   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
12403   verifyFormat("f< int, float >();", Spaces);
12404   verifyFormat("template <> g() {}", Spaces);
12405   verifyFormat("template < std::vector< int > > f() {}", Spaces);
12406   verifyFormat("std::function< void(int, int) > fct;", Spaces);
12407   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
12408                Spaces);
12409 
12410   Spaces.Standard = FormatStyle::LS_Cpp03;
12411   Spaces.SpacesInAngles = true;
12412   verifyFormat("A< A< int > >();", Spaces);
12413 
12414   Spaces.SpacesInAngles = false;
12415   verifyFormat("A<A<int> >();", Spaces);
12416 
12417   Spaces.Standard = FormatStyle::LS_Cpp11;
12418   Spaces.SpacesInAngles = true;
12419   verifyFormat("A< A< int > >();", Spaces);
12420 
12421   Spaces.SpacesInAngles = false;
12422   verifyFormat("A<A<int>>();", Spaces);
12423 }
12424 
12425 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
12426   FormatStyle Style = getLLVMStyle();
12427   Style.SpaceAfterTemplateKeyword = false;
12428   verifyFormat("template<int> void foo();", Style);
12429 }
12430 
12431 TEST_F(FormatTest, TripleAngleBrackets) {
12432   verifyFormat("f<<<1, 1>>>();");
12433   verifyFormat("f<<<1, 1, 1, s>>>();");
12434   verifyFormat("f<<<a, b, c, d>>>();");
12435   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
12436   verifyFormat("f<param><<<1, 1>>>();");
12437   verifyFormat("f<1><<<1, 1>>>();");
12438   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
12439   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12440                "aaaaaaaaaaa<<<\n    1, 1>>>();");
12441   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
12442                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
12443 }
12444 
12445 TEST_F(FormatTest, MergeLessLessAtEnd) {
12446   verifyFormat("<<");
12447   EXPECT_EQ("< < <", format("\\\n<<<"));
12448   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12449                "aaallvm::outs() <<");
12450   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12451                "aaaallvm::outs()\n    <<");
12452 }
12453 
12454 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
12455   std::string code = "#if A\n"
12456                      "#if B\n"
12457                      "a.\n"
12458                      "#endif\n"
12459                      "    a = 1;\n"
12460                      "#else\n"
12461                      "#endif\n"
12462                      "#if C\n"
12463                      "#else\n"
12464                      "#endif\n";
12465   EXPECT_EQ(code, format(code));
12466 }
12467 
12468 TEST_F(FormatTest, HandleConflictMarkers) {
12469   // Git/SVN conflict markers.
12470   EXPECT_EQ("int a;\n"
12471             "void f() {\n"
12472             "  callme(some(parameter1,\n"
12473             "<<<<<<< text by the vcs\n"
12474             "              parameter2),\n"
12475             "||||||| text by the vcs\n"
12476             "              parameter2),\n"
12477             "         parameter3,\n"
12478             "======= text by the vcs\n"
12479             "              parameter2, parameter3),\n"
12480             ">>>>>>> text by the vcs\n"
12481             "         otherparameter);\n",
12482             format("int a;\n"
12483                    "void f() {\n"
12484                    "  callme(some(parameter1,\n"
12485                    "<<<<<<< text by the vcs\n"
12486                    "  parameter2),\n"
12487                    "||||||| text by the vcs\n"
12488                    "  parameter2),\n"
12489                    "  parameter3,\n"
12490                    "======= text by the vcs\n"
12491                    "  parameter2,\n"
12492                    "  parameter3),\n"
12493                    ">>>>>>> text by the vcs\n"
12494                    "  otherparameter);\n"));
12495 
12496   // Perforce markers.
12497   EXPECT_EQ("void f() {\n"
12498             "  function(\n"
12499             ">>>> text by the vcs\n"
12500             "      parameter,\n"
12501             "==== text by the vcs\n"
12502             "      parameter,\n"
12503             "==== text by the vcs\n"
12504             "      parameter,\n"
12505             "<<<< text by the vcs\n"
12506             "      parameter);\n",
12507             format("void f() {\n"
12508                    "  function(\n"
12509                    ">>>> text by the vcs\n"
12510                    "  parameter,\n"
12511                    "==== text by the vcs\n"
12512                    "  parameter,\n"
12513                    "==== text by the vcs\n"
12514                    "  parameter,\n"
12515                    "<<<< text by the vcs\n"
12516                    "  parameter);\n"));
12517 
12518   EXPECT_EQ("<<<<<<<\n"
12519             "|||||||\n"
12520             "=======\n"
12521             ">>>>>>>",
12522             format("<<<<<<<\n"
12523                    "|||||||\n"
12524                    "=======\n"
12525                    ">>>>>>>"));
12526 
12527   EXPECT_EQ("<<<<<<<\n"
12528             "|||||||\n"
12529             "int i;\n"
12530             "=======\n"
12531             ">>>>>>>",
12532             format("<<<<<<<\n"
12533                    "|||||||\n"
12534                    "int i;\n"
12535                    "=======\n"
12536                    ">>>>>>>"));
12537 
12538   // FIXME: Handle parsing of macros around conflict markers correctly:
12539   EXPECT_EQ("#define Macro \\\n"
12540             "<<<<<<<\n"
12541             "Something \\\n"
12542             "|||||||\n"
12543             "Else \\\n"
12544             "=======\n"
12545             "Other \\\n"
12546             ">>>>>>>\n"
12547             "    End int i;\n",
12548             format("#define Macro \\\n"
12549                    "<<<<<<<\n"
12550                    "  Something \\\n"
12551                    "|||||||\n"
12552                    "  Else \\\n"
12553                    "=======\n"
12554                    "  Other \\\n"
12555                    ">>>>>>>\n"
12556                    "  End\n"
12557                    "int i;\n"));
12558 }
12559 
12560 TEST_F(FormatTest, DisableRegions) {
12561   EXPECT_EQ("int i;\n"
12562             "// clang-format off\n"
12563             "  int j;\n"
12564             "// clang-format on\n"
12565             "int k;",
12566             format(" int  i;\n"
12567                    "   // clang-format off\n"
12568                    "  int j;\n"
12569                    " // clang-format on\n"
12570                    "   int   k;"));
12571   EXPECT_EQ("int i;\n"
12572             "/* clang-format off */\n"
12573             "  int j;\n"
12574             "/* clang-format on */\n"
12575             "int k;",
12576             format(" int  i;\n"
12577                    "   /* clang-format off */\n"
12578                    "  int j;\n"
12579                    " /* clang-format on */\n"
12580                    "   int   k;"));
12581 
12582   // Don't reflow comments within disabled regions.
12583   EXPECT_EQ(
12584       "// clang-format off\n"
12585       "// long long long long long long line\n"
12586       "/* clang-format on */\n"
12587       "/* long long long\n"
12588       " * long long long\n"
12589       " * line */\n"
12590       "int i;\n"
12591       "/* clang-format off */\n"
12592       "/* long long long long long long line */\n",
12593       format("// clang-format off\n"
12594              "// long long long long long long line\n"
12595              "/* clang-format on */\n"
12596              "/* long long long long long long line */\n"
12597              "int i;\n"
12598              "/* clang-format off */\n"
12599              "/* long long long long long long line */\n",
12600              getLLVMStyleWithColumns(20)));
12601 }
12602 
12603 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12604   format("? ) =");
12605   verifyNoCrash("#define a\\\n /**/}");
12606 }
12607 
12608 TEST_F(FormatTest, FormatsTableGenCode) {
12609   FormatStyle Style = getLLVMStyle();
12610   Style.Language = FormatStyle::LK_TableGen;
12611   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12612 }
12613 
12614 TEST_F(FormatTest, ArrayOfTemplates) {
12615   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12616             format("auto a = new unique_ptr<int > [ 10];"));
12617 
12618   FormatStyle Spaces = getLLVMStyle();
12619   Spaces.SpacesInSquareBrackets = true;
12620   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12621             format("auto a = new unique_ptr<int > [10];", Spaces));
12622 }
12623 
12624 TEST_F(FormatTest, ArrayAsTemplateType) {
12625   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12626             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12627 
12628   FormatStyle Spaces = getLLVMStyle();
12629   Spaces.SpacesInSquareBrackets = true;
12630   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12631             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12632 }
12633 
12634 TEST_F(FormatTest, NoSpaceAfterSuper) {
12635     verifyFormat("__super::FooBar();");
12636 }
12637 
12638 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12639   llvm::vfs::InMemoryFileSystem FS;
12640   auto Style1 = getStyle("file", "", "Google", "", &FS);
12641   ASSERT_TRUE((bool)Style1);
12642   ASSERT_EQ(*Style1, getGoogleStyle());
12643 }
12644 
12645 TEST(FormatStyle, GetStyleOfFile) {
12646   llvm::vfs::InMemoryFileSystem FS;
12647   // Test 1: format file in the same directory.
12648   ASSERT_TRUE(
12649       FS.addFile("/a/.clang-format", 0,
12650                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12651   ASSERT_TRUE(
12652       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12653   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12654   ASSERT_TRUE((bool)Style1);
12655   ASSERT_EQ(*Style1, getLLVMStyle());
12656 
12657   // Test 2.1: fallback to default.
12658   ASSERT_TRUE(
12659       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12660   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12661   ASSERT_TRUE((bool)Style2);
12662   ASSERT_EQ(*Style2, getMozillaStyle());
12663 
12664   // Test 2.2: no format on 'none' fallback style.
12665   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12666   ASSERT_TRUE((bool)Style2);
12667   ASSERT_EQ(*Style2, getNoStyle());
12668 
12669   // Test 2.3: format if config is found with no based style while fallback is
12670   // 'none'.
12671   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12672                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12673   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12674   ASSERT_TRUE((bool)Style2);
12675   ASSERT_EQ(*Style2, getLLVMStyle());
12676 
12677   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12678   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12679   ASSERT_TRUE((bool)Style2);
12680   ASSERT_EQ(*Style2, getLLVMStyle());
12681 
12682   // Test 3: format file in parent directory.
12683   ASSERT_TRUE(
12684       FS.addFile("/c/.clang-format", 0,
12685                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12686   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12687                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12688   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12689   ASSERT_TRUE((bool)Style3);
12690   ASSERT_EQ(*Style3, getGoogleStyle());
12691 
12692   // Test 4: error on invalid fallback style
12693   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12694   ASSERT_FALSE((bool)Style4);
12695   llvm::consumeError(Style4.takeError());
12696 
12697   // Test 5: error on invalid yaml on command line
12698   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12699   ASSERT_FALSE((bool)Style5);
12700   llvm::consumeError(Style5.takeError());
12701 
12702   // Test 6: error on invalid style
12703   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12704   ASSERT_FALSE((bool)Style6);
12705   llvm::consumeError(Style6.takeError());
12706 
12707   // Test 7: found config file, error on parsing it
12708   ASSERT_TRUE(
12709       FS.addFile("/d/.clang-format", 0,
12710                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
12711                                                   "InvalidKey: InvalidValue")));
12712   ASSERT_TRUE(
12713       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12714   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
12715   ASSERT_FALSE((bool)Style7);
12716   llvm::consumeError(Style7.takeError());
12717 
12718   // Test 8: inferred per-language defaults apply.
12719   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
12720   ASSERT_TRUE((bool)StyleTd);
12721   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
12722 }
12723 
12724 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
12725   // Column limit is 20.
12726   std::string Code = "Type *a =\n"
12727                      "    new Type();\n"
12728                      "g(iiiii, 0, jjjjj,\n"
12729                      "  0, kkkkk, 0, mm);\n"
12730                      "int  bad     = format   ;";
12731   std::string Expected = "auto a = new Type();\n"
12732                          "g(iiiii, nullptr,\n"
12733                          "  jjjjj, nullptr,\n"
12734                          "  kkkkk, nullptr,\n"
12735                          "  mm);\n"
12736                          "int  bad     = format   ;";
12737   FileID ID = Context.createInMemoryFile("format.cpp", Code);
12738   tooling::Replacements Replaces = toReplacements(
12739       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
12740                             "auto "),
12741        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12742                             "nullptr"),
12743        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12744                             "nullptr"),
12745        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12746                             "nullptr")});
12747 
12748   format::FormatStyle Style = format::getLLVMStyle();
12749   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12750   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12751   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12752       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12753   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12754   EXPECT_TRUE(static_cast<bool>(Result));
12755   EXPECT_EQ(Expected, *Result);
12756 }
12757 
12758 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12759   std::string Code = "#include \"a.h\"\n"
12760                      "#include \"c.h\"\n"
12761                      "\n"
12762                      "int main() {\n"
12763                      "  return 0;\n"
12764                      "}";
12765   std::string Expected = "#include \"a.h\"\n"
12766                          "#include \"b.h\"\n"
12767                          "#include \"c.h\"\n"
12768                          "\n"
12769                          "int main() {\n"
12770                          "  return 0;\n"
12771                          "}";
12772   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12773   tooling::Replacements Replaces = toReplacements(
12774       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12775                             "#include \"b.h\"\n")});
12776 
12777   format::FormatStyle Style = format::getLLVMStyle();
12778   Style.SortIncludes = true;
12779   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12780   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12781       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12782   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12783   EXPECT_TRUE(static_cast<bool>(Result));
12784   EXPECT_EQ(Expected, *Result);
12785 }
12786 
12787 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12788   EXPECT_EQ("using std::cin;\n"
12789             "using std::cout;",
12790             format("using std::cout;\n"
12791                    "using std::cin;", getGoogleStyle()));
12792 }
12793 
12794 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12795   format::FormatStyle Style = format::getLLVMStyle();
12796   Style.Standard = FormatStyle::LS_Cpp03;
12797   // cpp03 recognize this string as identifier u8 and literal character 'a'
12798   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12799 }
12800 
12801 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12802   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12803   // all modes, including C++11, C++14 and C++17
12804   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12805 }
12806 
12807 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12808   EXPECT_EQ("<!-- ;> -->",
12809             format("<!-- ;> -->", getGoogleStyle()));
12810   EXPECT_EQ(" <!-- >; -->",
12811             format(" <!-- >; -->", getGoogleStyle()));
12812 }
12813 
12814 TEST_F(FormatTest, StructuredBindings) {
12815   // Structured bindings is a C++17 feature.
12816   // all modes, including C++11, C++14 and C++17
12817   verifyFormat("auto [a, b] = f();");
12818   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12819   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12820   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12821   EXPECT_EQ("auto const volatile [a, b] = f();",
12822             format("auto  const   volatile[a, b] = f();"));
12823   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12824   EXPECT_EQ("auto &[a, b, c] = f();",
12825             format("auto   &[  a  ,  b,c   ] = f();"));
12826   EXPECT_EQ("auto &&[a, b, c] = f();",
12827             format("auto   &&[  a  ,  b,c   ] = f();"));
12828   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12829   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12830             format("auto  const  volatile  &&[a, b] = f();"));
12831   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12832   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12833   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12834             format("const  auto   volatile  &&[a, b] = f();"));
12835   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12836             format("volatile  const  auto   &&[a, b] = f();"));
12837   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12838 
12839   // Make sure we don't mistake structured bindings for lambdas.
12840   FormatStyle PointerMiddle = getLLVMStyle();
12841   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12842   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12843   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12844   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12845   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12846   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12847   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12848   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12849   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12850   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12851   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12852   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12853   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12854 
12855   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12856             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12857   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12858             format("for (const auto   &   [a, b] : some_range) {\n}"));
12859   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12860             format("for (const auto[a, b] : some_range) {\n}"));
12861   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12862   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12863   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12864   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12865   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12866   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12867   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12868   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12869 
12870   format::FormatStyle Spaces = format::getLLVMStyle();
12871   Spaces.SpacesInSquareBrackets = true;
12872   verifyFormat("auto [ a, b ] = f();", Spaces);
12873   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12874   verifyFormat("auto &[ a, b ] = f();", Spaces);
12875   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12876   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12877 }
12878 
12879 TEST_F(FormatTest, FileAndCode) {
12880   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12881   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12882   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12883   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12884   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12885   EXPECT_EQ(
12886       FormatStyle::LK_ObjC,
12887       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12888   EXPECT_EQ(FormatStyle::LK_ObjC,
12889             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12890   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12891   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12892   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12893   EXPECT_EQ(FormatStyle::LK_ObjC,
12894             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12895   EXPECT_EQ(
12896       FormatStyle::LK_ObjC,
12897       guessLanguage("foo.h",
12898                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12899   EXPECT_EQ(
12900       FormatStyle::LK_Cpp,
12901       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12902 }
12903 
12904 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12905   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12906   EXPECT_EQ(FormatStyle::LK_ObjC,
12907             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12908   EXPECT_EQ(FormatStyle::LK_Cpp,
12909             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12910   EXPECT_EQ(
12911       FormatStyle::LK_Cpp,
12912       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12913   EXPECT_EQ(FormatStyle::LK_ObjC,
12914             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12915   EXPECT_EQ(FormatStyle::LK_Cpp,
12916             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12917   EXPECT_EQ(FormatStyle::LK_ObjC,
12918             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12919   EXPECT_EQ(FormatStyle::LK_Cpp,
12920             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12921   EXPECT_EQ(FormatStyle::LK_Cpp,
12922             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12923   EXPECT_EQ(FormatStyle::LK_ObjC,
12924             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12925   EXPECT_EQ(FormatStyle::LK_Cpp,
12926             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12927   EXPECT_EQ(
12928       FormatStyle::LK_Cpp,
12929       guessLanguage("foo.h",
12930                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12931   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12932 }
12933 
12934 TEST_F(FormatTest, GuessLanguageWithCaret) {
12935   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12936   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12937   EXPECT_EQ(FormatStyle::LK_ObjC,
12938             guessLanguage("foo.h", "int(^)(char, float);"));
12939   EXPECT_EQ(FormatStyle::LK_ObjC,
12940             guessLanguage("foo.h", "int(^foo)(char, float);"));
12941   EXPECT_EQ(FormatStyle::LK_ObjC,
12942             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12943   EXPECT_EQ(FormatStyle::LK_ObjC,
12944             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12945   EXPECT_EQ(
12946       FormatStyle::LK_ObjC,
12947       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12948 }
12949 
12950 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
12951   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12952                                                "void f() {\n"
12953                                                "  asm (\"mov %[e], %[d]\"\n"
12954                                                "     : [d] \"=rm\" (d)\n"
12955                                                "       [e] \"rm\" (*e));\n"
12956                                                "}"));
12957   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12958                                                "void f() {\n"
12959                                                "  _asm (\"mov %[e], %[d]\"\n"
12960                                                "     : [d] \"=rm\" (d)\n"
12961                                                "       [e] \"rm\" (*e));\n"
12962                                                "}"));
12963   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12964                                                "void f() {\n"
12965                                                "  __asm (\"mov %[e], %[d]\"\n"
12966                                                "     : [d] \"=rm\" (d)\n"
12967                                                "       [e] \"rm\" (*e));\n"
12968                                                "}"));
12969   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12970                                                "void f() {\n"
12971                                                "  __asm__ (\"mov %[e], %[d]\"\n"
12972                                                "     : [d] \"=rm\" (d)\n"
12973                                                "       [e] \"rm\" (*e));\n"
12974                                                "}"));
12975   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12976                                                "void f() {\n"
12977                                                "  asm (\"mov %[e], %[d]\"\n"
12978                                                "     : [d] \"=rm\" (d),\n"
12979                                                "       [e] \"rm\" (*e));\n"
12980                                                "}"));
12981   EXPECT_EQ(FormatStyle::LK_Cpp,
12982             guessLanguage("foo.h", "void f() {\n"
12983                                    "  asm volatile (\"mov %[e], %[d]\"\n"
12984                                    "     : [d] \"=rm\" (d)\n"
12985                                    "       [e] \"rm\" (*e));\n"
12986                                    "}"));
12987 }
12988 
12989 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12990   EXPECT_EQ(FormatStyle::LK_Cpp,
12991             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12992   EXPECT_EQ(FormatStyle::LK_ObjC,
12993             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12994   EXPECT_EQ(
12995       FormatStyle::LK_Cpp,
12996       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12997   EXPECT_EQ(
12998       FormatStyle::LK_ObjC,
12999       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
13000 }
13001 
13002 } // end namespace
13003 } // end namespace format
13004 } // end namespace clang
13005