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 = true;
443   verifyFormat("if (a)\n"
444                "  // comment\n"
445                "  f();",
446                AllowsMergedIf);
447   verifyFormat("{\n"
448                "  if (a)\n"
449                "  label:\n"
450                "    f();\n"
451                "}",
452                AllowsMergedIf);
453   verifyFormat("#define A \\\n"
454                "  if (a)  \\\n"
455                "  label:  \\\n"
456                "    f()",
457                AllowsMergedIf);
458   verifyFormat("if (a)\n"
459                "  ;",
460                AllowsMergedIf);
461   verifyFormat("if (a)\n"
462                "  if (b) return;",
463                AllowsMergedIf);
464 
465   verifyFormat("if (a) // Can't merge this\n"
466                "  f();\n",
467                AllowsMergedIf);
468   verifyFormat("if (a) /* still don't merge */\n"
469                "  f();",
470                AllowsMergedIf);
471   verifyFormat("if (a) { // Never merge this\n"
472                "  f();\n"
473                "}",
474                AllowsMergedIf);
475   verifyFormat("if (a) { /* Never merge this */\n"
476                "  f();\n"
477                "}",
478                AllowsMergedIf);
479 
480   AllowsMergedIf.ColumnLimit = 14;
481   verifyFormat("if (a) return;", AllowsMergedIf);
482   verifyFormat("if (aaaaaaaaa)\n"
483                "  return;",
484                AllowsMergedIf);
485 
486   AllowsMergedIf.ColumnLimit = 13;
487   verifyFormat("if (a)\n  return;", AllowsMergedIf);
488 }
489 
490 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
491   FormatStyle AllowsMergedLoops = getLLVMStyle();
492   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
493   verifyFormat("while (true) continue;", AllowsMergedLoops);
494   verifyFormat("for (;;) continue;", AllowsMergedLoops);
495   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
496   verifyFormat("while (true)\n"
497                "  ;",
498                AllowsMergedLoops);
499   verifyFormat("for (;;)\n"
500                "  ;",
501                AllowsMergedLoops);
502   verifyFormat("for (;;)\n"
503                "  for (;;) continue;",
504                AllowsMergedLoops);
505   verifyFormat("for (;;) // Can't merge this\n"
506                "  continue;",
507                AllowsMergedLoops);
508   verifyFormat("for (;;) /* still don't merge */\n"
509                "  continue;",
510                AllowsMergedLoops);
511 }
512 
513 TEST_F(FormatTest, FormatShortBracedStatements) {
514   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
515   AllowSimpleBracedStatements.ColumnLimit = 40;
516   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
517 
518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
519   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
520 
521   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
522   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
523   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
524 
525   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
526   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
527   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
528   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
529   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
530   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
531   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
532   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
533   verifyFormat("if (true) {\n"
534                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
535                "}",
536                AllowSimpleBracedStatements);
537   verifyFormat("if (true) { //\n"
538                "  f();\n"
539                "}",
540                AllowSimpleBracedStatements);
541   verifyFormat("if (true) {\n"
542                "  f();\n"
543                "  f();\n"
544                "}",
545                AllowSimpleBracedStatements);
546   verifyFormat("if (true) {\n"
547                "  f();\n"
548                "} else {\n"
549                "  f();\n"
550                "}",
551                AllowSimpleBracedStatements);
552 
553   verifyFormat("struct A2 {\n"
554                "  int X;\n"
555                "};",
556                AllowSimpleBracedStatements);
557   verifyFormat("typedef struct A2 {\n"
558                "  int X;\n"
559                "} A2_t;",
560                AllowSimpleBracedStatements);
561   verifyFormat("template <int> struct A2 {\n"
562                "  struct B {};\n"
563                "};",
564                AllowSimpleBracedStatements);
565 
566   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
567   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
568   verifyFormat("if (true) {\n"
569                "  f();\n"
570                "}",
571                AllowSimpleBracedStatements);
572   verifyFormat("if (true) {\n"
573                "  f();\n"
574                "} else {\n"
575                "  f();\n"
576                "}",
577                AllowSimpleBracedStatements);
578 
579   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
580   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
581   verifyFormat("while (true) {\n"
582                "  f();\n"
583                "}",
584                AllowSimpleBracedStatements);
585   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
586   verifyFormat("for (;;) {\n"
587                "  f();\n"
588                "}",
589                AllowSimpleBracedStatements);
590 
591   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
592   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
593   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
594 
595   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
596   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
597   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
598   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
599   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
600   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
601   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
602   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
603   verifyFormat("if (true)\n"
604                "{\n"
605                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
606                "}",
607                AllowSimpleBracedStatements);
608   verifyFormat("if (true)\n"
609                "{ //\n"
610                "  f();\n"
611                "}",
612                AllowSimpleBracedStatements);
613   verifyFormat("if (true)\n"
614                "{\n"
615                "  f();\n"
616                "  f();\n"
617                "}",
618                AllowSimpleBracedStatements);
619   verifyFormat("if (true)\n"
620                "{\n"
621                "  f();\n"
622                "} else\n"
623                "{\n"
624                "  f();\n"
625                "}",
626                AllowSimpleBracedStatements);
627 
628   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
629   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
630   verifyFormat("if (true)\n"
631                "{\n"
632                "  f();\n"
633                "}",
634                AllowSimpleBracedStatements);
635   verifyFormat("if (true)\n"
636                "{\n"
637                "  f();\n"
638                "} else\n"
639                "{\n"
640                "  f();\n"
641                "}",
642                AllowSimpleBracedStatements);
643 
644   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
645   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
646   verifyFormat("while (true)\n"
647                "{\n"
648                "  f();\n"
649                "}",
650                AllowSimpleBracedStatements);
651   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
652   verifyFormat("for (;;)\n"
653                "{\n"
654                "  f();\n"
655                "}",
656                AllowSimpleBracedStatements);
657 }
658 
659 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
660   FormatStyle Style = getLLVMStyleWithColumns(60);
661   Style.AllowShortBlocksOnASingleLine = true;
662   Style.AllowShortIfStatementsOnASingleLine = true;
663   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
664   EXPECT_EQ("#define A                                                  \\\n"
665             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
666             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
667             "X;",
668             format("#define A \\\n"
669                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
670                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
671                    "   }\n"
672                    "X;",
673                    Style));
674 }
675 
676 TEST_F(FormatTest, ParseIfElse) {
677   verifyFormat("if (true)\n"
678                "  if (true)\n"
679                "    if (true)\n"
680                "      f();\n"
681                "    else\n"
682                "      g();\n"
683                "  else\n"
684                "    h();\n"
685                "else\n"
686                "  i();");
687   verifyFormat("if (true)\n"
688                "  if (true)\n"
689                "    if (true) {\n"
690                "      if (true)\n"
691                "        f();\n"
692                "    } else {\n"
693                "      g();\n"
694                "    }\n"
695                "  else\n"
696                "    h();\n"
697                "else {\n"
698                "  i();\n"
699                "}");
700   verifyFormat("if (true)\n"
701                "  if constexpr (true)\n"
702                "    if (true) {\n"
703                "      if constexpr (true)\n"
704                "        f();\n"
705                "    } else {\n"
706                "      g();\n"
707                "    }\n"
708                "  else\n"
709                "    h();\n"
710                "else {\n"
711                "  i();\n"
712                "}");
713   verifyFormat("void f() {\n"
714                "  if (a) {\n"
715                "  } else {\n"
716                "  }\n"
717                "}");
718 }
719 
720 TEST_F(FormatTest, ElseIf) {
721   verifyFormat("if (a) {\n} else if (b) {\n}");
722   verifyFormat("if (a)\n"
723                "  f();\n"
724                "else if (b)\n"
725                "  g();\n"
726                "else\n"
727                "  h();");
728   verifyFormat("if constexpr (a)\n"
729                "  f();\n"
730                "else if constexpr (b)\n"
731                "  g();\n"
732                "else\n"
733                "  h();");
734   verifyFormat("if (a) {\n"
735                "  f();\n"
736                "}\n"
737                "// or else ..\n"
738                "else {\n"
739                "  g()\n"
740                "}");
741 
742   verifyFormat("if (a) {\n"
743                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
744                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
745                "}");
746   verifyFormat("if (a) {\n"
747                "} else if (\n"
748                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
749                "}",
750                getLLVMStyleWithColumns(62));
751   verifyFormat("if (a) {\n"
752                "} else if constexpr (\n"
753                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
754                "}",
755                getLLVMStyleWithColumns(62));
756 }
757 
758 TEST_F(FormatTest, FormatsForLoop) {
759   verifyFormat(
760       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
761       "     ++VeryVeryLongLoopVariable)\n"
762       "  ;");
763   verifyFormat("for (;;)\n"
764                "  f();");
765   verifyFormat("for (;;) {\n}");
766   verifyFormat("for (;;) {\n"
767                "  f();\n"
768                "}");
769   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
770 
771   verifyFormat(
772       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
773       "                                          E = UnwrappedLines.end();\n"
774       "     I != E; ++I) {\n}");
775 
776   verifyFormat(
777       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
778       "     ++IIIII) {\n}");
779   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
780                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
781                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
782   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
783                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
784                "         E = FD->getDeclsInPrototypeScope().end();\n"
785                "     I != E; ++I) {\n}");
786   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
787                "         I = Container.begin(),\n"
788                "         E = Container.end();\n"
789                "     I != E; ++I) {\n}",
790                getLLVMStyleWithColumns(76));
791 
792   verifyFormat(
793       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
794       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
795       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
796       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
797       "     ++aaaaaaaaaaa) {\n}");
798   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
799                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
800                "     ++i) {\n}");
801   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
802                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
803                "}");
804   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
805                "         aaaaaaaaaa);\n"
806                "     iter; ++iter) {\n"
807                "}");
808   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
809                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
810                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
811                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
812 
813   // These should not be formatted as Objective-C for-in loops.
814   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
815   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
816   verifyFormat("Foo *x;\nfor (x in y) {\n}");
817   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
818 
819   FormatStyle NoBinPacking = getLLVMStyle();
820   NoBinPacking.BinPackParameters = false;
821   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
822                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
823                "                                           aaaaaaaaaaaaaaaa,\n"
824                "                                           aaaaaaaaaaaaaaaa,\n"
825                "                                           aaaaaaaaaaaaaaaa);\n"
826                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
827                "}",
828                NoBinPacking);
829   verifyFormat(
830       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
831       "                                          E = UnwrappedLines.end();\n"
832       "     I != E;\n"
833       "     ++I) {\n}",
834       NoBinPacking);
835 
836   FormatStyle AlignLeft = getLLVMStyle();
837   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
838   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
839 }
840 
841 TEST_F(FormatTest, RangeBasedForLoops) {
842   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
843                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
844   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
845                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
846   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
847                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
848   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
849                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
850 }
851 
852 TEST_F(FormatTest, ForEachLoops) {
853   verifyFormat("void f() {\n"
854                "  foreach (Item *item, itemlist) {}\n"
855                "  Q_FOREACH (Item *item, itemlist) {}\n"
856                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
857                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
858                "}");
859 
860   // As function-like macros.
861   verifyFormat("#define foreach(x, y)\n"
862                "#define Q_FOREACH(x, y)\n"
863                "#define BOOST_FOREACH(x, y)\n"
864                "#define UNKNOWN_FOREACH(x, y)\n");
865 
866   // Not as function-like macros.
867   verifyFormat("#define foreach (x, y)\n"
868                "#define Q_FOREACH (x, y)\n"
869                "#define BOOST_FOREACH (x, y)\n"
870                "#define UNKNOWN_FOREACH (x, y)\n");
871 }
872 
873 TEST_F(FormatTest, FormatsWhileLoop) {
874   verifyFormat("while (true) {\n}");
875   verifyFormat("while (true)\n"
876                "  f();");
877   verifyFormat("while () {\n}");
878   verifyFormat("while () {\n"
879                "  f();\n"
880                "}");
881 }
882 
883 TEST_F(FormatTest, FormatsDoWhile) {
884   verifyFormat("do {\n"
885                "  do_something();\n"
886                "} while (something());");
887   verifyFormat("do\n"
888                "  do_something();\n"
889                "while (something());");
890 }
891 
892 TEST_F(FormatTest, FormatsSwitchStatement) {
893   verifyFormat("switch (x) {\n"
894                "case 1:\n"
895                "  f();\n"
896                "  break;\n"
897                "case kFoo:\n"
898                "case ns::kBar:\n"
899                "case kBaz:\n"
900                "  break;\n"
901                "default:\n"
902                "  g();\n"
903                "  break;\n"
904                "}");
905   verifyFormat("switch (x) {\n"
906                "case 1: {\n"
907                "  f();\n"
908                "  break;\n"
909                "}\n"
910                "case 2: {\n"
911                "  break;\n"
912                "}\n"
913                "}");
914   verifyFormat("switch (x) {\n"
915                "case 1: {\n"
916                "  f();\n"
917                "  {\n"
918                "    g();\n"
919                "    h();\n"
920                "  }\n"
921                "  break;\n"
922                "}\n"
923                "}");
924   verifyFormat("switch (x) {\n"
925                "case 1: {\n"
926                "  f();\n"
927                "  if (foo) {\n"
928                "    g();\n"
929                "    h();\n"
930                "  }\n"
931                "  break;\n"
932                "}\n"
933                "}");
934   verifyFormat("switch (x) {\n"
935                "case 1: {\n"
936                "  f();\n"
937                "  g();\n"
938                "} break;\n"
939                "}");
940   verifyFormat("switch (test)\n"
941                "  ;");
942   verifyFormat("switch (x) {\n"
943                "default: {\n"
944                "  // Do nothing.\n"
945                "}\n"
946                "}");
947   verifyFormat("switch (x) {\n"
948                "// comment\n"
949                "// if 1, do f()\n"
950                "case 1:\n"
951                "  f();\n"
952                "}");
953   verifyFormat("switch (x) {\n"
954                "case 1:\n"
955                "  // Do amazing stuff\n"
956                "  {\n"
957                "    f();\n"
958                "    g();\n"
959                "  }\n"
960                "  break;\n"
961                "}");
962   verifyFormat("#define A          \\\n"
963                "  switch (x) {     \\\n"
964                "  case a:          \\\n"
965                "    foo = b;       \\\n"
966                "  }",
967                getLLVMStyleWithColumns(20));
968   verifyFormat("#define OPERATION_CASE(name)           \\\n"
969                "  case OP_name:                        \\\n"
970                "    return operations::Operation##name\n",
971                getLLVMStyleWithColumns(40));
972   verifyFormat("switch (x) {\n"
973                "case 1:;\n"
974                "default:;\n"
975                "  int i;\n"
976                "}");
977 
978   verifyGoogleFormat("switch (x) {\n"
979                      "  case 1:\n"
980                      "    f();\n"
981                      "    break;\n"
982                      "  case kFoo:\n"
983                      "  case ns::kBar:\n"
984                      "  case kBaz:\n"
985                      "    break;\n"
986                      "  default:\n"
987                      "    g();\n"
988                      "    break;\n"
989                      "}");
990   verifyGoogleFormat("switch (x) {\n"
991                      "  case 1: {\n"
992                      "    f();\n"
993                      "    break;\n"
994                      "  }\n"
995                      "}");
996   verifyGoogleFormat("switch (test)\n"
997                      "  ;");
998 
999   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1000                      "  case OP_name:              \\\n"
1001                      "    return operations::Operation##name\n");
1002   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1003                      "  // Get the correction operation class.\n"
1004                      "  switch (OpCode) {\n"
1005                      "    CASE(Add);\n"
1006                      "    CASE(Subtract);\n"
1007                      "    default:\n"
1008                      "      return operations::Unknown;\n"
1009                      "  }\n"
1010                      "#undef OPERATION_CASE\n"
1011                      "}");
1012   verifyFormat("DEBUG({\n"
1013                "  switch (x) {\n"
1014                "  case A:\n"
1015                "    f();\n"
1016                "    break;\n"
1017                "    // fallthrough\n"
1018                "  case B:\n"
1019                "    g();\n"
1020                "    break;\n"
1021                "  }\n"
1022                "});");
1023   EXPECT_EQ("DEBUG({\n"
1024             "  switch (x) {\n"
1025             "  case A:\n"
1026             "    f();\n"
1027             "    break;\n"
1028             "  // On B:\n"
1029             "  case B:\n"
1030             "    g();\n"
1031             "    break;\n"
1032             "  }\n"
1033             "});",
1034             format("DEBUG({\n"
1035                    "  switch (x) {\n"
1036                    "  case A:\n"
1037                    "    f();\n"
1038                    "    break;\n"
1039                    "  // On B:\n"
1040                    "  case B:\n"
1041                    "    g();\n"
1042                    "    break;\n"
1043                    "  }\n"
1044                    "});",
1045                    getLLVMStyle()));
1046   EXPECT_EQ("switch (n) {\n"
1047             "case 0: {\n"
1048             "  return false;\n"
1049             "}\n"
1050             "default: {\n"
1051             "  return true;\n"
1052             "}\n"
1053             "}",
1054             format("switch (n)\n"
1055                    "{\n"
1056                    "case 0: {\n"
1057                    "  return false;\n"
1058                    "}\n"
1059                    "default: {\n"
1060                    "  return true;\n"
1061                    "}\n"
1062                    "}",
1063                    getLLVMStyle()));
1064   verifyFormat("switch (a) {\n"
1065                "case (b):\n"
1066                "  return;\n"
1067                "}");
1068 
1069   verifyFormat("switch (a) {\n"
1070                "case some_namespace::\n"
1071                "    some_constant:\n"
1072                "  return;\n"
1073                "}",
1074                getLLVMStyleWithColumns(34));
1075 
1076   FormatStyle Style = getLLVMStyle();
1077   Style.IndentCaseLabels = true;
1078   Style.AllowShortBlocksOnASingleLine = false;
1079   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1080   Style.BraceWrapping.AfterControlStatement = true;
1081   EXPECT_EQ("switch (n)\n"
1082             "{\n"
1083             "  case 0:\n"
1084             "  {\n"
1085             "    return false;\n"
1086             "  }\n"
1087             "  default:\n"
1088             "  {\n"
1089             "    return true;\n"
1090             "  }\n"
1091             "}",
1092             format("switch (n) {\n"
1093                    "  case 0: {\n"
1094                    "    return false;\n"
1095                    "  }\n"
1096                    "  default: {\n"
1097                    "    return true;\n"
1098                    "  }\n"
1099                    "}",
1100                    Style));
1101 }
1102 
1103 TEST_F(FormatTest, CaseRanges) {
1104   verifyFormat("switch (x) {\n"
1105                "case 'A' ... 'Z':\n"
1106                "case 1 ... 5:\n"
1107                "case a ... b:\n"
1108                "  break;\n"
1109                "}");
1110 }
1111 
1112 TEST_F(FormatTest, ShortCaseLabels) {
1113   FormatStyle Style = getLLVMStyle();
1114   Style.AllowShortCaseLabelsOnASingleLine = true;
1115   verifyFormat("switch (a) {\n"
1116                "case 1: x = 1; break;\n"
1117                "case 2: return;\n"
1118                "case 3:\n"
1119                "case 4:\n"
1120                "case 5: return;\n"
1121                "case 6: // comment\n"
1122                "  return;\n"
1123                "case 7:\n"
1124                "  // comment\n"
1125                "  return;\n"
1126                "case 8:\n"
1127                "  x = 8; // comment\n"
1128                "  break;\n"
1129                "default: y = 1; break;\n"
1130                "}",
1131                Style);
1132   verifyFormat("switch (a) {\n"
1133                "case 0: return; // comment\n"
1134                "case 1: break;  // comment\n"
1135                "case 2: return;\n"
1136                "// comment\n"
1137                "case 3: return;\n"
1138                "// comment 1\n"
1139                "// comment 2\n"
1140                "// comment 3\n"
1141                "case 4: break; /* comment */\n"
1142                "case 5:\n"
1143                "  // comment\n"
1144                "  break;\n"
1145                "case 6: /* comment */ x = 1; break;\n"
1146                "case 7: x = /* comment */ 1; break;\n"
1147                "case 8:\n"
1148                "  x = 1; /* comment */\n"
1149                "  break;\n"
1150                "case 9:\n"
1151                "  break; // comment line 1\n"
1152                "         // comment line 2\n"
1153                "}",
1154                Style);
1155   EXPECT_EQ("switch (a) {\n"
1156             "case 1:\n"
1157             "  x = 8;\n"
1158             "  // fall through\n"
1159             "case 2: x = 8;\n"
1160             "// comment\n"
1161             "case 3:\n"
1162             "  return; /* comment line 1\n"
1163             "           * comment line 2 */\n"
1164             "case 4: i = 8;\n"
1165             "// something else\n"
1166             "#if FOO\n"
1167             "case 5: break;\n"
1168             "#endif\n"
1169             "}",
1170             format("switch (a) {\n"
1171                    "case 1: x = 8;\n"
1172                    "  // fall through\n"
1173                    "case 2:\n"
1174                    "  x = 8;\n"
1175                    "// comment\n"
1176                    "case 3:\n"
1177                    "  return; /* comment line 1\n"
1178                    "           * comment line 2 */\n"
1179                    "case 4:\n"
1180                    "  i = 8;\n"
1181                    "// something else\n"
1182                    "#if FOO\n"
1183                    "case 5: break;\n"
1184                    "#endif\n"
1185                    "}",
1186                    Style));
1187   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1188             "  return; // long long long long long long long long long long long long comment\n"
1189             "          // line\n" "}",
1190             format("switch (a) {\n"
1191                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1192                    "}",
1193                    Style));
1194   EXPECT_EQ("switch (a) {\n"
1195             "case 0:\n"
1196             "  return; /* long long long long long long long long long long long long comment\n"
1197             "             line */\n"
1198             "}",
1199             format("switch (a) {\n"
1200                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1201                    "}",
1202                    Style));
1203   verifyFormat("switch (a) {\n"
1204                "#if FOO\n"
1205                "case 0: return 0;\n"
1206                "#endif\n"
1207                "}",
1208                Style);
1209   verifyFormat("switch (a) {\n"
1210                "case 1: {\n"
1211                "}\n"
1212                "case 2: {\n"
1213                "  return;\n"
1214                "}\n"
1215                "case 3: {\n"
1216                "  x = 1;\n"
1217                "  return;\n"
1218                "}\n"
1219                "case 4:\n"
1220                "  if (x)\n"
1221                "    return;\n"
1222                "}",
1223                Style);
1224   Style.ColumnLimit = 21;
1225   verifyFormat("switch (a) {\n"
1226                "case 1: x = 1; break;\n"
1227                "case 2: return;\n"
1228                "case 3:\n"
1229                "case 4:\n"
1230                "case 5: return;\n"
1231                "default:\n"
1232                "  y = 1;\n"
1233                "  break;\n"
1234                "}",
1235                Style);
1236   Style.ColumnLimit = 80;
1237   Style.AllowShortCaseLabelsOnASingleLine = false;
1238   Style.IndentCaseLabels = true;
1239   EXPECT_EQ("switch (n) {\n"
1240             "  default /*comments*/:\n"
1241             "    return true;\n"
1242             "  case 0:\n"
1243             "    return false;\n"
1244             "}",
1245             format("switch (n) {\n"
1246                    "default/*comments*/:\n"
1247                    "  return true;\n"
1248                    "case 0:\n"
1249                    "  return false;\n"
1250                    "}",
1251                    Style));
1252   Style.AllowShortCaseLabelsOnASingleLine = true;
1253   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1254   Style.BraceWrapping.AfterControlStatement = true;
1255   EXPECT_EQ("switch (n)\n"
1256             "{\n"
1257             "  case 0:\n"
1258             "  {\n"
1259             "    return false;\n"
1260             "  }\n"
1261             "  default:\n"
1262             "  {\n"
1263             "    return true;\n"
1264             "  }\n"
1265             "}",
1266             format("switch (n) {\n"
1267                    "  case 0: {\n"
1268                    "    return false;\n"
1269                    "  }\n"
1270                    "  default:\n"
1271                    "  {\n"
1272                    "    return true;\n"
1273                    "  }\n"
1274                    "}",
1275                    Style));
1276 }
1277 
1278 TEST_F(FormatTest, FormatsLabels) {
1279   verifyFormat("void f() {\n"
1280                "  some_code();\n"
1281                "test_label:\n"
1282                "  some_other_code();\n"
1283                "  {\n"
1284                "    some_more_code();\n"
1285                "  another_label:\n"
1286                "    some_more_code();\n"
1287                "  }\n"
1288                "}");
1289   verifyFormat("{\n"
1290                "  some_code();\n"
1291                "test_label:\n"
1292                "  some_other_code();\n"
1293                "}");
1294   verifyFormat("{\n"
1295                "  some_code();\n"
1296                "test_label:;\n"
1297                "  int i = 0;\n"
1298                "}");
1299 }
1300 
1301 //===----------------------------------------------------------------------===//
1302 // Tests for classes, namespaces, etc.
1303 //===----------------------------------------------------------------------===//
1304 
1305 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1306   verifyFormat("class A {};");
1307 }
1308 
1309 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1310   verifyFormat("class A {\n"
1311                "public:\n"
1312                "public: // comment\n"
1313                "protected:\n"
1314                "private:\n"
1315                "  void f() {}\n"
1316                "};");
1317   verifyFormat("export class A {\n"
1318                "public:\n"
1319                "public: // comment\n"
1320                "protected:\n"
1321                "private:\n"
1322                "  void f() {}\n"
1323                "};");
1324   verifyGoogleFormat("class A {\n"
1325                      " public:\n"
1326                      " protected:\n"
1327                      " private:\n"
1328                      "  void f() {}\n"
1329                      "};");
1330   verifyGoogleFormat("export class A {\n"
1331                      " public:\n"
1332                      " protected:\n"
1333                      " private:\n"
1334                      "  void f() {}\n"
1335                      "};");
1336   verifyFormat("class A {\n"
1337                "public slots:\n"
1338                "  void f1() {}\n"
1339                "public Q_SLOTS:\n"
1340                "  void f2() {}\n"
1341                "protected slots:\n"
1342                "  void f3() {}\n"
1343                "protected Q_SLOTS:\n"
1344                "  void f4() {}\n"
1345                "private slots:\n"
1346                "  void f5() {}\n"
1347                "private Q_SLOTS:\n"
1348                "  void f6() {}\n"
1349                "signals:\n"
1350                "  void g1();\n"
1351                "Q_SIGNALS:\n"
1352                "  void g2();\n"
1353                "};");
1354 
1355   // Don't interpret 'signals' the wrong way.
1356   verifyFormat("signals.set();");
1357   verifyFormat("for (Signals signals : f()) {\n}");
1358   verifyFormat("{\n"
1359                "  signals.set(); // This needs indentation.\n"
1360                "}");
1361   verifyFormat("void f() {\n"
1362                "label:\n"
1363                "  signals.baz();\n"
1364                "}");
1365 }
1366 
1367 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1368   EXPECT_EQ("class A {\n"
1369             "public:\n"
1370             "  void f();\n"
1371             "\n"
1372             "private:\n"
1373             "  void g() {}\n"
1374             "  // test\n"
1375             "protected:\n"
1376             "  int h;\n"
1377             "};",
1378             format("class A {\n"
1379                    "public:\n"
1380                    "void f();\n"
1381                    "private:\n"
1382                    "void g() {}\n"
1383                    "// test\n"
1384                    "protected:\n"
1385                    "int h;\n"
1386                    "};"));
1387   EXPECT_EQ("class A {\n"
1388             "protected:\n"
1389             "public:\n"
1390             "  void f();\n"
1391             "};",
1392             format("class A {\n"
1393                    "protected:\n"
1394                    "\n"
1395                    "public:\n"
1396                    "\n"
1397                    "  void f();\n"
1398                    "};"));
1399 
1400   // Even ensure proper spacing inside macros.
1401   EXPECT_EQ("#define B     \\\n"
1402             "  class A {   \\\n"
1403             "   protected: \\\n"
1404             "   public:    \\\n"
1405             "    void f(); \\\n"
1406             "  };",
1407             format("#define B     \\\n"
1408                    "  class A {   \\\n"
1409                    "   protected: \\\n"
1410                    "              \\\n"
1411                    "   public:    \\\n"
1412                    "              \\\n"
1413                    "    void f(); \\\n"
1414                    "  };",
1415                    getGoogleStyle()));
1416   // But don't remove empty lines after macros ending in access specifiers.
1417   EXPECT_EQ("#define A private:\n"
1418             "\n"
1419             "int i;",
1420             format("#define A         private:\n"
1421                    "\n"
1422                    "int              i;"));
1423 }
1424 
1425 TEST_F(FormatTest, FormatsClasses) {
1426   verifyFormat("class A : public B {};");
1427   verifyFormat("class A : public ::B {};");
1428 
1429   verifyFormat(
1430       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1431       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1432   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1433                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1434                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1435   verifyFormat(
1436       "class A : public B, public C, public D, public E, public F {};");
1437   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1438                "                     public C,\n"
1439                "                     public D,\n"
1440                "                     public E,\n"
1441                "                     public F,\n"
1442                "                     public G {};");
1443 
1444   verifyFormat("class\n"
1445                "    ReallyReallyLongClassName {\n"
1446                "  int i;\n"
1447                "};",
1448                getLLVMStyleWithColumns(32));
1449   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1450                "                           aaaaaaaaaaaaaaaa> {};");
1451   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1452                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1453                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1454   verifyFormat("template <class R, class C>\n"
1455                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1456                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1457   verifyFormat("class ::A::B {};");
1458 }
1459 
1460 TEST_F(FormatTest, BreakInheritanceStyle) {
1461   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1462   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1463           FormatStyle::BILS_BeforeComma;
1464   verifyFormat("class MyClass : public X {};",
1465                StyleWithInheritanceBreakBeforeComma);
1466   verifyFormat("class MyClass\n"
1467                "    : public X\n"
1468                "    , public Y {};",
1469                StyleWithInheritanceBreakBeforeComma);
1470   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1471                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1472                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1473                StyleWithInheritanceBreakBeforeComma);
1474   verifyFormat("struct aaaaaaaaaaaaa\n"
1475                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1476                "          aaaaaaaaaaaaaaaa> {};",
1477                StyleWithInheritanceBreakBeforeComma);
1478 
1479   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1480   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1481           FormatStyle::BILS_AfterColon;
1482   verifyFormat("class MyClass : public X {};",
1483                StyleWithInheritanceBreakAfterColon);
1484   verifyFormat("class MyClass : public X, public Y {};",
1485                StyleWithInheritanceBreakAfterColon);
1486   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1487                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1488                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1489                StyleWithInheritanceBreakAfterColon);
1490   verifyFormat("struct aaaaaaaaaaaaa :\n"
1491                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1492                "        aaaaaaaaaaaaaaaa> {};",
1493                StyleWithInheritanceBreakAfterColon);
1494 }
1495 
1496 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1497   verifyFormat("class A {\n} a, b;");
1498   verifyFormat("struct A {\n} a, b;");
1499   verifyFormat("union A {\n} a;");
1500 }
1501 
1502 TEST_F(FormatTest, FormatsEnum) {
1503   verifyFormat("enum {\n"
1504                "  Zero,\n"
1505                "  One = 1,\n"
1506                "  Two = One + 1,\n"
1507                "  Three = (One + Two),\n"
1508                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1509                "  Five = (One, Two, Three, Four, 5)\n"
1510                "};");
1511   verifyGoogleFormat("enum {\n"
1512                      "  Zero,\n"
1513                      "  One = 1,\n"
1514                      "  Two = One + 1,\n"
1515                      "  Three = (One + Two),\n"
1516                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1517                      "  Five = (One, Two, Three, Four, 5)\n"
1518                      "};");
1519   verifyFormat("enum Enum {};");
1520   verifyFormat("enum {};");
1521   verifyFormat("enum X E {} d;");
1522   verifyFormat("enum __attribute__((...)) E {} d;");
1523   verifyFormat("enum __declspec__((...)) E {} d;");
1524   verifyFormat("enum {\n"
1525                "  Bar = Foo<int, int>::value\n"
1526                "};",
1527                getLLVMStyleWithColumns(30));
1528 
1529   verifyFormat("enum ShortEnum { A, B, C };");
1530   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1531 
1532   EXPECT_EQ("enum KeepEmptyLines {\n"
1533             "  ONE,\n"
1534             "\n"
1535             "  TWO,\n"
1536             "\n"
1537             "  THREE\n"
1538             "}",
1539             format("enum KeepEmptyLines {\n"
1540                    "  ONE,\n"
1541                    "\n"
1542                    "  TWO,\n"
1543                    "\n"
1544                    "\n"
1545                    "  THREE\n"
1546                    "}"));
1547   verifyFormat("enum E { // comment\n"
1548                "  ONE,\n"
1549                "  TWO\n"
1550                "};\n"
1551                "int i;");
1552   // Not enums.
1553   verifyFormat("enum X f() {\n"
1554                "  a();\n"
1555                "  return 42;\n"
1556                "}");
1557   verifyFormat("enum X Type::f() {\n"
1558                "  a();\n"
1559                "  return 42;\n"
1560                "}");
1561   verifyFormat("enum ::X f() {\n"
1562                "  a();\n"
1563                "  return 42;\n"
1564                "}");
1565   verifyFormat("enum ns::X f() {\n"
1566                "  a();\n"
1567                "  return 42;\n"
1568                "}");
1569 }
1570 
1571 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1572   verifyFormat("enum Type {\n"
1573                "  One = 0; // These semicolons should be commas.\n"
1574                "  Two = 1;\n"
1575                "};");
1576   verifyFormat("namespace n {\n"
1577                "enum Type {\n"
1578                "  One,\n"
1579                "  Two, // missing };\n"
1580                "  int i;\n"
1581                "}\n"
1582                "void g() {}");
1583 }
1584 
1585 TEST_F(FormatTest, FormatsEnumStruct) {
1586   verifyFormat("enum struct {\n"
1587                "  Zero,\n"
1588                "  One = 1,\n"
1589                "  Two = One + 1,\n"
1590                "  Three = (One + Two),\n"
1591                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1592                "  Five = (One, Two, Three, Four, 5)\n"
1593                "};");
1594   verifyFormat("enum struct Enum {};");
1595   verifyFormat("enum struct {};");
1596   verifyFormat("enum struct X E {} d;");
1597   verifyFormat("enum struct __attribute__((...)) E {} d;");
1598   verifyFormat("enum struct __declspec__((...)) E {} d;");
1599   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1600 }
1601 
1602 TEST_F(FormatTest, FormatsEnumClass) {
1603   verifyFormat("enum class {\n"
1604                "  Zero,\n"
1605                "  One = 1,\n"
1606                "  Two = One + 1,\n"
1607                "  Three = (One + Two),\n"
1608                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1609                "  Five = (One, Two, Three, Four, 5)\n"
1610                "};");
1611   verifyFormat("enum class Enum {};");
1612   verifyFormat("enum class {};");
1613   verifyFormat("enum class X E {} d;");
1614   verifyFormat("enum class __attribute__((...)) E {} d;");
1615   verifyFormat("enum class __declspec__((...)) E {} d;");
1616   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1617 }
1618 
1619 TEST_F(FormatTest, FormatsEnumTypes) {
1620   verifyFormat("enum X : int {\n"
1621                "  A, // Force multiple lines.\n"
1622                "  B\n"
1623                "};");
1624   verifyFormat("enum X : int { A, B };");
1625   verifyFormat("enum X : std::uint32_t { A, B };");
1626 }
1627 
1628 TEST_F(FormatTest, FormatsTypedefEnum) {
1629   FormatStyle Style = getLLVMStyle();
1630   Style.ColumnLimit = 40;
1631   verifyFormat("typedef enum {} EmptyEnum;");
1632   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1633   verifyFormat("typedef enum {\n"
1634                "  ZERO = 0,\n"
1635                "  ONE = 1,\n"
1636                "  TWO = 2,\n"
1637                "  THREE = 3\n"
1638                "} LongEnum;",
1639                Style);
1640   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1641   Style.BraceWrapping.AfterEnum = true;
1642   verifyFormat("typedef enum {} EmptyEnum;");
1643   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1644   verifyFormat("typedef enum\n"
1645                "{\n"
1646                "  ZERO = 0,\n"
1647                "  ONE = 1,\n"
1648                "  TWO = 2,\n"
1649                "  THREE = 3\n"
1650                "} LongEnum;",
1651                Style);
1652 }
1653 
1654 TEST_F(FormatTest, FormatsNSEnums) {
1655   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1656   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1657                      "  // Information about someDecentlyLongValue.\n"
1658                      "  someDecentlyLongValue,\n"
1659                      "  // Information about anotherDecentlyLongValue.\n"
1660                      "  anotherDecentlyLongValue,\n"
1661                      "  // Information about aThirdDecentlyLongValue.\n"
1662                      "  aThirdDecentlyLongValue\n"
1663                      "};");
1664   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1665                      "  a = 1,\n"
1666                      "  b = 2,\n"
1667                      "  c = 3,\n"
1668                      "};");
1669   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1670                      "  a = 1,\n"
1671                      "  b = 2,\n"
1672                      "  c = 3,\n"
1673                      "};");
1674   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1675                      "  a = 1,\n"
1676                      "  b = 2,\n"
1677                      "  c = 3,\n"
1678                      "};");
1679 }
1680 
1681 TEST_F(FormatTest, FormatsBitfields) {
1682   verifyFormat("struct Bitfields {\n"
1683                "  unsigned sClass : 8;\n"
1684                "  unsigned ValueKind : 2;\n"
1685                "};");
1686   verifyFormat("struct A {\n"
1687                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1688                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1689                "};");
1690   verifyFormat("struct MyStruct {\n"
1691                "  uchar data;\n"
1692                "  uchar : 8;\n"
1693                "  uchar : 8;\n"
1694                "  uchar other;\n"
1695                "};");
1696 }
1697 
1698 TEST_F(FormatTest, FormatsNamespaces) {
1699   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1700   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1701 
1702   verifyFormat("namespace some_namespace {\n"
1703                "class A {};\n"
1704                "void f() { f(); }\n"
1705                "}",
1706                LLVMWithNoNamespaceFix);
1707   verifyFormat("/* something */ namespace some_namespace {\n"
1708                "class A {};\n"
1709                "void f() { f(); }\n"
1710                "}",
1711                LLVMWithNoNamespaceFix);
1712   verifyFormat("namespace {\n"
1713                "class A {};\n"
1714                "void f() { f(); }\n"
1715                "}",
1716                LLVMWithNoNamespaceFix);
1717   verifyFormat("/* something */ namespace {\n"
1718                "class A {};\n"
1719                "void f() { f(); }\n"
1720                "}",
1721                LLVMWithNoNamespaceFix);
1722   verifyFormat("inline namespace X {\n"
1723                "class A {};\n"
1724                "void f() { f(); }\n"
1725                "}",
1726                LLVMWithNoNamespaceFix);
1727   verifyFormat("/* something */ inline namespace X {\n"
1728                "class A {};\n"
1729                "void f() { f(); }\n"
1730                "}",
1731                LLVMWithNoNamespaceFix);
1732   verifyFormat("export namespace X {\n"
1733                "class A {};\n"
1734                "void f() { f(); }\n"
1735                "}",
1736                LLVMWithNoNamespaceFix);
1737   verifyFormat("using namespace some_namespace;\n"
1738                "class A {};\n"
1739                "void f() { f(); }",
1740                LLVMWithNoNamespaceFix);
1741 
1742   // This code is more common than we thought; if we
1743   // layout this correctly the semicolon will go into
1744   // its own line, which is undesirable.
1745   verifyFormat("namespace {};",
1746                LLVMWithNoNamespaceFix);
1747   verifyFormat("namespace {\n"
1748                "class A {};\n"
1749                "};",
1750                LLVMWithNoNamespaceFix);
1751 
1752   verifyFormat("namespace {\n"
1753                "int SomeVariable = 0; // comment\n"
1754                "} // namespace",
1755                LLVMWithNoNamespaceFix);
1756   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1757             "#define HEADER_GUARD\n"
1758             "namespace my_namespace {\n"
1759             "int i;\n"
1760             "} // my_namespace\n"
1761             "#endif // HEADER_GUARD",
1762             format("#ifndef HEADER_GUARD\n"
1763                    " #define HEADER_GUARD\n"
1764                    "   namespace my_namespace {\n"
1765                    "int i;\n"
1766                    "}    // my_namespace\n"
1767                    "#endif    // HEADER_GUARD",
1768                    LLVMWithNoNamespaceFix));
1769 
1770   EXPECT_EQ("namespace A::B {\n"
1771             "class C {};\n"
1772             "}",
1773             format("namespace A::B {\n"
1774                    "class C {};\n"
1775                    "}",
1776                    LLVMWithNoNamespaceFix));
1777 
1778   FormatStyle Style = getLLVMStyle();
1779   Style.NamespaceIndentation = FormatStyle::NI_All;
1780   EXPECT_EQ("namespace out {\n"
1781             "  int i;\n"
1782             "  namespace in {\n"
1783             "    int i;\n"
1784             "  } // namespace in\n"
1785             "} // namespace out",
1786             format("namespace out {\n"
1787                    "int i;\n"
1788                    "namespace in {\n"
1789                    "int i;\n"
1790                    "} // namespace in\n"
1791                    "} // namespace out",
1792                    Style));
1793 
1794   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1795   EXPECT_EQ("namespace out {\n"
1796             "int i;\n"
1797             "namespace in {\n"
1798             "  int i;\n"
1799             "} // namespace in\n"
1800             "} // namespace out",
1801             format("namespace out {\n"
1802                    "int i;\n"
1803                    "namespace in {\n"
1804                    "int i;\n"
1805                    "} // namespace in\n"
1806                    "} // namespace out",
1807                    Style));
1808 }
1809 
1810 TEST_F(FormatTest, FormatsCompactNamespaces) {
1811   FormatStyle Style = getLLVMStyle();
1812   Style.CompactNamespaces = true;
1813 
1814   verifyFormat("namespace A { namespace B {\n"
1815 			   "}} // namespace A::B",
1816 			   Style);
1817 
1818   EXPECT_EQ("namespace out { namespace in {\n"
1819             "}} // namespace out::in",
1820             format("namespace out {\n"
1821                    "namespace in {\n"
1822                    "} // namespace in\n"
1823                    "} // namespace out",
1824                    Style));
1825 
1826   // Only namespaces which have both consecutive opening and end get compacted
1827   EXPECT_EQ("namespace out {\n"
1828             "namespace in1 {\n"
1829             "} // namespace in1\n"
1830             "namespace in2 {\n"
1831             "} // namespace in2\n"
1832             "} // namespace out",
1833             format("namespace out {\n"
1834                    "namespace in1 {\n"
1835                    "} // namespace in1\n"
1836                    "namespace in2 {\n"
1837                    "} // namespace in2\n"
1838                    "} // namespace out",
1839                    Style));
1840 
1841   EXPECT_EQ("namespace out {\n"
1842             "int i;\n"
1843             "namespace in {\n"
1844             "int j;\n"
1845             "} // namespace in\n"
1846             "int k;\n"
1847             "} // namespace out",
1848             format("namespace out { int i;\n"
1849                    "namespace in { int j; } // namespace in\n"
1850                    "int k; } // namespace out",
1851                    Style));
1852 
1853   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1854             "}}} // namespace A::B::C\n",
1855             format("namespace A { namespace B {\n"
1856                    "namespace C {\n"
1857                    "}} // namespace B::C\n"
1858                    "} // namespace A\n",
1859                    Style));
1860 
1861   Style.ColumnLimit = 40;
1862   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1863             "namespace bbbbbbbbbb {\n"
1864             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1865             format("namespace aaaaaaaaaa {\n"
1866                    "namespace bbbbbbbbbb {\n"
1867                    "} // namespace bbbbbbbbbb\n"
1868                    "} // namespace aaaaaaaaaa",
1869                    Style));
1870 
1871   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1872             "namespace cccccc {\n"
1873             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1874             format("namespace aaaaaa {\n"
1875                    "namespace bbbbbb {\n"
1876                    "namespace cccccc {\n"
1877                    "} // namespace cccccc\n"
1878                    "} // namespace bbbbbb\n"
1879                    "} // namespace aaaaaa",
1880                    Style));
1881   Style.ColumnLimit = 80;
1882 
1883   // Extra semicolon after 'inner' closing brace prevents merging
1884   EXPECT_EQ("namespace out { namespace in {\n"
1885             "}; } // namespace out::in",
1886             format("namespace out {\n"
1887                    "namespace in {\n"
1888                    "}; // namespace in\n"
1889                    "} // namespace out",
1890                    Style));
1891 
1892   // Extra semicolon after 'outer' closing brace is conserved
1893   EXPECT_EQ("namespace out { namespace in {\n"
1894             "}}; // namespace out::in",
1895             format("namespace out {\n"
1896                    "namespace in {\n"
1897                    "} // namespace in\n"
1898                    "}; // namespace out",
1899                    Style));
1900 
1901   Style.NamespaceIndentation = FormatStyle::NI_All;
1902   EXPECT_EQ("namespace out { namespace in {\n"
1903             "  int i;\n"
1904             "}} // namespace out::in",
1905             format("namespace out {\n"
1906                    "namespace in {\n"
1907                    "int i;\n"
1908                    "} // namespace in\n"
1909                    "} // namespace out",
1910                    Style));
1911   EXPECT_EQ("namespace out { namespace mid {\n"
1912             "  namespace in {\n"
1913             "    int j;\n"
1914             "  } // namespace in\n"
1915             "  int k;\n"
1916             "}} // namespace out::mid",
1917             format("namespace out { namespace mid {\n"
1918                    "namespace in { int j; } // namespace in\n"
1919                    "int k; }} // namespace out::mid",
1920                    Style));
1921 
1922   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1923   EXPECT_EQ("namespace out { namespace in {\n"
1924             "  int i;\n"
1925             "}} // namespace out::in",
1926             format("namespace out {\n"
1927                    "namespace in {\n"
1928                    "int i;\n"
1929                    "} // namespace in\n"
1930                    "} // namespace out",
1931                    Style));
1932   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1933             "  int i;\n"
1934             "}}} // namespace out::mid::in",
1935             format("namespace out {\n"
1936                    "namespace mid {\n"
1937                    "namespace in {\n"
1938                    "int i;\n"
1939                    "} // namespace in\n"
1940                    "} // namespace mid\n"
1941                    "} // namespace out",
1942                    Style));
1943 }
1944 
1945 TEST_F(FormatTest, FormatsExternC) {
1946   verifyFormat("extern \"C\" {\nint a;");
1947   verifyFormat("extern \"C\" {}");
1948   verifyFormat("extern \"C\" {\n"
1949                "int foo();\n"
1950                "}");
1951   verifyFormat("extern \"C\" int foo() {}");
1952   verifyFormat("extern \"C\" int foo();");
1953   verifyFormat("extern \"C\" int foo() {\n"
1954                "  int i = 42;\n"
1955                "  return i;\n"
1956                "}");
1957 
1958   FormatStyle Style = getLLVMStyle();
1959   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1960   Style.BraceWrapping.AfterFunction = true;
1961   verifyFormat("extern \"C\" int foo() {}", Style);
1962   verifyFormat("extern \"C\" int foo();", Style);
1963   verifyFormat("extern \"C\" int foo()\n"
1964                "{\n"
1965                "  int i = 42;\n"
1966                "  return i;\n"
1967                "}",
1968                Style);
1969 
1970   Style.BraceWrapping.AfterExternBlock = true;
1971   Style.BraceWrapping.SplitEmptyRecord = false;
1972   verifyFormat("extern \"C\"\n"
1973                "{}",
1974                Style);
1975   verifyFormat("extern \"C\"\n"
1976                "{\n"
1977                "  int foo();\n"
1978                "}",
1979                Style);
1980 }
1981 
1982 TEST_F(FormatTest, FormatsInlineASM) {
1983   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1984   verifyFormat("asm(\"nop\" ::: \"memory\");");
1985   verifyFormat(
1986       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1987       "    \"cpuid\\n\\t\"\n"
1988       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1989       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1990       "    : \"a\"(value));");
1991   EXPECT_EQ(
1992       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1993       "  __asm {\n"
1994       "        mov     edx,[that] // vtable in edx\n"
1995       "        mov     eax,methodIndex\n"
1996       "        call    [edx][eax*4] // stdcall\n"
1997       "  }\n"
1998       "}",
1999       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2000              "    __asm {\n"
2001              "        mov     edx,[that] // vtable in edx\n"
2002              "        mov     eax,methodIndex\n"
2003              "        call    [edx][eax*4] // stdcall\n"
2004              "    }\n"
2005              "}"));
2006   EXPECT_EQ("_asm {\n"
2007             "  xor eax, eax;\n"
2008             "  cpuid;\n"
2009             "}",
2010             format("_asm {\n"
2011                    "  xor eax, eax;\n"
2012                    "  cpuid;\n"
2013                    "}"));
2014   verifyFormat("void function() {\n"
2015                "  // comment\n"
2016                "  asm(\"\");\n"
2017                "}");
2018   EXPECT_EQ("__asm {\n"
2019             "}\n"
2020             "int i;",
2021             format("__asm   {\n"
2022                    "}\n"
2023                    "int   i;"));
2024 }
2025 
2026 TEST_F(FormatTest, FormatTryCatch) {
2027   verifyFormat("try {\n"
2028                "  throw a * b;\n"
2029                "} catch (int a) {\n"
2030                "  // Do nothing.\n"
2031                "} catch (...) {\n"
2032                "  exit(42);\n"
2033                "}");
2034 
2035   // Function-level try statements.
2036   verifyFormat("int f() try { return 4; } catch (...) {\n"
2037                "  return 5;\n"
2038                "}");
2039   verifyFormat("class A {\n"
2040                "  int a;\n"
2041                "  A() try : a(0) {\n"
2042                "  } catch (...) {\n"
2043                "    throw;\n"
2044                "  }\n"
2045                "};\n");
2046 
2047   // Incomplete try-catch blocks.
2048   verifyIncompleteFormat("try {} catch (");
2049 }
2050 
2051 TEST_F(FormatTest, FormatSEHTryCatch) {
2052   verifyFormat("__try {\n"
2053                "  int a = b * c;\n"
2054                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2055                "  // Do nothing.\n"
2056                "}");
2057 
2058   verifyFormat("__try {\n"
2059                "  int a = b * c;\n"
2060                "} __finally {\n"
2061                "  // Do nothing.\n"
2062                "}");
2063 
2064   verifyFormat("DEBUG({\n"
2065                "  __try {\n"
2066                "  } __finally {\n"
2067                "  }\n"
2068                "});\n");
2069 }
2070 
2071 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2072   verifyFormat("try {\n"
2073                "  f();\n"
2074                "} catch {\n"
2075                "  g();\n"
2076                "}");
2077   verifyFormat("try {\n"
2078                "  f();\n"
2079                "} catch (A a) MACRO(x) {\n"
2080                "  g();\n"
2081                "} catch (B b) MACRO(x) {\n"
2082                "  g();\n"
2083                "}");
2084 }
2085 
2086 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2087   FormatStyle Style = getLLVMStyle();
2088   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2089                           FormatStyle::BS_WebKit}) {
2090     Style.BreakBeforeBraces = BraceStyle;
2091     verifyFormat("try {\n"
2092                  "  // something\n"
2093                  "} catch (...) {\n"
2094                  "  // something\n"
2095                  "}",
2096                  Style);
2097   }
2098   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2099   verifyFormat("try {\n"
2100                "  // something\n"
2101                "}\n"
2102                "catch (...) {\n"
2103                "  // something\n"
2104                "}",
2105                Style);
2106   verifyFormat("__try {\n"
2107                "  // something\n"
2108                "}\n"
2109                "__finally {\n"
2110                "  // something\n"
2111                "}",
2112                Style);
2113   verifyFormat("@try {\n"
2114                "  // something\n"
2115                "}\n"
2116                "@finally {\n"
2117                "  // something\n"
2118                "}",
2119                Style);
2120   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2121   verifyFormat("try\n"
2122                "{\n"
2123                "  // something\n"
2124                "}\n"
2125                "catch (...)\n"
2126                "{\n"
2127                "  // something\n"
2128                "}",
2129                Style);
2130   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2131   verifyFormat("try\n"
2132                "  {\n"
2133                "    // something\n"
2134                "  }\n"
2135                "catch (...)\n"
2136                "  {\n"
2137                "    // something\n"
2138                "  }",
2139                Style);
2140   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2141   Style.BraceWrapping.BeforeCatch = true;
2142   verifyFormat("try {\n"
2143                "  // something\n"
2144                "}\n"
2145                "catch (...) {\n"
2146                "  // something\n"
2147                "}",
2148                Style);
2149 }
2150 
2151 TEST_F(FormatTest, StaticInitializers) {
2152   verifyFormat("static SomeClass SC = {1, 'a'};");
2153 
2154   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2155                "    100000000, "
2156                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2157 
2158   // Here, everything other than the "}" would fit on a line.
2159   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2160                "    10000000000000000000000000};");
2161   EXPECT_EQ("S s = {a,\n"
2162             "\n"
2163             "       b};",
2164             format("S s = {\n"
2165                    "  a,\n"
2166                    "\n"
2167                    "  b\n"
2168                    "};"));
2169 
2170   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2171   // line. However, the formatting looks a bit off and this probably doesn't
2172   // happen often in practice.
2173   verifyFormat("static int Variable[1] = {\n"
2174                "    {1000000000000000000000000000000000000}};",
2175                getLLVMStyleWithColumns(40));
2176 }
2177 
2178 TEST_F(FormatTest, DesignatedInitializers) {
2179   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2180   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2181                "                    .bbbbbbbbbb = 2,\n"
2182                "                    .cccccccccc = 3,\n"
2183                "                    .dddddddddd = 4,\n"
2184                "                    .eeeeeeeeee = 5};");
2185   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2186                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2187                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2188                "    .ccccccccccccccccccccccccccc = 3,\n"
2189                "    .ddddddddddddddddddddddddddd = 4,\n"
2190                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2191 
2192   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2193 
2194   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2195   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2196                "                    [2] = bbbbbbbbbb,\n"
2197                "                    [3] = cccccccccc,\n"
2198                "                    [4] = dddddddddd,\n"
2199                "                    [5] = eeeeeeeeee};");
2200   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2201                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2202                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2203                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2204                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2205                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2206 }
2207 
2208 TEST_F(FormatTest, NestedStaticInitializers) {
2209   verifyFormat("static A x = {{{}}};\n");
2210   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2211                "               {init1, init2, init3, init4}}};",
2212                getLLVMStyleWithColumns(50));
2213 
2214   verifyFormat("somes Status::global_reps[3] = {\n"
2215                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2216                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2217                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2218                getLLVMStyleWithColumns(60));
2219   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2220                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2221                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2222                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2223   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2224                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2225                "rect.fTop}};");
2226 
2227   verifyFormat(
2228       "SomeArrayOfSomeType a = {\n"
2229       "    {{1, 2, 3},\n"
2230       "     {1, 2, 3},\n"
2231       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2232       "      333333333333333333333333333333},\n"
2233       "     {1, 2, 3},\n"
2234       "     {1, 2, 3}}};");
2235   verifyFormat(
2236       "SomeArrayOfSomeType a = {\n"
2237       "    {{1, 2, 3}},\n"
2238       "    {{1, 2, 3}},\n"
2239       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2240       "      333333333333333333333333333333}},\n"
2241       "    {{1, 2, 3}},\n"
2242       "    {{1, 2, 3}}};");
2243 
2244   verifyFormat("struct {\n"
2245                "  unsigned bit;\n"
2246                "  const char *const name;\n"
2247                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2248                "                 {kOsWin, \"Windows\"},\n"
2249                "                 {kOsLinux, \"Linux\"},\n"
2250                "                 {kOsCrOS, \"Chrome OS\"}};");
2251   verifyFormat("struct {\n"
2252                "  unsigned bit;\n"
2253                "  const char *const name;\n"
2254                "} kBitsToOs[] = {\n"
2255                "    {kOsMac, \"Mac\"},\n"
2256                "    {kOsWin, \"Windows\"},\n"
2257                "    {kOsLinux, \"Linux\"},\n"
2258                "    {kOsCrOS, \"Chrome OS\"},\n"
2259                "};");
2260 }
2261 
2262 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2263   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2264                "                      \\\n"
2265                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2266 }
2267 
2268 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2269   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2270                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2271 
2272   // Do break defaulted and deleted functions.
2273   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2274                "    default;",
2275                getLLVMStyleWithColumns(40));
2276   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2277                "    delete;",
2278                getLLVMStyleWithColumns(40));
2279 }
2280 
2281 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2282   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2283                getLLVMStyleWithColumns(40));
2284   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2285                getLLVMStyleWithColumns(40));
2286   EXPECT_EQ("#define Q                              \\\n"
2287             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2288             "  \"aaaaaaaa.cpp\"",
2289             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2290                    getLLVMStyleWithColumns(40)));
2291 }
2292 
2293 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2294   EXPECT_EQ("# 123 \"A string literal\"",
2295             format("   #     123    \"A string literal\""));
2296 }
2297 
2298 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2299   EXPECT_EQ("#;", format("#;"));
2300   verifyFormat("#\n;\n;\n;");
2301 }
2302 
2303 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2304   EXPECT_EQ("#line 42 \"test\"\n",
2305             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2306   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2307                                     getLLVMStyleWithColumns(12)));
2308 }
2309 
2310 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2311   EXPECT_EQ("#line 42 \"test\"",
2312             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2313   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2314 }
2315 
2316 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2317   verifyFormat("#define A \\x20");
2318   verifyFormat("#define A \\ x20");
2319   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2320   verifyFormat("#define A ''");
2321   verifyFormat("#define A ''qqq");
2322   verifyFormat("#define A `qqq");
2323   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2324   EXPECT_EQ("const char *c = STRINGIFY(\n"
2325             "\\na : b);",
2326             format("const char * c = STRINGIFY(\n"
2327                    "\\na : b);"));
2328 
2329   verifyFormat("a\r\\");
2330   verifyFormat("a\v\\");
2331   verifyFormat("a\f\\");
2332 }
2333 
2334 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2335   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2336   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2337   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2338   // FIXME: We never break before the macro name.
2339   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2340 
2341   verifyFormat("#define A A\n#define A A");
2342   verifyFormat("#define A(X) A\n#define A A");
2343 
2344   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2345   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2346 }
2347 
2348 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2349   EXPECT_EQ("// somecomment\n"
2350             "#include \"a.h\"\n"
2351             "#define A(  \\\n"
2352             "    A, B)\n"
2353             "#include \"b.h\"\n"
2354             "// somecomment\n",
2355             format("  // somecomment\n"
2356                    "  #include \"a.h\"\n"
2357                    "#define A(A,\\\n"
2358                    "    B)\n"
2359                    "    #include \"b.h\"\n"
2360                    " // somecomment\n",
2361                    getLLVMStyleWithColumns(13)));
2362 }
2363 
2364 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2365 
2366 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2367   EXPECT_EQ("#define A    \\\n"
2368             "  c;         \\\n"
2369             "  e;\n"
2370             "f;",
2371             format("#define A c; e;\n"
2372                    "f;",
2373                    getLLVMStyleWithColumns(14)));
2374 }
2375 
2376 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2377 
2378 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2379   EXPECT_EQ("int x,\n"
2380             "#define A\n"
2381             "    y;",
2382             format("int x,\n#define A\ny;"));
2383 }
2384 
2385 TEST_F(FormatTest, HashInMacroDefinition) {
2386   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2387   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2388   verifyFormat("#define A  \\\n"
2389                "  {        \\\n"
2390                "    f(#c); \\\n"
2391                "  }",
2392                getLLVMStyleWithColumns(11));
2393 
2394   verifyFormat("#define A(X)         \\\n"
2395                "  void function##X()",
2396                getLLVMStyleWithColumns(22));
2397 
2398   verifyFormat("#define A(a, b, c)   \\\n"
2399                "  void a##b##c()",
2400                getLLVMStyleWithColumns(22));
2401 
2402   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2403 }
2404 
2405 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2406   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2407   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2408 }
2409 
2410 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2411   EXPECT_EQ("#define A b;", format("#define A \\\n"
2412                                    "          \\\n"
2413                                    "  b;",
2414                                    getLLVMStyleWithColumns(25)));
2415   EXPECT_EQ("#define A \\\n"
2416             "          \\\n"
2417             "  a;      \\\n"
2418             "  b;",
2419             format("#define A \\\n"
2420                    "          \\\n"
2421                    "  a;      \\\n"
2422                    "  b;",
2423                    getLLVMStyleWithColumns(11)));
2424   EXPECT_EQ("#define A \\\n"
2425             "  a;      \\\n"
2426             "          \\\n"
2427             "  b;",
2428             format("#define A \\\n"
2429                    "  a;      \\\n"
2430                    "          \\\n"
2431                    "  b;",
2432                    getLLVMStyleWithColumns(11)));
2433 }
2434 
2435 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2436   verifyIncompleteFormat("#define A :");
2437   verifyFormat("#define SOMECASES  \\\n"
2438                "  case 1:          \\\n"
2439                "  case 2\n",
2440                getLLVMStyleWithColumns(20));
2441   verifyFormat("#define MACRO(a) \\\n"
2442                "  if (a)         \\\n"
2443                "    f();         \\\n"
2444                "  else           \\\n"
2445                "    g()",
2446                getLLVMStyleWithColumns(18));
2447   verifyFormat("#define A template <typename T>");
2448   verifyIncompleteFormat("#define STR(x) #x\n"
2449                          "f(STR(this_is_a_string_literal{));");
2450   verifyFormat("#pragma omp threadprivate( \\\n"
2451                "    y)), // expected-warning",
2452                getLLVMStyleWithColumns(28));
2453   verifyFormat("#d, = };");
2454   verifyFormat("#if \"a");
2455   verifyIncompleteFormat("({\n"
2456                          "#define b     \\\n"
2457                          "  }           \\\n"
2458                          "  a\n"
2459                          "a",
2460                          getLLVMStyleWithColumns(15));
2461   verifyFormat("#define A     \\\n"
2462                "  {           \\\n"
2463                "    {\n"
2464                "#define B     \\\n"
2465                "  }           \\\n"
2466                "  }",
2467                getLLVMStyleWithColumns(15));
2468   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2469   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2470   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2471   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2472 }
2473 
2474 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2475   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2476   EXPECT_EQ("class A : public QObject {\n"
2477             "  Q_OBJECT\n"
2478             "\n"
2479             "  A() {}\n"
2480             "};",
2481             format("class A  :  public QObject {\n"
2482                    "     Q_OBJECT\n"
2483                    "\n"
2484                    "  A() {\n}\n"
2485                    "}  ;"));
2486   EXPECT_EQ("MACRO\n"
2487             "/*static*/ int i;",
2488             format("MACRO\n"
2489                    " /*static*/ int   i;"));
2490   EXPECT_EQ("SOME_MACRO\n"
2491             "namespace {\n"
2492             "void f();\n"
2493             "} // namespace",
2494             format("SOME_MACRO\n"
2495                    "  namespace    {\n"
2496                    "void   f(  );\n"
2497                    "} // namespace"));
2498   // Only if the identifier contains at least 5 characters.
2499   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2500   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2501   // Only if everything is upper case.
2502   EXPECT_EQ("class A : public QObject {\n"
2503             "  Q_Object A() {}\n"
2504             "};",
2505             format("class A  :  public QObject {\n"
2506                    "     Q_Object\n"
2507                    "  A() {\n}\n"
2508                    "}  ;"));
2509 
2510   // Only if the next line can actually start an unwrapped line.
2511   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2512             format("SOME_WEIRD_LOG_MACRO\n"
2513                    "<< SomeThing;"));
2514 
2515   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2516                "(n, buffers))\n",
2517                getChromiumStyle(FormatStyle::LK_Cpp));
2518 }
2519 
2520 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2521   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2522             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2523             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2524             "class X {};\n"
2525             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2526             "int *createScopDetectionPass() { return 0; }",
2527             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2528                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2529                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2530                    "  class X {};\n"
2531                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2532                    "  int *createScopDetectionPass() { return 0; }"));
2533   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2534   // braces, so that inner block is indented one level more.
2535   EXPECT_EQ("int q() {\n"
2536             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2537             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2538             "  IPC_END_MESSAGE_MAP()\n"
2539             "}",
2540             format("int q() {\n"
2541                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2542                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2543                    "  IPC_END_MESSAGE_MAP()\n"
2544                    "}"));
2545 
2546   // Same inside macros.
2547   EXPECT_EQ("#define LIST(L) \\\n"
2548             "  L(A)          \\\n"
2549             "  L(B)          \\\n"
2550             "  L(C)",
2551             format("#define LIST(L) \\\n"
2552                    "  L(A) \\\n"
2553                    "  L(B) \\\n"
2554                    "  L(C)",
2555                    getGoogleStyle()));
2556 
2557   // These must not be recognized as macros.
2558   EXPECT_EQ("int q() {\n"
2559             "  f(x);\n"
2560             "  f(x) {}\n"
2561             "  f(x)->g();\n"
2562             "  f(x)->*g();\n"
2563             "  f(x).g();\n"
2564             "  f(x) = x;\n"
2565             "  f(x) += x;\n"
2566             "  f(x) -= x;\n"
2567             "  f(x) *= x;\n"
2568             "  f(x) /= x;\n"
2569             "  f(x) %= x;\n"
2570             "  f(x) &= x;\n"
2571             "  f(x) |= x;\n"
2572             "  f(x) ^= x;\n"
2573             "  f(x) >>= x;\n"
2574             "  f(x) <<= x;\n"
2575             "  f(x)[y].z();\n"
2576             "  LOG(INFO) << x;\n"
2577             "  ifstream(x) >> x;\n"
2578             "}\n",
2579             format("int q() {\n"
2580                    "  f(x)\n;\n"
2581                    "  f(x)\n {}\n"
2582                    "  f(x)\n->g();\n"
2583                    "  f(x)\n->*g();\n"
2584                    "  f(x)\n.g();\n"
2585                    "  f(x)\n = x;\n"
2586                    "  f(x)\n += x;\n"
2587                    "  f(x)\n -= x;\n"
2588                    "  f(x)\n *= x;\n"
2589                    "  f(x)\n /= x;\n"
2590                    "  f(x)\n %= x;\n"
2591                    "  f(x)\n &= x;\n"
2592                    "  f(x)\n |= x;\n"
2593                    "  f(x)\n ^= x;\n"
2594                    "  f(x)\n >>= x;\n"
2595                    "  f(x)\n <<= x;\n"
2596                    "  f(x)\n[y].z();\n"
2597                    "  LOG(INFO)\n << x;\n"
2598                    "  ifstream(x)\n >> x;\n"
2599                    "}\n"));
2600   EXPECT_EQ("int q() {\n"
2601             "  F(x)\n"
2602             "  if (1) {\n"
2603             "  }\n"
2604             "  F(x)\n"
2605             "  while (1) {\n"
2606             "  }\n"
2607             "  F(x)\n"
2608             "  G(x);\n"
2609             "  F(x)\n"
2610             "  try {\n"
2611             "    Q();\n"
2612             "  } catch (...) {\n"
2613             "  }\n"
2614             "}\n",
2615             format("int q() {\n"
2616                    "F(x)\n"
2617                    "if (1) {}\n"
2618                    "F(x)\n"
2619                    "while (1) {}\n"
2620                    "F(x)\n"
2621                    "G(x);\n"
2622                    "F(x)\n"
2623                    "try { Q(); } catch (...) {}\n"
2624                    "}\n"));
2625   EXPECT_EQ("class A {\n"
2626             "  A() : t(0) {}\n"
2627             "  A(int i) noexcept() : {}\n"
2628             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2629             "  try : t(0) {\n"
2630             "  } catch (...) {\n"
2631             "  }\n"
2632             "};",
2633             format("class A {\n"
2634                    "  A()\n : t(0) {}\n"
2635                    "  A(int i)\n noexcept() : {}\n"
2636                    "  A(X x)\n"
2637                    "  try : t(0) {} catch (...) {}\n"
2638                    "};"));
2639   FormatStyle Style = getLLVMStyle();
2640   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2641   Style.BraceWrapping.AfterControlStatement = true;
2642   Style.BraceWrapping.AfterFunction = true;
2643   EXPECT_EQ("void f()\n"
2644             "try\n"
2645             "{\n"
2646             "}",
2647             format("void f() try {\n"
2648                    "}", Style));
2649   EXPECT_EQ("class SomeClass {\n"
2650             "public:\n"
2651             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2652             "};",
2653             format("class SomeClass {\n"
2654                    "public:\n"
2655                    "  SomeClass()\n"
2656                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2657                    "};"));
2658   EXPECT_EQ("class SomeClass {\n"
2659             "public:\n"
2660             "  SomeClass()\n"
2661             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2662             "};",
2663             format("class SomeClass {\n"
2664                    "public:\n"
2665                    "  SomeClass()\n"
2666                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2667                    "};",
2668                    getLLVMStyleWithColumns(40)));
2669 
2670   verifyFormat("MACRO(>)");
2671 
2672   // Some macros contain an implicit semicolon.
2673   Style = getLLVMStyle();
2674   Style.StatementMacros.push_back("FOO");
2675   verifyFormat("FOO(a) int b = 0;");
2676   verifyFormat("FOO(a)\n"
2677                "int b = 0;",
2678                Style);
2679   verifyFormat("FOO(a);\n"
2680                "int b = 0;",
2681                Style);
2682   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
2683                "int b = 0;",
2684                Style);
2685   verifyFormat("FOO()\n"
2686                "int b = 0;",
2687                Style);
2688   verifyFormat("FOO\n"
2689                "int b = 0;",
2690                Style);
2691   verifyFormat("void f() {\n"
2692                "  FOO(a)\n"
2693                "  return a;\n"
2694                "}",
2695                Style);
2696   verifyFormat("FOO(a)\n"
2697                "FOO(b)",
2698                Style);
2699   verifyFormat("int a = 0;\n"
2700                "FOO(b)\n"
2701                "int c = 0;",
2702                Style);
2703   verifyFormat("int a = 0;\n"
2704                "int x = FOO(a)\n"
2705                "int b = 0;",
2706                Style);
2707   verifyFormat("void foo(int a) { FOO(a) }\n"
2708                "uint32_t bar() {}",
2709                Style);
2710 }
2711 
2712 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2713   verifyFormat("#define A \\\n"
2714                "  f({     \\\n"
2715                "    g();  \\\n"
2716                "  });",
2717                getLLVMStyleWithColumns(11));
2718 }
2719 
2720 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2721   FormatStyle Style = getLLVMStyle();
2722   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2723   Style.ColumnLimit = 40;
2724   verifyFormat("#ifdef _WIN32\n"
2725                "#define A 0\n"
2726                "#ifdef VAR2\n"
2727                "#define B 1\n"
2728                "#include <someheader.h>\n"
2729                "#define MACRO                          \\\n"
2730                "  some_very_long_func_aaaaaaaaaa();\n"
2731                "#endif\n"
2732                "#else\n"
2733                "#define A 1\n"
2734                "#endif",
2735                Style);
2736   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2737   verifyFormat("#ifdef _WIN32\n"
2738                "#  define A 0\n"
2739                "#  ifdef VAR2\n"
2740                "#    define B 1\n"
2741                "#    include <someheader.h>\n"
2742                "#    define MACRO                      \\\n"
2743                "      some_very_long_func_aaaaaaaaaa();\n"
2744                "#  endif\n"
2745                "#else\n"
2746                "#  define A 1\n"
2747                "#endif",
2748                Style);
2749   verifyFormat("#if A\n"
2750                "#  define MACRO                        \\\n"
2751                "    void a(int x) {                    \\\n"
2752                "      b();                             \\\n"
2753                "      c();                             \\\n"
2754                "      d();                             \\\n"
2755                "      e();                             \\\n"
2756                "      f();                             \\\n"
2757                "    }\n"
2758                "#endif",
2759                Style);
2760   // Comments before include guard.
2761   verifyFormat("// file comment\n"
2762                "// file comment\n"
2763                "#ifndef HEADER_H\n"
2764                "#define HEADER_H\n"
2765                "code();\n"
2766                "#endif",
2767                Style);
2768   // Test with include guards.
2769   verifyFormat("#ifndef HEADER_H\n"
2770                "#define HEADER_H\n"
2771                "code();\n"
2772                "#endif",
2773                Style);
2774   // Include guards must have a #define with the same variable immediately
2775   // after #ifndef.
2776   verifyFormat("#ifndef NOT_GUARD\n"
2777                "#  define FOO\n"
2778                "code();\n"
2779                "#endif",
2780                Style);
2781 
2782   // Include guards must cover the entire file.
2783   verifyFormat("code();\n"
2784                "code();\n"
2785                "#ifndef NOT_GUARD\n"
2786                "#  define NOT_GUARD\n"
2787                "code();\n"
2788                "#endif",
2789                Style);
2790   verifyFormat("#ifndef NOT_GUARD\n"
2791                "#  define NOT_GUARD\n"
2792                "code();\n"
2793                "#endif\n"
2794                "code();",
2795                Style);
2796   // Test with trailing blank lines.
2797   verifyFormat("#ifndef HEADER_H\n"
2798                "#define HEADER_H\n"
2799                "code();\n"
2800                "#endif\n",
2801                Style);
2802   // Include guards don't have #else.
2803   verifyFormat("#ifndef NOT_GUARD\n"
2804                "#  define NOT_GUARD\n"
2805                "code();\n"
2806                "#else\n"
2807                "#endif",
2808                Style);
2809   verifyFormat("#ifndef NOT_GUARD\n"
2810                "#  define NOT_GUARD\n"
2811                "code();\n"
2812                "#elif FOO\n"
2813                "#endif",
2814                Style);
2815   // Non-identifier #define after potential include guard.
2816   verifyFormat("#ifndef FOO\n"
2817                "#  define 1\n"
2818                "#endif\n",
2819                Style);
2820   // #if closes past last non-preprocessor line.
2821   verifyFormat("#ifndef FOO\n"
2822                "#define FOO\n"
2823                "#if 1\n"
2824                "int i;\n"
2825                "#  define A 0\n"
2826                "#endif\n"
2827                "#endif\n",
2828                Style);
2829   // FIXME: This doesn't handle the case where there's code between the
2830   // #ifndef and #define but all other conditions hold. This is because when
2831   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2832   // previous code line yet, so we can't detect it.
2833   EXPECT_EQ("#ifndef NOT_GUARD\n"
2834             "code();\n"
2835             "#define NOT_GUARD\n"
2836             "code();\n"
2837             "#endif",
2838             format("#ifndef NOT_GUARD\n"
2839                    "code();\n"
2840                    "#  define NOT_GUARD\n"
2841                    "code();\n"
2842                    "#endif",
2843                    Style));
2844   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2845   // be outside an include guard. Examples are #pragma once and
2846   // #pragma GCC diagnostic, or anything else that does not change the meaning
2847   // of the file if it's included multiple times.
2848   EXPECT_EQ("#ifdef WIN32\n"
2849             "#  pragma once\n"
2850             "#endif\n"
2851             "#ifndef HEADER_H\n"
2852             "#  define HEADER_H\n"
2853             "code();\n"
2854             "#endif",
2855             format("#ifdef WIN32\n"
2856                    "#  pragma once\n"
2857                    "#endif\n"
2858                    "#ifndef HEADER_H\n"
2859                    "#define HEADER_H\n"
2860                    "code();\n"
2861                    "#endif",
2862                    Style));
2863   // FIXME: This does not detect when there is a single non-preprocessor line
2864   // in front of an include-guard-like structure where other conditions hold
2865   // because ScopedLineState hides the line.
2866   EXPECT_EQ("code();\n"
2867             "#ifndef HEADER_H\n"
2868             "#define HEADER_H\n"
2869             "code();\n"
2870             "#endif",
2871             format("code();\n"
2872                    "#ifndef HEADER_H\n"
2873                    "#  define HEADER_H\n"
2874                    "code();\n"
2875                    "#endif",
2876                    Style));
2877   // Keep comments aligned with #, otherwise indent comments normally. These
2878   // tests cannot use verifyFormat because messUp manipulates leading
2879   // whitespace.
2880   {
2881     const char *Expected = ""
2882                            "void f() {\n"
2883                            "#if 1\n"
2884                            "// Preprocessor aligned.\n"
2885                            "#  define A 0\n"
2886                            "  // Code. Separated by blank line.\n"
2887                            "\n"
2888                            "#  define B 0\n"
2889                            "  // Code. Not aligned with #\n"
2890                            "#  define C 0\n"
2891                            "#endif";
2892     const char *ToFormat = ""
2893                            "void f() {\n"
2894                            "#if 1\n"
2895                            "// Preprocessor aligned.\n"
2896                            "#  define A 0\n"
2897                            "// Code. Separated by blank line.\n"
2898                            "\n"
2899                            "#  define B 0\n"
2900                            "   // Code. Not aligned with #\n"
2901                            "#  define C 0\n"
2902                            "#endif";
2903     EXPECT_EQ(Expected, format(ToFormat, Style));
2904     EXPECT_EQ(Expected, format(Expected, Style));
2905   }
2906   // Keep block quotes aligned.
2907   {
2908     const char *Expected = ""
2909                            "void f() {\n"
2910                            "#if 1\n"
2911                            "/* Preprocessor aligned. */\n"
2912                            "#  define A 0\n"
2913                            "  /* Code. Separated by blank line. */\n"
2914                            "\n"
2915                            "#  define B 0\n"
2916                            "  /* Code. Not aligned with # */\n"
2917                            "#  define C 0\n"
2918                            "#endif";
2919     const char *ToFormat = ""
2920                            "void f() {\n"
2921                            "#if 1\n"
2922                            "/* Preprocessor aligned. */\n"
2923                            "#  define A 0\n"
2924                            "/* Code. Separated by blank line. */\n"
2925                            "\n"
2926                            "#  define B 0\n"
2927                            "   /* Code. Not aligned with # */\n"
2928                            "#  define C 0\n"
2929                            "#endif";
2930     EXPECT_EQ(Expected, format(ToFormat, Style));
2931     EXPECT_EQ(Expected, format(Expected, Style));
2932   }
2933   // Keep comments aligned with un-indented directives.
2934   {
2935     const char *Expected = ""
2936                            "void f() {\n"
2937                            "// Preprocessor aligned.\n"
2938                            "#define A 0\n"
2939                            "  // Code. Separated by blank line.\n"
2940                            "\n"
2941                            "#define B 0\n"
2942                            "  // Code. Not aligned with #\n"
2943                            "#define C 0\n";
2944     const char *ToFormat = ""
2945                            "void f() {\n"
2946                            "// Preprocessor aligned.\n"
2947                            "#define A 0\n"
2948                            "// Code. Separated by blank line.\n"
2949                            "\n"
2950                            "#define B 0\n"
2951                            "   // Code. Not aligned with #\n"
2952                            "#define C 0\n";
2953     EXPECT_EQ(Expected, format(ToFormat, Style));
2954     EXPECT_EQ(Expected, format(Expected, Style));
2955   }
2956   // Test with tabs.
2957   Style.UseTab = FormatStyle::UT_Always;
2958   Style.IndentWidth = 8;
2959   Style.TabWidth = 8;
2960   verifyFormat("#ifdef _WIN32\n"
2961                "#\tdefine A 0\n"
2962                "#\tifdef VAR2\n"
2963                "#\t\tdefine B 1\n"
2964                "#\t\tinclude <someheader.h>\n"
2965                "#\t\tdefine MACRO          \\\n"
2966                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2967                "#\tendif\n"
2968                "#else\n"
2969                "#\tdefine A 1\n"
2970                "#endif",
2971                Style);
2972 
2973   // Regression test: Multiline-macro inside include guards.
2974   verifyFormat("#ifndef HEADER_H\n"
2975                "#define HEADER_H\n"
2976                "#define A()        \\\n"
2977                "  int i;           \\\n"
2978                "  int j;\n"
2979                "#endif // HEADER_H",
2980                getLLVMStyleWithColumns(20));
2981 }
2982 
2983 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2984   verifyFormat("{\n  { a #c; }\n}");
2985 }
2986 
2987 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2988   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2989             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2990   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2991             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2992 }
2993 
2994 TEST_F(FormatTest, EscapedNewlines) {
2995   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2996   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2997             format("#define A \\\nint i;\\\n  int j;", Narrow));
2998   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2999   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3000   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
3001   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3002 
3003   FormatStyle AlignLeft = getLLVMStyle();
3004   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3005   EXPECT_EQ("#define MACRO(x) \\\n"
3006             "private:         \\\n"
3007             "  int x(int a);\n",
3008             format("#define MACRO(x) \\\n"
3009                    "private:         \\\n"
3010                    "  int x(int a);\n",
3011                    AlignLeft));
3012 
3013   // CRLF line endings
3014   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
3015             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
3016   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3017   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3018   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
3019   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3020   EXPECT_EQ("#define MACRO(x) \\\r\n"
3021             "private:         \\\r\n"
3022             "  int x(int a);\r\n",
3023             format("#define MACRO(x) \\\r\n"
3024                    "private:         \\\r\n"
3025                    "  int x(int a);\r\n",
3026                    AlignLeft));
3027 
3028   FormatStyle DontAlign = getLLVMStyle();
3029   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3030   DontAlign.MaxEmptyLinesToKeep = 3;
3031   // FIXME: can't use verifyFormat here because the newline before
3032   // "public:" is not inserted the first time it's reformatted
3033   EXPECT_EQ("#define A \\\n"
3034             "  class Foo { \\\n"
3035             "    void bar(); \\\n"
3036             "\\\n"
3037             "\\\n"
3038             "\\\n"
3039             "  public: \\\n"
3040             "    void baz(); \\\n"
3041             "  };",
3042             format("#define A \\\n"
3043                    "  class Foo { \\\n"
3044                    "    void bar(); \\\n"
3045                    "\\\n"
3046                    "\\\n"
3047                    "\\\n"
3048                    "  public: \\\n"
3049                    "    void baz(); \\\n"
3050                    "  };",
3051                    DontAlign));
3052 }
3053 
3054 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3055   verifyFormat("#define A \\\n"
3056                "  int v(  \\\n"
3057                "      a); \\\n"
3058                "  int i;",
3059                getLLVMStyleWithColumns(11));
3060 }
3061 
3062 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3063   EXPECT_EQ(
3064       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3065       "                      \\\n"
3066       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3067       "\n"
3068       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3069       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3070       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3071              "\\\n"
3072              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3073              "  \n"
3074              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3075              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3076 }
3077 
3078 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3079   EXPECT_EQ("int\n"
3080             "#define A\n"
3081             "    a;",
3082             format("int\n#define A\na;"));
3083   verifyFormat("functionCallTo(\n"
3084                "    someOtherFunction(\n"
3085                "        withSomeParameters, whichInSequence,\n"
3086                "        areLongerThanALine(andAnotherCall,\n"
3087                "#define A B\n"
3088                "                           withMoreParamters,\n"
3089                "                           whichStronglyInfluenceTheLayout),\n"
3090                "        andMoreParameters),\n"
3091                "    trailing);",
3092                getLLVMStyleWithColumns(69));
3093   verifyFormat("Foo::Foo()\n"
3094                "#ifdef BAR\n"
3095                "    : baz(0)\n"
3096                "#endif\n"
3097                "{\n"
3098                "}");
3099   verifyFormat("void f() {\n"
3100                "  if (true)\n"
3101                "#ifdef A\n"
3102                "    f(42);\n"
3103                "  x();\n"
3104                "#else\n"
3105                "    g();\n"
3106                "  x();\n"
3107                "#endif\n"
3108                "}");
3109   verifyFormat("void f(param1, param2,\n"
3110                "       param3,\n"
3111                "#ifdef A\n"
3112                "       param4(param5,\n"
3113                "#ifdef A1\n"
3114                "              param6,\n"
3115                "#ifdef A2\n"
3116                "              param7),\n"
3117                "#else\n"
3118                "              param8),\n"
3119                "       param9,\n"
3120                "#endif\n"
3121                "       param10,\n"
3122                "#endif\n"
3123                "       param11)\n"
3124                "#else\n"
3125                "       param12)\n"
3126                "#endif\n"
3127                "{\n"
3128                "  x();\n"
3129                "}",
3130                getLLVMStyleWithColumns(28));
3131   verifyFormat("#if 1\n"
3132                "int i;");
3133   verifyFormat("#if 1\n"
3134                "#endif\n"
3135                "#if 1\n"
3136                "#else\n"
3137                "#endif\n");
3138   verifyFormat("DEBUG({\n"
3139                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3140                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3141                "});\n"
3142                "#if a\n"
3143                "#else\n"
3144                "#endif");
3145 
3146   verifyIncompleteFormat("void f(\n"
3147                          "#if A\n"
3148                          ");\n"
3149                          "#else\n"
3150                          "#endif");
3151 }
3152 
3153 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3154   verifyFormat("#endif\n"
3155                "#if B");
3156 }
3157 
3158 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3159   FormatStyle SingleLine = getLLVMStyle();
3160   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3161   verifyFormat("#if 0\n"
3162                "#elif 1\n"
3163                "#endif\n"
3164                "void foo() {\n"
3165                "  if (test) foo2();\n"
3166                "}",
3167                SingleLine);
3168 }
3169 
3170 TEST_F(FormatTest, LayoutBlockInsideParens) {
3171   verifyFormat("functionCall({ int i; });");
3172   verifyFormat("functionCall({\n"
3173                "  int i;\n"
3174                "  int j;\n"
3175                "});");
3176   verifyFormat("functionCall(\n"
3177                "    {\n"
3178                "      int i;\n"
3179                "      int j;\n"
3180                "    },\n"
3181                "    aaaa, bbbb, cccc);");
3182   verifyFormat("functionA(functionB({\n"
3183                "            int i;\n"
3184                "            int j;\n"
3185                "          }),\n"
3186                "          aaaa, bbbb, cccc);");
3187   verifyFormat("functionCall(\n"
3188                "    {\n"
3189                "      int i;\n"
3190                "      int j;\n"
3191                "    },\n"
3192                "    aaaa, bbbb, // comment\n"
3193                "    cccc);");
3194   verifyFormat("functionA(functionB({\n"
3195                "            int i;\n"
3196                "            int j;\n"
3197                "          }),\n"
3198                "          aaaa, bbbb, // comment\n"
3199                "          cccc);");
3200   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3201   verifyFormat("functionCall(aaaa, bbbb, {\n"
3202                "  int i;\n"
3203                "  int j;\n"
3204                "});");
3205   verifyFormat(
3206       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3207       "    {\n"
3208       "      int i; // break\n"
3209       "    },\n"
3210       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3211       "                                     ccccccccccccccccc));");
3212   verifyFormat("DEBUG({\n"
3213                "  if (a)\n"
3214                "    f();\n"
3215                "});");
3216 }
3217 
3218 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3219   EXPECT_EQ("SOME_MACRO { int i; }\n"
3220             "int i;",
3221             format("  SOME_MACRO  {int i;}  int i;"));
3222 }
3223 
3224 TEST_F(FormatTest, LayoutNestedBlocks) {
3225   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3226                "  struct s {\n"
3227                "    int i;\n"
3228                "  };\n"
3229                "  s kBitsToOs[] = {{10}};\n"
3230                "  for (int i = 0; i < 10; ++i)\n"
3231                "    return;\n"
3232                "}");
3233   verifyFormat("call(parameter, {\n"
3234                "  something();\n"
3235                "  // Comment using all columns.\n"
3236                "  somethingelse();\n"
3237                "});",
3238                getLLVMStyleWithColumns(40));
3239   verifyFormat("DEBUG( //\n"
3240                "    { f(); }, a);");
3241   verifyFormat("DEBUG( //\n"
3242                "    {\n"
3243                "      f(); //\n"
3244                "    },\n"
3245                "    a);");
3246 
3247   EXPECT_EQ("call(parameter, {\n"
3248             "  something();\n"
3249             "  // Comment too\n"
3250             "  // looooooooooong.\n"
3251             "  somethingElse();\n"
3252             "});",
3253             format("call(parameter, {\n"
3254                    "  something();\n"
3255                    "  // Comment too looooooooooong.\n"
3256                    "  somethingElse();\n"
3257                    "});",
3258                    getLLVMStyleWithColumns(29)));
3259   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3260   EXPECT_EQ("DEBUG({ // comment\n"
3261             "  int i;\n"
3262             "});",
3263             format("DEBUG({ // comment\n"
3264                    "int  i;\n"
3265                    "});"));
3266   EXPECT_EQ("DEBUG({\n"
3267             "  int i;\n"
3268             "\n"
3269             "  // comment\n"
3270             "  int j;\n"
3271             "});",
3272             format("DEBUG({\n"
3273                    "  int  i;\n"
3274                    "\n"
3275                    "  // comment\n"
3276                    "  int  j;\n"
3277                    "});"));
3278 
3279   verifyFormat("DEBUG({\n"
3280                "  if (a)\n"
3281                "    return;\n"
3282                "});");
3283   verifyGoogleFormat("DEBUG({\n"
3284                      "  if (a) return;\n"
3285                      "});");
3286   FormatStyle Style = getGoogleStyle();
3287   Style.ColumnLimit = 45;
3288   verifyFormat("Debug(\n"
3289                "    aaaaa,\n"
3290                "    {\n"
3291                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3292                "    },\n"
3293                "    a);",
3294                Style);
3295 
3296   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3297 
3298   verifyNoCrash("^{v^{a}}");
3299 }
3300 
3301 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3302   EXPECT_EQ("#define MACRO()                     \\\n"
3303             "  Debug(aaa, /* force line break */ \\\n"
3304             "        {                           \\\n"
3305             "          int i;                    \\\n"
3306             "          int j;                    \\\n"
3307             "        })",
3308             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3309                    "          {  int   i;  int  j;   })",
3310                    getGoogleStyle()));
3311 
3312   EXPECT_EQ("#define A                                       \\\n"
3313             "  [] {                                          \\\n"
3314             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3315             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3316             "  }",
3317             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3318                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3319                    getGoogleStyle()));
3320 }
3321 
3322 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3323   EXPECT_EQ("{}", format("{}"));
3324   verifyFormat("enum E {};");
3325   verifyFormat("enum E {}");
3326 }
3327 
3328 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3329   FormatStyle Style = getLLVMStyle();
3330   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3331   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3332   verifyFormat("FOO_BEGIN\n"
3333                "  FOO_ENTRY\n"
3334                "FOO_END", Style);
3335   verifyFormat("FOO_BEGIN\n"
3336                "  NESTED_FOO_BEGIN\n"
3337                "    NESTED_FOO_ENTRY\n"
3338                "  NESTED_FOO_END\n"
3339                "FOO_END", Style);
3340   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3341                "  int x;\n"
3342                "  x = 1;\n"
3343                "FOO_END(Baz)", Style);
3344 }
3345 
3346 //===----------------------------------------------------------------------===//
3347 // Line break tests.
3348 //===----------------------------------------------------------------------===//
3349 
3350 TEST_F(FormatTest, PreventConfusingIndents) {
3351   verifyFormat(
3352       "void f() {\n"
3353       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3354       "                         parameter, parameter, parameter)),\n"
3355       "                     SecondLongCall(parameter));\n"
3356       "}");
3357   verifyFormat(
3358       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3359       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3360       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3361       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3362   verifyFormat(
3363       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3364       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3365       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3366       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3367   verifyFormat(
3368       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3369       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3370       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3371       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3372   verifyFormat("int a = bbbb && ccc &&\n"
3373                "        fffff(\n"
3374                "#define A Just forcing a new line\n"
3375                "            ddd);");
3376 }
3377 
3378 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3379   verifyFormat(
3380       "bool aaaaaaa =\n"
3381       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3382       "    bbbbbbbb();");
3383   verifyFormat(
3384       "bool aaaaaaa =\n"
3385       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3386       "    bbbbbbbb();");
3387 
3388   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3389                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3390                "    ccccccccc == ddddddddddd;");
3391   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3392                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3393                "    ccccccccc == ddddddddddd;");
3394   verifyFormat(
3395       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3396       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3397       "    ccccccccc == ddddddddddd;");
3398 
3399   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3400                "                 aaaaaa) &&\n"
3401                "         bbbbbb && cccccc;");
3402   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3403                "                 aaaaaa) >>\n"
3404                "         bbbbbb;");
3405   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3406                "    SourceMgr.getSpellingColumnNumber(\n"
3407                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3408                "    1);");
3409 
3410   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3411                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3412                "    cccccc) {\n}");
3413   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3414                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3415                "              cccccc) {\n}");
3416   verifyFormat("b = a &&\n"
3417                "    // Comment\n"
3418                "    b.c && d;");
3419 
3420   // If the LHS of a comparison is not a binary expression itself, the
3421   // additional linebreak confuses many people.
3422   verifyFormat(
3423       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3424       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3425       "}");
3426   verifyFormat(
3427       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3428       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3429       "}");
3430   verifyFormat(
3431       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3432       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3433       "}");
3434   verifyFormat(
3435       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3436       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3437       "}");
3438   // Even explicit parentheses stress the precedence enough to make the
3439   // additional break unnecessary.
3440   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3441                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3442                "}");
3443   // This cases is borderline, but with the indentation it is still readable.
3444   verifyFormat(
3445       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3446       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3447       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3448       "}",
3449       getLLVMStyleWithColumns(75));
3450 
3451   // If the LHS is a binary expression, we should still use the additional break
3452   // as otherwise the formatting hides the operator precedence.
3453   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3454                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3455                "    5) {\n"
3456                "}");
3457   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3458                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3459                "    5) {\n"
3460                "}");
3461 
3462   FormatStyle OnePerLine = getLLVMStyle();
3463   OnePerLine.BinPackParameters = false;
3464   verifyFormat(
3465       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3466       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3467       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3468       OnePerLine);
3469 
3470   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3471                "                .aaa(aaaaaaaaaaaaa) *\n"
3472                "            aaaaaaa +\n"
3473                "        aaaaaaa;",
3474                getLLVMStyleWithColumns(40));
3475 }
3476 
3477 TEST_F(FormatTest, ExpressionIndentation) {
3478   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3479                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3480                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3481                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3482                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3483                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3484                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3485                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3486                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3487   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3488                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3489                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3490                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3491   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3492                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3493                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3494                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3495   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3496                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3497                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3498                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3499   verifyFormat("if () {\n"
3500                "} else if (aaaaa && bbbbb > // break\n"
3501                "                        ccccc) {\n"
3502                "}");
3503   verifyFormat("if () {\n"
3504                "} else if (aaaaa &&\n"
3505                "           bbbbb > // break\n"
3506                "               ccccc &&\n"
3507                "           ddddd) {\n"
3508                "}");
3509 
3510   // Presence of a trailing comment used to change indentation of b.
3511   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3512                "       b;\n"
3513                "return aaaaaaaaaaaaaaaaaaa +\n"
3514                "       b; //",
3515                getLLVMStyleWithColumns(30));
3516 }
3517 
3518 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3519   // Not sure what the best system is here. Like this, the LHS can be found
3520   // immediately above an operator (everything with the same or a higher
3521   // indent). The RHS is aligned right of the operator and so compasses
3522   // everything until something with the same indent as the operator is found.
3523   // FIXME: Is this a good system?
3524   FormatStyle Style = getLLVMStyle();
3525   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3526   verifyFormat(
3527       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3528       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3529       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3530       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3531       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3532       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3533       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3534       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3535       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3536       Style);
3537   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3538                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3539                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3540                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3541                Style);
3542   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3543                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3544                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3545                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3546                Style);
3547   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3548                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3549                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3550                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3551                Style);
3552   verifyFormat("if () {\n"
3553                "} else if (aaaaa\n"
3554                "           && bbbbb // break\n"
3555                "                  > ccccc) {\n"
3556                "}",
3557                Style);
3558   verifyFormat("return (a)\n"
3559                "       // comment\n"
3560                "       + b;",
3561                Style);
3562   verifyFormat(
3563       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3564       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3565       "             + cc;",
3566       Style);
3567 
3568   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3569                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3570                Style);
3571 
3572   // Forced by comments.
3573   verifyFormat(
3574       "unsigned ContentSize =\n"
3575       "    sizeof(int16_t)   // DWARF ARange version number\n"
3576       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3577       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3578       "    + sizeof(int8_t); // Segment Size (in bytes)");
3579 
3580   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3581                "       == boost::fusion::at_c<1>(iiii).second;",
3582                Style);
3583 
3584   Style.ColumnLimit = 60;
3585   verifyFormat("zzzzzzzzzz\n"
3586                "    = bbbbbbbbbbbbbbbbb\n"
3587                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3588                Style);
3589 
3590   Style.ColumnLimit = 80;
3591   Style.IndentWidth = 4;
3592   Style.TabWidth = 4;
3593   Style.UseTab = FormatStyle::UT_Always;
3594   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3595   Style.AlignOperands = false;
3596   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3597             "\t&& (someOtherLongishConditionPart1\n"
3598             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3599             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3600                    Style));
3601 }
3602 
3603 TEST_F(FormatTest, EnforcedOperatorWraps) {
3604   // Here we'd like to wrap after the || operators, but a comment is forcing an
3605   // earlier wrap.
3606   verifyFormat("bool x = aaaaa //\n"
3607                "         || bbbbb\n"
3608                "         //\n"
3609                "         || cccc;");
3610 }
3611 
3612 TEST_F(FormatTest, NoOperandAlignment) {
3613   FormatStyle Style = getLLVMStyle();
3614   Style.AlignOperands = false;
3615   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3616                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3617                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3618                Style);
3619   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3620   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3621                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3622                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3623                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3624                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3625                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3626                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3627                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3628                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3629                Style);
3630 
3631   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3632                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3633                "    + cc;",
3634                Style);
3635   verifyFormat("int a = aa\n"
3636                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3637                "        * cccccccccccccccccccccccccccccccccccc;\n",
3638                Style);
3639 
3640   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3641   verifyFormat("return (a > b\n"
3642                "    // comment1\n"
3643                "    // comment2\n"
3644                "    || c);",
3645                Style);
3646 }
3647 
3648 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3649   FormatStyle Style = getLLVMStyle();
3650   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3651   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3652                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3653                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3654                Style);
3655 }
3656 
3657 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3658   FormatStyle Style = getLLVMStyle();
3659   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3660   Style.BinPackArguments = false;
3661   Style.ColumnLimit = 40;
3662   verifyFormat("void test() {\n"
3663                "  someFunction(\n"
3664                "      this + argument + is + quite\n"
3665                "      + long + so + it + gets + wrapped\n"
3666                "      + but + remains + bin - packed);\n"
3667                "}",
3668                Style);
3669   verifyFormat("void test() {\n"
3670                "  someFunction(arg1,\n"
3671                "               this + argument + is\n"
3672                "                   + quite + long + so\n"
3673                "                   + it + gets + wrapped\n"
3674                "                   + but + remains + bin\n"
3675                "                   - packed,\n"
3676                "               arg3);\n"
3677                "}",
3678                Style);
3679   verifyFormat("void test() {\n"
3680                "  someFunction(\n"
3681                "      arg1,\n"
3682                "      this + argument + has\n"
3683                "          + anotherFunc(nested,\n"
3684                "                        calls + whose\n"
3685                "                            + arguments\n"
3686                "                            + are + also\n"
3687                "                            + wrapped,\n"
3688                "                        in + addition)\n"
3689                "          + to + being + bin - packed,\n"
3690                "      arg3);\n"
3691                "}",
3692                Style);
3693 
3694   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3695   verifyFormat("void test() {\n"
3696                "  someFunction(\n"
3697                "      arg1,\n"
3698                "      this + argument + has +\n"
3699                "          anotherFunc(nested,\n"
3700                "                      calls + whose +\n"
3701                "                          arguments +\n"
3702                "                          are + also +\n"
3703                "                          wrapped,\n"
3704                "                      in + addition) +\n"
3705                "          to + being + bin - packed,\n"
3706                "      arg3);\n"
3707                "}",
3708                Style);
3709 }
3710 
3711 TEST_F(FormatTest, ConstructorInitializers) {
3712   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3713   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3714                getLLVMStyleWithColumns(45));
3715   verifyFormat("Constructor()\n"
3716                "    : Inttializer(FitsOnTheLine) {}",
3717                getLLVMStyleWithColumns(44));
3718   verifyFormat("Constructor()\n"
3719                "    : Inttializer(FitsOnTheLine) {}",
3720                getLLVMStyleWithColumns(43));
3721 
3722   verifyFormat("template <typename T>\n"
3723                "Constructor() : Initializer(FitsOnTheLine) {}",
3724                getLLVMStyleWithColumns(45));
3725 
3726   verifyFormat(
3727       "SomeClass::Constructor()\n"
3728       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3729 
3730   verifyFormat(
3731       "SomeClass::Constructor()\n"
3732       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3733       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3734   verifyFormat(
3735       "SomeClass::Constructor()\n"
3736       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3737       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3738   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3739                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3740                "    : aaaaaaaaaa(aaaaaa) {}");
3741 
3742   verifyFormat("Constructor()\n"
3743                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3744                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3745                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3746                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3747 
3748   verifyFormat("Constructor()\n"
3749                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3750                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3751 
3752   verifyFormat("Constructor(int Parameter = 0)\n"
3753                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3754                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3755   verifyFormat("Constructor()\n"
3756                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3757                "}",
3758                getLLVMStyleWithColumns(60));
3759   verifyFormat("Constructor()\n"
3760                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3761                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3762 
3763   // Here a line could be saved by splitting the second initializer onto two
3764   // lines, but that is not desirable.
3765   verifyFormat("Constructor()\n"
3766                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3767                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3768                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3769 
3770   FormatStyle OnePerLine = getLLVMStyle();
3771   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3772   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3773   verifyFormat("SomeClass::Constructor()\n"
3774                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3775                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3776                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3777                OnePerLine);
3778   verifyFormat("SomeClass::Constructor()\n"
3779                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3780                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3781                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3782                OnePerLine);
3783   verifyFormat("MyClass::MyClass(int var)\n"
3784                "    : some_var_(var),            // 4 space indent\n"
3785                "      some_other_var_(var + 1) { // lined up\n"
3786                "}",
3787                OnePerLine);
3788   verifyFormat("Constructor()\n"
3789                "    : aaaaa(aaaaaa),\n"
3790                "      aaaaa(aaaaaa),\n"
3791                "      aaaaa(aaaaaa),\n"
3792                "      aaaaa(aaaaaa),\n"
3793                "      aaaaa(aaaaaa) {}",
3794                OnePerLine);
3795   verifyFormat("Constructor()\n"
3796                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3797                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3798                OnePerLine);
3799   OnePerLine.BinPackParameters = false;
3800   verifyFormat(
3801       "Constructor()\n"
3802       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3803       "          aaaaaaaaaaa().aaa(),\n"
3804       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3805       OnePerLine);
3806   OnePerLine.ColumnLimit = 60;
3807   verifyFormat("Constructor()\n"
3808                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3809                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3810                OnePerLine);
3811 
3812   EXPECT_EQ("Constructor()\n"
3813             "    : // Comment forcing unwanted break.\n"
3814             "      aaaa(aaaa) {}",
3815             format("Constructor() :\n"
3816                    "    // Comment forcing unwanted break.\n"
3817                    "    aaaa(aaaa) {}"));
3818 }
3819 
3820 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3821   FormatStyle Style = getLLVMStyle();
3822   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3823 
3824   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3825   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3826                getStyleWithColumns(Style, 45));
3827   verifyFormat("Constructor() :\n"
3828                "    Initializer(FitsOnTheLine) {}",
3829                getStyleWithColumns(Style, 44));
3830   verifyFormat("Constructor() :\n"
3831                "    Initializer(FitsOnTheLine) {}",
3832                getStyleWithColumns(Style, 43));
3833 
3834   verifyFormat("template <typename T>\n"
3835                "Constructor() : Initializer(FitsOnTheLine) {}",
3836                getStyleWithColumns(Style, 50));
3837 
3838   verifyFormat(
3839       "SomeClass::Constructor() :\n"
3840       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3841 	  Style);
3842 
3843   verifyFormat(
3844       "SomeClass::Constructor() :\n"
3845       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3846       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3847 	  Style);
3848   verifyFormat(
3849       "SomeClass::Constructor() :\n"
3850       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3851       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3852 	  Style);
3853   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3854                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3855                "    aaaaaaaaaa(aaaaaa) {}",
3856 			   Style);
3857 
3858   verifyFormat("Constructor() :\n"
3859                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3860                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3861                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3862                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3863 			   Style);
3864 
3865   verifyFormat("Constructor() :\n"
3866                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3867                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3868 			   Style);
3869 
3870   verifyFormat("Constructor(int Parameter = 0) :\n"
3871                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3872                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3873 			   Style);
3874   verifyFormat("Constructor() :\n"
3875                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3876                "}",
3877                getStyleWithColumns(Style, 60));
3878   verifyFormat("Constructor() :\n"
3879                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3880                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3881 			   Style);
3882 
3883   // Here a line could be saved by splitting the second initializer onto two
3884   // lines, but that is not desirable.
3885   verifyFormat("Constructor() :\n"
3886                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3887                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3888                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3889 			   Style);
3890 
3891   FormatStyle OnePerLine = Style;
3892   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3893   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3894   verifyFormat("SomeClass::Constructor() :\n"
3895                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3896                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3897                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3898                OnePerLine);
3899   verifyFormat("SomeClass::Constructor() :\n"
3900                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3901                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3902                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3903                OnePerLine);
3904   verifyFormat("MyClass::MyClass(int var) :\n"
3905                "    some_var_(var),            // 4 space indent\n"
3906                "    some_other_var_(var + 1) { // lined up\n"
3907                "}",
3908                OnePerLine);
3909   verifyFormat("Constructor() :\n"
3910                "    aaaaa(aaaaaa),\n"
3911                "    aaaaa(aaaaaa),\n"
3912                "    aaaaa(aaaaaa),\n"
3913                "    aaaaa(aaaaaa),\n"
3914                "    aaaaa(aaaaaa) {}",
3915                OnePerLine);
3916   verifyFormat("Constructor() :\n"
3917                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3918                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3919                OnePerLine);
3920   OnePerLine.BinPackParameters = false;
3921   verifyFormat(
3922       "Constructor() :\n"
3923       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3924       "        aaaaaaaaaaa().aaa(),\n"
3925       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3926       OnePerLine);
3927   OnePerLine.ColumnLimit = 60;
3928   verifyFormat("Constructor() :\n"
3929                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3930                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3931                OnePerLine);
3932 
3933   EXPECT_EQ("Constructor() :\n"
3934             "    // Comment forcing unwanted break.\n"
3935             "    aaaa(aaaa) {}",
3936             format("Constructor() :\n"
3937                    "    // Comment forcing unwanted break.\n"
3938                    "    aaaa(aaaa) {}",
3939 				   Style));
3940 
3941   Style.ColumnLimit = 0;
3942   verifyFormat("SomeClass::Constructor() :\n"
3943                "    a(a) {}",
3944                Style);
3945   verifyFormat("SomeClass::Constructor() noexcept :\n"
3946                "    a(a) {}",
3947                Style);
3948   verifyFormat("SomeClass::Constructor() :\n"
3949 			   "    a(a), b(b), c(c) {}",
3950                Style);
3951   verifyFormat("SomeClass::Constructor() :\n"
3952                "    a(a) {\n"
3953                "  foo();\n"
3954                "  bar();\n"
3955                "}",
3956                Style);
3957 
3958   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3959   verifyFormat("SomeClass::Constructor() :\n"
3960 			   "    a(a), b(b), c(c) {\n"
3961 			   "}",
3962                Style);
3963   verifyFormat("SomeClass::Constructor() :\n"
3964                "    a(a) {\n"
3965 			   "}",
3966                Style);
3967 
3968   Style.ColumnLimit = 80;
3969   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3970   Style.ConstructorInitializerIndentWidth = 2;
3971   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3972                Style);
3973   verifyFormat("SomeClass::Constructor() :\n"
3974                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3975                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3976                Style);
3977 
3978   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
3979   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
3980   verifyFormat("class SomeClass\n"
3981                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3982                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3983                Style);
3984   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
3985   verifyFormat("class SomeClass\n"
3986                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3987                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3988                Style);
3989   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
3990   verifyFormat("class SomeClass :\n"
3991                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3992                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3993                Style);
3994 }
3995 
3996 #ifndef EXPENSIVE_CHECKS
3997 // Expensive checks enables libstdc++ checking which includes validating the
3998 // state of ranges used in std::priority_queue - this blows out the
3999 // runtime/scalability of the function and makes this test unacceptably slow.
4000 TEST_F(FormatTest, MemoizationTests) {
4001   // This breaks if the memoization lookup does not take \c Indent and
4002   // \c LastSpace into account.
4003   verifyFormat(
4004       "extern CFRunLoopTimerRef\n"
4005       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
4006       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
4007       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
4008       "                     CFRunLoopTimerContext *context) {}");
4009 
4010   // Deep nesting somewhat works around our memoization.
4011   verifyFormat(
4012       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4013       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4014       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4015       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4016       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
4017       getLLVMStyleWithColumns(65));
4018   verifyFormat(
4019       "aaaaa(\n"
4020       "    aaaaa,\n"
4021       "    aaaaa(\n"
4022       "        aaaaa,\n"
4023       "        aaaaa(\n"
4024       "            aaaaa,\n"
4025       "            aaaaa(\n"
4026       "                aaaaa,\n"
4027       "                aaaaa(\n"
4028       "                    aaaaa,\n"
4029       "                    aaaaa(\n"
4030       "                        aaaaa,\n"
4031       "                        aaaaa(\n"
4032       "                            aaaaa,\n"
4033       "                            aaaaa(\n"
4034       "                                aaaaa,\n"
4035       "                                aaaaa(\n"
4036       "                                    aaaaa,\n"
4037       "                                    aaaaa(\n"
4038       "                                        aaaaa,\n"
4039       "                                        aaaaa(\n"
4040       "                                            aaaaa,\n"
4041       "                                            aaaaa(\n"
4042       "                                                aaaaa,\n"
4043       "                                                aaaaa))))))))))));",
4044       getLLVMStyleWithColumns(65));
4045   verifyFormat(
4046       "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"
4047       "                                  a),\n"
4048       "                                a),\n"
4049       "                              a),\n"
4050       "                            a),\n"
4051       "                          a),\n"
4052       "                        a),\n"
4053       "                      a),\n"
4054       "                    a),\n"
4055       "                  a),\n"
4056       "                a),\n"
4057       "              a),\n"
4058       "            a),\n"
4059       "          a),\n"
4060       "        a),\n"
4061       "      a),\n"
4062       "    a),\n"
4063       "  a)",
4064       getLLVMStyleWithColumns(65));
4065 
4066   // This test takes VERY long when memoization is broken.
4067   FormatStyle OnePerLine = getLLVMStyle();
4068   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4069   OnePerLine.BinPackParameters = false;
4070   std::string input = "Constructor()\n"
4071                       "    : aaaa(a,\n";
4072   for (unsigned i = 0, e = 80; i != e; ++i) {
4073     input += "           a,\n";
4074   }
4075   input += "           a) {}";
4076   verifyFormat(input, OnePerLine);
4077 }
4078 #endif
4079 
4080 TEST_F(FormatTest, BreaksAsHighAsPossible) {
4081   verifyFormat(
4082       "void f() {\n"
4083       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
4084       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
4085       "    f();\n"
4086       "}");
4087   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
4088                "    Intervals[i - 1].getRange().getLast()) {\n}");
4089 }
4090 
4091 TEST_F(FormatTest, BreaksFunctionDeclarations) {
4092   // Principially, we break function declarations in a certain order:
4093   // 1) break amongst arguments.
4094   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
4095                "                              Cccccccccccccc cccccccccccccc);");
4096   verifyFormat("template <class TemplateIt>\n"
4097                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
4098                "                            TemplateIt *stop) {}");
4099 
4100   // 2) break after return type.
4101   verifyFormat(
4102       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4103       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
4104       getGoogleStyle());
4105 
4106   // 3) break after (.
4107   verifyFormat(
4108       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
4109       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
4110       getGoogleStyle());
4111 
4112   // 4) break before after nested name specifiers.
4113   verifyFormat(
4114       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4115       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
4116       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
4117       getGoogleStyle());
4118 
4119   // However, there are exceptions, if a sufficient amount of lines can be
4120   // saved.
4121   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
4122   // more adjusting.
4123   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4124                "                                  Cccccccccccccc cccccccccc,\n"
4125                "                                  Cccccccccccccc cccccccccc,\n"
4126                "                                  Cccccccccccccc cccccccccc,\n"
4127                "                                  Cccccccccccccc cccccccccc);");
4128   verifyFormat(
4129       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4130       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4131       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4132       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
4133       getGoogleStyle());
4134   verifyFormat(
4135       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4136       "                                          Cccccccccccccc cccccccccc,\n"
4137       "                                          Cccccccccccccc cccccccccc,\n"
4138       "                                          Cccccccccccccc cccccccccc,\n"
4139       "                                          Cccccccccccccc cccccccccc,\n"
4140       "                                          Cccccccccccccc cccccccccc,\n"
4141       "                                          Cccccccccccccc cccccccccc);");
4142   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4143                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4144                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4145                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4146                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
4147 
4148   // Break after multi-line parameters.
4149   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4150                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4151                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4152                "    bbbb bbbb);");
4153   verifyFormat("void SomeLoooooooooooongFunction(\n"
4154                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4155                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4156                "    int bbbbbbbbbbbbb);");
4157 
4158   // Treat overloaded operators like other functions.
4159   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4160                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
4161   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4162                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
4163   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4164                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
4165   verifyGoogleFormat(
4166       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
4167       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4168   verifyGoogleFormat(
4169       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
4170       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4171   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4172                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4173   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
4174                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4175   verifyGoogleFormat(
4176       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
4177       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4178       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
4179   verifyGoogleFormat(
4180       "template <typename T>\n"
4181       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4182       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
4183       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
4184 
4185   FormatStyle Style = getLLVMStyle();
4186   Style.PointerAlignment = FormatStyle::PAS_Left;
4187   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4188                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
4189                Style);
4190   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4191                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4192                Style);
4193 }
4194 
4195 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
4196   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
4197   // Prefer keeping `::` followed by `operator` together.
4198   EXPECT_EQ("const aaaa::bbbbbbb &\n"
4199             "ccccccccc::operator++() {\n"
4200             "  stuff();\n"
4201             "}",
4202             format("const aaaa::bbbbbbb\n"
4203                    "&ccccccccc::operator++() { stuff(); }",
4204                    getLLVMStyleWithColumns(40)));
4205 }
4206 
4207 TEST_F(FormatTest, TrailingReturnType) {
4208   verifyFormat("auto foo() -> int;\n");
4209   verifyFormat("struct S {\n"
4210                "  auto bar() const -> int;\n"
4211                "};");
4212   verifyFormat("template <size_t Order, typename T>\n"
4213                "auto load_img(const std::string &filename)\n"
4214                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4215   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4216                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4217   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4218   verifyFormat("template <typename T>\n"
4219                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4220                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4221 
4222   // Not trailing return types.
4223   verifyFormat("void f() { auto a = b->c(); }");
4224 }
4225 
4226 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4227   // Avoid breaking before trailing 'const' or other trailing annotations, if
4228   // they are not function-like.
4229   FormatStyle Style = getGoogleStyle();
4230   Style.ColumnLimit = 47;
4231   verifyFormat("void someLongFunction(\n"
4232                "    int someLoooooooooooooongParameter) const {\n}",
4233                getLLVMStyleWithColumns(47));
4234   verifyFormat("LoooooongReturnType\n"
4235                "someLoooooooongFunction() const {}",
4236                getLLVMStyleWithColumns(47));
4237   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4238                "    const {}",
4239                Style);
4240   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4241                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4242   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4243                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4244   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4245                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4246   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4247                "                   aaaaaaaaaaa aaaaa) const override;");
4248   verifyGoogleFormat(
4249       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4250       "    const override;");
4251 
4252   // Even if the first parameter has to be wrapped.
4253   verifyFormat("void someLongFunction(\n"
4254                "    int someLongParameter) const {}",
4255                getLLVMStyleWithColumns(46));
4256   verifyFormat("void someLongFunction(\n"
4257                "    int someLongParameter) const {}",
4258                Style);
4259   verifyFormat("void someLongFunction(\n"
4260                "    int someLongParameter) override {}",
4261                Style);
4262   verifyFormat("void someLongFunction(\n"
4263                "    int someLongParameter) OVERRIDE {}",
4264                Style);
4265   verifyFormat("void someLongFunction(\n"
4266                "    int someLongParameter) final {}",
4267                Style);
4268   verifyFormat("void someLongFunction(\n"
4269                "    int someLongParameter) FINAL {}",
4270                Style);
4271   verifyFormat("void someLongFunction(\n"
4272                "    int parameter) const override {}",
4273                Style);
4274 
4275   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4276   verifyFormat("void someLongFunction(\n"
4277                "    int someLongParameter) const\n"
4278                "{\n"
4279                "}",
4280                Style);
4281 
4282   // Unless these are unknown annotations.
4283   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4284                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4285                "    LONG_AND_UGLY_ANNOTATION;");
4286 
4287   // Breaking before function-like trailing annotations is fine to keep them
4288   // close to their arguments.
4289   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4290                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4291   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4292                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4293   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4294                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4295   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4296                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4297   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4298 
4299   verifyFormat(
4300       "void aaaaaaaaaaaaaaaaaa()\n"
4301       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4302       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4303   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4304                "    __attribute__((unused));");
4305   verifyGoogleFormat(
4306       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4307       "    GUARDED_BY(aaaaaaaaaaaa);");
4308   verifyGoogleFormat(
4309       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4310       "    GUARDED_BY(aaaaaaaaaaaa);");
4311   verifyGoogleFormat(
4312       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4313       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4314   verifyGoogleFormat(
4315       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4316       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4317 }
4318 
4319 TEST_F(FormatTest, FunctionAnnotations) {
4320   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4321                "int OldFunction(const string &parameter) {}");
4322   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4323                "string OldFunction(const string &parameter) {}");
4324   verifyFormat("template <typename T>\n"
4325                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4326                "string OldFunction(const string &parameter) {}");
4327 
4328   // Not function annotations.
4329   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4330                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4331   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4332                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4333   verifyFormat("MACRO(abc).function() // wrap\n"
4334                "    << abc;");
4335   verifyFormat("MACRO(abc)->function() // wrap\n"
4336                "    << abc;");
4337   verifyFormat("MACRO(abc)::function() // wrap\n"
4338                "    << abc;");
4339 }
4340 
4341 TEST_F(FormatTest, BreaksDesireably) {
4342   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4343                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4344                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4345   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4346                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4347                "}");
4348 
4349   verifyFormat(
4350       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4351       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4352 
4353   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4354                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4355                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4356 
4357   verifyFormat(
4358       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4359       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4360       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4361       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4362       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4363 
4364   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4365                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4366 
4367   verifyFormat(
4368       "void f() {\n"
4369       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4370       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4371       "}");
4372   verifyFormat(
4373       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4374       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4375   verifyFormat(
4376       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4377       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4378   verifyFormat(
4379       "aaaaaa(aaa,\n"
4380       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4381       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4382       "       aaaa);");
4383   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4384                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4385                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4386 
4387   // Indent consistently independent of call expression and unary operator.
4388   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4389                "    dddddddddddddddddddddddddddddd));");
4390   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4391                "    dddddddddddddddddddddddddddddd));");
4392   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4393                "    dddddddddddddddddddddddddddddd));");
4394 
4395   // This test case breaks on an incorrect memoization, i.e. an optimization not
4396   // taking into account the StopAt value.
4397   verifyFormat(
4398       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4399       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4400       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4401       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4402 
4403   verifyFormat("{\n  {\n    {\n"
4404                "      Annotation.SpaceRequiredBefore =\n"
4405                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4406                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4407                "    }\n  }\n}");
4408 
4409   // Break on an outer level if there was a break on an inner level.
4410   EXPECT_EQ("f(g(h(a, // comment\n"
4411             "      b, c),\n"
4412             "    d, e),\n"
4413             "  x, y);",
4414             format("f(g(h(a, // comment\n"
4415                    "    b, c), d, e), x, y);"));
4416 
4417   // Prefer breaking similar line breaks.
4418   verifyFormat(
4419       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4420       "                             NSTrackingMouseEnteredAndExited |\n"
4421       "                             NSTrackingActiveAlways;");
4422 }
4423 
4424 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4425   FormatStyle NoBinPacking = getGoogleStyle();
4426   NoBinPacking.BinPackParameters = false;
4427   NoBinPacking.BinPackArguments = true;
4428   verifyFormat("void f() {\n"
4429                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4430                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4431                "}",
4432                NoBinPacking);
4433   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4434                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4435                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4436                NoBinPacking);
4437 
4438   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4439   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4440                "                        vector<int> bbbbbbbbbbbbbbb);",
4441                NoBinPacking);
4442   // FIXME: This behavior difference is probably not wanted. However, currently
4443   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4444   // template arguments from BreakBeforeParameter being set because of the
4445   // one-per-line formatting.
4446   verifyFormat(
4447       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4448       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4449       NoBinPacking);
4450   verifyFormat(
4451       "void fffffffffff(\n"
4452       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4453       "        aaaaaaaaaa);");
4454 }
4455 
4456 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4457   FormatStyle NoBinPacking = getGoogleStyle();
4458   NoBinPacking.BinPackParameters = false;
4459   NoBinPacking.BinPackArguments = false;
4460   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4461                "  aaaaaaaaaaaaaaaaaaaa,\n"
4462                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4463                NoBinPacking);
4464   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4465                "        aaaaaaaaaaaaa,\n"
4466                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4467                NoBinPacking);
4468   verifyFormat(
4469       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4470       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4471       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4472       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4473       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4474       NoBinPacking);
4475   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4476                "    .aaaaaaaaaaaaaaaaaa();",
4477                NoBinPacking);
4478   verifyFormat("void f() {\n"
4479                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4480                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4481                "}",
4482                NoBinPacking);
4483 
4484   verifyFormat(
4485       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4486       "             aaaaaaaaaaaa,\n"
4487       "             aaaaaaaaaaaa);",
4488       NoBinPacking);
4489   verifyFormat(
4490       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4491       "                               ddddddddddddddddddddddddddddd),\n"
4492       "             test);",
4493       NoBinPacking);
4494 
4495   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4496                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4497                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4498                "    aaaaaaaaaaaaaaaaaa;",
4499                NoBinPacking);
4500   verifyFormat("a(\"a\"\n"
4501                "  \"a\",\n"
4502                "  a);");
4503 
4504   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4505   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4506                "                aaaaaaaaa,\n"
4507                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4508                NoBinPacking);
4509   verifyFormat(
4510       "void f() {\n"
4511       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4512       "      .aaaaaaa();\n"
4513       "}",
4514       NoBinPacking);
4515   verifyFormat(
4516       "template <class SomeType, class SomeOtherType>\n"
4517       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4518       NoBinPacking);
4519 }
4520 
4521 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4522   FormatStyle Style = getLLVMStyleWithColumns(15);
4523   Style.ExperimentalAutoDetectBinPacking = true;
4524   EXPECT_EQ("aaa(aaaa,\n"
4525             "    aaaa,\n"
4526             "    aaaa);\n"
4527             "aaa(aaaa,\n"
4528             "    aaaa,\n"
4529             "    aaaa);",
4530             format("aaa(aaaa,\n" // one-per-line
4531                    "  aaaa,\n"
4532                    "    aaaa  );\n"
4533                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4534                    Style));
4535   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4536             "    aaaa);\n"
4537             "aaa(aaaa, aaaa,\n"
4538             "    aaaa);",
4539             format("aaa(aaaa,  aaaa,\n" // bin-packed
4540                    "    aaaa  );\n"
4541                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4542                    Style));
4543 }
4544 
4545 TEST_F(FormatTest, FormatsBuilderPattern) {
4546   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4547                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4548                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4549                "    .StartsWith(\".init\", ORDER_INIT)\n"
4550                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4551                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4552                "    .Default(ORDER_TEXT);\n");
4553 
4554   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4555                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4556   verifyFormat(
4557       "aaaaaaa->aaaaaaa\n"
4558       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4559       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4560       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4561   verifyFormat(
4562       "aaaaaaa->aaaaaaa\n"
4563       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4564       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4565   verifyFormat(
4566       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4567       "    aaaaaaaaaaaaaa);");
4568   verifyFormat(
4569       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4570       "    aaaaaa->aaaaaaaaaaaa()\n"
4571       "        ->aaaaaaaaaaaaaaaa(\n"
4572       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4573       "        ->aaaaaaaaaaaaaaaaa();");
4574   verifyGoogleFormat(
4575       "void f() {\n"
4576       "  someo->Add((new util::filetools::Handler(dir))\n"
4577       "                 ->OnEvent1(NewPermanentCallback(\n"
4578       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4579       "                 ->OnEvent2(NewPermanentCallback(\n"
4580       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4581       "                 ->OnEvent3(NewPermanentCallback(\n"
4582       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4583       "                 ->OnEvent5(NewPermanentCallback(\n"
4584       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4585       "                 ->OnEvent6(NewPermanentCallback(\n"
4586       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4587       "}");
4588 
4589   verifyFormat(
4590       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4591   verifyFormat("aaaaaaaaaaaaaaa()\n"
4592                "    .aaaaaaaaaaaaaaa()\n"
4593                "    .aaaaaaaaaaaaaaa()\n"
4594                "    .aaaaaaaaaaaaaaa()\n"
4595                "    .aaaaaaaaaaaaaaa();");
4596   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4597                "    .aaaaaaaaaaaaaaa()\n"
4598                "    .aaaaaaaaaaaaaaa()\n"
4599                "    .aaaaaaaaaaaaaaa();");
4600   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4601                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4602                "    .aaaaaaaaaaaaaaa();");
4603   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4604                "    ->aaaaaaaaaaaaaae(0)\n"
4605                "    ->aaaaaaaaaaaaaaa();");
4606 
4607   // Don't linewrap after very short segments.
4608   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4609                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4610                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4611   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4612                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4613                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4614   verifyFormat("aaa()\n"
4615                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4616                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4617                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4618 
4619   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4620                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4621                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4622   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4623                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4624                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4625 
4626   // Prefer not to break after empty parentheses.
4627   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4628                "    First->LastNewlineOffset);");
4629 
4630   // Prefer not to create "hanging" indents.
4631   verifyFormat(
4632       "return !soooooooooooooome_map\n"
4633       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4634       "            .second;");
4635   verifyFormat(
4636       "return aaaaaaaaaaaaaaaa\n"
4637       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4638       "    .aaaa(aaaaaaaaaaaaaa);");
4639   // No hanging indent here.
4640   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4641                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4642   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4643                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4644   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4645                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4646                getLLVMStyleWithColumns(60));
4647   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4648                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4649                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4650                getLLVMStyleWithColumns(59));
4651   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4652                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4653                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4654 
4655   // Dont break if only closing statements before member call
4656   verifyFormat("test() {\n"
4657                "  ([]() -> {\n"
4658                "    int b = 32;\n"
4659                "    return 3;\n"
4660                "  }).foo();\n"
4661                "}");
4662   verifyFormat("test() {\n"
4663                "  (\n"
4664                "      []() -> {\n"
4665                "        int b = 32;\n"
4666                "        return 3;\n"
4667                "      },\n"
4668                "      foo, bar)\n"
4669                "      .foo();\n"
4670                "}");
4671   verifyFormat("test() {\n"
4672                "  ([]() -> {\n"
4673                "    int b = 32;\n"
4674                "    return 3;\n"
4675                "  })\n"
4676                "      .foo()\n"
4677                "      .bar();\n"
4678                "}");
4679   verifyFormat("test() {\n"
4680                "  ([]() -> {\n"
4681                "    int b = 32;\n"
4682                "    return 3;\n"
4683                "  })\n"
4684                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
4685                "           \"bbbb\");\n"
4686                "}",
4687                getLLVMStyleWithColumns(30));
4688 }
4689 
4690 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4691   verifyFormat(
4692       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4693       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4694   verifyFormat(
4695       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4696       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4697 
4698   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4699                "    ccccccccccccccccccccccccc) {\n}");
4700   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4701                "    ccccccccccccccccccccccccc) {\n}");
4702 
4703   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4704                "    ccccccccccccccccccccccccc) {\n}");
4705   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4706                "    ccccccccccccccccccccccccc) {\n}");
4707 
4708   verifyFormat(
4709       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4710       "    ccccccccccccccccccccccccc) {\n}");
4711   verifyFormat(
4712       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4713       "    ccccccccccccccccccccccccc) {\n}");
4714 
4715   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4716                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4717                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4718                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4719   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4720                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4721                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4722                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4723 
4724   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4725                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4726                "    aaaaaaaaaaaaaaa != aa) {\n}");
4727   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4728                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4729                "    aaaaaaaaaaaaaaa != aa) {\n}");
4730 }
4731 
4732 TEST_F(FormatTest, BreaksAfterAssignments) {
4733   verifyFormat(
4734       "unsigned Cost =\n"
4735       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4736       "                        SI->getPointerAddressSpaceee());\n");
4737   verifyFormat(
4738       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4739       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4740 
4741   verifyFormat(
4742       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4743       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4744   verifyFormat("unsigned OriginalStartColumn =\n"
4745                "    SourceMgr.getSpellingColumnNumber(\n"
4746                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4747                "    1;");
4748 }
4749 
4750 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4751   FormatStyle Style = getLLVMStyle();
4752   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4753                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4754                Style);
4755 
4756   Style.PenaltyBreakAssignment = 20;
4757   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4758                "                                 cccccccccccccccccccccccccc;",
4759                Style);
4760 }
4761 
4762 TEST_F(FormatTest, AlignsAfterAssignments) {
4763   verifyFormat(
4764       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4765       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4766   verifyFormat(
4767       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4768       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4769   verifyFormat(
4770       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4771       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4772   verifyFormat(
4773       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4774       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4775   verifyFormat(
4776       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4777       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4778       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4779 }
4780 
4781 TEST_F(FormatTest, AlignsAfterReturn) {
4782   verifyFormat(
4783       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4784       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4785   verifyFormat(
4786       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4787       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4788   verifyFormat(
4789       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4790       "       aaaaaaaaaaaaaaaaaaaaaa();");
4791   verifyFormat(
4792       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4793       "        aaaaaaaaaaaaaaaaaaaaaa());");
4794   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4795                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4796   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4797                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4798                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4799   verifyFormat("return\n"
4800                "    // true if code is one of a or b.\n"
4801                "    code == a || code == b;");
4802 }
4803 
4804 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4805   verifyFormat(
4806       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4807       "                                                aaaaaaaaa aaaaaaa) {}");
4808   verifyFormat(
4809       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4810       "                                               aaaaaaaaaaa aaaaaaaaa);");
4811   verifyFormat(
4812       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4813       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4814   FormatStyle Style = getLLVMStyle();
4815   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4816   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4817                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4818                Style);
4819   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4820                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4821                Style);
4822   verifyFormat("SomeLongVariableName->someFunction(\n"
4823                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4824                Style);
4825   verifyFormat(
4826       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4827       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4828       Style);
4829   verifyFormat(
4830       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4831       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4832       Style);
4833   verifyFormat(
4834       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4835       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4836       Style);
4837 
4838   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4839                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4840                "        b));",
4841                Style);
4842 
4843   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4844   Style.BinPackArguments = false;
4845   Style.BinPackParameters = false;
4846   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4847                "    aaaaaaaaaaa aaaaaaaa,\n"
4848                "    aaaaaaaaa aaaaaaa,\n"
4849                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4850                Style);
4851   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4852                "    aaaaaaaaaaa aaaaaaaaa,\n"
4853                "    aaaaaaaaaaa aaaaaaaaa,\n"
4854                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4855                Style);
4856   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4857                "    aaaaaaaaaaaaaaa,\n"
4858                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4859                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4860                Style);
4861   verifyFormat(
4862       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4863       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4864       Style);
4865   verifyFormat(
4866       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4867       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4868       Style);
4869   verifyFormat(
4870       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4871       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4872       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4873       "    aaaaaaaaaaaaaaaa);",
4874       Style);
4875   verifyFormat(
4876       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4877       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4878       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4879       "    aaaaaaaaaaaaaaaa);",
4880       Style);
4881 }
4882 
4883 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4884   FormatStyle Style = getLLVMStyleWithColumns(40);
4885   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4886                "          bbbbbbbbbbbbbbbbbbbbbb);",
4887                Style);
4888   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4889   Style.AlignOperands = false;
4890   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4891                "          bbbbbbbbbbbbbbbbbbbbbb);",
4892                Style);
4893   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4894   Style.AlignOperands = true;
4895   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4896                "          bbbbbbbbbbbbbbbbbbbbbb);",
4897                Style);
4898   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4899   Style.AlignOperands = false;
4900   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4901                "    bbbbbbbbbbbbbbbbbbbbbb);",
4902                Style);
4903 }
4904 
4905 TEST_F(FormatTest, BreaksConditionalExpressions) {
4906   verifyFormat(
4907       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4908       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4909       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4910   verifyFormat(
4911       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4912       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4913       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4914   verifyFormat(
4915       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4916       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4917   verifyFormat(
4918       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4919       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4920       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4921   verifyFormat(
4922       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4923       "                                                    : aaaaaaaaaaaaa);");
4924   verifyFormat(
4925       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4926       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4927       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4928       "                   aaaaaaaaaaaaa);");
4929   verifyFormat(
4930       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4931       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4932       "                   aaaaaaaaaaaaa);");
4933   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4934                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4935                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4936                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4937                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4938   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4939                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4940                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4941                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4942                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4943                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4944                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4945   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4946                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4947                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4948                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4949                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4950   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4951                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4952                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4953   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4954                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4955                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4956                "        : aaaaaaaaaaaaaaaa;");
4957   verifyFormat(
4958       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4959       "    ? aaaaaaaaaaaaaaa\n"
4960       "    : aaaaaaaaaaaaaaa;");
4961   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4962                "          aaaaaaaaa\n"
4963                "      ? b\n"
4964                "      : c);");
4965   verifyFormat("return aaaa == bbbb\n"
4966                "           // comment\n"
4967                "           ? aaaa\n"
4968                "           : bbbb;");
4969   verifyFormat("unsigned Indent =\n"
4970                "    format(TheLine.First,\n"
4971                "           IndentForLevel[TheLine.Level] >= 0\n"
4972                "               ? IndentForLevel[TheLine.Level]\n"
4973                "               : TheLine * 2,\n"
4974                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4975                getLLVMStyleWithColumns(60));
4976   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4977                "                  ? aaaaaaaaaaaaaaa\n"
4978                "                  : bbbbbbbbbbbbbbb //\n"
4979                "                        ? ccccccccccccccc\n"
4980                "                        : ddddddddddddddd;");
4981   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4982                "                  ? aaaaaaaaaaaaaaa\n"
4983                "                  : (bbbbbbbbbbbbbbb //\n"
4984                "                         ? ccccccccccccccc\n"
4985                "                         : ddddddddddddddd);");
4986   verifyFormat(
4987       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4988       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4989       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4990       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4991       "                                      : aaaaaaaaaa;");
4992   verifyFormat(
4993       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4994       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4995       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4996 
4997   FormatStyle NoBinPacking = getLLVMStyle();
4998   NoBinPacking.BinPackArguments = false;
4999   verifyFormat(
5000       "void f() {\n"
5001       "  g(aaa,\n"
5002       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5003       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5004       "        ? aaaaaaaaaaaaaaa\n"
5005       "        : aaaaaaaaaaaaaaa);\n"
5006       "}",
5007       NoBinPacking);
5008   verifyFormat(
5009       "void f() {\n"
5010       "  g(aaa,\n"
5011       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5012       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5013       "        ?: aaaaaaaaaaaaaaa);\n"
5014       "}",
5015       NoBinPacking);
5016 
5017   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
5018                "             // comment.\n"
5019                "             ccccccccccccccccccccccccccccccccccccccc\n"
5020                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5021                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
5022 
5023   // Assignments in conditional expressions. Apparently not uncommon :-(.
5024   verifyFormat("return a != b\n"
5025                "           // comment\n"
5026                "           ? a = b\n"
5027                "           : a = b;");
5028   verifyFormat("return a != b\n"
5029                "           // comment\n"
5030                "           ? a = a != b\n"
5031                "                     // comment\n"
5032                "                     ? a = b\n"
5033                "                     : a\n"
5034                "           : a;\n");
5035   verifyFormat("return a != b\n"
5036                "           // comment\n"
5037                "           ? a\n"
5038                "           : a = a != b\n"
5039                "                     // comment\n"
5040                "                     ? a = b\n"
5041                "                     : a;");
5042 }
5043 
5044 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
5045   FormatStyle Style = getLLVMStyle();
5046   Style.BreakBeforeTernaryOperators = false;
5047   Style.ColumnLimit = 70;
5048   verifyFormat(
5049       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5050       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5051       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5052       Style);
5053   verifyFormat(
5054       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5055       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5056       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5057       Style);
5058   verifyFormat(
5059       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5060       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5061       Style);
5062   verifyFormat(
5063       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
5064       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5065       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5066       Style);
5067   verifyFormat(
5068       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
5069       "                                                      aaaaaaaaaaaaa);",
5070       Style);
5071   verifyFormat(
5072       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5073       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5074       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5075       "                   aaaaaaaaaaaaa);",
5076       Style);
5077   verifyFormat(
5078       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5079       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5080       "                   aaaaaaaaaaaaa);",
5081       Style);
5082   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5083                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5084                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5085                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5086                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5087                Style);
5088   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5089                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5090                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5091                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5092                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5093                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5094                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5095                Style);
5096   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5097                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
5098                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5099                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5100                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5101                Style);
5102   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5103                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5104                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5105                Style);
5106   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5107                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5108                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5109                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5110                Style);
5111   verifyFormat(
5112       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5113       "    aaaaaaaaaaaaaaa :\n"
5114       "    aaaaaaaaaaaaaaa;",
5115       Style);
5116   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5117                "          aaaaaaaaa ?\n"
5118                "      b :\n"
5119                "      c);",
5120                Style);
5121   verifyFormat("unsigned Indent =\n"
5122                "    format(TheLine.First,\n"
5123                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
5124                "               IndentForLevel[TheLine.Level] :\n"
5125                "               TheLine * 2,\n"
5126                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5127                Style);
5128   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5129                "                  aaaaaaaaaaaaaaa :\n"
5130                "                  bbbbbbbbbbbbbbb ? //\n"
5131                "                      ccccccccccccccc :\n"
5132                "                      ddddddddddddddd;",
5133                Style);
5134   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5135                "                  aaaaaaaaaaaaaaa :\n"
5136                "                  (bbbbbbbbbbbbbbb ? //\n"
5137                "                       ccccccccccccccc :\n"
5138                "                       ddddddddddddddd);",
5139                Style);
5140   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5141                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
5142                "            ccccccccccccccccccccccccccc;",
5143                Style);
5144   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5145                "           aaaaa :\n"
5146                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
5147                Style);
5148 }
5149 
5150 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
5151   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
5152                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
5153   verifyFormat("bool a = true, b = false;");
5154 
5155   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5156                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
5157                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
5158                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
5159   verifyFormat(
5160       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5161       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
5162       "     d = e && f;");
5163   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
5164                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
5165   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5166                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
5167   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
5168                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
5169 
5170   FormatStyle Style = getGoogleStyle();
5171   Style.PointerAlignment = FormatStyle::PAS_Left;
5172   Style.DerivePointerAlignment = false;
5173   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5174                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
5175                "    *b = bbbbbbbbbbbbbbbbbbb;",
5176                Style);
5177   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5178                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
5179                Style);
5180   verifyFormat("vector<int*> a, b;", Style);
5181   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
5182 }
5183 
5184 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
5185   verifyFormat("arr[foo ? bar : baz];");
5186   verifyFormat("f()[foo ? bar : baz];");
5187   verifyFormat("(a + b)[foo ? bar : baz];");
5188   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
5189 }
5190 
5191 TEST_F(FormatTest, AlignsStringLiterals) {
5192   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
5193                "                                      \"short literal\");");
5194   verifyFormat(
5195       "looooooooooooooooooooooooongFunction(\n"
5196       "    \"short literal\"\n"
5197       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
5198   verifyFormat("someFunction(\"Always break between multi-line\"\n"
5199                "             \" string literals\",\n"
5200                "             and, other, parameters);");
5201   EXPECT_EQ("fun + \"1243\" /* comment */\n"
5202             "      \"5678\";",
5203             format("fun + \"1243\" /* comment */\n"
5204                    "    \"5678\";",
5205                    getLLVMStyleWithColumns(28)));
5206   EXPECT_EQ(
5207       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
5208       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
5209       "         \"aaaaaaaaaaaaaaaa\";",
5210       format("aaaaaa ="
5211              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
5212              "aaaaaaaaaaaaaaaaaaaaa\" "
5213              "\"aaaaaaaaaaaaaaaa\";"));
5214   verifyFormat("a = a + \"a\"\n"
5215                "        \"a\"\n"
5216                "        \"a\";");
5217   verifyFormat("f(\"a\", \"b\"\n"
5218                "       \"c\");");
5219 
5220   verifyFormat(
5221       "#define LL_FORMAT \"ll\"\n"
5222       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
5223       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
5224 
5225   verifyFormat("#define A(X)          \\\n"
5226                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
5227                "  \"ccccc\"",
5228                getLLVMStyleWithColumns(23));
5229   verifyFormat("#define A \"def\"\n"
5230                "f(\"abc\" A \"ghi\"\n"
5231                "  \"jkl\");");
5232 
5233   verifyFormat("f(L\"a\"\n"
5234                "  L\"b\");");
5235   verifyFormat("#define A(X)            \\\n"
5236                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
5237                "  L\"ccccc\"",
5238                getLLVMStyleWithColumns(25));
5239 
5240   verifyFormat("f(@\"a\"\n"
5241                "  @\"b\");");
5242   verifyFormat("NSString s = @\"a\"\n"
5243                "             @\"b\"\n"
5244                "             @\"c\";");
5245   verifyFormat("NSString s = @\"a\"\n"
5246                "              \"b\"\n"
5247                "              \"c\";");
5248 }
5249 
5250 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5251   FormatStyle Style = getLLVMStyle();
5252   // No declarations or definitions should be moved to own line.
5253   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5254   verifyFormat("class A {\n"
5255                "  int f() { return 1; }\n"
5256                "  int g();\n"
5257                "};\n"
5258                "int f() { return 1; }\n"
5259                "int g();\n",
5260                Style);
5261 
5262   // All declarations and definitions should have the return type moved to its
5263   // own
5264   // line.
5265   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5266   verifyFormat("class E {\n"
5267                "  int\n"
5268                "  f() {\n"
5269                "    return 1;\n"
5270                "  }\n"
5271                "  int\n"
5272                "  g();\n"
5273                "};\n"
5274                "int\n"
5275                "f() {\n"
5276                "  return 1;\n"
5277                "}\n"
5278                "int\n"
5279                "g();\n",
5280                Style);
5281 
5282   // Top-level definitions, and no kinds of declarations should have the
5283   // return type moved to its own line.
5284   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5285   verifyFormat("class B {\n"
5286                "  int f() { return 1; }\n"
5287                "  int g();\n"
5288                "};\n"
5289                "int\n"
5290                "f() {\n"
5291                "  return 1;\n"
5292                "}\n"
5293                "int g();\n",
5294                Style);
5295 
5296   // Top-level definitions and declarations should have the return type moved
5297   // to its own line.
5298   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5299   verifyFormat("class C {\n"
5300                "  int f() { return 1; }\n"
5301                "  int g();\n"
5302                "};\n"
5303                "int\n"
5304                "f() {\n"
5305                "  return 1;\n"
5306                "}\n"
5307                "int\n"
5308                "g();\n",
5309                Style);
5310 
5311   // All definitions should have the return type moved to its own line, but no
5312   // kinds of declarations.
5313   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5314   verifyFormat("class D {\n"
5315                "  int\n"
5316                "  f() {\n"
5317                "    return 1;\n"
5318                "  }\n"
5319                "  int g();\n"
5320                "};\n"
5321                "int\n"
5322                "f() {\n"
5323                "  return 1;\n"
5324                "}\n"
5325                "int g();\n",
5326                Style);
5327   verifyFormat("const char *\n"
5328                "f(void) {\n" // Break here.
5329                "  return \"\";\n"
5330                "}\n"
5331                "const char *bar(void);\n", // No break here.
5332                Style);
5333   verifyFormat("template <class T>\n"
5334                "T *\n"
5335                "f(T &c) {\n" // Break here.
5336                "  return NULL;\n"
5337                "}\n"
5338                "template <class T> T *f(T &c);\n", // No break here.
5339                Style);
5340   verifyFormat("class C {\n"
5341                "  int\n"
5342                "  operator+() {\n"
5343                "    return 1;\n"
5344                "  }\n"
5345                "  int\n"
5346                "  operator()() {\n"
5347                "    return 1;\n"
5348                "  }\n"
5349                "};\n",
5350                Style);
5351   verifyFormat("void\n"
5352                "A::operator()() {}\n"
5353                "void\n"
5354                "A::operator>>() {}\n"
5355                "void\n"
5356                "A::operator+() {}\n",
5357                Style);
5358   verifyFormat("void *operator new(std::size_t s);", // No break here.
5359                Style);
5360   verifyFormat("void *\n"
5361                "operator new(std::size_t s) {}",
5362                Style);
5363   verifyFormat("void *\n"
5364                "operator delete[](void *ptr) {}",
5365                Style);
5366   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5367   verifyFormat("const char *\n"
5368                "f(void)\n" // Break here.
5369                "{\n"
5370                "  return \"\";\n"
5371                "}\n"
5372                "const char *bar(void);\n", // No break here.
5373                Style);
5374   verifyFormat("template <class T>\n"
5375                "T *\n"     // Problem here: no line break
5376                "f(T &c)\n" // Break here.
5377                "{\n"
5378                "  return NULL;\n"
5379                "}\n"
5380                "template <class T> T *f(T &c);\n", // No break here.
5381                Style);
5382 }
5383 
5384 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5385   FormatStyle NoBreak = getLLVMStyle();
5386   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5387   FormatStyle Break = getLLVMStyle();
5388   Break.AlwaysBreakBeforeMultilineStrings = true;
5389   verifyFormat("aaaa = \"bbbb\"\n"
5390                "       \"cccc\";",
5391                NoBreak);
5392   verifyFormat("aaaa =\n"
5393                "    \"bbbb\"\n"
5394                "    \"cccc\";",
5395                Break);
5396   verifyFormat("aaaa(\"bbbb\"\n"
5397                "     \"cccc\");",
5398                NoBreak);
5399   verifyFormat("aaaa(\n"
5400                "    \"bbbb\"\n"
5401                "    \"cccc\");",
5402                Break);
5403   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5404                "          \"cccc\");",
5405                NoBreak);
5406   verifyFormat("aaaa(qqq,\n"
5407                "     \"bbbb\"\n"
5408                "     \"cccc\");",
5409                Break);
5410   verifyFormat("aaaa(qqq,\n"
5411                "     L\"bbbb\"\n"
5412                "     L\"cccc\");",
5413                Break);
5414   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5415                "                      \"bbbb\"));",
5416                Break);
5417   verifyFormat("string s = someFunction(\n"
5418                "    \"abc\"\n"
5419                "    \"abc\");",
5420                Break);
5421 
5422   // As we break before unary operators, breaking right after them is bad.
5423   verifyFormat("string foo = abc ? \"x\"\n"
5424                "                   \"blah blah blah blah blah blah\"\n"
5425                "                 : \"y\";",
5426                Break);
5427 
5428   // Don't break if there is no column gain.
5429   verifyFormat("f(\"aaaa\"\n"
5430                "  \"bbbb\");",
5431                Break);
5432 
5433   // Treat literals with escaped newlines like multi-line string literals.
5434   EXPECT_EQ("x = \"a\\\n"
5435             "b\\\n"
5436             "c\";",
5437             format("x = \"a\\\n"
5438                    "b\\\n"
5439                    "c\";",
5440                    NoBreak));
5441   EXPECT_EQ("xxxx =\n"
5442             "    \"a\\\n"
5443             "b\\\n"
5444             "c\";",
5445             format("xxxx = \"a\\\n"
5446                    "b\\\n"
5447                    "c\";",
5448                    Break));
5449 
5450   EXPECT_EQ("NSString *const kString =\n"
5451             "    @\"aaaa\"\n"
5452             "    @\"bbbb\";",
5453             format("NSString *const kString = @\"aaaa\"\n"
5454                    "@\"bbbb\";",
5455                    Break));
5456 
5457   Break.ColumnLimit = 0;
5458   verifyFormat("const char *hello = \"hello llvm\";", Break);
5459 }
5460 
5461 TEST_F(FormatTest, AlignsPipes) {
5462   verifyFormat(
5463       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5464       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5465       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5466   verifyFormat(
5467       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5468       "                     << aaaaaaaaaaaaaaaaaaaa;");
5469   verifyFormat(
5470       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5471       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5472   verifyFormat(
5473       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5474       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5475   verifyFormat(
5476       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5477       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5478       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5479   verifyFormat(
5480       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5481       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5482       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5483   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5484                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5485                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5486                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5487   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5488                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5489   verifyFormat(
5490       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5491       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5492   verifyFormat(
5493       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5494       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5495 
5496   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5497                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5498   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5499                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5500                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5501                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5502   verifyFormat("LOG_IF(aaa == //\n"
5503                "       bbb)\n"
5504                "    << a << b;");
5505 
5506   // But sometimes, breaking before the first "<<" is desirable.
5507   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5508                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5509   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5510                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5511                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5512   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5513                "    << BEF << IsTemplate << Description << E->getType();");
5514   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5515                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5516                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5517   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5518                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5519                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5520                "    << aaa;");
5521 
5522   verifyFormat(
5523       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5524       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5525 
5526   // Incomplete string literal.
5527   EXPECT_EQ("llvm::errs() << \"\n"
5528             "             << a;",
5529             format("llvm::errs() << \"\n<<a;"));
5530 
5531   verifyFormat("void f() {\n"
5532                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5533                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5534                "}");
5535 
5536   // Handle 'endl'.
5537   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5538                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5539   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5540 
5541   // Handle '\n'.
5542   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5543                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5544   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5545                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5546   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5547                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5548   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5549 }
5550 
5551 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5552   verifyFormat("return out << \"somepacket = {\\n\"\n"
5553                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5554                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5555                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5556                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5557                "           << \"}\";");
5558 
5559   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5560                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5561                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5562   verifyFormat(
5563       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5564       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5565       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5566       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5567       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5568   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5569                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5570   verifyFormat(
5571       "void f() {\n"
5572       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5573       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5574       "}");
5575 
5576   // Breaking before the first "<<" is generally not desirable.
5577   verifyFormat(
5578       "llvm::errs()\n"
5579       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5580       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5581       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5582       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5583       getLLVMStyleWithColumns(70));
5584   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5585                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5586                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5587                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5588                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5589                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5590                getLLVMStyleWithColumns(70));
5591 
5592   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5593                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5594                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5595   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5596                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5597                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5598   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5599                "           (aaaa + aaaa);",
5600                getLLVMStyleWithColumns(40));
5601   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5602                "                  (aaaaaaa + aaaaa));",
5603                getLLVMStyleWithColumns(40));
5604   verifyFormat(
5605       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5606       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5607       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5608 }
5609 
5610 TEST_F(FormatTest, UnderstandsEquals) {
5611   verifyFormat(
5612       "aaaaaaaaaaaaaaaaa =\n"
5613       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5614   verifyFormat(
5615       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5616       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5617   verifyFormat(
5618       "if (a) {\n"
5619       "  f();\n"
5620       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5621       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5622       "}");
5623 
5624   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5625                "        100000000 + 10000000) {\n}");
5626 }
5627 
5628 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5629   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5630                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5631 
5632   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5633                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5634 
5635   verifyFormat(
5636       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5637       "                                                          Parameter2);");
5638 
5639   verifyFormat(
5640       "ShortObject->shortFunction(\n"
5641       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5642       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5643 
5644   verifyFormat("loooooooooooooongFunction(\n"
5645                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5646 
5647   verifyFormat(
5648       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5649       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5650 
5651   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5652                "    .WillRepeatedly(Return(SomeValue));");
5653   verifyFormat("void f() {\n"
5654                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5655                "      .Times(2)\n"
5656                "      .WillRepeatedly(Return(SomeValue));\n"
5657                "}");
5658   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5659                "    ccccccccccccccccccccccc);");
5660   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5661                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5662                "          .aaaaa(aaaaa),\n"
5663                "      aaaaaaaaaaaaaaaaaaaaa);");
5664   verifyFormat("void f() {\n"
5665                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5666                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5667                "}");
5668   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5669                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5670                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5671                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5672                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5673   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5674                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5675                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5676                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5677                "}");
5678 
5679   // Here, it is not necessary to wrap at "." or "->".
5680   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5681                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5682   verifyFormat(
5683       "aaaaaaaaaaa->aaaaaaaaa(\n"
5684       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5685       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5686 
5687   verifyFormat(
5688       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5689       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5690   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5691                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5692   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5693                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5694 
5695   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5696                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5697                "    .a();");
5698 
5699   FormatStyle NoBinPacking = getLLVMStyle();
5700   NoBinPacking.BinPackParameters = false;
5701   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5702                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5703                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5704                "                         aaaaaaaaaaaaaaaaaaa,\n"
5705                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5706                NoBinPacking);
5707 
5708   // If there is a subsequent call, change to hanging indentation.
5709   verifyFormat(
5710       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5711       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5712       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5713   verifyFormat(
5714       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5715       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5716   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5717                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5718                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5719   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5720                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5721                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5722 }
5723 
5724 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5725   verifyFormat("template <typename T>\n"
5726                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5727   verifyFormat("template <typename T>\n"
5728                "// T should be one of {A, B}.\n"
5729                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5730   verifyFormat(
5731       "template <typename T>\n"
5732       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5733   verifyFormat("template <typename T>\n"
5734                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5735                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5736   verifyFormat(
5737       "template <typename T>\n"
5738       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5739       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5740   verifyFormat(
5741       "template <typename T>\n"
5742       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5743       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5744       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5745   verifyFormat("template <typename T>\n"
5746                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5747                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5748   verifyFormat(
5749       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5750       "          typename T4 = char>\n"
5751       "void f();");
5752   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5753                "          template <typename> class cccccccccccccccccccccc,\n"
5754                "          typename ddddddddddddd>\n"
5755                "class C {};");
5756   verifyFormat(
5757       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5758       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5759 
5760   verifyFormat("void f() {\n"
5761                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5762                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5763                "}");
5764 
5765   verifyFormat("template <typename T> class C {};");
5766   verifyFormat("template <typename T> void f();");
5767   verifyFormat("template <typename T> void f() {}");
5768   verifyFormat(
5769       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5770       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5771       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5772       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5773       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5774       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5775       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5776       getLLVMStyleWithColumns(72));
5777   EXPECT_EQ("static_cast<A< //\n"
5778             "    B> *>(\n"
5779             "\n"
5780             ");",
5781             format("static_cast<A<//\n"
5782                    "    B>*>(\n"
5783                    "\n"
5784                    "    );"));
5785   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5786                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5787 
5788   FormatStyle AlwaysBreak = getLLVMStyle();
5789   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
5790   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5791   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5792   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5793   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5794                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5795                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5796   verifyFormat("template <template <typename> class Fooooooo,\n"
5797                "          template <typename> class Baaaaaaar>\n"
5798                "struct C {};",
5799                AlwaysBreak);
5800   verifyFormat("template <typename T> // T can be A, B or C.\n"
5801                "struct C {};",
5802                AlwaysBreak);
5803   verifyFormat("template <enum E> class A {\n"
5804                "public:\n"
5805                "  E *f();\n"
5806                "};");
5807 
5808   FormatStyle NeverBreak = getLLVMStyle();
5809   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
5810   verifyFormat("template <typename T> class C {};", NeverBreak);
5811   verifyFormat("template <typename T> void f();", NeverBreak);
5812   verifyFormat("template <typename T> void f() {}", NeverBreak);
5813   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5814                NeverBreak);
5815   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5816                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5817                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
5818                NeverBreak);
5819   verifyFormat("template <template <typename> class Fooooooo,\n"
5820                "          template <typename> class Baaaaaaar>\n"
5821                "struct C {};",
5822                NeverBreak);
5823   verifyFormat("template <typename T> // T can be A, B or C.\n"
5824                "struct C {};",
5825                NeverBreak);
5826   verifyFormat("template <enum E> class A {\n"
5827                "public:\n"
5828                "  E *f();\n"
5829                "};", NeverBreak);
5830   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
5831   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5832                NeverBreak);
5833 }
5834 
5835 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
5836   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
5837   Style.ColumnLimit = 60;
5838   EXPECT_EQ("// Baseline - no comments.\n"
5839             "template <\n"
5840             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5841             "void f() {}",
5842             format("// Baseline - no comments.\n"
5843                    "template <\n"
5844                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5845                    "void f() {}",
5846                    Style));
5847 
5848   EXPECT_EQ("template <\n"
5849             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5850             "void f() {}",
5851             format("template <\n"
5852                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5853                    "void f() {}",
5854                    Style));
5855 
5856   EXPECT_EQ(
5857       "template <\n"
5858       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
5859       "void f() {}",
5860       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
5861              "void f() {}",
5862              Style));
5863 
5864   EXPECT_EQ(
5865       "template <\n"
5866       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5867       "                                               // multiline\n"
5868       "void f() {}",
5869       format("template <\n"
5870              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5871              "                                              // multiline\n"
5872              "void f() {}",
5873              Style));
5874 
5875   EXPECT_EQ(
5876       "template <typename aaaaaaaaaa<\n"
5877       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
5878       "void f() {}",
5879       format(
5880           "template <\n"
5881           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
5882           "void f() {}",
5883           Style));
5884 }
5885 
5886 TEST_F(FormatTest, WrapsTemplateParameters) {
5887   FormatStyle Style = getLLVMStyle();
5888   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5889   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5890   verifyFormat(
5891       "template <typename... a> struct q {};\n"
5892       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5893       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5894       "    y;",
5895       Style);
5896   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5897   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5898   verifyFormat(
5899       "template <typename... a> struct r {};\n"
5900       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5901       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5902       "    y;",
5903       Style);
5904   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5905   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5906   verifyFormat(
5907       "template <typename... a> struct s {};\n"
5908       "extern s<\n"
5909       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5910       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5911       "    y;",
5912       Style);
5913   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5914   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5915   verifyFormat(
5916       "template <typename... a> struct t {};\n"
5917       "extern t<\n"
5918       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5919       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5920       "    y;",
5921       Style);
5922 }
5923 
5924 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5925   verifyFormat(
5926       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5927       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5928   verifyFormat(
5929       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5930       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5931       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5932 
5933   // FIXME: Should we have the extra indent after the second break?
5934   verifyFormat(
5935       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5936       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5937       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5938 
5939   verifyFormat(
5940       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5941       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5942 
5943   // Breaking at nested name specifiers is generally not desirable.
5944   verifyFormat(
5945       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5946       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5947 
5948   verifyFormat(
5949       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5950       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5951       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5952       "                   aaaaaaaaaaaaaaaaaaaaa);",
5953       getLLVMStyleWithColumns(74));
5954 
5955   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5956                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5957                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5958 }
5959 
5960 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5961   verifyFormat("A<int> a;");
5962   verifyFormat("A<A<A<int>>> a;");
5963   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5964   verifyFormat("bool x = a < 1 || 2 > a;");
5965   verifyFormat("bool x = 5 < f<int>();");
5966   verifyFormat("bool x = f<int>() > 5;");
5967   verifyFormat("bool x = 5 < a<int>::x;");
5968   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5969   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5970 
5971   verifyGoogleFormat("A<A<int>> a;");
5972   verifyGoogleFormat("A<A<A<int>>> a;");
5973   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5974   verifyGoogleFormat("A<A<int> > a;");
5975   verifyGoogleFormat("A<A<A<int> > > a;");
5976   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5977   verifyGoogleFormat("A<::A<int>> a;");
5978   verifyGoogleFormat("A<::A> a;");
5979   verifyGoogleFormat("A< ::A> a;");
5980   verifyGoogleFormat("A< ::A<int> > a;");
5981   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5982   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5983   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5984   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5985   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5986             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5987 
5988   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5989 
5990   verifyFormat("test >> a >> b;");
5991   verifyFormat("test << a >> b;");
5992 
5993   verifyFormat("f<int>();");
5994   verifyFormat("template <typename T> void f() {}");
5995   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5996   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5997                "sizeof(char)>::type>;");
5998   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5999   verifyFormat("f(a.operator()<A>());");
6000   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6001                "      .template operator()<A>());",
6002                getLLVMStyleWithColumns(35));
6003 
6004   // Not template parameters.
6005   verifyFormat("return a < b && c > d;");
6006   verifyFormat("void f() {\n"
6007                "  while (a < b && c > d) {\n"
6008                "  }\n"
6009                "}");
6010   verifyFormat("template <typename... Types>\n"
6011                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
6012 
6013   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6014                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
6015                getLLVMStyleWithColumns(60));
6016   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
6017   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
6018   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
6019 }
6020 
6021 TEST_F(FormatTest, BitshiftOperatorWidth) {
6022   EXPECT_EQ("int a = 1 << 2; /* foo\n"
6023             "                   bar */",
6024             format("int    a=1<<2;  /* foo\n"
6025                    "                   bar */"));
6026 
6027   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
6028             "                     bar */",
6029             format("int  b  =256>>1 ;  /* foo\n"
6030                    "                      bar */"));
6031 }
6032 
6033 TEST_F(FormatTest, UnderstandsBinaryOperators) {
6034   verifyFormat("COMPARE(a, ==, b);");
6035   verifyFormat("auto s = sizeof...(Ts) - 1;");
6036 }
6037 
6038 TEST_F(FormatTest, UnderstandsPointersToMembers) {
6039   verifyFormat("int A::*x;");
6040   verifyFormat("int (S::*func)(void *);");
6041   verifyFormat("void f() { int (S::*func)(void *); }");
6042   verifyFormat("typedef bool *(Class::*Member)() const;");
6043   verifyFormat("void f() {\n"
6044                "  (a->*f)();\n"
6045                "  a->*x;\n"
6046                "  (a.*f)();\n"
6047                "  ((*a).*f)();\n"
6048                "  a.*x;\n"
6049                "}");
6050   verifyFormat("void f() {\n"
6051                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6052                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
6053                "}");
6054   verifyFormat(
6055       "(aaaaaaaaaa->*bbbbbbb)(\n"
6056       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6057   FormatStyle Style = getLLVMStyle();
6058   Style.PointerAlignment = FormatStyle::PAS_Left;
6059   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
6060 }
6061 
6062 TEST_F(FormatTest, UnderstandsUnaryOperators) {
6063   verifyFormat("int a = -2;");
6064   verifyFormat("f(-1, -2, -3);");
6065   verifyFormat("a[-1] = 5;");
6066   verifyFormat("int a = 5 + -2;");
6067   verifyFormat("if (i == -1) {\n}");
6068   verifyFormat("if (i != -1) {\n}");
6069   verifyFormat("if (i > -1) {\n}");
6070   verifyFormat("if (i < -1) {\n}");
6071   verifyFormat("++(a->f());");
6072   verifyFormat("--(a->f());");
6073   verifyFormat("(a->f())++;");
6074   verifyFormat("a[42]++;");
6075   verifyFormat("if (!(a->f())) {\n}");
6076   verifyFormat("if (!+i) {\n}");
6077   verifyFormat("~&a;");
6078 
6079   verifyFormat("a-- > b;");
6080   verifyFormat("b ? -a : c;");
6081   verifyFormat("n * sizeof char16;");
6082   verifyFormat("n * alignof char16;", getGoogleStyle());
6083   verifyFormat("sizeof(char);");
6084   verifyFormat("alignof(char);", getGoogleStyle());
6085 
6086   verifyFormat("return -1;");
6087   verifyFormat("switch (a) {\n"
6088                "case -1:\n"
6089                "  break;\n"
6090                "}");
6091   verifyFormat("#define X -1");
6092   verifyFormat("#define X -kConstant");
6093 
6094   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
6095   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
6096 
6097   verifyFormat("int a = /* confusing comment */ -1;");
6098   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
6099   verifyFormat("int a = i /* confusing comment */++;");
6100 }
6101 
6102 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
6103   verifyFormat("if (!aaaaaaaaaa( // break\n"
6104                "        aaaaa)) {\n"
6105                "}");
6106   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
6107                "    aaaaa));");
6108   verifyFormat("*aaa = aaaaaaa( // break\n"
6109                "    bbbbbb);");
6110 }
6111 
6112 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
6113   verifyFormat("bool operator<();");
6114   verifyFormat("bool operator>();");
6115   verifyFormat("bool operator=();");
6116   verifyFormat("bool operator==();");
6117   verifyFormat("bool operator!=();");
6118   verifyFormat("int operator+();");
6119   verifyFormat("int operator++();");
6120   verifyFormat("int operator++(int) volatile noexcept;");
6121   verifyFormat("bool operator,();");
6122   verifyFormat("bool operator();");
6123   verifyFormat("bool operator()();");
6124   verifyFormat("bool operator[]();");
6125   verifyFormat("operator bool();");
6126   verifyFormat("operator int();");
6127   verifyFormat("operator void *();");
6128   verifyFormat("operator SomeType<int>();");
6129   verifyFormat("operator SomeType<int, int>();");
6130   verifyFormat("operator SomeType<SomeType<int>>();");
6131   verifyFormat("void *operator new(std::size_t size);");
6132   verifyFormat("void *operator new[](std::size_t size);");
6133   verifyFormat("void operator delete(void *ptr);");
6134   verifyFormat("void operator delete[](void *ptr);");
6135   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
6136                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
6137   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
6138                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
6139 
6140   verifyFormat(
6141       "ostream &operator<<(ostream &OutputStream,\n"
6142       "                    SomeReallyLongType WithSomeReallyLongValue);");
6143   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
6144                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
6145                "  return left.group < right.group;\n"
6146                "}");
6147   verifyFormat("SomeType &operator=(const SomeType &S);");
6148   verifyFormat("f.template operator()<int>();");
6149 
6150   verifyGoogleFormat("operator void*();");
6151   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
6152   verifyGoogleFormat("operator ::A();");
6153 
6154   verifyFormat("using A::operator+;");
6155   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
6156                "int i;");
6157 }
6158 
6159 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
6160   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
6161   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
6162   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
6163   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
6164   verifyFormat("Deleted &operator=(const Deleted &) &;");
6165   verifyFormat("Deleted &operator=(const Deleted &) &&;");
6166   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
6167   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
6168   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
6169   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
6170   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
6171   verifyFormat("void Fn(T const &) const &;");
6172   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
6173   verifyFormat("template <typename T>\n"
6174                "void F(T) && = delete;",
6175                getGoogleStyle());
6176 
6177   FormatStyle AlignLeft = getLLVMStyle();
6178   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
6179   verifyFormat("void A::b() && {}", AlignLeft);
6180   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
6181   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
6182                AlignLeft);
6183   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
6184   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
6185   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
6186   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
6187   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
6188   verifyFormat("auto Function(T) & -> void;", AlignLeft);
6189   verifyFormat("void Fn(T const&) const&;", AlignLeft);
6190   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
6191 
6192   FormatStyle Spaces = getLLVMStyle();
6193   Spaces.SpacesInCStyleCastParentheses = true;
6194   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
6195   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
6196   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
6197   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
6198 
6199   Spaces.SpacesInCStyleCastParentheses = false;
6200   Spaces.SpacesInParentheses = true;
6201   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
6202   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
6203   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
6204   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
6205 }
6206 
6207 TEST_F(FormatTest, UnderstandsNewAndDelete) {
6208   verifyFormat("void f() {\n"
6209                "  A *a = new A;\n"
6210                "  A *a = new (placement) A;\n"
6211                "  delete a;\n"
6212                "  delete (A *)a;\n"
6213                "}");
6214   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6215                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6216   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6217                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6218                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6219   verifyFormat("delete[] h->p;");
6220 }
6221 
6222 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
6223   verifyFormat("int *f(int *a) {}");
6224   verifyFormat("int main(int argc, char **argv) {}");
6225   verifyFormat("Test::Test(int b) : a(b * b) {}");
6226   verifyIndependentOfContext("f(a, *a);");
6227   verifyFormat("void g() { f(*a); }");
6228   verifyIndependentOfContext("int a = b * 10;");
6229   verifyIndependentOfContext("int a = 10 * b;");
6230   verifyIndependentOfContext("int a = b * c;");
6231   verifyIndependentOfContext("int a += b * c;");
6232   verifyIndependentOfContext("int a -= b * c;");
6233   verifyIndependentOfContext("int a *= b * c;");
6234   verifyIndependentOfContext("int a /= b * c;");
6235   verifyIndependentOfContext("int a = *b;");
6236   verifyIndependentOfContext("int a = *b * c;");
6237   verifyIndependentOfContext("int a = b * *c;");
6238   verifyIndependentOfContext("int a = b * (10);");
6239   verifyIndependentOfContext("S << b * (10);");
6240   verifyIndependentOfContext("return 10 * b;");
6241   verifyIndependentOfContext("return *b * *c;");
6242   verifyIndependentOfContext("return a & ~b;");
6243   verifyIndependentOfContext("f(b ? *c : *d);");
6244   verifyIndependentOfContext("int a = b ? *c : *d;");
6245   verifyIndependentOfContext("*b = a;");
6246   verifyIndependentOfContext("a * ~b;");
6247   verifyIndependentOfContext("a * !b;");
6248   verifyIndependentOfContext("a * +b;");
6249   verifyIndependentOfContext("a * -b;");
6250   verifyIndependentOfContext("a * ++b;");
6251   verifyIndependentOfContext("a * --b;");
6252   verifyIndependentOfContext("a[4] * b;");
6253   verifyIndependentOfContext("a[a * a] = 1;");
6254   verifyIndependentOfContext("f() * b;");
6255   verifyIndependentOfContext("a * [self dostuff];");
6256   verifyIndependentOfContext("int x = a * (a + b);");
6257   verifyIndependentOfContext("(a *)(a + b);");
6258   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6259   verifyIndependentOfContext("int *pa = (int *)&a;");
6260   verifyIndependentOfContext("return sizeof(int **);");
6261   verifyIndependentOfContext("return sizeof(int ******);");
6262   verifyIndependentOfContext("return (int **&)a;");
6263   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6264   verifyFormat("void f(Type (*parameter)[10]) {}");
6265   verifyFormat("void f(Type (&parameter)[10]) {}");
6266   verifyGoogleFormat("return sizeof(int**);");
6267   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6268   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6269   verifyFormat("auto a = [](int **&, int ***) {};");
6270   verifyFormat("auto PointerBinding = [](const char *S) {};");
6271   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6272   verifyFormat("[](const decltype(*a) &value) {}");
6273   verifyFormat("decltype(a * b) F();");
6274   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6275   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6276   verifyIndependentOfContext("typedef void (*f)(int *a);");
6277   verifyIndependentOfContext("int i{a * b};");
6278   verifyIndependentOfContext("aaa && aaa->f();");
6279   verifyIndependentOfContext("int x = ~*p;");
6280   verifyFormat("Constructor() : a(a), area(width * height) {}");
6281   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6282   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6283   verifyFormat("void f() { f(a, c * d); }");
6284   verifyFormat("void f() { f(new a(), c * d); }");
6285   verifyFormat("void f(const MyOverride &override);");
6286   verifyFormat("void f(const MyFinal &final);");
6287   verifyIndependentOfContext("bool a = f() && override.f();");
6288   verifyIndependentOfContext("bool a = f() && final.f();");
6289 
6290   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6291 
6292   verifyIndependentOfContext("A<int *> a;");
6293   verifyIndependentOfContext("A<int **> a;");
6294   verifyIndependentOfContext("A<int *, int *> a;");
6295   verifyIndependentOfContext("A<int *[]> a;");
6296   verifyIndependentOfContext(
6297       "const char *const p = reinterpret_cast<const char *const>(q);");
6298   verifyIndependentOfContext("A<int **, int **> a;");
6299   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6300   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6301   verifyFormat("for (; a && b;) {\n}");
6302   verifyFormat("bool foo = true && [] { return false; }();");
6303 
6304   verifyFormat(
6305       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6306       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6307 
6308   verifyGoogleFormat("int const* a = &b;");
6309   verifyGoogleFormat("**outparam = 1;");
6310   verifyGoogleFormat("*outparam = a * b;");
6311   verifyGoogleFormat("int main(int argc, char** argv) {}");
6312   verifyGoogleFormat("A<int*> a;");
6313   verifyGoogleFormat("A<int**> a;");
6314   verifyGoogleFormat("A<int*, int*> a;");
6315   verifyGoogleFormat("A<int**, int**> a;");
6316   verifyGoogleFormat("f(b ? *c : *d);");
6317   verifyGoogleFormat("int a = b ? *c : *d;");
6318   verifyGoogleFormat("Type* t = **x;");
6319   verifyGoogleFormat("Type* t = *++*x;");
6320   verifyGoogleFormat("*++*x;");
6321   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6322   verifyGoogleFormat("Type* t = x++ * y;");
6323   verifyGoogleFormat(
6324       "const char* const p = reinterpret_cast<const char* const>(q);");
6325   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6326   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6327   verifyGoogleFormat("template <typename T>\n"
6328                      "void f(int i = 0, SomeType** temps = NULL);");
6329 
6330   FormatStyle Left = getLLVMStyle();
6331   Left.PointerAlignment = FormatStyle::PAS_Left;
6332   verifyFormat("x = *a(x) = *a(y);", Left);
6333   verifyFormat("for (;; *a = b) {\n}", Left);
6334   verifyFormat("return *this += 1;", Left);
6335   verifyFormat("throw *x;", Left);
6336   verifyFormat("delete *x;", Left);
6337   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6338   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6339   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6340 
6341   verifyIndependentOfContext("a = *(x + y);");
6342   verifyIndependentOfContext("a = &(x + y);");
6343   verifyIndependentOfContext("*(x + y).call();");
6344   verifyIndependentOfContext("&(x + y)->call();");
6345   verifyFormat("void f() { &(*I).first; }");
6346 
6347   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6348   verifyFormat(
6349       "int *MyValues = {\n"
6350       "    *A, // Operator detection might be confused by the '{'\n"
6351       "    *BB // Operator detection might be confused by previous comment\n"
6352       "};");
6353 
6354   verifyIndependentOfContext("if (int *a = &b)");
6355   verifyIndependentOfContext("if (int &a = *b)");
6356   verifyIndependentOfContext("if (a & b[i])");
6357   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6358   verifyIndependentOfContext("if (*b[i])");
6359   verifyIndependentOfContext("if (int *a = (&b))");
6360   verifyIndependentOfContext("while (int *a = &b)");
6361   verifyIndependentOfContext("size = sizeof *a;");
6362   verifyIndependentOfContext("if (a && (b = c))");
6363   verifyFormat("void f() {\n"
6364                "  for (const int &v : Values) {\n"
6365                "  }\n"
6366                "}");
6367   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6368   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6369   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6370 
6371   verifyFormat("#define A (!a * b)");
6372   verifyFormat("#define MACRO     \\\n"
6373                "  int *i = a * b; \\\n"
6374                "  void f(a *b);",
6375                getLLVMStyleWithColumns(19));
6376 
6377   verifyIndependentOfContext("A = new SomeType *[Length];");
6378   verifyIndependentOfContext("A = new SomeType *[Length]();");
6379   verifyIndependentOfContext("T **t = new T *;");
6380   verifyIndependentOfContext("T **t = new T *();");
6381   verifyGoogleFormat("A = new SomeType*[Length]();");
6382   verifyGoogleFormat("A = new SomeType*[Length];");
6383   verifyGoogleFormat("T** t = new T*;");
6384   verifyGoogleFormat("T** t = new T*();");
6385 
6386   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6387   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6388   verifyFormat("template <bool a, bool b> "
6389                "typename t::if<x && y>::type f() {}");
6390   verifyFormat("template <int *y> f() {}");
6391   verifyFormat("vector<int *> v;");
6392   verifyFormat("vector<int *const> v;");
6393   verifyFormat("vector<int *const **const *> v;");
6394   verifyFormat("vector<int *volatile> v;");
6395   verifyFormat("vector<a * b> v;");
6396   verifyFormat("foo<b && false>();");
6397   verifyFormat("foo<b & 1>();");
6398   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6399   verifyFormat(
6400       "template <class T, class = typename std::enable_if<\n"
6401       "                       std::is_integral<T>::value &&\n"
6402       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6403       "void F();",
6404       getLLVMStyleWithColumns(70));
6405   verifyFormat(
6406       "template <class T,\n"
6407       "          class = typename std::enable_if<\n"
6408       "              std::is_integral<T>::value &&\n"
6409       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6410       "          class U>\n"
6411       "void F();",
6412       getLLVMStyleWithColumns(70));
6413   verifyFormat(
6414       "template <class T,\n"
6415       "          class = typename ::std::enable_if<\n"
6416       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6417       "void F();",
6418       getGoogleStyleWithColumns(68));
6419 
6420   verifyIndependentOfContext("MACRO(int *i);");
6421   verifyIndependentOfContext("MACRO(auto *a);");
6422   verifyIndependentOfContext("MACRO(const A *a);");
6423   verifyIndependentOfContext("MACRO(A *const a);");
6424   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6425   verifyFormat("void f() { f(float{1}, a * a); }");
6426   // FIXME: Is there a way to make this work?
6427   // verifyIndependentOfContext("MACRO(A *a);");
6428 
6429   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6430   verifyFormat("return options != nullptr && operator==(*options);");
6431 
6432   EXPECT_EQ("#define OP(x)                                    \\\n"
6433             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6434             "    return s << a.DebugString();                 \\\n"
6435             "  }",
6436             format("#define OP(x) \\\n"
6437                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6438                    "    return s << a.DebugString(); \\\n"
6439                    "  }",
6440                    getLLVMStyleWithColumns(50)));
6441 
6442   // FIXME: We cannot handle this case yet; we might be able to figure out that
6443   // foo<x> d > v; doesn't make sense.
6444   verifyFormat("foo<a<b && c> d> v;");
6445 
6446   FormatStyle PointerMiddle = getLLVMStyle();
6447   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6448   verifyFormat("delete *x;", PointerMiddle);
6449   verifyFormat("int * x;", PointerMiddle);
6450   verifyFormat("int *[] x;", PointerMiddle);
6451   verifyFormat("template <int * y> f() {}", PointerMiddle);
6452   verifyFormat("int * f(int * a) {}", PointerMiddle);
6453   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6454   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6455   verifyFormat("A<int *> a;", PointerMiddle);
6456   verifyFormat("A<int **> a;", PointerMiddle);
6457   verifyFormat("A<int *, int *> a;", PointerMiddle);
6458   verifyFormat("A<int *[]> a;", PointerMiddle);
6459   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6460   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6461   verifyFormat("T ** t = new T *;", PointerMiddle);
6462 
6463   // Member function reference qualifiers aren't binary operators.
6464   verifyFormat("string // break\n"
6465                "operator()() & {}");
6466   verifyFormat("string // break\n"
6467                "operator()() && {}");
6468   verifyGoogleFormat("template <typename T>\n"
6469                      "auto x() & -> int {}");
6470 }
6471 
6472 TEST_F(FormatTest, UnderstandsAttributes) {
6473   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6474   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6475                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6476   FormatStyle AfterType = getLLVMStyle();
6477   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6478   verifyFormat("__attribute__((nodebug)) void\n"
6479                "foo() {}\n",
6480                AfterType);
6481 }
6482 
6483 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6484   verifyFormat("SomeType s [[unused]] (InitValue);");
6485   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6486   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6487   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6488   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6489   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6490                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6491 
6492   // Make sure we do not mistake attributes for array subscripts.
6493   verifyFormat("int a() {}\n"
6494                "[[unused]] int b() {}\n");
6495   verifyFormat("NSArray *arr;\n"
6496                "arr[[Foo() bar]];");
6497 
6498   // On the other hand, we still need to correctly find array subscripts.
6499   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6500 
6501   // Make sure we do not parse attributes as lambda introducers.
6502   FormatStyle MultiLineFunctions = getLLVMStyle();
6503   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6504   verifyFormat("[[unused]] int b() {\n"
6505                "  return 42;\n"
6506                "}\n",
6507                MultiLineFunctions);
6508 }
6509 
6510 TEST_F(FormatTest, UnderstandsEllipsis) {
6511   verifyFormat("int printf(const char *fmt, ...);");
6512   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6513   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6514 
6515   FormatStyle PointersLeft = getLLVMStyle();
6516   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6517   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6518 }
6519 
6520 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6521   EXPECT_EQ("int *a;\n"
6522             "int *a;\n"
6523             "int *a;",
6524             format("int *a;\n"
6525                    "int* a;\n"
6526                    "int *a;",
6527                    getGoogleStyle()));
6528   EXPECT_EQ("int* a;\n"
6529             "int* a;\n"
6530             "int* a;",
6531             format("int* a;\n"
6532                    "int* a;\n"
6533                    "int *a;",
6534                    getGoogleStyle()));
6535   EXPECT_EQ("int *a;\n"
6536             "int *a;\n"
6537             "int *a;",
6538             format("int *a;\n"
6539                    "int * a;\n"
6540                    "int *  a;",
6541                    getGoogleStyle()));
6542   EXPECT_EQ("auto x = [] {\n"
6543             "  int *a;\n"
6544             "  int *a;\n"
6545             "  int *a;\n"
6546             "};",
6547             format("auto x=[]{int *a;\n"
6548                    "int * a;\n"
6549                    "int *  a;};",
6550                    getGoogleStyle()));
6551 }
6552 
6553 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6554   verifyFormat("int f(int &&a) {}");
6555   verifyFormat("int f(int a, char &&b) {}");
6556   verifyFormat("void f() { int &&a = b; }");
6557   verifyGoogleFormat("int f(int a, char&& b) {}");
6558   verifyGoogleFormat("void f() { int&& a = b; }");
6559 
6560   verifyIndependentOfContext("A<int &&> a;");
6561   verifyIndependentOfContext("A<int &&, int &&> a;");
6562   verifyGoogleFormat("A<int&&> a;");
6563   verifyGoogleFormat("A<int&&, int&&> a;");
6564 
6565   // Not rvalue references:
6566   verifyFormat("template <bool B, bool C> class A {\n"
6567                "  static_assert(B && C, \"Something is wrong\");\n"
6568                "};");
6569   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6570   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6571   verifyFormat("#define A(a, b) (a && b)");
6572 }
6573 
6574 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6575   verifyFormat("void f() {\n"
6576                "  x[aaaaaaaaa -\n"
6577                "    b] = 23;\n"
6578                "}",
6579                getLLVMStyleWithColumns(15));
6580 }
6581 
6582 TEST_F(FormatTest, FormatsCasts) {
6583   verifyFormat("Type *A = static_cast<Type *>(P);");
6584   verifyFormat("Type *A = (Type *)P;");
6585   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6586   verifyFormat("int a = (int)(2.0f);");
6587   verifyFormat("int a = (int)2.0f;");
6588   verifyFormat("x[(int32)y];");
6589   verifyFormat("x = (int32)y;");
6590   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6591   verifyFormat("int a = (int)*b;");
6592   verifyFormat("int a = (int)2.0f;");
6593   verifyFormat("int a = (int)~0;");
6594   verifyFormat("int a = (int)++a;");
6595   verifyFormat("int a = (int)sizeof(int);");
6596   verifyFormat("int a = (int)+2;");
6597   verifyFormat("my_int a = (my_int)2.0f;");
6598   verifyFormat("my_int a = (my_int)sizeof(int);");
6599   verifyFormat("return (my_int)aaa;");
6600   verifyFormat("#define x ((int)-1)");
6601   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6602   verifyFormat("#define p(q) ((int *)&q)");
6603   verifyFormat("fn(a)(b) + 1;");
6604 
6605   verifyFormat("void f() { my_int a = (my_int)*b; }");
6606   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6607   verifyFormat("my_int a = (my_int)~0;");
6608   verifyFormat("my_int a = (my_int)++a;");
6609   verifyFormat("my_int a = (my_int)-2;");
6610   verifyFormat("my_int a = (my_int)1;");
6611   verifyFormat("my_int a = (my_int *)1;");
6612   verifyFormat("my_int a = (const my_int)-1;");
6613   verifyFormat("my_int a = (const my_int *)-1;");
6614   verifyFormat("my_int a = (my_int)(my_int)-1;");
6615   verifyFormat("my_int a = (ns::my_int)-2;");
6616   verifyFormat("case (my_int)ONE:");
6617   verifyFormat("auto x = (X)this;");
6618 
6619   // FIXME: single value wrapped with paren will be treated as cast.
6620   verifyFormat("void f(int i = (kValue)*kMask) {}");
6621 
6622   verifyFormat("{ (void)F; }");
6623 
6624   // Don't break after a cast's
6625   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6626                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6627                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6628 
6629   // These are not casts.
6630   verifyFormat("void f(int *) {}");
6631   verifyFormat("f(foo)->b;");
6632   verifyFormat("f(foo).b;");
6633   verifyFormat("f(foo)(b);");
6634   verifyFormat("f(foo)[b];");
6635   verifyFormat("[](foo) { return 4; }(bar);");
6636   verifyFormat("(*funptr)(foo)[4];");
6637   verifyFormat("funptrs[4](foo)[4];");
6638   verifyFormat("void f(int *);");
6639   verifyFormat("void f(int *) = 0;");
6640   verifyFormat("void f(SmallVector<int>) {}");
6641   verifyFormat("void f(SmallVector<int>);");
6642   verifyFormat("void f(SmallVector<int>) = 0;");
6643   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6644   verifyFormat("int a = sizeof(int) * b;");
6645   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6646   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6647   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6648   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6649 
6650   // These are not casts, but at some point were confused with casts.
6651   verifyFormat("virtual void foo(int *) override;");
6652   verifyFormat("virtual void foo(char &) const;");
6653   verifyFormat("virtual void foo(int *a, char *) const;");
6654   verifyFormat("int a = sizeof(int *) + b;");
6655   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6656   verifyFormat("bool b = f(g<int>) && c;");
6657   verifyFormat("typedef void (*f)(int i) func;");
6658 
6659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6660                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6661   // FIXME: The indentation here is not ideal.
6662   verifyFormat(
6663       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6664       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6665       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6666 }
6667 
6668 TEST_F(FormatTest, FormatsFunctionTypes) {
6669   verifyFormat("A<bool()> a;");
6670   verifyFormat("A<SomeType()> a;");
6671   verifyFormat("A<void (*)(int, std::string)> a;");
6672   verifyFormat("A<void *(int)>;");
6673   verifyFormat("void *(*a)(int *, SomeType *);");
6674   verifyFormat("int (*func)(void *);");
6675   verifyFormat("void f() { int (*func)(void *); }");
6676   verifyFormat("template <class CallbackClass>\n"
6677                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6678 
6679   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6680   verifyGoogleFormat("void* (*a)(int);");
6681   verifyGoogleFormat(
6682       "template <class CallbackClass>\n"
6683       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6684 
6685   // Other constructs can look somewhat like function types:
6686   verifyFormat("A<sizeof(*x)> a;");
6687   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6688   verifyFormat("some_var = function(*some_pointer_var)[0];");
6689   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6690   verifyFormat("int x = f(&h)();");
6691   verifyFormat("returnsFunction(&param1, &param2)(param);");
6692   verifyFormat("std::function<\n"
6693                "    LooooooooooongTemplatedType<\n"
6694                "        SomeType>*(\n"
6695                "        LooooooooooooooooongType type)>\n"
6696                "    function;",
6697                getGoogleStyleWithColumns(40));
6698 }
6699 
6700 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6701   verifyFormat("A (*foo_)[6];");
6702   verifyFormat("vector<int> (*foo_)[6];");
6703 }
6704 
6705 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6706   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6707                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6708   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6709                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6710   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6711                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6712 
6713   // Different ways of ()-initializiation.
6714   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6715                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6716   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6717                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6718   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6719                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6720   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6721                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6722 
6723   // Lambdas should not confuse the variable declaration heuristic.
6724   verifyFormat("LooooooooooooooooongType\n"
6725                "    variable(nullptr, [](A *a) {});",
6726                getLLVMStyleWithColumns(40));
6727 }
6728 
6729 TEST_F(FormatTest, BreaksLongDeclarations) {
6730   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6731                "    AnotherNameForTheLongType;");
6732   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6733                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6734   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6735                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6736   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6737                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6738   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6739                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6740   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6741                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6742   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6743                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6744   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6745                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6746   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6747                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6748   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6749                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6750   FormatStyle Indented = getLLVMStyle();
6751   Indented.IndentWrappedFunctionNames = true;
6752   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6753                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6754                Indented);
6755   verifyFormat(
6756       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6757       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6758       Indented);
6759   verifyFormat(
6760       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6761       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6762       Indented);
6763   verifyFormat(
6764       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6765       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6766       Indented);
6767 
6768   // FIXME: Without the comment, this breaks after "(".
6769   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6770                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6771                getGoogleStyle());
6772 
6773   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6774                "                  int LoooooooooooooooooooongParam2) {}");
6775   verifyFormat(
6776       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6777       "                                   SourceLocation L, IdentifierIn *II,\n"
6778       "                                   Type *T) {}");
6779   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6780                "ReallyReaaallyLongFunctionName(\n"
6781                "    const std::string &SomeParameter,\n"
6782                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6783                "        &ReallyReallyLongParameterName,\n"
6784                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6785                "        &AnotherLongParameterName) {}");
6786   verifyFormat("template <typename A>\n"
6787                "SomeLoooooooooooooooooooooongType<\n"
6788                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6789                "Function() {}");
6790 
6791   verifyGoogleFormat(
6792       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6793       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6794   verifyGoogleFormat(
6795       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6796       "                                   SourceLocation L) {}");
6797   verifyGoogleFormat(
6798       "some_namespace::LongReturnType\n"
6799       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6800       "    int first_long_parameter, int second_parameter) {}");
6801 
6802   verifyGoogleFormat("template <typename T>\n"
6803                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6804                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6805   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6806                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6807 
6808   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6809                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6810                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6811   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6812                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6813                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6814   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6815                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6816                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6817                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6818 
6819   verifyFormat("template <typename T> // Templates on own line.\n"
6820                "static int            // Some comment.\n"
6821                "MyFunction(int a);",
6822                getLLVMStyle());
6823 }
6824 
6825 TEST_F(FormatTest, FormatsArrays) {
6826   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6827                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6828   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6829                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6830   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6831                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6832   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6833                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6834   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6835                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6836   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6837                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6838                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6839   verifyFormat(
6840       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6841       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6842       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6843   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6844                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6845 
6846   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6847                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6848   verifyFormat(
6849       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6850       "                                  .aaaaaaa[0]\n"
6851       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6852   verifyFormat("a[::b::c];");
6853 
6854   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6855 
6856   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6857   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6858 }
6859 
6860 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6861   verifyFormat("(a)->b();");
6862   verifyFormat("--a;");
6863 }
6864 
6865 TEST_F(FormatTest, HandlesIncludeDirectives) {
6866   verifyFormat("#include <string>\n"
6867                "#include <a/b/c.h>\n"
6868                "#include \"a/b/string\"\n"
6869                "#include \"string.h\"\n"
6870                "#include \"string.h\"\n"
6871                "#include <a-a>\n"
6872                "#include < path with space >\n"
6873                "#include_next <test.h>"
6874                "#include \"abc.h\" // this is included for ABC\n"
6875                "#include \"some long include\" // with a comment\n"
6876                "#include \"some very long include path\"\n"
6877                "#include <some/very/long/include/path>\n",
6878                getLLVMStyleWithColumns(35));
6879   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6880   EXPECT_EQ("#include <a>", format("#include<a>"));
6881 
6882   verifyFormat("#import <string>");
6883   verifyFormat("#import <a/b/c.h>");
6884   verifyFormat("#import \"a/b/string\"");
6885   verifyFormat("#import \"string.h\"");
6886   verifyFormat("#import \"string.h\"");
6887   verifyFormat("#if __has_include(<strstream>)\n"
6888                "#include <strstream>\n"
6889                "#endif");
6890 
6891   verifyFormat("#define MY_IMPORT <a/b>");
6892 
6893   verifyFormat("#if __has_include(<a/b>)");
6894   verifyFormat("#if __has_include_next(<a/b>)");
6895   verifyFormat("#define F __has_include(<a/b>)");
6896   verifyFormat("#define F __has_include_next(<a/b>)");
6897 
6898   // Protocol buffer definition or missing "#".
6899   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6900                getLLVMStyleWithColumns(30));
6901 
6902   FormatStyle Style = getLLVMStyle();
6903   Style.AlwaysBreakBeforeMultilineStrings = true;
6904   Style.ColumnLimit = 0;
6905   verifyFormat("#import \"abc.h\"", Style);
6906 
6907   // But 'import' might also be a regular C++ namespace.
6908   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6909                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6910 }
6911 
6912 //===----------------------------------------------------------------------===//
6913 // Error recovery tests.
6914 //===----------------------------------------------------------------------===//
6915 
6916 TEST_F(FormatTest, IncompleteParameterLists) {
6917   FormatStyle NoBinPacking = getLLVMStyle();
6918   NoBinPacking.BinPackParameters = false;
6919   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6920                "                        double *min_x,\n"
6921                "                        double *max_x,\n"
6922                "                        double *min_y,\n"
6923                "                        double *max_y,\n"
6924                "                        double *min_z,\n"
6925                "                        double *max_z, ) {}",
6926                NoBinPacking);
6927 }
6928 
6929 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6930   verifyFormat("void f() { return; }\n42");
6931   verifyFormat("void f() {\n"
6932                "  if (0)\n"
6933                "    return;\n"
6934                "}\n"
6935                "42");
6936   verifyFormat("void f() { return }\n42");
6937   verifyFormat("void f() {\n"
6938                "  if (0)\n"
6939                "    return\n"
6940                "}\n"
6941                "42");
6942 }
6943 
6944 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6945   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6946   EXPECT_EQ("void f() {\n"
6947             "  if (a)\n"
6948             "    return\n"
6949             "}",
6950             format("void  f  (  )  {  if  ( a )  return  }"));
6951   EXPECT_EQ("namespace N {\n"
6952             "void f()\n"
6953             "}",
6954             format("namespace  N  {  void f()  }"));
6955   EXPECT_EQ("namespace N {\n"
6956             "void f() {}\n"
6957             "void g()\n"
6958             "} // namespace N",
6959             format("namespace N  { void f( ) { } void g( ) }"));
6960 }
6961 
6962 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6963   verifyFormat("int aaaaaaaa =\n"
6964                "    // Overlylongcomment\n"
6965                "    b;",
6966                getLLVMStyleWithColumns(20));
6967   verifyFormat("function(\n"
6968                "    ShortArgument,\n"
6969                "    LoooooooooooongArgument);\n",
6970                getLLVMStyleWithColumns(20));
6971 }
6972 
6973 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6974   verifyFormat("public:");
6975   verifyFormat("class A {\n"
6976                "public\n"
6977                "  void f() {}\n"
6978                "};");
6979   verifyFormat("public\n"
6980                "int qwerty;");
6981   verifyFormat("public\n"
6982                "B {}");
6983   verifyFormat("public\n"
6984                "{}");
6985   verifyFormat("public\n"
6986                "B { int x; }");
6987 }
6988 
6989 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6990   verifyFormat("{");
6991   verifyFormat("#})");
6992   verifyNoCrash("(/**/[:!] ?[).");
6993 }
6994 
6995 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
6996   // Found by oss-fuzz:
6997   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
6998   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6999   Style.ColumnLimit = 60;
7000   verifyNoCrash(
7001       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
7002       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
7003       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
7004       Style);
7005 }
7006 
7007 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
7008   verifyFormat("do {\n}");
7009   verifyFormat("do {\n}\n"
7010                "f();");
7011   verifyFormat("do {\n}\n"
7012                "wheeee(fun);");
7013   verifyFormat("do {\n"
7014                "  f();\n"
7015                "}");
7016 }
7017 
7018 TEST_F(FormatTest, IncorrectCodeMissingParens) {
7019   verifyFormat("if {\n  foo;\n  foo();\n}");
7020   verifyFormat("switch {\n  foo;\n  foo();\n}");
7021   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
7022   verifyFormat("while {\n  foo;\n  foo();\n}");
7023   verifyFormat("do {\n  foo;\n  foo();\n} while;");
7024 }
7025 
7026 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
7027   verifyIncompleteFormat("namespace {\n"
7028                          "class Foo { Foo (\n"
7029                          "};\n"
7030                          "} // namespace");
7031 }
7032 
7033 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
7034   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
7035   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
7036   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
7037   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
7038 
7039   EXPECT_EQ("{\n"
7040             "  {\n"
7041             "    breakme(\n"
7042             "        qwe);\n"
7043             "  }\n",
7044             format("{\n"
7045                    "    {\n"
7046                    " breakme(qwe);\n"
7047                    "}\n",
7048                    getLLVMStyleWithColumns(10)));
7049 }
7050 
7051 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
7052   verifyFormat("int x = {\n"
7053                "    avariable,\n"
7054                "    b(alongervariable)};",
7055                getLLVMStyleWithColumns(25));
7056 }
7057 
7058 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
7059   verifyFormat("return (a)(b){1, 2, 3};");
7060 }
7061 
7062 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
7063   verifyFormat("vector<int> x{1, 2, 3, 4};");
7064   verifyFormat("vector<int> x{\n"
7065                "    1,\n"
7066                "    2,\n"
7067                "    3,\n"
7068                "    4,\n"
7069                "};");
7070   verifyFormat("vector<T> x{{}, {}, {}, {}};");
7071   verifyFormat("f({1, 2});");
7072   verifyFormat("auto v = Foo{-1};");
7073   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
7074   verifyFormat("Class::Class : member{1, 2, 3} {}");
7075   verifyFormat("new vector<int>{1, 2, 3};");
7076   verifyFormat("new int[3]{1, 2, 3};");
7077   verifyFormat("new int{1};");
7078   verifyFormat("return {arg1, arg2};");
7079   verifyFormat("return {arg1, SomeType{parameter}};");
7080   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
7081   verifyFormat("new T{arg1, arg2};");
7082   verifyFormat("f(MyMap[{composite, key}]);");
7083   verifyFormat("class Class {\n"
7084                "  T member = {arg1, arg2};\n"
7085                "};");
7086   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
7087   verifyFormat("const struct A a = {.a = 1, .b = 2};");
7088   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
7089   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
7090   verifyFormat("int a = std::is_integral<int>{} + 0;");
7091 
7092   verifyFormat("int foo(int i) { return fo1{}(i); }");
7093   verifyFormat("int foo(int i) { return fo1{}(i); }");
7094   verifyFormat("auto i = decltype(x){};");
7095   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
7096   verifyFormat("Node n{1, Node{1000}, //\n"
7097                "       2};");
7098   verifyFormat("Aaaa aaaaaaa{\n"
7099                "    {\n"
7100                "        aaaa,\n"
7101                "    },\n"
7102                "};");
7103   verifyFormat("class C : public D {\n"
7104                "  SomeClass SC{2};\n"
7105                "};");
7106   verifyFormat("class C : public A {\n"
7107                "  class D : public B {\n"
7108                "    void f() { int i{2}; }\n"
7109                "  };\n"
7110                "};");
7111   verifyFormat("#define A {a, a},");
7112 
7113   // Avoid breaking between equal sign and opening brace
7114   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
7115   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
7116   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
7117                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
7118                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
7119                "     {\"ccccccccccccccccccccc\", 2}};",
7120                AvoidBreakingFirstArgument);
7121 
7122   // Binpacking only if there is no trailing comma
7123   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
7124                "                      cccccccccc, dddddddddd};",
7125 			   getLLVMStyleWithColumns(50));
7126   verifyFormat("const Aaaaaa aaaaa = {\n"
7127                "    aaaaaaaaaaa,\n"
7128                "    bbbbbbbbbbb,\n"
7129                "    ccccccccccc,\n"
7130                "    ddddddddddd,\n"
7131                "};", getLLVMStyleWithColumns(50));
7132 
7133   // Cases where distinguising braced lists and blocks is hard.
7134   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
7135   verifyFormat("void f() {\n"
7136                "  return; // comment\n"
7137                "}\n"
7138                "SomeType t;");
7139   verifyFormat("void f() {\n"
7140                "  if (a) {\n"
7141                "    f();\n"
7142                "  }\n"
7143                "}\n"
7144                "SomeType t;");
7145 
7146   // In combination with BinPackArguments = false.
7147   FormatStyle NoBinPacking = getLLVMStyle();
7148   NoBinPacking.BinPackArguments = false;
7149   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
7150                "                      bbbbb,\n"
7151                "                      ccccc,\n"
7152                "                      ddddd,\n"
7153                "                      eeeee,\n"
7154                "                      ffffff,\n"
7155                "                      ggggg,\n"
7156                "                      hhhhhh,\n"
7157                "                      iiiiii,\n"
7158                "                      jjjjjj,\n"
7159                "                      kkkkkk};",
7160                NoBinPacking);
7161   verifyFormat("const Aaaaaa aaaaa = {\n"
7162                "    aaaaa,\n"
7163                "    bbbbb,\n"
7164                "    ccccc,\n"
7165                "    ddddd,\n"
7166                "    eeeee,\n"
7167                "    ffffff,\n"
7168                "    ggggg,\n"
7169                "    hhhhhh,\n"
7170                "    iiiiii,\n"
7171                "    jjjjjj,\n"
7172                "    kkkkkk,\n"
7173                "};",
7174                NoBinPacking);
7175   verifyFormat(
7176       "const Aaaaaa aaaaa = {\n"
7177       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
7178       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
7179       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
7180       "};",
7181       NoBinPacking);
7182 
7183   // FIXME: The alignment of these trailing comments might be bad. Then again,
7184   // this might be utterly useless in real code.
7185   verifyFormat("Constructor::Constructor()\n"
7186                "    : some_value{         //\n"
7187                "                 aaaaaaa, //\n"
7188                "                 bbbbbbb} {}");
7189 
7190   // In braced lists, the first comment is always assumed to belong to the
7191   // first element. Thus, it can be moved to the next or previous line as
7192   // appropriate.
7193   EXPECT_EQ("function({// First element:\n"
7194             "          1,\n"
7195             "          // Second element:\n"
7196             "          2});",
7197             format("function({\n"
7198                    "    // First element:\n"
7199                    "    1,\n"
7200                    "    // Second element:\n"
7201                    "    2});"));
7202   EXPECT_EQ("std::vector<int> MyNumbers{\n"
7203             "    // First element:\n"
7204             "    1,\n"
7205             "    // Second element:\n"
7206             "    2};",
7207             format("std::vector<int> MyNumbers{// First element:\n"
7208                    "                           1,\n"
7209                    "                           // Second element:\n"
7210                    "                           2};",
7211                    getLLVMStyleWithColumns(30)));
7212   // A trailing comma should still lead to an enforced line break and no
7213   // binpacking.
7214   EXPECT_EQ("vector<int> SomeVector = {\n"
7215             "    // aaa\n"
7216             "    1,\n"
7217             "    2,\n"
7218             "};",
7219             format("vector<int> SomeVector = { // aaa\n"
7220                    "    1, 2, };"));
7221 
7222   FormatStyle ExtraSpaces = getLLVMStyle();
7223   ExtraSpaces.Cpp11BracedListStyle = false;
7224   ExtraSpaces.ColumnLimit = 75;
7225   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
7226   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
7227   verifyFormat("f({ 1, 2 });", ExtraSpaces);
7228   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
7229   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
7230   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
7231   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
7232   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
7233   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
7234   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
7235   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
7236   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
7237   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
7238   verifyFormat("class Class {\n"
7239                "  T member = { arg1, arg2 };\n"
7240                "};",
7241                ExtraSpaces);
7242   verifyFormat(
7243       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7244       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7245       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7246       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7247       ExtraSpaces);
7248   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7249   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7250                ExtraSpaces);
7251   verifyFormat(
7252       "someFunction(OtherParam,\n"
7253       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7254       "                         param1, param2,\n"
7255       "                         // comment 2\n"
7256       "                         param3, param4 });",
7257       ExtraSpaces);
7258   verifyFormat(
7259       "std::this_thread::sleep_for(\n"
7260       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7261       ExtraSpaces);
7262   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7263                "    aaaaaaa,\n"
7264                "    aaaaaaaaaa,\n"
7265                "    aaaaa,\n"
7266                "    aaaaaaaaaaaaaaa,\n"
7267                "    aaa,\n"
7268                "    aaaaaaaaaa,\n"
7269                "    a,\n"
7270                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7271                "    aaaaaaaaaaaa,\n"
7272                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7273                "    aaaaaaa,\n"
7274                "    a};");
7275   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7276   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7277   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7278 
7279   // Avoid breaking between initializer/equal sign and opening brace
7280   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7281   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7282                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7283                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7284                "  { \"ccccccccccccccccccccc\", 2 }\n"
7285                "};",
7286                ExtraSpaces);
7287   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7288                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7289                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7290                "  { \"ccccccccccccccccccccc\", 2 }\n"
7291                "};",
7292                ExtraSpaces);
7293 
7294   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7295   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7296   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7297   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7298 }
7299 
7300 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7301   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7302                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7303                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7304                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7305                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7306                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7307   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7308                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7309                "                 1, 22, 333, 4444, 55555, //\n"
7310                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7311                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7312   verifyFormat(
7313       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7314       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7315       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7316       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7317       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7318       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7319       "                 7777777};");
7320   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7321                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7322                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7323   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7324                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7325                "    // Separating comment.\n"
7326                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7327   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7328                "    // Leading comment\n"
7329                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7330                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7331   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7332                "                 1, 1, 1, 1};",
7333                getLLVMStyleWithColumns(39));
7334   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7335                "                 1, 1, 1, 1};",
7336                getLLVMStyleWithColumns(38));
7337   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7338                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7339                getLLVMStyleWithColumns(43));
7340   verifyFormat(
7341       "static unsigned SomeValues[10][3] = {\n"
7342       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7343       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7344   verifyFormat("static auto fields = new vector<string>{\n"
7345                "    \"aaaaaaaaaaaaa\",\n"
7346                "    \"aaaaaaaaaaaaa\",\n"
7347                "    \"aaaaaaaaaaaa\",\n"
7348                "    \"aaaaaaaaaaaaaa\",\n"
7349                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7350                "    \"aaaaaaaaaaaa\",\n"
7351                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7352                "};");
7353   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7354   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7355                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7356                "                 3, cccccccccccccccccccccc};",
7357                getLLVMStyleWithColumns(60));
7358 
7359   // Trailing commas.
7360   verifyFormat("vector<int> x = {\n"
7361                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7362                "};",
7363                getLLVMStyleWithColumns(39));
7364   verifyFormat("vector<int> x = {\n"
7365                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7366                "};",
7367                getLLVMStyleWithColumns(39));
7368   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7369                "                 1, 1, 1, 1,\n"
7370                "                 /**/ /**/};",
7371                getLLVMStyleWithColumns(39));
7372 
7373   // Trailing comment in the first line.
7374   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7375                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7376                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7377                "    11111111,   22222222,   333333333,   44444444};");
7378   // Trailing comment in the last line.
7379   verifyFormat("int aaaaa[] = {\n"
7380                "    1, 2, 3, // comment\n"
7381                "    4, 5, 6  // comment\n"
7382                "};");
7383 
7384   // With nested lists, we should either format one item per line or all nested
7385   // lists one on line.
7386   // FIXME: For some nested lists, we can do better.
7387   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7388                "        {aaaaaaaaaaaaaaaaaaa},\n"
7389                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7390                "        {aaaaaaaaaaaaaaaaa}};",
7391                getLLVMStyleWithColumns(60));
7392   verifyFormat(
7393       "SomeStruct my_struct_array = {\n"
7394       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7395       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7396       "    {aaa, aaa},\n"
7397       "    {aaa, aaa},\n"
7398       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7399       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7400       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7401 
7402   // No column layout should be used here.
7403   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7404                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7405 
7406   verifyNoCrash("a<,");
7407 
7408   // No braced initializer here.
7409   verifyFormat("void f() {\n"
7410                "  struct Dummy {};\n"
7411                "  f(v);\n"
7412                "}");
7413 
7414   // Long lists should be formatted in columns even if they are nested.
7415   verifyFormat(
7416       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7417       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7418       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7419       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7420       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7421       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7422 
7423   // Allow "single-column" layout even if that violates the column limit. There
7424   // isn't going to be a better way.
7425   verifyFormat("std::vector<int> a = {\n"
7426                "    aaaaaaaa,\n"
7427                "    aaaaaaaa,\n"
7428                "    aaaaaaaa,\n"
7429                "    aaaaaaaa,\n"
7430                "    aaaaaaaaaa,\n"
7431                "    aaaaaaaa,\n"
7432                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7433                getLLVMStyleWithColumns(30));
7434   verifyFormat("vector<int> aaaa = {\n"
7435                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7436                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7437                "    aaaaaa.aaaaaaa,\n"
7438                "    aaaaaa.aaaaaaa,\n"
7439                "    aaaaaa.aaaaaaa,\n"
7440                "    aaaaaa.aaaaaaa,\n"
7441                "};");
7442 
7443   // Don't create hanging lists.
7444   verifyFormat("someFunction(Param, {List1, List2,\n"
7445                "                     List3});",
7446                getLLVMStyleWithColumns(35));
7447   verifyFormat("someFunction(Param, Param,\n"
7448                "             {List1, List2,\n"
7449                "              List3});",
7450                getLLVMStyleWithColumns(35));
7451   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7452                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7453 }
7454 
7455 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7456   FormatStyle DoNotMerge = getLLVMStyle();
7457   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7458 
7459   verifyFormat("void f() { return 42; }");
7460   verifyFormat("void f() {\n"
7461                "  return 42;\n"
7462                "}",
7463                DoNotMerge);
7464   verifyFormat("void f() {\n"
7465                "  // Comment\n"
7466                "}");
7467   verifyFormat("{\n"
7468                "#error {\n"
7469                "  int a;\n"
7470                "}");
7471   verifyFormat("{\n"
7472                "  int a;\n"
7473                "#error {\n"
7474                "}");
7475   verifyFormat("void f() {} // comment");
7476   verifyFormat("void f() { int a; } // comment");
7477   verifyFormat("void f() {\n"
7478                "} // comment",
7479                DoNotMerge);
7480   verifyFormat("void f() {\n"
7481                "  int a;\n"
7482                "} // comment",
7483                DoNotMerge);
7484   verifyFormat("void f() {\n"
7485                "} // comment",
7486                getLLVMStyleWithColumns(15));
7487 
7488   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7489   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7490 
7491   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7492   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7493   verifyFormat("class C {\n"
7494                "  C()\n"
7495                "      : iiiiiiii(nullptr),\n"
7496                "        kkkkkkk(nullptr),\n"
7497                "        mmmmmmm(nullptr),\n"
7498                "        nnnnnnn(nullptr) {}\n"
7499                "};",
7500                getGoogleStyle());
7501 
7502   FormatStyle NoColumnLimit = getLLVMStyle();
7503   NoColumnLimit.ColumnLimit = 0;
7504   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7505   EXPECT_EQ("class C {\n"
7506             "  A() : b(0) {}\n"
7507             "};",
7508             format("class C{A():b(0){}};", NoColumnLimit));
7509   EXPECT_EQ("A()\n"
7510             "    : b(0) {\n"
7511             "}",
7512             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7513 
7514   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7515   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7516       FormatStyle::SFS_None;
7517   EXPECT_EQ("A()\n"
7518             "    : b(0) {\n"
7519             "}",
7520             format("A():b(0){}", DoNotMergeNoColumnLimit));
7521   EXPECT_EQ("A()\n"
7522             "    : b(0) {\n"
7523             "}",
7524             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7525 
7526   verifyFormat("#define A          \\\n"
7527                "  void f() {       \\\n"
7528                "    int i;         \\\n"
7529                "  }",
7530                getLLVMStyleWithColumns(20));
7531   verifyFormat("#define A           \\\n"
7532                "  void f() { int i; }",
7533                getLLVMStyleWithColumns(21));
7534   verifyFormat("#define A            \\\n"
7535                "  void f() {         \\\n"
7536                "    int i;           \\\n"
7537                "  }                  \\\n"
7538                "  int j;",
7539                getLLVMStyleWithColumns(22));
7540   verifyFormat("#define A             \\\n"
7541                "  void f() { int i; } \\\n"
7542                "  int j;",
7543                getLLVMStyleWithColumns(23));
7544 }
7545 
7546 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7547   FormatStyle MergeEmptyOnly = getLLVMStyle();
7548   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7549   verifyFormat("class C {\n"
7550                "  int f() {}\n"
7551                "};",
7552                MergeEmptyOnly);
7553   verifyFormat("class C {\n"
7554                "  int f() {\n"
7555                "    return 42;\n"
7556                "  }\n"
7557                "};",
7558                MergeEmptyOnly);
7559   verifyFormat("int f() {}", MergeEmptyOnly);
7560   verifyFormat("int f() {\n"
7561                "  return 42;\n"
7562                "}",
7563                MergeEmptyOnly);
7564 
7565   // Also verify behavior when BraceWrapping.AfterFunction = true
7566   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7567   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7568   verifyFormat("int f() {}", MergeEmptyOnly);
7569   verifyFormat("class C {\n"
7570                "  int f() {}\n"
7571                "};",
7572                MergeEmptyOnly);
7573 }
7574 
7575 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7576   FormatStyle MergeInlineOnly = getLLVMStyle();
7577   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7578   verifyFormat("class C {\n"
7579                "  int f() { return 42; }\n"
7580                "};",
7581                MergeInlineOnly);
7582   verifyFormat("int f() {\n"
7583                "  return 42;\n"
7584                "}",
7585                MergeInlineOnly);
7586 
7587   // SFS_Inline implies SFS_Empty
7588   verifyFormat("class C {\n"
7589                "  int f() {}\n"
7590                "};",
7591                MergeInlineOnly);
7592   verifyFormat("int f() {}", MergeInlineOnly);
7593 
7594   // Also verify behavior when BraceWrapping.AfterFunction = true
7595   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7596   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7597   verifyFormat("class C {\n"
7598                "  int f() { return 42; }\n"
7599                "};",
7600                MergeInlineOnly);
7601   verifyFormat("int f()\n"
7602                "{\n"
7603                "  return 42;\n"
7604                "}",
7605                MergeInlineOnly);
7606 
7607   // SFS_Inline implies SFS_Empty
7608   verifyFormat("int f() {}", MergeInlineOnly);
7609   verifyFormat("class C {\n"
7610                "  int f() {}\n"
7611                "};",
7612                MergeInlineOnly);
7613 }
7614 
7615 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7616   FormatStyle MergeInlineOnly = getLLVMStyle();
7617   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7618       FormatStyle::SFS_InlineOnly;
7619   verifyFormat("class C {\n"
7620                "  int f() { return 42; }\n"
7621                "};",
7622                MergeInlineOnly);
7623   verifyFormat("int f() {\n"
7624                "  return 42;\n"
7625                "}",
7626                MergeInlineOnly);
7627 
7628   // SFS_InlineOnly does not imply SFS_Empty
7629   verifyFormat("class C {\n"
7630                "  int f() {}\n"
7631                "};",
7632                MergeInlineOnly);
7633   verifyFormat("int f() {\n"
7634                "}",
7635                MergeInlineOnly);
7636 
7637   // Also verify behavior when BraceWrapping.AfterFunction = true
7638   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7639   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7640   verifyFormat("class C {\n"
7641                "  int f() { return 42; }\n"
7642                "};",
7643                MergeInlineOnly);
7644   verifyFormat("int f()\n"
7645                "{\n"
7646                "  return 42;\n"
7647                "}",
7648                MergeInlineOnly);
7649 
7650   // SFS_InlineOnly does not imply SFS_Empty
7651   verifyFormat("int f()\n"
7652                "{\n"
7653                "}",
7654                MergeInlineOnly);
7655   verifyFormat("class C {\n"
7656                "  int f() {}\n"
7657                "};",
7658                MergeInlineOnly);
7659 }
7660 
7661 TEST_F(FormatTest, SplitEmptyFunction) {
7662   FormatStyle Style = getLLVMStyle();
7663   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7664   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7665   Style.BraceWrapping.AfterFunction = true;
7666   Style.BraceWrapping.SplitEmptyFunction = false;
7667   Style.ColumnLimit = 40;
7668 
7669   verifyFormat("int f()\n"
7670                "{}",
7671                Style);
7672   verifyFormat("int f()\n"
7673                "{\n"
7674                "  return 42;\n"
7675                "}",
7676                Style);
7677   verifyFormat("int f()\n"
7678                "{\n"
7679                "  // some comment\n"
7680                "}",
7681                Style);
7682 
7683   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7684   verifyFormat("int f() {}", Style);
7685   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7686                "{}",
7687                Style);
7688   verifyFormat("int f()\n"
7689                "{\n"
7690                "  return 0;\n"
7691                "}",
7692                Style);
7693 
7694   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7695   verifyFormat("class Foo {\n"
7696                "  int f() {}\n"
7697                "};\n",
7698                Style);
7699   verifyFormat("class Foo {\n"
7700                "  int f() { return 0; }\n"
7701                "};\n",
7702                Style);
7703   verifyFormat("class Foo {\n"
7704                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7705                "  {}\n"
7706                "};\n",
7707                Style);
7708   verifyFormat("class Foo {\n"
7709                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7710                "  {\n"
7711                "    return 0;\n"
7712                "  }\n"
7713                "};\n",
7714                Style);
7715 
7716   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7717   verifyFormat("int f() {}", Style);
7718   verifyFormat("int f() { return 0; }", Style);
7719   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7720                "{}",
7721                Style);
7722   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7723                "{\n"
7724                "  return 0;\n"
7725                "}",
7726                Style);
7727 }
7728 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7729   FormatStyle Style = getLLVMStyle();
7730   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7731   verifyFormat("#ifdef A\n"
7732                "int f() {}\n"
7733                "#else\n"
7734                "int g() {}\n"
7735                "#endif",
7736                Style);
7737 }
7738 
7739 TEST_F(FormatTest, SplitEmptyClass) {
7740   FormatStyle Style = getLLVMStyle();
7741   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7742   Style.BraceWrapping.AfterClass = true;
7743   Style.BraceWrapping.SplitEmptyRecord = false;
7744 
7745   verifyFormat("class Foo\n"
7746                "{};",
7747                Style);
7748   verifyFormat("/* something */ class Foo\n"
7749                "{};",
7750                Style);
7751   verifyFormat("template <typename X> class Foo\n"
7752                "{};",
7753                Style);
7754   verifyFormat("class Foo\n"
7755                "{\n"
7756                "  Foo();\n"
7757                "};",
7758                Style);
7759   verifyFormat("typedef class Foo\n"
7760                "{\n"
7761                "} Foo_t;",
7762                Style);
7763 }
7764 
7765 TEST_F(FormatTest, SplitEmptyStruct) {
7766   FormatStyle Style = getLLVMStyle();
7767   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7768   Style.BraceWrapping.AfterStruct = true;
7769   Style.BraceWrapping.SplitEmptyRecord = false;
7770 
7771   verifyFormat("struct Foo\n"
7772                "{};",
7773                Style);
7774   verifyFormat("/* something */ struct Foo\n"
7775                "{};",
7776                Style);
7777   verifyFormat("template <typename X> struct Foo\n"
7778                "{};",
7779                Style);
7780   verifyFormat("struct Foo\n"
7781                "{\n"
7782                "  Foo();\n"
7783                "};",
7784                Style);
7785   verifyFormat("typedef struct Foo\n"
7786                "{\n"
7787                "} Foo_t;",
7788                Style);
7789   //typedef struct Bar {} Bar_t;
7790 }
7791 
7792 TEST_F(FormatTest, SplitEmptyUnion) {
7793   FormatStyle Style = getLLVMStyle();
7794   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7795   Style.BraceWrapping.AfterUnion = true;
7796   Style.BraceWrapping.SplitEmptyRecord = false;
7797 
7798   verifyFormat("union Foo\n"
7799                "{};",
7800                Style);
7801   verifyFormat("/* something */ union Foo\n"
7802                "{};",
7803                Style);
7804   verifyFormat("union Foo\n"
7805                "{\n"
7806                "  A,\n"
7807                "};",
7808                Style);
7809   verifyFormat("typedef union Foo\n"
7810                "{\n"
7811                "} Foo_t;",
7812                Style);
7813 }
7814 
7815 TEST_F(FormatTest, SplitEmptyNamespace) {
7816   FormatStyle Style = getLLVMStyle();
7817   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7818   Style.BraceWrapping.AfterNamespace = true;
7819   Style.BraceWrapping.SplitEmptyNamespace = false;
7820 
7821   verifyFormat("namespace Foo\n"
7822                "{};",
7823                Style);
7824   verifyFormat("/* something */ namespace Foo\n"
7825                "{};",
7826                Style);
7827   verifyFormat("inline namespace Foo\n"
7828                "{};",
7829                Style);
7830   verifyFormat("/* something */ inline namespace Foo\n"
7831                "{};",
7832                Style);
7833   verifyFormat("export namespace Foo\n"
7834                "{};",
7835                Style);
7836   verifyFormat("namespace Foo\n"
7837                "{\n"
7838                "void Bar();\n"
7839                "};",
7840                Style);
7841 }
7842 
7843 TEST_F(FormatTest, NeverMergeShortRecords) {
7844   FormatStyle Style = getLLVMStyle();
7845 
7846   verifyFormat("class Foo {\n"
7847                "  Foo();\n"
7848                "};",
7849                Style);
7850   verifyFormat("typedef class Foo {\n"
7851                "  Foo();\n"
7852                "} Foo_t;",
7853                Style);
7854   verifyFormat("struct Foo {\n"
7855                "  Foo();\n"
7856                "};",
7857                Style);
7858   verifyFormat("typedef struct Foo {\n"
7859                "  Foo();\n"
7860                "} Foo_t;",
7861                Style);
7862   verifyFormat("union Foo {\n"
7863                "  A,\n"
7864                "};",
7865                Style);
7866   verifyFormat("typedef union Foo {\n"
7867                "  A,\n"
7868                "} Foo_t;",
7869                Style);
7870   verifyFormat("namespace Foo {\n"
7871                "void Bar();\n"
7872                "};",
7873                Style);
7874 
7875   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7876   Style.BraceWrapping.AfterClass = true;
7877   Style.BraceWrapping.AfterStruct = true;
7878   Style.BraceWrapping.AfterUnion = true;
7879   Style.BraceWrapping.AfterNamespace = true;
7880   verifyFormat("class Foo\n"
7881                "{\n"
7882                "  Foo();\n"
7883                "};",
7884                Style);
7885   verifyFormat("typedef class Foo\n"
7886                "{\n"
7887                "  Foo();\n"
7888                "} Foo_t;",
7889                Style);
7890   verifyFormat("struct Foo\n"
7891                "{\n"
7892                "  Foo();\n"
7893                "};",
7894                Style);
7895   verifyFormat("typedef struct Foo\n"
7896                "{\n"
7897                "  Foo();\n"
7898                "} Foo_t;",
7899                Style);
7900   verifyFormat("union Foo\n"
7901                "{\n"
7902                "  A,\n"
7903                "};",
7904                Style);
7905   verifyFormat("typedef union Foo\n"
7906                "{\n"
7907                "  A,\n"
7908                "} Foo_t;",
7909                Style);
7910   verifyFormat("namespace Foo\n"
7911                "{\n"
7912                "void Bar();\n"
7913                "};",
7914                Style);
7915 }
7916 
7917 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7918   // Elaborate type variable declarations.
7919   verifyFormat("struct foo a = {bar};\nint n;");
7920   verifyFormat("class foo a = {bar};\nint n;");
7921   verifyFormat("union foo a = {bar};\nint n;");
7922 
7923   // Elaborate types inside function definitions.
7924   verifyFormat("struct foo f() {}\nint n;");
7925   verifyFormat("class foo f() {}\nint n;");
7926   verifyFormat("union foo f() {}\nint n;");
7927 
7928   // Templates.
7929   verifyFormat("template <class X> void f() {}\nint n;");
7930   verifyFormat("template <struct X> void f() {}\nint n;");
7931   verifyFormat("template <union X> void f() {}\nint n;");
7932 
7933   // Actual definitions...
7934   verifyFormat("struct {\n} n;");
7935   verifyFormat(
7936       "template <template <class T, class Y>, class Z> class X {\n} n;");
7937   verifyFormat("union Z {\n  int n;\n} x;");
7938   verifyFormat("class MACRO Z {\n} n;");
7939   verifyFormat("class MACRO(X) Z {\n} n;");
7940   verifyFormat("class __attribute__(X) Z {\n} n;");
7941   verifyFormat("class __declspec(X) Z {\n} n;");
7942   verifyFormat("class A##B##C {\n} n;");
7943   verifyFormat("class alignas(16) Z {\n} n;");
7944   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7945   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7946 
7947   // Redefinition from nested context:
7948   verifyFormat("class A::B::C {\n} n;");
7949 
7950   // Template definitions.
7951   verifyFormat(
7952       "template <typename F>\n"
7953       "Matcher(const Matcher<F> &Other,\n"
7954       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7955       "                             !is_same<F, T>::value>::type * = 0)\n"
7956       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7957 
7958   // FIXME: This is still incorrectly handled at the formatter side.
7959   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7960   verifyFormat("int i = SomeFunction(a<b, a> b);");
7961 
7962   // FIXME:
7963   // This now gets parsed incorrectly as class definition.
7964   // verifyFormat("class A<int> f() {\n}\nint n;");
7965 
7966   // Elaborate types where incorrectly parsing the structural element would
7967   // break the indent.
7968   verifyFormat("if (true)\n"
7969                "  class X x;\n"
7970                "else\n"
7971                "  f();\n");
7972 
7973   // This is simply incomplete. Formatting is not important, but must not crash.
7974   verifyFormat("class A:");
7975 }
7976 
7977 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7978   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7979             format("#error Leave     all         white!!!!! space* alone!\n"));
7980   EXPECT_EQ(
7981       "#warning Leave     all         white!!!!! space* alone!\n",
7982       format("#warning Leave     all         white!!!!! space* alone!\n"));
7983   EXPECT_EQ("#error 1", format("  #  error   1"));
7984   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7985 }
7986 
7987 TEST_F(FormatTest, FormatHashIfExpressions) {
7988   verifyFormat("#if AAAA && BBBB");
7989   verifyFormat("#if (AAAA && BBBB)");
7990   verifyFormat("#elif (AAAA && BBBB)");
7991   // FIXME: Come up with a better indentation for #elif.
7992   verifyFormat(
7993       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7994       "    defined(BBBBBBBB)\n"
7995       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7996       "    defined(BBBBBBBB)\n"
7997       "#endif",
7998       getLLVMStyleWithColumns(65));
7999 }
8000 
8001 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
8002   FormatStyle AllowsMergedIf = getGoogleStyle();
8003   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
8004   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
8005   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
8006   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
8007   EXPECT_EQ("if (true) return 42;",
8008             format("if (true)\nreturn 42;", AllowsMergedIf));
8009   FormatStyle ShortMergedIf = AllowsMergedIf;
8010   ShortMergedIf.ColumnLimit = 25;
8011   verifyFormat("#define A \\\n"
8012                "  if (true) return 42;",
8013                ShortMergedIf);
8014   verifyFormat("#define A \\\n"
8015                "  f();    \\\n"
8016                "  if (true)\n"
8017                "#define B",
8018                ShortMergedIf);
8019   verifyFormat("#define A \\\n"
8020                "  f();    \\\n"
8021                "  if (true)\n"
8022                "g();",
8023                ShortMergedIf);
8024   verifyFormat("{\n"
8025                "#ifdef A\n"
8026                "  // Comment\n"
8027                "  if (true) continue;\n"
8028                "#endif\n"
8029                "  // Comment\n"
8030                "  if (true) continue;\n"
8031                "}",
8032                ShortMergedIf);
8033   ShortMergedIf.ColumnLimit = 33;
8034   verifyFormat("#define A \\\n"
8035                "  if constexpr (true) return 42;",
8036                ShortMergedIf);
8037   ShortMergedIf.ColumnLimit = 29;
8038   verifyFormat("#define A                   \\\n"
8039                "  if (aaaaaaaaaa) return 1; \\\n"
8040                "  return 2;",
8041                ShortMergedIf);
8042   ShortMergedIf.ColumnLimit = 28;
8043   verifyFormat("#define A         \\\n"
8044                "  if (aaaaaaaaaa) \\\n"
8045                "    return 1;     \\\n"
8046                "  return 2;",
8047                ShortMergedIf);
8048   verifyFormat("#define A                \\\n"
8049                "  if constexpr (aaaaaaa) \\\n"
8050                "    return 1;            \\\n"
8051                "  return 2;",
8052                ShortMergedIf);
8053 }
8054 
8055 TEST_F(FormatTest, FormatStarDependingOnContext) {
8056   verifyFormat("void f(int *a);");
8057   verifyFormat("void f() { f(fint * b); }");
8058   verifyFormat("class A {\n  void f(int *a);\n};");
8059   verifyFormat("class A {\n  int *a;\n};");
8060   verifyFormat("namespace a {\n"
8061                "namespace b {\n"
8062                "class A {\n"
8063                "  void f() {}\n"
8064                "  int *a;\n"
8065                "};\n"
8066                "} // namespace b\n"
8067                "} // namespace a");
8068 }
8069 
8070 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
8071   verifyFormat("while");
8072   verifyFormat("operator");
8073 }
8074 
8075 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
8076   // This code would be painfully slow to format if we didn't skip it.
8077   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
8078                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8079                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8080                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8081                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8082                    "A(1, 1)\n"
8083                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
8084                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8085                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8086                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8087                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8088                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8089                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8090                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8091                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8092                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
8093   // Deeply nested part is untouched, rest is formatted.
8094   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
8095             format(std::string("int    i;\n") + Code + "int    j;\n",
8096                    getLLVMStyle(), SC_ExpectIncomplete));
8097 }
8098 
8099 //===----------------------------------------------------------------------===//
8100 // Objective-C tests.
8101 //===----------------------------------------------------------------------===//
8102 
8103 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
8104   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
8105   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
8106             format("-(NSUInteger)indexOfObject:(id)anObject;"));
8107   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
8108   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
8109   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
8110             format("-(NSInteger)Method3:(id)anObject;"));
8111   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
8112             format("-(NSInteger)Method4:(id)anObject;"));
8113   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
8114             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
8115   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
8116             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
8117   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8118             "forAllCells:(BOOL)flag;",
8119             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8120                    "forAllCells:(BOOL)flag;"));
8121 
8122   // Very long objectiveC method declaration.
8123   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
8124                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
8125   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
8126                "                    inRange:(NSRange)range\n"
8127                "                   outRange:(NSRange)out_range\n"
8128                "                  outRange1:(NSRange)out_range1\n"
8129                "                  outRange2:(NSRange)out_range2\n"
8130                "                  outRange3:(NSRange)out_range3\n"
8131                "                  outRange4:(NSRange)out_range4\n"
8132                "                  outRange5:(NSRange)out_range5\n"
8133                "                  outRange6:(NSRange)out_range6\n"
8134                "                  outRange7:(NSRange)out_range7\n"
8135                "                  outRange8:(NSRange)out_range8\n"
8136                "                  outRange9:(NSRange)out_range9;");
8137 
8138   // When the function name has to be wrapped.
8139   FormatStyle Style = getLLVMStyle();
8140   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
8141   // and always indents instead.
8142   Style.IndentWrappedFunctionNames = false;
8143   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8144                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
8145                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
8146                "}",
8147                Style);
8148   Style.IndentWrappedFunctionNames = true;
8149   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8150                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
8151                "               anotherName:(NSString)dddddddddddddd {\n"
8152                "}",
8153                Style);
8154 
8155   verifyFormat("- (int)sum:(vector<int>)numbers;");
8156   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
8157   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
8158   // protocol lists (but not for template classes):
8159   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
8160 
8161   verifyFormat("- (int (*)())foo:(int (*)())f;");
8162   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
8163 
8164   // If there's no return type (very rare in practice!), LLVM and Google style
8165   // agree.
8166   verifyFormat("- foo;");
8167   verifyFormat("- foo:(int)f;");
8168   verifyGoogleFormat("- foo:(int)foo;");
8169 }
8170 
8171 
8172 TEST_F(FormatTest, BreaksStringLiterals) {
8173   EXPECT_EQ("\"some text \"\n"
8174             "\"other\";",
8175             format("\"some text other\";", getLLVMStyleWithColumns(12)));
8176   EXPECT_EQ("\"some text \"\n"
8177             "\"other\";",
8178             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
8179   EXPECT_EQ(
8180       "#define A  \\\n"
8181       "  \"some \"  \\\n"
8182       "  \"text \"  \\\n"
8183       "  \"other\";",
8184       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
8185   EXPECT_EQ(
8186       "#define A  \\\n"
8187       "  \"so \"    \\\n"
8188       "  \"text \"  \\\n"
8189       "  \"other\";",
8190       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
8191 
8192   EXPECT_EQ("\"some text\"",
8193             format("\"some text\"", getLLVMStyleWithColumns(1)));
8194   EXPECT_EQ("\"some text\"",
8195             format("\"some text\"", getLLVMStyleWithColumns(11)));
8196   EXPECT_EQ("\"some \"\n"
8197             "\"text\"",
8198             format("\"some text\"", getLLVMStyleWithColumns(10)));
8199   EXPECT_EQ("\"some \"\n"
8200             "\"text\"",
8201             format("\"some text\"", getLLVMStyleWithColumns(7)));
8202   EXPECT_EQ("\"some\"\n"
8203             "\" tex\"\n"
8204             "\"t\"",
8205             format("\"some text\"", getLLVMStyleWithColumns(6)));
8206   EXPECT_EQ("\"some\"\n"
8207             "\" tex\"\n"
8208             "\" and\"",
8209             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
8210   EXPECT_EQ("\"some\"\n"
8211             "\"/tex\"\n"
8212             "\"/and\"",
8213             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
8214 
8215   EXPECT_EQ("variable =\n"
8216             "    \"long string \"\n"
8217             "    \"literal\";",
8218             format("variable = \"long string literal\";",
8219                    getLLVMStyleWithColumns(20)));
8220 
8221   EXPECT_EQ("variable = f(\n"
8222             "    \"long string \"\n"
8223             "    \"literal\",\n"
8224             "    short,\n"
8225             "    loooooooooooooooooooong);",
8226             format("variable = f(\"long string literal\", short, "
8227                    "loooooooooooooooooooong);",
8228                    getLLVMStyleWithColumns(20)));
8229 
8230   EXPECT_EQ(
8231       "f(g(\"long string \"\n"
8232       "    \"literal\"),\n"
8233       "  b);",
8234       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
8235   EXPECT_EQ("f(g(\"long string \"\n"
8236             "    \"literal\",\n"
8237             "    a),\n"
8238             "  b);",
8239             format("f(g(\"long string literal\", a), b);",
8240                    getLLVMStyleWithColumns(20)));
8241   EXPECT_EQ(
8242       "f(\"one two\".split(\n"
8243       "    variable));",
8244       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8245   EXPECT_EQ("f(\"one two three four five six \"\n"
8246             "  \"seven\".split(\n"
8247             "      really_looooong_variable));",
8248             format("f(\"one two three four five six seven\"."
8249                    "split(really_looooong_variable));",
8250                    getLLVMStyleWithColumns(33)));
8251 
8252   EXPECT_EQ("f(\"some \"\n"
8253             "  \"text\",\n"
8254             "  other);",
8255             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8256 
8257   // Only break as a last resort.
8258   verifyFormat(
8259       "aaaaaaaaaaaaaaaaaaaa(\n"
8260       "    aaaaaaaaaaaaaaaaaaaa,\n"
8261       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8262 
8263   EXPECT_EQ("\"splitmea\"\n"
8264             "\"trandomp\"\n"
8265             "\"oint\"",
8266             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8267 
8268   EXPECT_EQ("\"split/\"\n"
8269             "\"pathat/\"\n"
8270             "\"slashes\"",
8271             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8272 
8273   EXPECT_EQ("\"split/\"\n"
8274             "\"pathat/\"\n"
8275             "\"slashes\"",
8276             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8277   EXPECT_EQ("\"split at \"\n"
8278             "\"spaces/at/\"\n"
8279             "\"slashes.at.any$\"\n"
8280             "\"non-alphanumeric%\"\n"
8281             "\"1111111111characte\"\n"
8282             "\"rs\"",
8283             format("\"split at "
8284                    "spaces/at/"
8285                    "slashes.at."
8286                    "any$non-"
8287                    "alphanumeric%"
8288                    "1111111111characte"
8289                    "rs\"",
8290                    getLLVMStyleWithColumns(20)));
8291 
8292   // Verify that splitting the strings understands
8293   // Style::AlwaysBreakBeforeMultilineStrings.
8294   EXPECT_EQ(
8295       "aaaaaaaaaaaa(\n"
8296       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8297       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8298       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8299              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8300              "aaaaaaaaaaaaaaaaaaaaaa\");",
8301              getGoogleStyle()));
8302   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8303             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8304             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8305                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8306                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8307                    getGoogleStyle()));
8308   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8309             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8310             format("llvm::outs() << "
8311                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8312                    "aaaaaaaaaaaaaaaaaaa\";"));
8313   EXPECT_EQ("ffff(\n"
8314             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8315             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8316             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8317                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8318                    getGoogleStyle()));
8319 
8320   FormatStyle Style = getLLVMStyleWithColumns(12);
8321   Style.BreakStringLiterals = false;
8322   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8323 
8324   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8325   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8326   EXPECT_EQ("#define A \\\n"
8327             "  \"some \" \\\n"
8328             "  \"text \" \\\n"
8329             "  \"other\";",
8330             format("#define A \"some text other\";", AlignLeft));
8331 }
8332 
8333 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8334   EXPECT_EQ("C a = \"some more \"\n"
8335             "      \"text\";",
8336             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8337 }
8338 
8339 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8340   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8341   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8342   EXPECT_EQ("int i = a(b());",
8343             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8344 }
8345 
8346 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8347   EXPECT_EQ(
8348       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8349       "(\n"
8350       "    \"x\t\");",
8351       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8352              "aaaaaaa("
8353              "\"x\t\");"));
8354 }
8355 
8356 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8357   EXPECT_EQ(
8358       "u8\"utf8 string \"\n"
8359       "u8\"literal\";",
8360       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8361   EXPECT_EQ(
8362       "u\"utf16 string \"\n"
8363       "u\"literal\";",
8364       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8365   EXPECT_EQ(
8366       "U\"utf32 string \"\n"
8367       "U\"literal\";",
8368       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8369   EXPECT_EQ("L\"wide string \"\n"
8370             "L\"literal\";",
8371             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8372   EXPECT_EQ("@\"NSString \"\n"
8373             "@\"literal\";",
8374             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8375   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8376 
8377   // This input makes clang-format try to split the incomplete unicode escape
8378   // sequence, which used to lead to a crasher.
8379   verifyNoCrash(
8380       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8381       getLLVMStyleWithColumns(60));
8382 }
8383 
8384 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8385   FormatStyle Style = getGoogleStyleWithColumns(15);
8386   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8387   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8388   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8389   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8390   EXPECT_EQ("u8R\"x(raw literal)x\";",
8391             format("u8R\"x(raw literal)x\";", Style));
8392 }
8393 
8394 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8395   FormatStyle Style = getLLVMStyleWithColumns(20);
8396   EXPECT_EQ(
8397       "_T(\"aaaaaaaaaaaaaa\")\n"
8398       "_T(\"aaaaaaaaaaaaaa\")\n"
8399       "_T(\"aaaaaaaaaaaa\")",
8400       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8401   EXPECT_EQ("f(x,\n"
8402             "  _T(\"aaaaaaaaaaaa\")\n"
8403             "  _T(\"aaa\"),\n"
8404             "  z);",
8405             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8406 
8407   // FIXME: Handle embedded spaces in one iteration.
8408   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8409   //            "_T(\"aaaaaaaaaaaaa\")\n"
8410   //            "_T(\"aaaaaaaaaaaaa\")\n"
8411   //            "_T(\"a\")",
8412   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8413   //                   getLLVMStyleWithColumns(20)));
8414   EXPECT_EQ(
8415       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8416       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8417   EXPECT_EQ("f(\n"
8418             "#if !TEST\n"
8419             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8420             "#endif\n"
8421             ");",
8422             format("f(\n"
8423                    "#if !TEST\n"
8424                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8425                    "#endif\n"
8426                    ");"));
8427   EXPECT_EQ("f(\n"
8428             "\n"
8429             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8430             format("f(\n"
8431                    "\n"
8432                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8433 }
8434 
8435 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8436   // In a function call with two operands, the second can be broken with no line
8437   // break before it.
8438   EXPECT_EQ("func(a, \"long long \"\n"
8439             "        \"long long\");",
8440             format("func(a, \"long long long long\");",
8441                    getLLVMStyleWithColumns(24)));
8442   // In a function call with three operands, the second must be broken with a
8443   // line break before it.
8444   EXPECT_EQ("func(a,\n"
8445             "     \"long long long \"\n"
8446             "     \"long\",\n"
8447             "     c);",
8448             format("func(a, \"long long long long\", c);",
8449                    getLLVMStyleWithColumns(24)));
8450   // In a function call with three operands, the third must be broken with a
8451   // line break before it.
8452   EXPECT_EQ("func(a, b,\n"
8453             "     \"long long long \"\n"
8454             "     \"long\");",
8455             format("func(a, b, \"long long long long\");",
8456                    getLLVMStyleWithColumns(24)));
8457   // In a function call with three operands, both the second and the third must
8458   // be broken with a line break before them.
8459   EXPECT_EQ("func(a,\n"
8460             "     \"long long long \"\n"
8461             "     \"long\",\n"
8462             "     \"long long long \"\n"
8463             "     \"long\");",
8464             format("func(a, \"long long long long\", \"long long long long\");",
8465                    getLLVMStyleWithColumns(24)));
8466   // In a chain of << with two operands, the second can be broken with no line
8467   // break before it.
8468   EXPECT_EQ("a << \"line line \"\n"
8469             "     \"line\";",
8470             format("a << \"line line line\";",
8471                    getLLVMStyleWithColumns(20)));
8472   // In a chain of << with three operands, the second can be broken with no line
8473   // break before it.
8474   EXPECT_EQ("abcde << \"line \"\n"
8475             "         \"line line\"\n"
8476             "      << c;",
8477             format("abcde << \"line line line\" << c;",
8478                    getLLVMStyleWithColumns(20)));
8479   // In a chain of << with three operands, the third must be broken with a line
8480   // break before it.
8481   EXPECT_EQ("a << b\n"
8482             "  << \"line line \"\n"
8483             "     \"line\";",
8484             format("a << b << \"line line line\";",
8485                    getLLVMStyleWithColumns(20)));
8486   // In a chain of << with three operands, the second can be broken with no line
8487   // break before it and the third must be broken with a line break before it.
8488   EXPECT_EQ("abcd << \"line line \"\n"
8489             "        \"line\"\n"
8490             "     << \"line line \"\n"
8491             "        \"line\";",
8492             format("abcd << \"line line line\" << \"line line line\";",
8493                    getLLVMStyleWithColumns(20)));
8494   // In a chain of binary operators with two operands, the second can be broken
8495   // with no line break before it.
8496   EXPECT_EQ("abcd + \"line line \"\n"
8497             "       \"line line\";",
8498             format("abcd + \"line line line line\";",
8499                    getLLVMStyleWithColumns(20)));
8500   // In a chain of binary operators with three operands, the second must be
8501   // broken with a line break before it.
8502   EXPECT_EQ("abcd +\n"
8503             "    \"line line \"\n"
8504             "    \"line line\" +\n"
8505             "    e;",
8506             format("abcd + \"line line line line\" + e;",
8507                    getLLVMStyleWithColumns(20)));
8508   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8509   // the first must be broken with a line break before it.
8510   FormatStyle Style = getLLVMStyleWithColumns(25);
8511   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8512   EXPECT_EQ("someFunction(\n"
8513             "    \"long long long \"\n"
8514             "    \"long\",\n"
8515             "    a);",
8516             format("someFunction(\"long long long long\", a);", Style));
8517 }
8518 
8519 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8520   EXPECT_EQ(
8521       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8522       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8523       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8524       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8525              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8526              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8527 }
8528 
8529 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8530   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8531             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8532   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8533             "multiline raw string literal xxxxxxxxxxxxxx\n"
8534             ")x\",\n"
8535             "              a),\n"
8536             "            b);",
8537             format("fffffffffff(g(R\"x(\n"
8538                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8539                    ")x\", a), b);",
8540                    getGoogleStyleWithColumns(20)));
8541   EXPECT_EQ("fffffffffff(\n"
8542             "    g(R\"x(qqq\n"
8543             "multiline raw string literal xxxxxxxxxxxxxx\n"
8544             ")x\",\n"
8545             "      a),\n"
8546             "    b);",
8547             format("fffffffffff(g(R\"x(qqq\n"
8548                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8549                    ")x\", a), b);",
8550                    getGoogleStyleWithColumns(20)));
8551 
8552   EXPECT_EQ("fffffffffff(R\"x(\n"
8553             "multiline raw string literal xxxxxxxxxxxxxx\n"
8554             ")x\");",
8555             format("fffffffffff(R\"x(\n"
8556                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8557                    ")x\");",
8558                    getGoogleStyleWithColumns(20)));
8559   EXPECT_EQ("fffffffffff(R\"x(\n"
8560             "multiline raw string literal xxxxxxxxxxxxxx\n"
8561             ")x\" + bbbbbb);",
8562             format("fffffffffff(R\"x(\n"
8563                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8564                    ")x\" +   bbbbbb);",
8565                    getGoogleStyleWithColumns(20)));
8566   EXPECT_EQ("fffffffffff(\n"
8567             "    R\"x(\n"
8568             "multiline raw string literal xxxxxxxxxxxxxx\n"
8569             ")x\" +\n"
8570             "    bbbbbb);",
8571             format("fffffffffff(\n"
8572                    " R\"x(\n"
8573                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8574                    ")x\" + bbbbbb);",
8575                    getGoogleStyleWithColumns(20)));
8576   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8577             format("fffffffffff(\n"
8578                    " R\"(single line raw string)\" + bbbbbb);"));
8579 }
8580 
8581 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8582   verifyFormat("string a = \"unterminated;");
8583   EXPECT_EQ("function(\"unterminated,\n"
8584             "         OtherParameter);",
8585             format("function(  \"unterminated,\n"
8586                    "    OtherParameter);"));
8587 }
8588 
8589 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8590   FormatStyle Style = getLLVMStyle();
8591   Style.Standard = FormatStyle::LS_Cpp03;
8592   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8593             format("#define x(_a) printf(\"foo\"_a);", Style));
8594 }
8595 
8596 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8597 
8598 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8599   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8600             "             \"ddeeefff\");",
8601             format("someFunction(\"aaabbbcccdddeeefff\");",
8602                    getLLVMStyleWithColumns(25)));
8603   EXPECT_EQ("someFunction1234567890(\n"
8604             "    \"aaabbbcccdddeeefff\");",
8605             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8606                    getLLVMStyleWithColumns(26)));
8607   EXPECT_EQ("someFunction1234567890(\n"
8608             "    \"aaabbbcccdddeeeff\"\n"
8609             "    \"f\");",
8610             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8611                    getLLVMStyleWithColumns(25)));
8612   EXPECT_EQ("someFunction1234567890(\n"
8613             "    \"aaabbbcccdddeeeff\"\n"
8614             "    \"f\");",
8615             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8616                    getLLVMStyleWithColumns(24)));
8617   EXPECT_EQ("someFunction(\n"
8618             "    \"aaabbbcc ddde \"\n"
8619             "    \"efff\");",
8620             format("someFunction(\"aaabbbcc ddde efff\");",
8621                    getLLVMStyleWithColumns(25)));
8622   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8623             "             \"ddeeefff\");",
8624             format("someFunction(\"aaabbbccc ddeeefff\");",
8625                    getLLVMStyleWithColumns(25)));
8626   EXPECT_EQ("someFunction1234567890(\n"
8627             "    \"aaabb \"\n"
8628             "    \"cccdddeeefff\");",
8629             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8630                    getLLVMStyleWithColumns(25)));
8631   EXPECT_EQ("#define A          \\\n"
8632             "  string s =       \\\n"
8633             "      \"123456789\"  \\\n"
8634             "      \"0\";         \\\n"
8635             "  int i;",
8636             format("#define A string s = \"1234567890\"; int i;",
8637                    getLLVMStyleWithColumns(20)));
8638   EXPECT_EQ("someFunction(\n"
8639             "    \"aaabbbcc \"\n"
8640             "    \"dddeeefff\");",
8641             format("someFunction(\"aaabbbcc dddeeefff\");",
8642                    getLLVMStyleWithColumns(25)));
8643 }
8644 
8645 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8646   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8647   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8648   EXPECT_EQ("\"test\"\n"
8649             "\"\\n\"",
8650             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8651   EXPECT_EQ("\"tes\\\\\"\n"
8652             "\"n\"",
8653             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8654   EXPECT_EQ("\"\\\\\\\\\"\n"
8655             "\"\\n\"",
8656             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8657   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8658   EXPECT_EQ("\"\\uff01\"\n"
8659             "\"test\"",
8660             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8661   EXPECT_EQ("\"\\Uff01ff02\"",
8662             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8663   EXPECT_EQ("\"\\x000000000001\"\n"
8664             "\"next\"",
8665             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8666   EXPECT_EQ("\"\\x000000000001next\"",
8667             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8668   EXPECT_EQ("\"\\x000000000001\"",
8669             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8670   EXPECT_EQ("\"test\"\n"
8671             "\"\\000000\"\n"
8672             "\"000001\"",
8673             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8674   EXPECT_EQ("\"test\\000\"\n"
8675             "\"00000000\"\n"
8676             "\"1\"",
8677             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8678 }
8679 
8680 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8681   verifyFormat("void f() {\n"
8682                "  return g() {}\n"
8683                "  void h() {}");
8684   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8685                "g();\n"
8686                "}");
8687 }
8688 
8689 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8690   verifyFormat(
8691       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8692 }
8693 
8694 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8695   verifyFormat("class X {\n"
8696                "  void f() {\n"
8697                "  }\n"
8698                "};",
8699                getLLVMStyleWithColumns(12));
8700 }
8701 
8702 TEST_F(FormatTest, ConfigurableIndentWidth) {
8703   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8704   EightIndent.IndentWidth = 8;
8705   EightIndent.ContinuationIndentWidth = 8;
8706   verifyFormat("void f() {\n"
8707                "        someFunction();\n"
8708                "        if (true) {\n"
8709                "                f();\n"
8710                "        }\n"
8711                "}",
8712                EightIndent);
8713   verifyFormat("class X {\n"
8714                "        void f() {\n"
8715                "        }\n"
8716                "};",
8717                EightIndent);
8718   verifyFormat("int x[] = {\n"
8719                "        call(),\n"
8720                "        call()};",
8721                EightIndent);
8722 }
8723 
8724 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8725   verifyFormat("double\n"
8726                "f();",
8727                getLLVMStyleWithColumns(8));
8728 }
8729 
8730 TEST_F(FormatTest, ConfigurableUseOfTab) {
8731   FormatStyle Tab = getLLVMStyleWithColumns(42);
8732   Tab.IndentWidth = 8;
8733   Tab.UseTab = FormatStyle::UT_Always;
8734   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8735 
8736   EXPECT_EQ("if (aaaaaaaa && // q\n"
8737             "    bb)\t\t// w\n"
8738             "\t;",
8739             format("if (aaaaaaaa &&// q\n"
8740                    "bb)// w\n"
8741                    ";",
8742                    Tab));
8743   EXPECT_EQ("if (aaa && bbb) // w\n"
8744             "\t;",
8745             format("if(aaa&&bbb)// w\n"
8746                    ";",
8747                    Tab));
8748 
8749   verifyFormat("class X {\n"
8750                "\tvoid f() {\n"
8751                "\t\tsomeFunction(parameter1,\n"
8752                "\t\t\t     parameter2);\n"
8753                "\t}\n"
8754                "};",
8755                Tab);
8756   verifyFormat("#define A                        \\\n"
8757                "\tvoid f() {               \\\n"
8758                "\t\tsomeFunction(    \\\n"
8759                "\t\t    parameter1,  \\\n"
8760                "\t\t    parameter2); \\\n"
8761                "\t}",
8762                Tab);
8763   verifyFormat("int a;\t      // x\n"
8764                "int bbbbbbbb; // x\n",
8765                Tab);
8766 
8767   Tab.TabWidth = 4;
8768   Tab.IndentWidth = 8;
8769   verifyFormat("class TabWidth4Indent8 {\n"
8770                "\t\tvoid f() {\n"
8771                "\t\t\t\tsomeFunction(parameter1,\n"
8772                "\t\t\t\t\t\t\t parameter2);\n"
8773                "\t\t}\n"
8774                "};",
8775                Tab);
8776 
8777   Tab.TabWidth = 4;
8778   Tab.IndentWidth = 4;
8779   verifyFormat("class TabWidth4Indent4 {\n"
8780                "\tvoid f() {\n"
8781                "\t\tsomeFunction(parameter1,\n"
8782                "\t\t\t\t\t parameter2);\n"
8783                "\t}\n"
8784                "};",
8785                Tab);
8786 
8787   Tab.TabWidth = 8;
8788   Tab.IndentWidth = 4;
8789   verifyFormat("class TabWidth8Indent4 {\n"
8790                "    void f() {\n"
8791                "\tsomeFunction(parameter1,\n"
8792                "\t\t     parameter2);\n"
8793                "    }\n"
8794                "};",
8795                Tab);
8796 
8797   Tab.TabWidth = 8;
8798   Tab.IndentWidth = 8;
8799   EXPECT_EQ("/*\n"
8800             "\t      a\t\tcomment\n"
8801             "\t      in multiple lines\n"
8802             "       */",
8803             format("   /*\t \t \n"
8804                    " \t \t a\t\tcomment\t \t\n"
8805                    " \t \t in multiple lines\t\n"
8806                    " \t  */",
8807                    Tab));
8808 
8809   Tab.UseTab = FormatStyle::UT_ForIndentation;
8810   verifyFormat("{\n"
8811                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8812                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8813                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8814                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8815                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8816                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8817                "};",
8818                Tab);
8819   verifyFormat("enum AA {\n"
8820                "\ta1, // Force multiple lines\n"
8821                "\ta2,\n"
8822                "\ta3\n"
8823                "};",
8824                Tab);
8825   EXPECT_EQ("if (aaaaaaaa && // q\n"
8826             "    bb)         // w\n"
8827             "\t;",
8828             format("if (aaaaaaaa &&// q\n"
8829                    "bb)// w\n"
8830                    ";",
8831                    Tab));
8832   verifyFormat("class X {\n"
8833                "\tvoid f() {\n"
8834                "\t\tsomeFunction(parameter1,\n"
8835                "\t\t             parameter2);\n"
8836                "\t}\n"
8837                "};",
8838                Tab);
8839   verifyFormat("{\n"
8840                "\tQ(\n"
8841                "\t    {\n"
8842                "\t\t    int a;\n"
8843                "\t\t    someFunction(aaaaaaaa,\n"
8844                "\t\t                 bbbbbbb);\n"
8845                "\t    },\n"
8846                "\t    p);\n"
8847                "}",
8848                Tab);
8849   EXPECT_EQ("{\n"
8850             "\t/* aaaa\n"
8851             "\t   bbbb */\n"
8852             "}",
8853             format("{\n"
8854                    "/* aaaa\n"
8855                    "   bbbb */\n"
8856                    "}",
8857                    Tab));
8858   EXPECT_EQ("{\n"
8859             "\t/*\n"
8860             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8861             "\t  bbbbbbbbbbbbb\n"
8862             "\t*/\n"
8863             "}",
8864             format("{\n"
8865                    "/*\n"
8866                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8867                    "*/\n"
8868                    "}",
8869                    Tab));
8870   EXPECT_EQ("{\n"
8871             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8872             "\t// bbbbbbbbbbbbb\n"
8873             "}",
8874             format("{\n"
8875                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8876                    "}",
8877                    Tab));
8878   EXPECT_EQ("{\n"
8879             "\t/*\n"
8880             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8881             "\t  bbbbbbbbbbbbb\n"
8882             "\t*/\n"
8883             "}",
8884             format("{\n"
8885                    "\t/*\n"
8886                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8887                    "\t*/\n"
8888                    "}",
8889                    Tab));
8890   EXPECT_EQ("{\n"
8891             "\t/*\n"
8892             "\n"
8893             "\t*/\n"
8894             "}",
8895             format("{\n"
8896                    "\t/*\n"
8897                    "\n"
8898                    "\t*/\n"
8899                    "}",
8900                    Tab));
8901   EXPECT_EQ("{\n"
8902             "\t/*\n"
8903             " asdf\n"
8904             "\t*/\n"
8905             "}",
8906             format("{\n"
8907                    "\t/*\n"
8908                    " asdf\n"
8909                    "\t*/\n"
8910                    "}",
8911                    Tab));
8912 
8913   Tab.UseTab = FormatStyle::UT_Never;
8914   EXPECT_EQ("/*\n"
8915             "              a\t\tcomment\n"
8916             "              in multiple lines\n"
8917             "       */",
8918             format("   /*\t \t \n"
8919                    " \t \t a\t\tcomment\t \t\n"
8920                    " \t \t in multiple lines\t\n"
8921                    " \t  */",
8922                    Tab));
8923   EXPECT_EQ("/* some\n"
8924             "   comment */",
8925             format(" \t \t /* some\n"
8926                    " \t \t    comment */",
8927                    Tab));
8928   EXPECT_EQ("int a; /* some\n"
8929             "   comment */",
8930             format(" \t \t int a; /* some\n"
8931                    " \t \t    comment */",
8932                    Tab));
8933 
8934   EXPECT_EQ("int a; /* some\n"
8935             "comment */",
8936             format(" \t \t int\ta; /* some\n"
8937                    " \t \t    comment */",
8938                    Tab));
8939   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8940             "    comment */",
8941             format(" \t \t f(\"\t\t\"); /* some\n"
8942                    " \t \t    comment */",
8943                    Tab));
8944   EXPECT_EQ("{\n"
8945             "  /*\n"
8946             "   * Comment\n"
8947             "   */\n"
8948             "  int i;\n"
8949             "}",
8950             format("{\n"
8951                    "\t/*\n"
8952                    "\t * Comment\n"
8953                    "\t */\n"
8954                    "\t int i;\n"
8955                    "}"));
8956 
8957   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8958   Tab.TabWidth = 8;
8959   Tab.IndentWidth = 8;
8960   EXPECT_EQ("if (aaaaaaaa && // q\n"
8961             "    bb)         // w\n"
8962             "\t;",
8963             format("if (aaaaaaaa &&// q\n"
8964                    "bb)// w\n"
8965                    ";",
8966                    Tab));
8967   EXPECT_EQ("if (aaa && bbb) // w\n"
8968             "\t;",
8969             format("if(aaa&&bbb)// w\n"
8970                    ";",
8971                    Tab));
8972   verifyFormat("class X {\n"
8973                "\tvoid f() {\n"
8974                "\t\tsomeFunction(parameter1,\n"
8975                "\t\t\t     parameter2);\n"
8976                "\t}\n"
8977                "};",
8978                Tab);
8979   verifyFormat("#define A                        \\\n"
8980                "\tvoid f() {               \\\n"
8981                "\t\tsomeFunction(    \\\n"
8982                "\t\t    parameter1,  \\\n"
8983                "\t\t    parameter2); \\\n"
8984                "\t}",
8985                Tab);
8986   Tab.TabWidth = 4;
8987   Tab.IndentWidth = 8;
8988   verifyFormat("class TabWidth4Indent8 {\n"
8989                "\t\tvoid f() {\n"
8990                "\t\t\t\tsomeFunction(parameter1,\n"
8991                "\t\t\t\t\t\t\t parameter2);\n"
8992                "\t\t}\n"
8993                "};",
8994                Tab);
8995   Tab.TabWidth = 4;
8996   Tab.IndentWidth = 4;
8997   verifyFormat("class TabWidth4Indent4 {\n"
8998                "\tvoid f() {\n"
8999                "\t\tsomeFunction(parameter1,\n"
9000                "\t\t\t\t\t parameter2);\n"
9001                "\t}\n"
9002                "};",
9003                Tab);
9004   Tab.TabWidth = 8;
9005   Tab.IndentWidth = 4;
9006   verifyFormat("class TabWidth8Indent4 {\n"
9007                "    void f() {\n"
9008                "\tsomeFunction(parameter1,\n"
9009                "\t\t     parameter2);\n"
9010                "    }\n"
9011                "};",
9012                Tab);
9013   Tab.TabWidth = 8;
9014   Tab.IndentWidth = 8;
9015   EXPECT_EQ("/*\n"
9016             "\t      a\t\tcomment\n"
9017             "\t      in multiple lines\n"
9018             "       */",
9019             format("   /*\t \t \n"
9020                    " \t \t a\t\tcomment\t \t\n"
9021                    " \t \t in multiple lines\t\n"
9022                    " \t  */",
9023                    Tab));
9024   verifyFormat("{\n"
9025                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9026                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9027                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9028                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9029                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9030                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9031                "};",
9032                Tab);
9033   verifyFormat("enum AA {\n"
9034                "\ta1, // Force multiple lines\n"
9035                "\ta2,\n"
9036                "\ta3\n"
9037                "};",
9038                Tab);
9039   EXPECT_EQ("if (aaaaaaaa && // q\n"
9040             "    bb)         // w\n"
9041             "\t;",
9042             format("if (aaaaaaaa &&// q\n"
9043                    "bb)// w\n"
9044                    ";",
9045                    Tab));
9046   verifyFormat("class X {\n"
9047                "\tvoid f() {\n"
9048                "\t\tsomeFunction(parameter1,\n"
9049                "\t\t\t     parameter2);\n"
9050                "\t}\n"
9051                "};",
9052                Tab);
9053   verifyFormat("{\n"
9054                "\tQ(\n"
9055                "\t    {\n"
9056                "\t\t    int a;\n"
9057                "\t\t    someFunction(aaaaaaaa,\n"
9058                "\t\t\t\t bbbbbbb);\n"
9059                "\t    },\n"
9060                "\t    p);\n"
9061                "}",
9062                Tab);
9063   EXPECT_EQ("{\n"
9064             "\t/* aaaa\n"
9065             "\t   bbbb */\n"
9066             "}",
9067             format("{\n"
9068                    "/* aaaa\n"
9069                    "   bbbb */\n"
9070                    "}",
9071                    Tab));
9072   EXPECT_EQ("{\n"
9073             "\t/*\n"
9074             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9075             "\t  bbbbbbbbbbbbb\n"
9076             "\t*/\n"
9077             "}",
9078             format("{\n"
9079                    "/*\n"
9080                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9081                    "*/\n"
9082                    "}",
9083                    Tab));
9084   EXPECT_EQ("{\n"
9085             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9086             "\t// bbbbbbbbbbbbb\n"
9087             "}",
9088             format("{\n"
9089                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9090                    "}",
9091                    Tab));
9092   EXPECT_EQ("{\n"
9093             "\t/*\n"
9094             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9095             "\t  bbbbbbbbbbbbb\n"
9096             "\t*/\n"
9097             "}",
9098             format("{\n"
9099                    "\t/*\n"
9100                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9101                    "\t*/\n"
9102                    "}",
9103                    Tab));
9104   EXPECT_EQ("{\n"
9105             "\t/*\n"
9106             "\n"
9107             "\t*/\n"
9108             "}",
9109             format("{\n"
9110                    "\t/*\n"
9111                    "\n"
9112                    "\t*/\n"
9113                    "}",
9114                    Tab));
9115   EXPECT_EQ("{\n"
9116             "\t/*\n"
9117             " asdf\n"
9118             "\t*/\n"
9119             "}",
9120             format("{\n"
9121                    "\t/*\n"
9122                    " asdf\n"
9123                    "\t*/\n"
9124                    "}",
9125                    Tab));
9126   EXPECT_EQ("/*\n"
9127             "\t      a\t\tcomment\n"
9128             "\t      in multiple lines\n"
9129             "       */",
9130             format("   /*\t \t \n"
9131                    " \t \t a\t\tcomment\t \t\n"
9132                    " \t \t in multiple lines\t\n"
9133                    " \t  */",
9134                    Tab));
9135   EXPECT_EQ("/* some\n"
9136             "   comment */",
9137             format(" \t \t /* some\n"
9138                    " \t \t    comment */",
9139                    Tab));
9140   EXPECT_EQ("int a; /* some\n"
9141             "   comment */",
9142             format(" \t \t int a; /* some\n"
9143                    " \t \t    comment */",
9144                    Tab));
9145   EXPECT_EQ("int a; /* some\n"
9146             "comment */",
9147             format(" \t \t int\ta; /* some\n"
9148                    " \t \t    comment */",
9149                    Tab));
9150   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9151             "    comment */",
9152             format(" \t \t f(\"\t\t\"); /* some\n"
9153                    " \t \t    comment */",
9154                    Tab));
9155   EXPECT_EQ("{\n"
9156             "  /*\n"
9157             "   * Comment\n"
9158             "   */\n"
9159             "  int i;\n"
9160             "}",
9161             format("{\n"
9162                    "\t/*\n"
9163                    "\t * Comment\n"
9164                    "\t */\n"
9165                    "\t int i;\n"
9166                    "}"));
9167   Tab.AlignConsecutiveAssignments = true;
9168   Tab.AlignConsecutiveDeclarations = true;
9169   Tab.TabWidth = 4;
9170   Tab.IndentWidth = 4;
9171   verifyFormat("class Assign {\n"
9172                "\tvoid f() {\n"
9173                "\t\tint         x      = 123;\n"
9174                "\t\tint         random = 4;\n"
9175                "\t\tstd::string alphabet =\n"
9176                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
9177                "\t}\n"
9178                "};",
9179                Tab);
9180 }
9181 
9182 TEST_F(FormatTest, CalculatesOriginalColumn) {
9183   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9184             "q\"; /* some\n"
9185             "       comment */",
9186             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9187                    "q\"; /* some\n"
9188                    "       comment */",
9189                    getLLVMStyle()));
9190   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9191             "/* some\n"
9192             "   comment */",
9193             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9194                    " /* some\n"
9195                    "    comment */",
9196                    getLLVMStyle()));
9197   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9198             "qqq\n"
9199             "/* some\n"
9200             "   comment */",
9201             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9202                    "qqq\n"
9203                    " /* some\n"
9204                    "    comment */",
9205                    getLLVMStyle()));
9206   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9207             "wwww; /* some\n"
9208             "         comment */",
9209             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9210                    "wwww; /* some\n"
9211                    "         comment */",
9212                    getLLVMStyle()));
9213 }
9214 
9215 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
9216   FormatStyle NoSpace = getLLVMStyle();
9217   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
9218 
9219   verifyFormat("while(true)\n"
9220                "  continue;",
9221                NoSpace);
9222   verifyFormat("for(;;)\n"
9223                "  continue;",
9224                NoSpace);
9225   verifyFormat("if(true)\n"
9226                "  f();\n"
9227                "else if(true)\n"
9228                "  f();",
9229                NoSpace);
9230   verifyFormat("do {\n"
9231                "  do_something();\n"
9232                "} while(something());",
9233                NoSpace);
9234   verifyFormat("switch(x) {\n"
9235                "default:\n"
9236                "  break;\n"
9237                "}",
9238                NoSpace);
9239   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
9240   verifyFormat("size_t x = sizeof(x);", NoSpace);
9241   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
9242   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
9243   verifyFormat("alignas(128) char a[128];", NoSpace);
9244   verifyFormat("size_t x = alignof(MyType);", NoSpace);
9245   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
9246   verifyFormat("int f() throw(Deprecated);", NoSpace);
9247   verifyFormat("typedef void (*cb)(int);", NoSpace);
9248   verifyFormat("T A::operator()();", NoSpace);
9249   verifyFormat("X A::operator++(T);", NoSpace);
9250   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
9251 
9252   FormatStyle Space = getLLVMStyle();
9253   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9254 
9255   verifyFormat("int f ();", Space);
9256   verifyFormat("void f (int a, T b) {\n"
9257                "  while (true)\n"
9258                "    continue;\n"
9259                "}",
9260                Space);
9261   verifyFormat("if (true)\n"
9262                "  f ();\n"
9263                "else if (true)\n"
9264                "  f ();",
9265                Space);
9266   verifyFormat("do {\n"
9267                "  do_something ();\n"
9268                "} while (something ());",
9269                Space);
9270   verifyFormat("switch (x) {\n"
9271                "default:\n"
9272                "  break;\n"
9273                "}",
9274                Space);
9275   verifyFormat("A::A () : a (1) {}", Space);
9276   verifyFormat("void f () __attribute__ ((asdf));", Space);
9277   verifyFormat("*(&a + 1);\n"
9278                "&((&a)[1]);\n"
9279                "a[(b + c) * d];\n"
9280                "(((a + 1) * 2) + 3) * 4;",
9281                Space);
9282   verifyFormat("#define A(x) x", Space);
9283   verifyFormat("#define A (x) x", Space);
9284   verifyFormat("#if defined(x)\n"
9285                "#endif",
9286                Space);
9287   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9288   verifyFormat("size_t x = sizeof (x);", Space);
9289   verifyFormat("auto f (int x) -> decltype (x);", Space);
9290   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9291   verifyFormat("alignas (128) char a[128];", Space);
9292   verifyFormat("size_t x = alignof (MyType);", Space);
9293   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9294   verifyFormat("int f () throw (Deprecated);", Space);
9295   verifyFormat("typedef void (*cb) (int);", Space);
9296   verifyFormat("T A::operator() ();", Space);
9297   verifyFormat("X A::operator++ (T);", Space);
9298   verifyFormat("auto lambda = [] () { return 0; };", Space);
9299 }
9300 
9301 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9302   FormatStyle Spaces = getLLVMStyle();
9303 
9304   Spaces.SpacesInParentheses = true;
9305   verifyFormat("do_something( ::globalVar );", Spaces);
9306   verifyFormat("call( x, y, z );", Spaces);
9307   verifyFormat("call();", Spaces);
9308   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9309   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9310                Spaces);
9311   verifyFormat("while ( (bool)1 )\n"
9312                "  continue;",
9313                Spaces);
9314   verifyFormat("for ( ;; )\n"
9315                "  continue;",
9316                Spaces);
9317   verifyFormat("if ( true )\n"
9318                "  f();\n"
9319                "else if ( true )\n"
9320                "  f();",
9321                Spaces);
9322   verifyFormat("do {\n"
9323                "  do_something( (int)i );\n"
9324                "} while ( something() );",
9325                Spaces);
9326   verifyFormat("switch ( x ) {\n"
9327                "default:\n"
9328                "  break;\n"
9329                "}",
9330                Spaces);
9331 
9332   Spaces.SpacesInParentheses = false;
9333   Spaces.SpacesInCStyleCastParentheses = true;
9334   verifyFormat("Type *A = ( Type * )P;", Spaces);
9335   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9336   verifyFormat("x = ( int32 )y;", Spaces);
9337   verifyFormat("int a = ( int )(2.0f);", Spaces);
9338   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9339   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9340   verifyFormat("#define x (( int )-1)", Spaces);
9341 
9342   // Run the first set of tests again with:
9343   Spaces.SpacesInParentheses = false;
9344   Spaces.SpaceInEmptyParentheses = true;
9345   Spaces.SpacesInCStyleCastParentheses = true;
9346   verifyFormat("call(x, y, z);", Spaces);
9347   verifyFormat("call( );", Spaces);
9348   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9349   verifyFormat("while (( bool )1)\n"
9350                "  continue;",
9351                Spaces);
9352   verifyFormat("for (;;)\n"
9353                "  continue;",
9354                Spaces);
9355   verifyFormat("if (true)\n"
9356                "  f( );\n"
9357                "else if (true)\n"
9358                "  f( );",
9359                Spaces);
9360   verifyFormat("do {\n"
9361                "  do_something(( int )i);\n"
9362                "} while (something( ));",
9363                Spaces);
9364   verifyFormat("switch (x) {\n"
9365                "default:\n"
9366                "  break;\n"
9367                "}",
9368                Spaces);
9369 
9370   // Run the first set of tests again with:
9371   Spaces.SpaceAfterCStyleCast = true;
9372   verifyFormat("call(x, y, z);", Spaces);
9373   verifyFormat("call( );", Spaces);
9374   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9375   verifyFormat("while (( bool ) 1)\n"
9376                "  continue;",
9377                Spaces);
9378   verifyFormat("for (;;)\n"
9379                "  continue;",
9380                Spaces);
9381   verifyFormat("if (true)\n"
9382                "  f( );\n"
9383                "else if (true)\n"
9384                "  f( );",
9385                Spaces);
9386   verifyFormat("do {\n"
9387                "  do_something(( int ) i);\n"
9388                "} while (something( ));",
9389                Spaces);
9390   verifyFormat("switch (x) {\n"
9391                "default:\n"
9392                "  break;\n"
9393                "}",
9394                Spaces);
9395 
9396   // Run subset of tests again with:
9397   Spaces.SpacesInCStyleCastParentheses = false;
9398   Spaces.SpaceAfterCStyleCast = true;
9399   verifyFormat("while ((bool) 1)\n"
9400                "  continue;",
9401                Spaces);
9402   verifyFormat("do {\n"
9403                "  do_something((int) i);\n"
9404                "} while (something( ));",
9405                Spaces);
9406 }
9407 
9408 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9409   verifyFormat("int a[5];");
9410   verifyFormat("a[3] += 42;");
9411 
9412   FormatStyle Spaces = getLLVMStyle();
9413   Spaces.SpacesInSquareBrackets = true;
9414   // Lambdas unchanged.
9415   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9416   verifyFormat("return [i, args...] {};", Spaces);
9417 
9418   // Not lambdas.
9419   verifyFormat("int a[ 5 ];", Spaces);
9420   verifyFormat("a[ 3 ] += 42;", Spaces);
9421   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9422   verifyFormat("double &operator[](int i) { return 0; }\n"
9423                "int i;",
9424                Spaces);
9425   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9426   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9427   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9428 }
9429 
9430 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9431   verifyFormat("int a = 5;");
9432   verifyFormat("a += 42;");
9433   verifyFormat("a or_eq 8;");
9434 
9435   FormatStyle Spaces = getLLVMStyle();
9436   Spaces.SpaceBeforeAssignmentOperators = false;
9437   verifyFormat("int a= 5;", Spaces);
9438   verifyFormat("a+= 42;", Spaces);
9439   verifyFormat("a or_eq 8;", Spaces);
9440 }
9441 
9442 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9443   verifyFormat("class Foo : public Bar {};");
9444   verifyFormat("Foo::Foo() : foo(1) {}");
9445   verifyFormat("for (auto a : b) {\n}");
9446   verifyFormat("int x = a ? b : c;");
9447   verifyFormat("{\n"
9448                "label0:\n"
9449                "  int x = 0;\n"
9450                "}");
9451   verifyFormat("switch (x) {\n"
9452                "case 1:\n"
9453                "default:\n"
9454                "}");
9455 
9456   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9457   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9458   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9459   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9460   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9461   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9462   verifyFormat("{\n"
9463                "label1:\n"
9464                "  int x = 0;\n"
9465                "}",
9466                CtorInitializerStyle);
9467   verifyFormat("switch (x) {\n"
9468                "case 1:\n"
9469                "default:\n"
9470                "}",
9471                CtorInitializerStyle);
9472   CtorInitializerStyle.BreakConstructorInitializers =
9473       FormatStyle::BCIS_AfterColon;
9474   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9475                "    aaaaaaaaaaaaaaaa(1),\n"
9476                "    bbbbbbbbbbbbbbbb(2) {}",
9477                CtorInitializerStyle);
9478   CtorInitializerStyle.BreakConstructorInitializers =
9479       FormatStyle::BCIS_BeforeComma;
9480   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9481                "    : aaaaaaaaaaaaaaaa(1)\n"
9482                "    , bbbbbbbbbbbbbbbb(2) {}",
9483                CtorInitializerStyle);
9484   CtorInitializerStyle.BreakConstructorInitializers =
9485       FormatStyle::BCIS_BeforeColon;
9486   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9487                "    : aaaaaaaaaaaaaaaa(1),\n"
9488                "      bbbbbbbbbbbbbbbb(2) {}",
9489                CtorInitializerStyle);
9490   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9491   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9492                ": aaaaaaaaaaaaaaaa(1),\n"
9493                "  bbbbbbbbbbbbbbbb(2) {}",
9494                CtorInitializerStyle);
9495 
9496   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9497   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9498   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9499   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9500   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9501   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9502   verifyFormat("{\n"
9503                "label2:\n"
9504                "  int x = 0;\n"
9505                "}",
9506                InheritanceStyle);
9507   verifyFormat("switch (x) {\n"
9508                "case 1:\n"
9509                "default:\n"
9510                "}",
9511                InheritanceStyle);
9512   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9513   verifyFormat("class Foooooooooooooooooooooo:\n"
9514                "    public aaaaaaaaaaaaaaaaaa,\n"
9515                "    public bbbbbbbbbbbbbbbbbb {\n"
9516                "}",
9517                InheritanceStyle);
9518   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9519   verifyFormat("class Foooooooooooooooooooooo\n"
9520                "    : public aaaaaaaaaaaaaaaaaa\n"
9521                "    , public bbbbbbbbbbbbbbbbbb {\n"
9522                "}",
9523                InheritanceStyle);
9524   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9525   verifyFormat("class Foooooooooooooooooooooo\n"
9526                "    : public aaaaaaaaaaaaaaaaaa,\n"
9527                "      public bbbbbbbbbbbbbbbbbb {\n"
9528                "}",
9529                InheritanceStyle);
9530   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9531   verifyFormat("class Foooooooooooooooooooooo\n"
9532                ": public aaaaaaaaaaaaaaaaaa,\n"
9533                "  public bbbbbbbbbbbbbbbbbb {}",
9534                InheritanceStyle);
9535 
9536   FormatStyle ForLoopStyle = getLLVMStyle();
9537   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9538   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9539   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9540   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9541   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9542   verifyFormat("{\n"
9543                "label2:\n"
9544                "  int x = 0;\n"
9545                "}",
9546                ForLoopStyle);
9547   verifyFormat("switch (x) {\n"
9548                "case 1:\n"
9549                "default:\n"
9550                "}",
9551                ForLoopStyle);
9552 
9553   FormatStyle NoSpaceStyle = getLLVMStyle();
9554   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9555   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9556   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9557   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9558   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9559   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9560   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9561   verifyFormat("{\n"
9562                "label3:\n"
9563                "  int x = 0;\n"
9564                "}",
9565                NoSpaceStyle);
9566   verifyFormat("switch (x) {\n"
9567                "case 1:\n"
9568                "default:\n"
9569                "}",
9570                NoSpaceStyle);
9571 }
9572 
9573 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9574   FormatStyle Alignment = getLLVMStyle();
9575   Alignment.AlignConsecutiveAssignments = false;
9576   verifyFormat("int a = 5;\n"
9577                "int oneTwoThree = 123;",
9578                Alignment);
9579   verifyFormat("int a = 5;\n"
9580                "int oneTwoThree = 123;",
9581                Alignment);
9582 
9583   Alignment.AlignConsecutiveAssignments = true;
9584   verifyFormat("int a           = 5;\n"
9585                "int oneTwoThree = 123;",
9586                Alignment);
9587   verifyFormat("int a           = method();\n"
9588                "int oneTwoThree = 133;",
9589                Alignment);
9590   verifyFormat("a &= 5;\n"
9591                "bcd *= 5;\n"
9592                "ghtyf += 5;\n"
9593                "dvfvdb -= 5;\n"
9594                "a /= 5;\n"
9595                "vdsvsv %= 5;\n"
9596                "sfdbddfbdfbb ^= 5;\n"
9597                "dvsdsv |= 5;\n"
9598                "int dsvvdvsdvvv = 123;",
9599                Alignment);
9600   verifyFormat("int i = 1, j = 10;\n"
9601                "something = 2000;",
9602                Alignment);
9603   verifyFormat("something = 2000;\n"
9604                "int i = 1, j = 10;\n",
9605                Alignment);
9606   verifyFormat("something = 2000;\n"
9607                "another   = 911;\n"
9608                "int i = 1, j = 10;\n"
9609                "oneMore = 1;\n"
9610                "i       = 2;",
9611                Alignment);
9612   verifyFormat("int a   = 5;\n"
9613                "int one = 1;\n"
9614                "method();\n"
9615                "int oneTwoThree = 123;\n"
9616                "int oneTwo      = 12;",
9617                Alignment);
9618   verifyFormat("int oneTwoThree = 123;\n"
9619                "int oneTwo      = 12;\n"
9620                "method();\n",
9621                Alignment);
9622   verifyFormat("int oneTwoThree = 123; // comment\n"
9623                "int oneTwo      = 12;  // comment",
9624                Alignment);
9625   EXPECT_EQ("int a = 5;\n"
9626             "\n"
9627             "int oneTwoThree = 123;",
9628             format("int a       = 5;\n"
9629                    "\n"
9630                    "int oneTwoThree= 123;",
9631                    Alignment));
9632   EXPECT_EQ("int a   = 5;\n"
9633             "int one = 1;\n"
9634             "\n"
9635             "int oneTwoThree = 123;",
9636             format("int a = 5;\n"
9637                    "int one = 1;\n"
9638                    "\n"
9639                    "int oneTwoThree = 123;",
9640                    Alignment));
9641   EXPECT_EQ("int a   = 5;\n"
9642             "int one = 1;\n"
9643             "\n"
9644             "int oneTwoThree = 123;\n"
9645             "int oneTwo      = 12;",
9646             format("int a = 5;\n"
9647                    "int one = 1;\n"
9648                    "\n"
9649                    "int oneTwoThree = 123;\n"
9650                    "int oneTwo = 12;",
9651                    Alignment));
9652   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9653   verifyFormat("#define A \\\n"
9654                "  int aaaa       = 12; \\\n"
9655                "  int b          = 23; \\\n"
9656                "  int ccc        = 234; \\\n"
9657                "  int dddddddddd = 2345;",
9658                Alignment);
9659   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9660   verifyFormat("#define A               \\\n"
9661                "  int aaaa       = 12;  \\\n"
9662                "  int b          = 23;  \\\n"
9663                "  int ccc        = 234; \\\n"
9664                "  int dddddddddd = 2345;",
9665                Alignment);
9666   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9667   verifyFormat("#define A                                                      "
9668                "                \\\n"
9669                "  int aaaa       = 12;                                         "
9670                "                \\\n"
9671                "  int b          = 23;                                         "
9672                "                \\\n"
9673                "  int ccc        = 234;                                        "
9674                "                \\\n"
9675                "  int dddddddddd = 2345;",
9676                Alignment);
9677   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9678                "k = 4, int l = 5,\n"
9679                "                  int m = 6) {\n"
9680                "  int j      = 10;\n"
9681                "  otherThing = 1;\n"
9682                "}",
9683                Alignment);
9684   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9685                "  int i   = 1;\n"
9686                "  int j   = 2;\n"
9687                "  int big = 10000;\n"
9688                "}",
9689                Alignment);
9690   verifyFormat("class C {\n"
9691                "public:\n"
9692                "  int i            = 1;\n"
9693                "  virtual void f() = 0;\n"
9694                "};",
9695                Alignment);
9696   verifyFormat("int i = 1;\n"
9697                "if (SomeType t = getSomething()) {\n"
9698                "}\n"
9699                "int j   = 2;\n"
9700                "int big = 10000;",
9701                Alignment);
9702   verifyFormat("int j = 7;\n"
9703                "for (int k = 0; k < N; ++k) {\n"
9704                "}\n"
9705                "int j   = 2;\n"
9706                "int big = 10000;\n"
9707                "}",
9708                Alignment);
9709   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9710   verifyFormat("int i = 1;\n"
9711                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9712                "    = someLooooooooooooooooongFunction();\n"
9713                "int j = 2;",
9714                Alignment);
9715   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9716   verifyFormat("int i = 1;\n"
9717                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9718                "    someLooooooooooooooooongFunction();\n"
9719                "int j = 2;",
9720                Alignment);
9721 
9722   verifyFormat("auto lambda = []() {\n"
9723                "  auto i = 0;\n"
9724                "  return 0;\n"
9725                "};\n"
9726                "int i  = 0;\n"
9727                "auto v = type{\n"
9728                "    i = 1,   //\n"
9729                "    (i = 2), //\n"
9730                "    i = 3    //\n"
9731                "};",
9732                Alignment);
9733 
9734   verifyFormat(
9735       "int i      = 1;\n"
9736       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9737       "                          loooooooooooooooooooooongParameterB);\n"
9738       "int j      = 2;",
9739       Alignment);
9740 
9741   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9742                "          typename B   = very_long_type_name_1,\n"
9743                "          typename T_2 = very_long_type_name_2>\n"
9744                "auto foo() {}\n",
9745                Alignment);
9746   verifyFormat("int a, b = 1;\n"
9747                "int c  = 2;\n"
9748                "int dd = 3;\n",
9749                Alignment);
9750   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9751                "float b[1][] = {{3.f}};\n",
9752                Alignment);
9753   verifyFormat("for (int i = 0; i < 1; i++)\n"
9754                "  int x = 1;\n",
9755                Alignment);
9756   verifyFormat("for (i = 0; i < 1; i++)\n"
9757                "  x = 1;\n"
9758                "y = 1;\n",
9759                Alignment);
9760 }
9761 
9762 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9763   FormatStyle Alignment = getLLVMStyle();
9764   Alignment.AlignConsecutiveDeclarations = false;
9765   verifyFormat("float const a = 5;\n"
9766                "int oneTwoThree = 123;",
9767                Alignment);
9768   verifyFormat("int a = 5;\n"
9769                "float const oneTwoThree = 123;",
9770                Alignment);
9771 
9772   Alignment.AlignConsecutiveDeclarations = true;
9773   verifyFormat("float const a = 5;\n"
9774                "int         oneTwoThree = 123;",
9775                Alignment);
9776   verifyFormat("int         a = method();\n"
9777                "float const oneTwoThree = 133;",
9778                Alignment);
9779   verifyFormat("int i = 1, j = 10;\n"
9780                "something = 2000;",
9781                Alignment);
9782   verifyFormat("something = 2000;\n"
9783                "int i = 1, j = 10;\n",
9784                Alignment);
9785   verifyFormat("float      something = 2000;\n"
9786                "double     another = 911;\n"
9787                "int        i = 1, j = 10;\n"
9788                "const int *oneMore = 1;\n"
9789                "unsigned   i = 2;",
9790                Alignment);
9791   verifyFormat("float a = 5;\n"
9792                "int   one = 1;\n"
9793                "method();\n"
9794                "const double       oneTwoThree = 123;\n"
9795                "const unsigned int oneTwo = 12;",
9796                Alignment);
9797   verifyFormat("int      oneTwoThree{0}; // comment\n"
9798                "unsigned oneTwo;         // comment",
9799                Alignment);
9800   EXPECT_EQ("float const a = 5;\n"
9801             "\n"
9802             "int oneTwoThree = 123;",
9803             format("float const   a = 5;\n"
9804                    "\n"
9805                    "int           oneTwoThree= 123;",
9806                    Alignment));
9807   EXPECT_EQ("float a = 5;\n"
9808             "int   one = 1;\n"
9809             "\n"
9810             "unsigned oneTwoThree = 123;",
9811             format("float    a = 5;\n"
9812                    "int      one = 1;\n"
9813                    "\n"
9814                    "unsigned oneTwoThree = 123;",
9815                    Alignment));
9816   EXPECT_EQ("float a = 5;\n"
9817             "int   one = 1;\n"
9818             "\n"
9819             "unsigned oneTwoThree = 123;\n"
9820             "int      oneTwo = 12;",
9821             format("float    a = 5;\n"
9822                    "int one = 1;\n"
9823                    "\n"
9824                    "unsigned oneTwoThree = 123;\n"
9825                    "int oneTwo = 12;",
9826                    Alignment));
9827   // Function prototype alignment
9828   verifyFormat("int    a();\n"
9829                "double b();",
9830                Alignment);
9831   verifyFormat("int    a(int x);\n"
9832                "double b();",
9833                Alignment);
9834   unsigned OldColumnLimit = Alignment.ColumnLimit;
9835   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9836   // otherwise the function parameters will be re-flowed onto a single line.
9837   Alignment.ColumnLimit = 0;
9838   EXPECT_EQ("int    a(int   x,\n"
9839             "         float y);\n"
9840             "double b(int    x,\n"
9841             "         double y);",
9842             format("int a(int x,\n"
9843                    " float y);\n"
9844                    "double b(int x,\n"
9845                    " double y);",
9846                    Alignment));
9847   // This ensures that function parameters of function declarations are
9848   // correctly indented when their owning functions are indented.
9849   // The failure case here is for 'double y' to not be indented enough.
9850   EXPECT_EQ("double a(int x);\n"
9851             "int    b(int    y,\n"
9852             "         double z);",
9853             format("double a(int x);\n"
9854                    "int b(int y,\n"
9855                    " double z);",
9856                    Alignment));
9857   // Set ColumnLimit low so that we induce wrapping immediately after
9858   // the function name and opening paren.
9859   Alignment.ColumnLimit = 13;
9860   verifyFormat("int function(\n"
9861                "    int  x,\n"
9862                "    bool y);",
9863                Alignment);
9864   Alignment.ColumnLimit = OldColumnLimit;
9865   // Ensure function pointers don't screw up recursive alignment
9866   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9867                "double b();",
9868                Alignment);
9869   Alignment.AlignConsecutiveAssignments = true;
9870   // Ensure recursive alignment is broken by function braces, so that the
9871   // "a = 1" does not align with subsequent assignments inside the function
9872   // body.
9873   verifyFormat("int func(int a = 1) {\n"
9874                "  int b  = 2;\n"
9875                "  int cc = 3;\n"
9876                "}",
9877                Alignment);
9878   verifyFormat("float      something = 2000;\n"
9879                "double     another   = 911;\n"
9880                "int        i = 1, j = 10;\n"
9881                "const int *oneMore = 1;\n"
9882                "unsigned   i       = 2;",
9883                Alignment);
9884   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9885                "unsigned oneTwo      = 0;   // comment",
9886                Alignment);
9887   // Make sure that scope is correctly tracked, in the absence of braces
9888   verifyFormat("for (int i = 0; i < n; i++)\n"
9889                "  j = i;\n"
9890                "double x = 1;\n",
9891                Alignment);
9892   verifyFormat("if (int i = 0)\n"
9893                "  j = i;\n"
9894                "double x = 1;\n",
9895                Alignment);
9896   // Ensure operator[] and operator() are comprehended
9897   verifyFormat("struct test {\n"
9898                "  long long int foo();\n"
9899                "  int           operator[](int a);\n"
9900                "  double        bar();\n"
9901                "};\n",
9902                Alignment);
9903   verifyFormat("struct test {\n"
9904                "  long long int foo();\n"
9905                "  int           operator()(int a);\n"
9906                "  double        bar();\n"
9907                "};\n",
9908                Alignment);
9909   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9910             "  int const i   = 1;\n"
9911             "  int *     j   = 2;\n"
9912             "  int       big = 10000;\n"
9913             "\n"
9914             "  unsigned oneTwoThree = 123;\n"
9915             "  int      oneTwo      = 12;\n"
9916             "  method();\n"
9917             "  float k  = 2;\n"
9918             "  int   ll = 10000;\n"
9919             "}",
9920             format("void SomeFunction(int parameter= 0) {\n"
9921                    " int const  i= 1;\n"
9922                    "  int *j=2;\n"
9923                    " int big  =  10000;\n"
9924                    "\n"
9925                    "unsigned oneTwoThree  =123;\n"
9926                    "int oneTwo = 12;\n"
9927                    "  method();\n"
9928                    "float k= 2;\n"
9929                    "int ll=10000;\n"
9930                    "}",
9931                    Alignment));
9932   Alignment.AlignConsecutiveAssignments = false;
9933   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9934   verifyFormat("#define A \\\n"
9935                "  int       aaaa = 12; \\\n"
9936                "  float     b = 23; \\\n"
9937                "  const int ccc = 234; \\\n"
9938                "  unsigned  dddddddddd = 2345;",
9939                Alignment);
9940   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9941   verifyFormat("#define A              \\\n"
9942                "  int       aaaa = 12; \\\n"
9943                "  float     b = 23;    \\\n"
9944                "  const int ccc = 234; \\\n"
9945                "  unsigned  dddddddddd = 2345;",
9946                Alignment);
9947   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9948   Alignment.ColumnLimit = 30;
9949   verifyFormat("#define A                    \\\n"
9950                "  int       aaaa = 12;       \\\n"
9951                "  float     b = 23;          \\\n"
9952                "  const int ccc = 234;       \\\n"
9953                "  int       dddddddddd = 2345;",
9954                Alignment);
9955   Alignment.ColumnLimit = 80;
9956   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9957                "k = 4, int l = 5,\n"
9958                "                  int m = 6) {\n"
9959                "  const int j = 10;\n"
9960                "  otherThing = 1;\n"
9961                "}",
9962                Alignment);
9963   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9964                "  int const i = 1;\n"
9965                "  int *     j = 2;\n"
9966                "  int       big = 10000;\n"
9967                "}",
9968                Alignment);
9969   verifyFormat("class C {\n"
9970                "public:\n"
9971                "  int          i = 1;\n"
9972                "  virtual void f() = 0;\n"
9973                "};",
9974                Alignment);
9975   verifyFormat("float i = 1;\n"
9976                "if (SomeType t = getSomething()) {\n"
9977                "}\n"
9978                "const unsigned j = 2;\n"
9979                "int            big = 10000;",
9980                Alignment);
9981   verifyFormat("float j = 7;\n"
9982                "for (int k = 0; k < N; ++k) {\n"
9983                "}\n"
9984                "unsigned j = 2;\n"
9985                "int      big = 10000;\n"
9986                "}",
9987                Alignment);
9988   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9989   verifyFormat("float              i = 1;\n"
9990                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9991                "    = someLooooooooooooooooongFunction();\n"
9992                "int j = 2;",
9993                Alignment);
9994   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9995   verifyFormat("int                i = 1;\n"
9996                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9997                "    someLooooooooooooooooongFunction();\n"
9998                "int j = 2;",
9999                Alignment);
10000 
10001   Alignment.AlignConsecutiveAssignments = true;
10002   verifyFormat("auto lambda = []() {\n"
10003                "  auto  ii = 0;\n"
10004                "  float j  = 0;\n"
10005                "  return 0;\n"
10006                "};\n"
10007                "int   i  = 0;\n"
10008                "float i2 = 0;\n"
10009                "auto  v  = type{\n"
10010                "    i = 1,   //\n"
10011                "    (i = 2), //\n"
10012                "    i = 3    //\n"
10013                "};",
10014                Alignment);
10015   Alignment.AlignConsecutiveAssignments = false;
10016 
10017   verifyFormat(
10018       "int      i = 1;\n"
10019       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10020       "                          loooooooooooooooooooooongParameterB);\n"
10021       "int      j = 2;",
10022       Alignment);
10023 
10024   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
10025   // We expect declarations and assignments to align, as long as it doesn't
10026   // exceed the column limit, starting a new alignment sequence whenever it
10027   // happens.
10028   Alignment.AlignConsecutiveAssignments = true;
10029   Alignment.ColumnLimit = 30;
10030   verifyFormat("float    ii              = 1;\n"
10031                "unsigned j               = 2;\n"
10032                "int someVerylongVariable = 1;\n"
10033                "AnotherLongType  ll = 123456;\n"
10034                "VeryVeryLongType k  = 2;\n"
10035                "int              myvar = 1;",
10036                Alignment);
10037   Alignment.ColumnLimit = 80;
10038   Alignment.AlignConsecutiveAssignments = false;
10039 
10040   verifyFormat(
10041       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
10042       "          typename LongType, typename B>\n"
10043       "auto foo() {}\n",
10044       Alignment);
10045   verifyFormat("float a, b = 1;\n"
10046                "int   c = 2;\n"
10047                "int   dd = 3;\n",
10048                Alignment);
10049   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
10050                "float b[1][] = {{3.f}};\n",
10051                Alignment);
10052   Alignment.AlignConsecutiveAssignments = true;
10053   verifyFormat("float a, b = 1;\n"
10054                "int   c  = 2;\n"
10055                "int   dd = 3;\n",
10056                Alignment);
10057   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
10058                "float b[1][] = {{3.f}};\n",
10059                Alignment);
10060   Alignment.AlignConsecutiveAssignments = false;
10061 
10062   Alignment.ColumnLimit = 30;
10063   Alignment.BinPackParameters = false;
10064   verifyFormat("void foo(float     a,\n"
10065                "         float     b,\n"
10066                "         int       c,\n"
10067                "         uint32_t *d) {\n"
10068                "  int *  e = 0;\n"
10069                "  float  f = 0;\n"
10070                "  double g = 0;\n"
10071                "}\n"
10072                "void bar(ino_t     a,\n"
10073                "         int       b,\n"
10074                "         uint32_t *c,\n"
10075                "         bool      d) {}\n",
10076                Alignment);
10077   Alignment.BinPackParameters = true;
10078   Alignment.ColumnLimit = 80;
10079 
10080   // Bug 33507
10081   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
10082   verifyFormat(
10083       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
10084       "  static const Version verVs2017;\n"
10085       "  return true;\n"
10086       "});\n",
10087       Alignment);
10088   Alignment.PointerAlignment = FormatStyle::PAS_Right;
10089 
10090   // See llvm.org/PR35641
10091   Alignment.AlignConsecutiveDeclarations = true;
10092   verifyFormat("int func() { //\n"
10093                "  int      b;\n"
10094                "  unsigned c;\n"
10095                "}",
10096                Alignment);
10097 }
10098 
10099 TEST_F(FormatTest, LinuxBraceBreaking) {
10100   FormatStyle LinuxBraceStyle = getLLVMStyle();
10101   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
10102   verifyFormat("namespace a\n"
10103                "{\n"
10104                "class A\n"
10105                "{\n"
10106                "  void f()\n"
10107                "  {\n"
10108                "    if (true) {\n"
10109                "      a();\n"
10110                "      b();\n"
10111                "    } else {\n"
10112                "      a();\n"
10113                "    }\n"
10114                "  }\n"
10115                "  void g() { return; }\n"
10116                "};\n"
10117                "struct B {\n"
10118                "  int x;\n"
10119                "};\n"
10120                "} // namespace a\n",
10121                LinuxBraceStyle);
10122   verifyFormat("enum X {\n"
10123                "  Y = 0,\n"
10124                "}\n",
10125                LinuxBraceStyle);
10126   verifyFormat("struct S {\n"
10127                "  int Type;\n"
10128                "  union {\n"
10129                "    int x;\n"
10130                "    double y;\n"
10131                "  } Value;\n"
10132                "  class C\n"
10133                "  {\n"
10134                "    MyFavoriteType Value;\n"
10135                "  } Class;\n"
10136                "}\n",
10137                LinuxBraceStyle);
10138 }
10139 
10140 TEST_F(FormatTest, MozillaBraceBreaking) {
10141   FormatStyle MozillaBraceStyle = getLLVMStyle();
10142   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
10143   MozillaBraceStyle.FixNamespaceComments = false;
10144   verifyFormat("namespace a {\n"
10145                "class A\n"
10146                "{\n"
10147                "  void f()\n"
10148                "  {\n"
10149                "    if (true) {\n"
10150                "      a();\n"
10151                "      b();\n"
10152                "    }\n"
10153                "  }\n"
10154                "  void g() { return; }\n"
10155                "};\n"
10156                "enum E\n"
10157                "{\n"
10158                "  A,\n"
10159                "  // foo\n"
10160                "  B,\n"
10161                "  C\n"
10162                "};\n"
10163                "struct B\n"
10164                "{\n"
10165                "  int x;\n"
10166                "};\n"
10167                "}\n",
10168                MozillaBraceStyle);
10169   verifyFormat("struct S\n"
10170                "{\n"
10171                "  int Type;\n"
10172                "  union\n"
10173                "  {\n"
10174                "    int x;\n"
10175                "    double y;\n"
10176                "  } Value;\n"
10177                "  class C\n"
10178                "  {\n"
10179                "    MyFavoriteType Value;\n"
10180                "  } Class;\n"
10181                "}\n",
10182                MozillaBraceStyle);
10183 }
10184 
10185 TEST_F(FormatTest, StroustrupBraceBreaking) {
10186   FormatStyle StroustrupBraceStyle = getLLVMStyle();
10187   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10188   verifyFormat("namespace a {\n"
10189                "class A {\n"
10190                "  void f()\n"
10191                "  {\n"
10192                "    if (true) {\n"
10193                "      a();\n"
10194                "      b();\n"
10195                "    }\n"
10196                "  }\n"
10197                "  void g() { return; }\n"
10198                "};\n"
10199                "struct B {\n"
10200                "  int x;\n"
10201                "};\n"
10202                "} // namespace a\n",
10203                StroustrupBraceStyle);
10204 
10205   verifyFormat("void foo()\n"
10206                "{\n"
10207                "  if (a) {\n"
10208                "    a();\n"
10209                "  }\n"
10210                "  else {\n"
10211                "    b();\n"
10212                "  }\n"
10213                "}\n",
10214                StroustrupBraceStyle);
10215 
10216   verifyFormat("#ifdef _DEBUG\n"
10217                "int foo(int i = 0)\n"
10218                "#else\n"
10219                "int foo(int i = 5)\n"
10220                "#endif\n"
10221                "{\n"
10222                "  return i;\n"
10223                "}",
10224                StroustrupBraceStyle);
10225 
10226   verifyFormat("void foo() {}\n"
10227                "void bar()\n"
10228                "#ifdef _DEBUG\n"
10229                "{\n"
10230                "  foo();\n"
10231                "}\n"
10232                "#else\n"
10233                "{\n"
10234                "}\n"
10235                "#endif",
10236                StroustrupBraceStyle);
10237 
10238   verifyFormat("void foobar() { int i = 5; }\n"
10239                "#ifdef _DEBUG\n"
10240                "void bar() {}\n"
10241                "#else\n"
10242                "void bar() { foobar(); }\n"
10243                "#endif",
10244                StroustrupBraceStyle);
10245 }
10246 
10247 TEST_F(FormatTest, AllmanBraceBreaking) {
10248   FormatStyle AllmanBraceStyle = getLLVMStyle();
10249   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
10250 
10251   EXPECT_EQ("namespace a\n"
10252             "{\n"
10253             "void f();\n"
10254             "void g();\n"
10255             "} // namespace a\n",
10256             format("namespace a\n"
10257                    "{\n"
10258                    "void f();\n"
10259                    "void g();\n"
10260                    "}\n",
10261                    AllmanBraceStyle));
10262 
10263   verifyFormat("namespace a\n"
10264                "{\n"
10265                "class A\n"
10266                "{\n"
10267                "  void f()\n"
10268                "  {\n"
10269                "    if (true)\n"
10270                "    {\n"
10271                "      a();\n"
10272                "      b();\n"
10273                "    }\n"
10274                "  }\n"
10275                "  void g() { return; }\n"
10276                "};\n"
10277                "struct B\n"
10278                "{\n"
10279                "  int x;\n"
10280                "};\n"
10281                "} // namespace a",
10282                AllmanBraceStyle);
10283 
10284   verifyFormat("void f()\n"
10285                "{\n"
10286                "  if (true)\n"
10287                "  {\n"
10288                "    a();\n"
10289                "  }\n"
10290                "  else if (false)\n"
10291                "  {\n"
10292                "    b();\n"
10293                "  }\n"
10294                "  else\n"
10295                "  {\n"
10296                "    c();\n"
10297                "  }\n"
10298                "}\n",
10299                AllmanBraceStyle);
10300 
10301   verifyFormat("void f()\n"
10302                "{\n"
10303                "  for (int i = 0; i < 10; ++i)\n"
10304                "  {\n"
10305                "    a();\n"
10306                "  }\n"
10307                "  while (false)\n"
10308                "  {\n"
10309                "    b();\n"
10310                "  }\n"
10311                "  do\n"
10312                "  {\n"
10313                "    c();\n"
10314                "  } while (false)\n"
10315                "}\n",
10316                AllmanBraceStyle);
10317 
10318   verifyFormat("void f(int a)\n"
10319                "{\n"
10320                "  switch (a)\n"
10321                "  {\n"
10322                "  case 0:\n"
10323                "    break;\n"
10324                "  case 1:\n"
10325                "  {\n"
10326                "    break;\n"
10327                "  }\n"
10328                "  case 2:\n"
10329                "  {\n"
10330                "  }\n"
10331                "  break;\n"
10332                "  default:\n"
10333                "    break;\n"
10334                "  }\n"
10335                "}\n",
10336                AllmanBraceStyle);
10337 
10338   verifyFormat("enum X\n"
10339                "{\n"
10340                "  Y = 0,\n"
10341                "}\n",
10342                AllmanBraceStyle);
10343   verifyFormat("enum X\n"
10344                "{\n"
10345                "  Y = 0\n"
10346                "}\n",
10347                AllmanBraceStyle);
10348 
10349   verifyFormat("@interface BSApplicationController ()\n"
10350                "{\n"
10351                "@private\n"
10352                "  id _extraIvar;\n"
10353                "}\n"
10354                "@end\n",
10355                AllmanBraceStyle);
10356 
10357   verifyFormat("#ifdef _DEBUG\n"
10358                "int foo(int i = 0)\n"
10359                "#else\n"
10360                "int foo(int i = 5)\n"
10361                "#endif\n"
10362                "{\n"
10363                "  return i;\n"
10364                "}",
10365                AllmanBraceStyle);
10366 
10367   verifyFormat("void foo() {}\n"
10368                "void bar()\n"
10369                "#ifdef _DEBUG\n"
10370                "{\n"
10371                "  foo();\n"
10372                "}\n"
10373                "#else\n"
10374                "{\n"
10375                "}\n"
10376                "#endif",
10377                AllmanBraceStyle);
10378 
10379   verifyFormat("void foobar() { int i = 5; }\n"
10380                "#ifdef _DEBUG\n"
10381                "void bar() {}\n"
10382                "#else\n"
10383                "void bar() { foobar(); }\n"
10384                "#endif",
10385                AllmanBraceStyle);
10386 
10387   // This shouldn't affect ObjC blocks..
10388   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10389                "  // ...\n"
10390                "  int i;\n"
10391                "}];",
10392                AllmanBraceStyle);
10393   verifyFormat("void (^block)(void) = ^{\n"
10394                "  // ...\n"
10395                "  int i;\n"
10396                "};",
10397                AllmanBraceStyle);
10398   // .. or dict literals.
10399   verifyFormat("void f()\n"
10400                "{\n"
10401                "  // ...\n"
10402                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10403                "}",
10404                AllmanBraceStyle);
10405   verifyFormat("void f()\n"
10406                "{\n"
10407                "  // ...\n"
10408                "  [object someMethod:@{a : @\"b\"}];\n"
10409                "}",
10410                AllmanBraceStyle);
10411   verifyFormat("int f()\n"
10412                "{ // comment\n"
10413                "  return 42;\n"
10414                "}",
10415                AllmanBraceStyle);
10416 
10417   AllmanBraceStyle.ColumnLimit = 19;
10418   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10419   AllmanBraceStyle.ColumnLimit = 18;
10420   verifyFormat("void f()\n"
10421                "{\n"
10422                "  int i;\n"
10423                "}",
10424                AllmanBraceStyle);
10425   AllmanBraceStyle.ColumnLimit = 80;
10426 
10427   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10428   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
10429   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10430   verifyFormat("void f(bool b)\n"
10431                "{\n"
10432                "  if (b)\n"
10433                "  {\n"
10434                "    return;\n"
10435                "  }\n"
10436                "}\n",
10437                BreakBeforeBraceShortIfs);
10438   verifyFormat("void f(bool b)\n"
10439                "{\n"
10440                "  if constexpr (b)\n"
10441                "  {\n"
10442                "    return;\n"
10443                "  }\n"
10444                "}\n",
10445                BreakBeforeBraceShortIfs);
10446   verifyFormat("void f(bool b)\n"
10447                "{\n"
10448                "  if (b) return;\n"
10449                "}\n",
10450                BreakBeforeBraceShortIfs);
10451   verifyFormat("void f(bool b)\n"
10452                "{\n"
10453                "  if constexpr (b) return;\n"
10454                "}\n",
10455                BreakBeforeBraceShortIfs);
10456   verifyFormat("void f(bool b)\n"
10457                "{\n"
10458                "  while (b)\n"
10459                "  {\n"
10460                "    return;\n"
10461                "  }\n"
10462                "}\n",
10463                BreakBeforeBraceShortIfs);
10464 }
10465 
10466 TEST_F(FormatTest, GNUBraceBreaking) {
10467   FormatStyle GNUBraceStyle = getLLVMStyle();
10468   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10469   verifyFormat("namespace a\n"
10470                "{\n"
10471                "class A\n"
10472                "{\n"
10473                "  void f()\n"
10474                "  {\n"
10475                "    int a;\n"
10476                "    {\n"
10477                "      int b;\n"
10478                "    }\n"
10479                "    if (true)\n"
10480                "      {\n"
10481                "        a();\n"
10482                "        b();\n"
10483                "      }\n"
10484                "  }\n"
10485                "  void g() { return; }\n"
10486                "}\n"
10487                "} // namespace a",
10488                GNUBraceStyle);
10489 
10490   verifyFormat("void f()\n"
10491                "{\n"
10492                "  if (true)\n"
10493                "    {\n"
10494                "      a();\n"
10495                "    }\n"
10496                "  else if (false)\n"
10497                "    {\n"
10498                "      b();\n"
10499                "    }\n"
10500                "  else\n"
10501                "    {\n"
10502                "      c();\n"
10503                "    }\n"
10504                "}\n",
10505                GNUBraceStyle);
10506 
10507   verifyFormat("void f()\n"
10508                "{\n"
10509                "  for (int i = 0; i < 10; ++i)\n"
10510                "    {\n"
10511                "      a();\n"
10512                "    }\n"
10513                "  while (false)\n"
10514                "    {\n"
10515                "      b();\n"
10516                "    }\n"
10517                "  do\n"
10518                "    {\n"
10519                "      c();\n"
10520                "    }\n"
10521                "  while (false);\n"
10522                "}\n",
10523                GNUBraceStyle);
10524 
10525   verifyFormat("void f(int a)\n"
10526                "{\n"
10527                "  switch (a)\n"
10528                "    {\n"
10529                "    case 0:\n"
10530                "      break;\n"
10531                "    case 1:\n"
10532                "      {\n"
10533                "        break;\n"
10534                "      }\n"
10535                "    case 2:\n"
10536                "      {\n"
10537                "      }\n"
10538                "      break;\n"
10539                "    default:\n"
10540                "      break;\n"
10541                "    }\n"
10542                "}\n",
10543                GNUBraceStyle);
10544 
10545   verifyFormat("enum X\n"
10546                "{\n"
10547                "  Y = 0,\n"
10548                "}\n",
10549                GNUBraceStyle);
10550 
10551   verifyFormat("@interface BSApplicationController ()\n"
10552                "{\n"
10553                "@private\n"
10554                "  id _extraIvar;\n"
10555                "}\n"
10556                "@end\n",
10557                GNUBraceStyle);
10558 
10559   verifyFormat("#ifdef _DEBUG\n"
10560                "int foo(int i = 0)\n"
10561                "#else\n"
10562                "int foo(int i = 5)\n"
10563                "#endif\n"
10564                "{\n"
10565                "  return i;\n"
10566                "}",
10567                GNUBraceStyle);
10568 
10569   verifyFormat("void foo() {}\n"
10570                "void bar()\n"
10571                "#ifdef _DEBUG\n"
10572                "{\n"
10573                "  foo();\n"
10574                "}\n"
10575                "#else\n"
10576                "{\n"
10577                "}\n"
10578                "#endif",
10579                GNUBraceStyle);
10580 
10581   verifyFormat("void foobar() { int i = 5; }\n"
10582                "#ifdef _DEBUG\n"
10583                "void bar() {}\n"
10584                "#else\n"
10585                "void bar() { foobar(); }\n"
10586                "#endif",
10587                GNUBraceStyle);
10588 }
10589 
10590 TEST_F(FormatTest, WebKitBraceBreaking) {
10591   FormatStyle WebKitBraceStyle = getLLVMStyle();
10592   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10593   WebKitBraceStyle.FixNamespaceComments = false;
10594   verifyFormat("namespace a {\n"
10595                "class A {\n"
10596                "  void f()\n"
10597                "  {\n"
10598                "    if (true) {\n"
10599                "      a();\n"
10600                "      b();\n"
10601                "    }\n"
10602                "  }\n"
10603                "  void g() { return; }\n"
10604                "};\n"
10605                "enum E {\n"
10606                "  A,\n"
10607                "  // foo\n"
10608                "  B,\n"
10609                "  C\n"
10610                "};\n"
10611                "struct B {\n"
10612                "  int x;\n"
10613                "};\n"
10614                "}\n",
10615                WebKitBraceStyle);
10616   verifyFormat("struct S {\n"
10617                "  int Type;\n"
10618                "  union {\n"
10619                "    int x;\n"
10620                "    double y;\n"
10621                "  } Value;\n"
10622                "  class C {\n"
10623                "    MyFavoriteType Value;\n"
10624                "  } Class;\n"
10625                "};\n",
10626                WebKitBraceStyle);
10627 }
10628 
10629 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10630   verifyFormat("void f() {\n"
10631                "  try {\n"
10632                "  } catch (const Exception &e) {\n"
10633                "  }\n"
10634                "}\n",
10635                getLLVMStyle());
10636 }
10637 
10638 TEST_F(FormatTest, UnderstandsPragmas) {
10639   verifyFormat("#pragma omp reduction(| : var)");
10640   verifyFormat("#pragma omp reduction(+ : var)");
10641 
10642   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10643             "(including parentheses).",
10644             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10645                    "(including parentheses)."));
10646 }
10647 
10648 TEST_F(FormatTest, UnderstandPragmaOption) {
10649   verifyFormat("#pragma option -C -A");
10650 
10651   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10652 }
10653 
10654 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10655   FormatStyle Style = getLLVMStyle();
10656   Style.ColumnLimit = 20;
10657 
10658   verifyFormat("int a; // the\n"
10659                "       // comment", Style);
10660   EXPECT_EQ("int a; /* first line\n"
10661             "        * second\n"
10662             "        * line third\n"
10663             "        * line\n"
10664             "        */",
10665             format("int a; /* first line\n"
10666                    "        * second\n"
10667                    "        * line third\n"
10668                    "        * line\n"
10669                    "        */",
10670                    Style));
10671   EXPECT_EQ("int a; // first line\n"
10672             "       // second\n"
10673             "       // line third\n"
10674             "       // line",
10675             format("int a; // first line\n"
10676                    "       // second line\n"
10677                    "       // third line",
10678                    Style));
10679 
10680   Style.PenaltyExcessCharacter = 90;
10681   verifyFormat("int a; // the comment", Style);
10682   EXPECT_EQ("int a; // the comment\n"
10683             "       // aaa",
10684             format("int a; // the comment aaa", Style));
10685   EXPECT_EQ("int a; /* first line\n"
10686             "        * second line\n"
10687             "        * third line\n"
10688             "        */",
10689             format("int a; /* first line\n"
10690                    "        * second line\n"
10691                    "        * third line\n"
10692                    "        */",
10693                    Style));
10694   EXPECT_EQ("int a; // first line\n"
10695             "       // second line\n"
10696             "       // third line",
10697             format("int a; // first line\n"
10698                    "       // second line\n"
10699                    "       // third line",
10700                    Style));
10701   // FIXME: Investigate why this is not getting the same layout as the test
10702   // above.
10703   EXPECT_EQ("int a; /* first line\n"
10704             "        * second line\n"
10705             "        * third line\n"
10706             "        */",
10707             format("int a; /* first line second line third line"
10708                    "\n*/",
10709                    Style));
10710 
10711   EXPECT_EQ("// foo bar baz bazfoo\n"
10712             "// foo bar foo bar\n",
10713             format("// foo bar baz bazfoo\n"
10714                    "// foo bar foo           bar\n",
10715                    Style));
10716   EXPECT_EQ("// foo bar baz bazfoo\n"
10717             "// foo bar foo bar\n",
10718             format("// foo bar baz      bazfoo\n"
10719                    "// foo            bar foo bar\n",
10720                    Style));
10721 
10722   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10723   // next one.
10724   EXPECT_EQ("// foo bar baz bazfoo\n"
10725             "// bar foo bar\n",
10726             format("// foo bar baz      bazfoo bar\n"
10727                    "// foo            bar\n",
10728                    Style));
10729 
10730   EXPECT_EQ("// foo bar baz bazfoo\n"
10731             "// foo bar baz bazfoo\n"
10732             "// bar foo bar\n",
10733             format("// foo bar baz      bazfoo\n"
10734                    "// foo bar baz      bazfoo bar\n"
10735                    "// foo bar\n",
10736                    Style));
10737 
10738   EXPECT_EQ("// foo bar baz bazfoo\n"
10739             "// foo bar baz bazfoo\n"
10740             "// bar foo bar\n",
10741             format("// foo bar baz      bazfoo\n"
10742                    "// foo bar baz      bazfoo bar\n"
10743                    "// foo           bar\n",
10744                    Style));
10745 
10746   // Make sure we do not keep protruding characters if strict mode reflow is
10747   // cheaper than keeping protruding characters.
10748   Style.ColumnLimit = 21;
10749   EXPECT_EQ("// foo foo foo foo\n"
10750             "// foo foo foo foo\n"
10751             "// foo foo foo foo\n",
10752             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10753                            Style));
10754 
10755   EXPECT_EQ("int a = /* long block\n"
10756             "           comment */\n"
10757             "    42;",
10758             format("int a = /* long block comment */ 42;", Style));
10759 }
10760 
10761 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10762   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10763   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10764                                   << " differs from Style #0"
10765 
10766 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10767   SmallVector<FormatStyle, 3> Styles;
10768   Styles.resize(3);
10769 
10770   Styles[0] = getLLVMStyle();
10771   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10772   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10773   EXPECT_ALL_STYLES_EQUAL(Styles);
10774 
10775   Styles[0] = getGoogleStyle();
10776   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10777   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10778   EXPECT_ALL_STYLES_EQUAL(Styles);
10779 
10780   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10781   EXPECT_TRUE(
10782       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10783   EXPECT_TRUE(
10784       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10785   EXPECT_ALL_STYLES_EQUAL(Styles);
10786 
10787   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10788   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10789   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10790   EXPECT_ALL_STYLES_EQUAL(Styles);
10791 
10792   Styles[0] = getMozillaStyle();
10793   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10794   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10795   EXPECT_ALL_STYLES_EQUAL(Styles);
10796 
10797   Styles[0] = getWebKitStyle();
10798   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10799   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10800   EXPECT_ALL_STYLES_EQUAL(Styles);
10801 
10802   Styles[0] = getGNUStyle();
10803   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10804   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10805   EXPECT_ALL_STYLES_EQUAL(Styles);
10806 
10807   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10808 }
10809 
10810 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10811   SmallVector<FormatStyle, 8> Styles;
10812   Styles.resize(2);
10813 
10814   Styles[0] = getGoogleStyle();
10815   Styles[1] = getLLVMStyle();
10816   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10817   EXPECT_ALL_STYLES_EQUAL(Styles);
10818 
10819   Styles.resize(5);
10820   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10821   Styles[1] = getLLVMStyle();
10822   Styles[1].Language = FormatStyle::LK_JavaScript;
10823   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10824 
10825   Styles[2] = getLLVMStyle();
10826   Styles[2].Language = FormatStyle::LK_JavaScript;
10827   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10828                                   "BasedOnStyle: Google",
10829                                   &Styles[2])
10830                    .value());
10831 
10832   Styles[3] = getLLVMStyle();
10833   Styles[3].Language = FormatStyle::LK_JavaScript;
10834   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10835                                   "Language: JavaScript",
10836                                   &Styles[3])
10837                    .value());
10838 
10839   Styles[4] = getLLVMStyle();
10840   Styles[4].Language = FormatStyle::LK_JavaScript;
10841   EXPECT_EQ(0, parseConfiguration("---\n"
10842                                   "BasedOnStyle: LLVM\n"
10843                                   "IndentWidth: 123\n"
10844                                   "---\n"
10845                                   "BasedOnStyle: Google\n"
10846                                   "Language: JavaScript",
10847                                   &Styles[4])
10848                    .value());
10849   EXPECT_ALL_STYLES_EQUAL(Styles);
10850 }
10851 
10852 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10853   Style.FIELD = false;                                                         \
10854   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10855   EXPECT_TRUE(Style.FIELD);                                                    \
10856   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10857   EXPECT_FALSE(Style.FIELD);
10858 
10859 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10860 
10861 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10862   Style.STRUCT.FIELD = false;                                                  \
10863   EXPECT_EQ(0,                                                                 \
10864             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10865                 .value());                                                     \
10866   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10867   EXPECT_EQ(0,                                                                 \
10868             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10869                 .value());                                                     \
10870   EXPECT_FALSE(Style.STRUCT.FIELD);
10871 
10872 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10873   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10874 
10875 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10876   EXPECT_NE(VALUE, Style.FIELD);                                               \
10877   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10878   EXPECT_EQ(VALUE, Style.FIELD)
10879 
10880 TEST_F(FormatTest, ParsesConfigurationBools) {
10881   FormatStyle Style = {};
10882   Style.Language = FormatStyle::LK_Cpp;
10883   CHECK_PARSE_BOOL(AlignOperands);
10884   CHECK_PARSE_BOOL(AlignTrailingComments);
10885   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10886   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10887   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10888   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10889   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10890   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10891   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10892   CHECK_PARSE_BOOL(BinPackArguments);
10893   CHECK_PARSE_BOOL(BinPackParameters);
10894   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10895   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10896   CHECK_PARSE_BOOL(BreakStringLiterals);
10897   CHECK_PARSE_BOOL(CompactNamespaces);
10898   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10899   CHECK_PARSE_BOOL(DerivePointerAlignment);
10900   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10901   CHECK_PARSE_BOOL(DisableFormat);
10902   CHECK_PARSE_BOOL(IndentCaseLabels);
10903   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10904   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10905   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10906   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10907   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10908   CHECK_PARSE_BOOL(ReflowComments);
10909   CHECK_PARSE_BOOL(SortIncludes);
10910   CHECK_PARSE_BOOL(SortUsingDeclarations);
10911   CHECK_PARSE_BOOL(SpacesInParentheses);
10912   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10913   CHECK_PARSE_BOOL(SpacesInAngles);
10914   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10915   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10916   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10917   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10918   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10919   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10920   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
10921   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10922   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10923   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10924 
10925   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10926   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10927   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10928   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10929   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10930   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10931   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10932   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10933   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10934   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10935   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10936   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10937   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10938   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10939   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10940 }
10941 
10942 #undef CHECK_PARSE_BOOL
10943 
10944 TEST_F(FormatTest, ParsesConfiguration) {
10945   FormatStyle Style = {};
10946   Style.Language = FormatStyle::LK_Cpp;
10947   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10948   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10949               ConstructorInitializerIndentWidth, 1234u);
10950   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10951   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10952   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10953   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10954               PenaltyBreakAssignment, 1234u);
10955   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10956               PenaltyBreakBeforeFirstCallParameter, 1234u);
10957   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
10958               PenaltyBreakTemplateDeclaration, 1234u);
10959   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10960   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10961               PenaltyReturnTypeOnItsOwnLine, 1234u);
10962   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10963               SpacesBeforeTrailingComments, 1234u);
10964   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10965   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10966   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10967 
10968   Style.PointerAlignment = FormatStyle::PAS_Middle;
10969   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10970               FormatStyle::PAS_Left);
10971   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10972               FormatStyle::PAS_Right);
10973   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10974               FormatStyle::PAS_Middle);
10975   // For backward compatibility:
10976   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10977               FormatStyle::PAS_Left);
10978   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10979               FormatStyle::PAS_Right);
10980   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10981               FormatStyle::PAS_Middle);
10982 
10983   Style.Standard = FormatStyle::LS_Auto;
10984   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10985   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10986   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10987   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10988   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10989 
10990   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10991   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10992               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10993   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10994               FormatStyle::BOS_None);
10995   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10996               FormatStyle::BOS_All);
10997   // For backward compatibility:
10998   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10999               FormatStyle::BOS_None);
11000   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
11001               FormatStyle::BOS_All);
11002 
11003   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
11004   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
11005               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11006   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
11007               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
11008   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
11009               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
11010   // For backward compatibility:
11011   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
11012               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11013 
11014   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
11015   CHECK_PARSE("BreakInheritanceList: BeforeComma",
11016               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11017   CHECK_PARSE("BreakInheritanceList: AfterColon",
11018               BreakInheritanceList, FormatStyle::BILS_AfterColon);
11019   CHECK_PARSE("BreakInheritanceList: BeforeColon",
11020               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
11021   // For backward compatibility:
11022   CHECK_PARSE("BreakBeforeInheritanceComma: true",
11023               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11024 
11025   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11026   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
11027               FormatStyle::BAS_Align);
11028   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
11029               FormatStyle::BAS_DontAlign);
11030   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
11031               FormatStyle::BAS_AlwaysBreak);
11032   // For backward compatibility:
11033   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
11034               FormatStyle::BAS_DontAlign);
11035   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
11036               FormatStyle::BAS_Align);
11037 
11038   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11039   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
11040               FormatStyle::ENAS_DontAlign);
11041   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
11042               FormatStyle::ENAS_Left);
11043   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
11044               FormatStyle::ENAS_Right);
11045   // For backward compatibility:
11046   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
11047               FormatStyle::ENAS_Left);
11048   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
11049               FormatStyle::ENAS_Right);
11050 
11051   Style.UseTab = FormatStyle::UT_ForIndentation;
11052   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
11053   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
11054   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
11055   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
11056               FormatStyle::UT_ForContinuationAndIndentation);
11057   // For backward compatibility:
11058   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
11059   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
11060 
11061   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11062   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
11063               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11064   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
11065               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
11066   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
11067               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
11068   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
11069               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11070   // For backward compatibility:
11071   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
11072               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11073   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
11074               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11075 
11076   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
11077   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
11078               FormatStyle::SBPO_Never);
11079   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
11080               FormatStyle::SBPO_Always);
11081   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
11082               FormatStyle::SBPO_ControlStatements);
11083   // For backward compatibility:
11084   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
11085               FormatStyle::SBPO_Never);
11086   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
11087               FormatStyle::SBPO_ControlStatements);
11088 
11089   Style.ColumnLimit = 123;
11090   FormatStyle BaseStyle = getLLVMStyle();
11091   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
11092   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
11093 
11094   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
11095   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
11096               FormatStyle::BS_Attach);
11097   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
11098               FormatStyle::BS_Linux);
11099   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
11100               FormatStyle::BS_Mozilla);
11101   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
11102               FormatStyle::BS_Stroustrup);
11103   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
11104               FormatStyle::BS_Allman);
11105   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
11106   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
11107               FormatStyle::BS_WebKit);
11108   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
11109               FormatStyle::BS_Custom);
11110 
11111   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11112   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
11113               FormatStyle::RTBS_None);
11114   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
11115               FormatStyle::RTBS_All);
11116   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
11117               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
11118   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
11119               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
11120   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
11121               AlwaysBreakAfterReturnType,
11122               FormatStyle::RTBS_TopLevelDefinitions);
11123 
11124   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11125   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
11126               FormatStyle::BTDS_No);
11127   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
11128               FormatStyle::BTDS_MultiLine);
11129   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
11130               FormatStyle::BTDS_Yes);
11131   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
11132               FormatStyle::BTDS_MultiLine);
11133   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
11134               FormatStyle::BTDS_Yes);
11135 
11136   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
11137   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
11138               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
11139   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
11140               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
11141   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
11142               AlwaysBreakAfterDefinitionReturnType,
11143               FormatStyle::DRTBS_TopLevel);
11144 
11145   Style.NamespaceIndentation = FormatStyle::NI_All;
11146   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
11147               FormatStyle::NI_None);
11148   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
11149               FormatStyle::NI_Inner);
11150   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
11151               FormatStyle::NI_All);
11152 
11153   // FIXME: This is required because parsing a configuration simply overwrites
11154   // the first N elements of the list instead of resetting it.
11155   Style.ForEachMacros.clear();
11156   std::vector<std::string> BoostForeach;
11157   BoostForeach.push_back("BOOST_FOREACH");
11158   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
11159   std::vector<std::string> BoostAndQForeach;
11160   BoostAndQForeach.push_back("BOOST_FOREACH");
11161   BoostAndQForeach.push_back("Q_FOREACH");
11162   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
11163               BoostAndQForeach);
11164 
11165   Style.StatementMacros.clear();
11166   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
11167               std::vector<std::string>{"QUNUSED"});
11168   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
11169               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
11170 
11171   Style.IncludeStyle.IncludeCategories.clear();
11172   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
11173       {"abc/.*", 2}, {".*", 1}};
11174   CHECK_PARSE("IncludeCategories:\n"
11175               "  - Regex: abc/.*\n"
11176               "    Priority: 2\n"
11177               "  - Regex: .*\n"
11178               "    Priority: 1",
11179               IncludeStyle.IncludeCategories, ExpectedCategories);
11180   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
11181               "abc$");
11182 
11183   Style.RawStringFormats.clear();
11184   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
11185       {
11186           FormatStyle::LK_TextProto,
11187           {"pb", "proto"},
11188           {"PARSE_TEXT_PROTO"},
11189           /*CanonicalDelimiter=*/"",
11190           "llvm",
11191       },
11192       {
11193           FormatStyle::LK_Cpp,
11194           {"cc", "cpp"},
11195           {"C_CODEBLOCK", "CPPEVAL"},
11196           /*CanonicalDelimiter=*/"cc",
11197           /*BasedOnStyle=*/"",
11198       },
11199   };
11200 
11201   CHECK_PARSE("RawStringFormats:\n"
11202               "  - Language: TextProto\n"
11203               "    Delimiters:\n"
11204               "      - 'pb'\n"
11205               "      - 'proto'\n"
11206               "    EnclosingFunctions:\n"
11207               "      - 'PARSE_TEXT_PROTO'\n"
11208               "    BasedOnStyle: llvm\n"
11209               "  - Language: Cpp\n"
11210               "    Delimiters:\n"
11211               "      - 'cc'\n"
11212               "      - 'cpp'\n"
11213               "    EnclosingFunctions:\n"
11214               "      - 'C_CODEBLOCK'\n"
11215               "      - 'CPPEVAL'\n"
11216               "    CanonicalDelimiter: 'cc'",
11217               RawStringFormats, ExpectedRawStringFormats);
11218 }
11219 
11220 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
11221   FormatStyle Style = {};
11222   Style.Language = FormatStyle::LK_Cpp;
11223   CHECK_PARSE("Language: Cpp\n"
11224               "IndentWidth: 12",
11225               IndentWidth, 12u);
11226   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
11227                                "IndentWidth: 34",
11228                                &Style),
11229             ParseError::Unsuitable);
11230   EXPECT_EQ(12u, Style.IndentWidth);
11231   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11232   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11233 
11234   Style.Language = FormatStyle::LK_JavaScript;
11235   CHECK_PARSE("Language: JavaScript\n"
11236               "IndentWidth: 12",
11237               IndentWidth, 12u);
11238   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
11239   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
11240                                "IndentWidth: 34",
11241                                &Style),
11242             ParseError::Unsuitable);
11243   EXPECT_EQ(23u, Style.IndentWidth);
11244   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11245   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11246 
11247   CHECK_PARSE("BasedOnStyle: LLVM\n"
11248               "IndentWidth: 67",
11249               IndentWidth, 67u);
11250 
11251   CHECK_PARSE("---\n"
11252               "Language: JavaScript\n"
11253               "IndentWidth: 12\n"
11254               "---\n"
11255               "Language: Cpp\n"
11256               "IndentWidth: 34\n"
11257               "...\n",
11258               IndentWidth, 12u);
11259 
11260   Style.Language = FormatStyle::LK_Cpp;
11261   CHECK_PARSE("---\n"
11262               "Language: JavaScript\n"
11263               "IndentWidth: 12\n"
11264               "---\n"
11265               "Language: Cpp\n"
11266               "IndentWidth: 34\n"
11267               "...\n",
11268               IndentWidth, 34u);
11269   CHECK_PARSE("---\n"
11270               "IndentWidth: 78\n"
11271               "---\n"
11272               "Language: JavaScript\n"
11273               "IndentWidth: 56\n"
11274               "...\n",
11275               IndentWidth, 78u);
11276 
11277   Style.ColumnLimit = 123;
11278   Style.IndentWidth = 234;
11279   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
11280   Style.TabWidth = 345;
11281   EXPECT_FALSE(parseConfiguration("---\n"
11282                                   "IndentWidth: 456\n"
11283                                   "BreakBeforeBraces: Allman\n"
11284                                   "---\n"
11285                                   "Language: JavaScript\n"
11286                                   "IndentWidth: 111\n"
11287                                   "TabWidth: 111\n"
11288                                   "---\n"
11289                                   "Language: Cpp\n"
11290                                   "BreakBeforeBraces: Stroustrup\n"
11291                                   "TabWidth: 789\n"
11292                                   "...\n",
11293                                   &Style));
11294   EXPECT_EQ(123u, Style.ColumnLimit);
11295   EXPECT_EQ(456u, Style.IndentWidth);
11296   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11297   EXPECT_EQ(789u, Style.TabWidth);
11298 
11299   EXPECT_EQ(parseConfiguration("---\n"
11300                                "Language: JavaScript\n"
11301                                "IndentWidth: 56\n"
11302                                "---\n"
11303                                "IndentWidth: 78\n"
11304                                "...\n",
11305                                &Style),
11306             ParseError::Error);
11307   EXPECT_EQ(parseConfiguration("---\n"
11308                                "Language: JavaScript\n"
11309                                "IndentWidth: 56\n"
11310                                "---\n"
11311                                "Language: JavaScript\n"
11312                                "IndentWidth: 78\n"
11313                                "...\n",
11314                                &Style),
11315             ParseError::Error);
11316 
11317   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11318 }
11319 
11320 #undef CHECK_PARSE
11321 
11322 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11323   FormatStyle Style = {};
11324   Style.Language = FormatStyle::LK_JavaScript;
11325   Style.BreakBeforeTernaryOperators = true;
11326   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11327   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11328 
11329   Style.BreakBeforeTernaryOperators = true;
11330   EXPECT_EQ(0, parseConfiguration("---\n"
11331                                   "BasedOnStyle: Google\n"
11332                                   "---\n"
11333                                   "Language: JavaScript\n"
11334                                   "IndentWidth: 76\n"
11335                                   "...\n",
11336                                   &Style)
11337                    .value());
11338   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11339   EXPECT_EQ(76u, Style.IndentWidth);
11340   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11341 }
11342 
11343 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11344   FormatStyle Style = getLLVMStyle();
11345   std::string YAML = configurationAsText(Style);
11346   FormatStyle ParsedStyle = {};
11347   ParsedStyle.Language = FormatStyle::LK_Cpp;
11348   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11349   EXPECT_EQ(Style, ParsedStyle);
11350 }
11351 
11352 TEST_F(FormatTest, WorksFor8bitEncodings) {
11353   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11354             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11355             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11356             "\"\xef\xee\xf0\xf3...\"",
11357             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11358                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11359                    "\xef\xee\xf0\xf3...\"",
11360                    getLLVMStyleWithColumns(12)));
11361 }
11362 
11363 TEST_F(FormatTest, HandlesUTF8BOM) {
11364   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11365   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11366             format("\xef\xbb\xbf#include <iostream>"));
11367   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11368             format("\xef\xbb\xbf\n#include <iostream>"));
11369 }
11370 
11371 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11372 #if !defined(_MSC_VER)
11373 
11374 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11375   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11376                getLLVMStyleWithColumns(35));
11377   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11378                getLLVMStyleWithColumns(31));
11379   verifyFormat("// Однажды в студёную зимнюю пору...",
11380                getLLVMStyleWithColumns(36));
11381   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11382   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11383                getLLVMStyleWithColumns(39));
11384   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11385                getLLVMStyleWithColumns(35));
11386 }
11387 
11388 TEST_F(FormatTest, SplitsUTF8Strings) {
11389   // Non-printable characters' width is currently considered to be the length in
11390   // bytes in UTF8. The characters can be displayed in very different manner
11391   // (zero-width, single width with a substitution glyph, expanded to their code
11392   // (e.g. "<8d>"), so there's no single correct way to handle them.
11393   EXPECT_EQ("\"aaaaÄ\"\n"
11394             "\"\xc2\x8d\";",
11395             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11396   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11397             "\"\xc2\x8d\";",
11398             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11399   EXPECT_EQ("\"Однажды, в \"\n"
11400             "\"студёную \"\n"
11401             "\"зимнюю \"\n"
11402             "\"пору,\"",
11403             format("\"Однажды, в студёную зимнюю пору,\"",
11404                    getLLVMStyleWithColumns(13)));
11405   EXPECT_EQ(
11406       "\"一 二 三 \"\n"
11407       "\"四 五六 \"\n"
11408       "\"七 八 九 \"\n"
11409       "\"十\"",
11410       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11411   EXPECT_EQ("\"一\t\"\n"
11412             "\"二 \t\"\n"
11413             "\"三 四 \"\n"
11414             "\"五\t\"\n"
11415             "\"六 \t\"\n"
11416             "\"七 \"\n"
11417             "\"八九十\tqq\"",
11418             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11419                    getLLVMStyleWithColumns(11)));
11420 
11421   // UTF8 character in an escape sequence.
11422   EXPECT_EQ("\"aaaaaa\"\n"
11423             "\"\\\xC2\x8D\"",
11424             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11425 }
11426 
11427 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11428   EXPECT_EQ("const char *sssss =\n"
11429             "    \"一二三四五六七八\\\n"
11430             " 九 十\";",
11431             format("const char *sssss = \"一二三四五六七八\\\n"
11432                    " 九 十\";",
11433                    getLLVMStyleWithColumns(30)));
11434 }
11435 
11436 TEST_F(FormatTest, SplitsUTF8LineComments) {
11437   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11438             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11439   EXPECT_EQ("// Я из лесу\n"
11440             "// вышел; был\n"
11441             "// сильный\n"
11442             "// мороз.",
11443             format("// Я из лесу вышел; был сильный мороз.",
11444                    getLLVMStyleWithColumns(13)));
11445   EXPECT_EQ("// 一二三\n"
11446             "// 四五六七\n"
11447             "// 八  九\n"
11448             "// 十",
11449             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11450 }
11451 
11452 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11453   EXPECT_EQ("/* Гляжу,\n"
11454             " * поднимается\n"
11455             " * медленно в\n"
11456             " * гору\n"
11457             " * Лошадка,\n"
11458             " * везущая\n"
11459             " * хворосту\n"
11460             " * воз. */",
11461             format("/* Гляжу, поднимается медленно в гору\n"
11462                    " * Лошадка, везущая хворосту воз. */",
11463                    getLLVMStyleWithColumns(13)));
11464   EXPECT_EQ(
11465       "/* 一二三\n"
11466       " * 四五六七\n"
11467       " * 八  九\n"
11468       " * 十  */",
11469       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11470   EXPECT_EQ("/* �������� ��������\n"
11471             " * ��������\n"
11472             " * ������-�� */",
11473             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11474 }
11475 
11476 #endif // _MSC_VER
11477 
11478 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11479   FormatStyle Style = getLLVMStyle();
11480 
11481   Style.ConstructorInitializerIndentWidth = 4;
11482   verifyFormat(
11483       "SomeClass::Constructor()\n"
11484       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11485       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11486       Style);
11487 
11488   Style.ConstructorInitializerIndentWidth = 2;
11489   verifyFormat(
11490       "SomeClass::Constructor()\n"
11491       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11492       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11493       Style);
11494 
11495   Style.ConstructorInitializerIndentWidth = 0;
11496   verifyFormat(
11497       "SomeClass::Constructor()\n"
11498       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11499       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11500       Style);
11501   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11502   verifyFormat(
11503       "SomeLongTemplateVariableName<\n"
11504       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11505       Style);
11506   verifyFormat(
11507       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11508       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11509       Style);
11510 }
11511 
11512 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11513   FormatStyle Style = getLLVMStyle();
11514   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11515   Style.ConstructorInitializerIndentWidth = 4;
11516   verifyFormat("SomeClass::Constructor()\n"
11517                "    : a(a)\n"
11518                "    , b(b)\n"
11519                "    , c(c) {}",
11520                Style);
11521   verifyFormat("SomeClass::Constructor()\n"
11522                "    : a(a) {}",
11523                Style);
11524 
11525   Style.ColumnLimit = 0;
11526   verifyFormat("SomeClass::Constructor()\n"
11527                "    : a(a) {}",
11528                Style);
11529   verifyFormat("SomeClass::Constructor() noexcept\n"
11530                "    : a(a) {}",
11531                Style);
11532   verifyFormat("SomeClass::Constructor()\n"
11533                "    : a(a)\n"
11534                "    , b(b)\n"
11535                "    , c(c) {}",
11536                Style);
11537   verifyFormat("SomeClass::Constructor()\n"
11538                "    : a(a) {\n"
11539                "  foo();\n"
11540                "  bar();\n"
11541                "}",
11542                Style);
11543 
11544   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11545   verifyFormat("SomeClass::Constructor()\n"
11546                "    : a(a)\n"
11547                "    , b(b)\n"
11548                "    , c(c) {\n}",
11549                Style);
11550   verifyFormat("SomeClass::Constructor()\n"
11551                "    : a(a) {\n}",
11552                Style);
11553 
11554   Style.ColumnLimit = 80;
11555   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11556   Style.ConstructorInitializerIndentWidth = 2;
11557   verifyFormat("SomeClass::Constructor()\n"
11558                "  : a(a)\n"
11559                "  , b(b)\n"
11560                "  , c(c) {}",
11561                Style);
11562 
11563   Style.ConstructorInitializerIndentWidth = 0;
11564   verifyFormat("SomeClass::Constructor()\n"
11565                ": a(a)\n"
11566                ", b(b)\n"
11567                ", c(c) {}",
11568                Style);
11569 
11570   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11571   Style.ConstructorInitializerIndentWidth = 4;
11572   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11573   verifyFormat(
11574       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11575       Style);
11576   verifyFormat(
11577       "SomeClass::Constructor()\n"
11578       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11579       Style);
11580   Style.ConstructorInitializerIndentWidth = 4;
11581   Style.ColumnLimit = 60;
11582   verifyFormat("SomeClass::Constructor()\n"
11583                "    : aaaaaaaa(aaaaaaaa)\n"
11584                "    , aaaaaaaa(aaaaaaaa)\n"
11585                "    , aaaaaaaa(aaaaaaaa) {}",
11586                Style);
11587 }
11588 
11589 TEST_F(FormatTest, Destructors) {
11590   verifyFormat("void F(int &i) { i.~int(); }");
11591   verifyFormat("void F(int &i) { i->~int(); }");
11592 }
11593 
11594 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11595   FormatStyle Style = getWebKitStyle();
11596 
11597   // Don't indent in outer namespaces.
11598   verifyFormat("namespace outer {\n"
11599                "int i;\n"
11600                "namespace inner {\n"
11601                "    int i;\n"
11602                "} // namespace inner\n"
11603                "} // namespace outer\n"
11604                "namespace other_outer {\n"
11605                "int i;\n"
11606                "}",
11607                Style);
11608 
11609   // Don't indent case labels.
11610   verifyFormat("switch (variable) {\n"
11611                "case 1:\n"
11612                "case 2:\n"
11613                "    doSomething();\n"
11614                "    break;\n"
11615                "default:\n"
11616                "    ++variable;\n"
11617                "}",
11618                Style);
11619 
11620   // Wrap before binary operators.
11621   EXPECT_EQ("void f()\n"
11622             "{\n"
11623             "    if (aaaaaaaaaaaaaaaa\n"
11624             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11625             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11626             "        return;\n"
11627             "}",
11628             format("void f() {\n"
11629                    "if (aaaaaaaaaaaaaaaa\n"
11630                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11631                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11632                    "return;\n"
11633                    "}",
11634                    Style));
11635 
11636   // Allow functions on a single line.
11637   verifyFormat("void f() { return; }", Style);
11638 
11639   // Constructor initializers are formatted one per line with the "," on the
11640   // new line.
11641   verifyFormat("Constructor()\n"
11642                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11643                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11644                "          aaaaaaaaaaaaaa)\n"
11645                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11646                "{\n"
11647                "}",
11648                Style);
11649   verifyFormat("SomeClass::Constructor()\n"
11650                "    : a(a)\n"
11651                "{\n"
11652                "}",
11653                Style);
11654   EXPECT_EQ("SomeClass::Constructor()\n"
11655             "    : a(a)\n"
11656             "{\n"
11657             "}",
11658             format("SomeClass::Constructor():a(a){}", Style));
11659   verifyFormat("SomeClass::Constructor()\n"
11660                "    : a(a)\n"
11661                "    , b(b)\n"
11662                "    , c(c)\n"
11663                "{\n"
11664                "}",
11665                Style);
11666   verifyFormat("SomeClass::Constructor()\n"
11667                "    : a(a)\n"
11668                "{\n"
11669                "    foo();\n"
11670                "    bar();\n"
11671                "}",
11672                Style);
11673 
11674   // Access specifiers should be aligned left.
11675   verifyFormat("class C {\n"
11676                "public:\n"
11677                "    int i;\n"
11678                "};",
11679                Style);
11680 
11681   // Do not align comments.
11682   verifyFormat("int a; // Do not\n"
11683                "double b; // align comments.",
11684                Style);
11685 
11686   // Do not align operands.
11687   EXPECT_EQ("ASSERT(aaaa\n"
11688             "    || bbbb);",
11689             format("ASSERT ( aaaa\n||bbbb);", Style));
11690 
11691   // Accept input's line breaks.
11692   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11693             "    || bbbbbbbbbbbbbbb) {\n"
11694             "    i++;\n"
11695             "}",
11696             format("if (aaaaaaaaaaaaaaa\n"
11697                    "|| bbbbbbbbbbbbbbb) { i++; }",
11698                    Style));
11699   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11700             "    i++;\n"
11701             "}",
11702             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11703 
11704   // Don't automatically break all macro definitions (llvm.org/PR17842).
11705   verifyFormat("#define aNumber 10", Style);
11706   // However, generally keep the line breaks that the user authored.
11707   EXPECT_EQ("#define aNumber \\\n"
11708             "    10",
11709             format("#define aNumber \\\n"
11710                    " 10",
11711                    Style));
11712 
11713   // Keep empty and one-element array literals on a single line.
11714   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11715             "                                  copyItems:YES];",
11716             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11717                    "copyItems:YES];",
11718                    Style));
11719   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11720             "                                  copyItems:YES];",
11721             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11722                    "             copyItems:YES];",
11723                    Style));
11724   // FIXME: This does not seem right, there should be more indentation before
11725   // the array literal's entries. Nested blocks have the same problem.
11726   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11727             "    @\"a\",\n"
11728             "    @\"a\"\n"
11729             "]\n"
11730             "                                  copyItems:YES];",
11731             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11732                    "     @\"a\",\n"
11733                    "     @\"a\"\n"
11734                    "     ]\n"
11735                    "       copyItems:YES];",
11736                    Style));
11737   EXPECT_EQ(
11738       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11739       "                                  copyItems:YES];",
11740       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11741              "   copyItems:YES];",
11742              Style));
11743 
11744   verifyFormat("[self.a b:c c:d];", Style);
11745   EXPECT_EQ("[self.a b:c\n"
11746             "        c:d];",
11747             format("[self.a b:c\n"
11748                    "c:d];",
11749                    Style));
11750 }
11751 
11752 TEST_F(FormatTest, FormatsLambdas) {
11753   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11754   verifyFormat(
11755       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
11756   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11757   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11758   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11759   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11760   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11761   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11762   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11763   verifyFormat("int x = f(*+[] {});");
11764   verifyFormat("void f() {\n"
11765                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11766                "}\n");
11767   verifyFormat("void f() {\n"
11768                "  other(x.begin(), //\n"
11769                "        x.end(),   //\n"
11770                "        [&](int, int) { return 1; });\n"
11771                "}\n");
11772   verifyFormat("void f() {\n"
11773                "  other.other.other.other.other(\n"
11774                "      x.begin(), x.end(),\n"
11775                "      [something, rather](int, int, int, int, int, int, int) { return 1; });\n"
11776                "}\n");
11777   verifyFormat("void f() {\n"
11778                "  other.other.other.other.other(\n"
11779                "      x.begin(), x.end(),\n"
11780                "      [something, rather](int, int, int, int, int, int, int) {\n"
11781                "        //\n"
11782                "      });\n"
11783                "}\n");
11784   verifyFormat("SomeFunction([]() { // A cool function...\n"
11785                "  return 43;\n"
11786                "});");
11787   EXPECT_EQ("SomeFunction([]() {\n"
11788             "#define A a\n"
11789             "  return 43;\n"
11790             "});",
11791             format("SomeFunction([](){\n"
11792                    "#define A a\n"
11793                    "return 43;\n"
11794                    "});"));
11795   verifyFormat("void f() {\n"
11796                "  SomeFunction([](decltype(x), A *a) {});\n"
11797                "}");
11798   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11799                "    [](const aaaaaaaaaa &a) { return a; });");
11800   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11801                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11802                "});");
11803   verifyFormat("Constructor()\n"
11804                "    : Field([] { // comment\n"
11805                "        int i;\n"
11806                "      }) {}");
11807   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11808                "  return some_parameter.size();\n"
11809                "};");
11810   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11811                "    [](const string &s) { return s; };");
11812   verifyFormat("int i = aaaaaa ? 1 //\n"
11813                "               : [] {\n"
11814                "                   return 2; //\n"
11815                "                 }();");
11816   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11817                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11818                "                  return x == 2; // force break\n"
11819                "                });");
11820   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11821                "    [=](int iiiiiiiiiiii) {\n"
11822                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11823                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11824                "    });",
11825                getLLVMStyleWithColumns(60));
11826   verifyFormat("SomeFunction({[&] {\n"
11827                "                // comment\n"
11828                "              },\n"
11829                "              [&] {\n"
11830                "                // comment\n"
11831                "              }});");
11832   verifyFormat("SomeFunction({[&] {\n"
11833                "  // comment\n"
11834                "}});");
11835   verifyFormat("virtual aaaaaaaaaaaaaaaa(\n"
11836                "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
11837                "    aaaaa aaaaaaaaa);");
11838 
11839   // Lambdas with return types.
11840   verifyFormat("int c = []() -> int { return 2; }();\n");
11841   verifyFormat("int c = []() -> int * { return 2; }();\n");
11842   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11843   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11844   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11845   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11846   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11847   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11848   verifyFormat("[a, a]() -> a<1> {};");
11849   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
11850   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
11851   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
11852   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
11853   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
11854   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
11855   verifyFormat("[]() -> foo<!5> { return {}; };");
11856   verifyFormat("[]() -> foo<~5> { return {}; };");
11857   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
11858   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
11859   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
11860   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
11861   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
11862   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
11863   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
11864   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
11865   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
11866   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
11867   verifyFormat("namespace bar {\n"
11868               "// broken:\n"
11869               "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
11870               "} // namespace bar");
11871   verifyFormat("namespace bar {\n"
11872               "// broken:\n"
11873               "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
11874               "} // namespace bar");
11875   verifyFormat("namespace bar {\n"
11876               "// broken:\n"
11877               "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
11878               "} // namespace bar");
11879   verifyFormat("namespace bar {\n"
11880               "// broken:\n"
11881               "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
11882               "} // namespace bar");
11883   verifyFormat("namespace bar {\n"
11884               "// broken:\n"
11885               "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
11886               "} // namespace bar");
11887   verifyFormat("namespace bar {\n"
11888               "// broken:\n"
11889               "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
11890               "} // namespace bar");
11891   verifyFormat("namespace bar {\n"
11892               "// broken:\n"
11893               "auto foo{[]() -> foo<!5> { return {}; }};\n"
11894               "} // namespace bar");
11895   verifyFormat("namespace bar {\n"
11896               "// broken:\n"
11897               "auto foo{[]() -> foo<~5> { return {}; }};\n"
11898               "} // namespace bar");
11899   verifyFormat("namespace bar {\n"
11900               "// broken:\n"
11901               "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
11902               "} // namespace bar");
11903   verifyFormat("namespace bar {\n"
11904               "// broken:\n"
11905               "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
11906               "} // namespace bar");
11907   verifyFormat("namespace bar {\n"
11908               "// broken:\n"
11909               "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
11910               "} // namespace bar");
11911   verifyFormat("namespace bar {\n"
11912               "// broken:\n"
11913               "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
11914               "} // namespace bar");
11915   verifyFormat("namespace bar {\n"
11916               "// broken:\n"
11917               "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
11918               "} // namespace bar");
11919   verifyFormat("namespace bar {\n"
11920               "// broken:\n"
11921               "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
11922               "} // namespace bar");
11923   verifyFormat("namespace bar {\n"
11924               "// broken:\n"
11925               "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
11926               "} // namespace bar");
11927   verifyFormat("namespace bar {\n"
11928               "// broken:\n"
11929               "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
11930               "} // namespace bar");
11931   verifyFormat("namespace bar {\n"
11932               "// broken:\n"
11933               "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
11934               "} // namespace bar");
11935   verifyFormat("namespace bar {\n"
11936               "// broken:\n"
11937               "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
11938               "} // namespace bar");
11939   verifyFormat("[]() -> a<1> {};");
11940   verifyFormat("[]() -> a<1> { ; };");
11941   verifyFormat("[]() -> a<1> { ; }();");
11942   verifyFormat("[a, a]() -> a<true> {};");
11943   verifyFormat("[]() -> a<true> {};");
11944   verifyFormat("[]() -> a<true> { ; };");
11945   verifyFormat("[]() -> a<true> { ; }();");
11946   verifyFormat("[a, a]() -> a<false> {};");
11947   verifyFormat("[]() -> a<false> {};");
11948   verifyFormat("[]() -> a<false> { ; };");
11949   verifyFormat("[]() -> a<false> { ; }();");
11950   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
11951   verifyFormat("namespace bar {\n"
11952                "auto foo{[]() -> foo<false> { ; }};\n"
11953                "} // namespace bar");
11954   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11955                "                   int j) -> int {\n"
11956                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11957                "};");
11958   verifyFormat(
11959       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11960       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11961       "      return aaaaaaaaaaaaaaaaa;\n"
11962       "    });",
11963       getLLVMStyleWithColumns(70));
11964   verifyFormat("[]() //\n"
11965                "    -> int {\n"
11966                "  return 1; //\n"
11967                "};");
11968 
11969   // Multiple lambdas in the same parentheses change indentation rules. These
11970   // lambdas are forced to start on new lines.
11971   verifyFormat("SomeFunction(\n"
11972                "    []() {\n"
11973                "      //\n"
11974                "    },\n"
11975                "    []() {\n"
11976                "      //\n"
11977                "    });");
11978 
11979   // A lambda passed as arg0 is always pushed to the next line.
11980   verifyFormat("SomeFunction(\n"
11981                "    [this] {\n"
11982                "      //\n"
11983                "    },\n"
11984                "    1);\n");
11985 
11986   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0
11987   // case above.
11988   auto Style = getGoogleStyle();
11989   Style.BinPackArguments = false;
11990   verifyFormat("SomeFunction(\n"
11991                "    a,\n"
11992                "    [this] {\n"
11993                "      //\n"
11994                "    },\n"
11995                "    b);\n",
11996                Style);
11997   verifyFormat("SomeFunction(\n"
11998                "    a,\n"
11999                "    [this] {\n"
12000                "      //\n"
12001                "    },\n"
12002                "    b);\n");
12003 
12004   // A lambda with a very long line forces arg0 to be pushed out irrespective of
12005   // the BinPackArguments value (as long as the code is wide enough).
12006   verifyFormat("something->SomeFunction(\n"
12007                "    a,\n"
12008                "    [this] {\n"
12009                "      D0000000000000000000000000000000000000000000000000000000000001();\n"
12010                "    },\n"
12011                "    b);\n");
12012 
12013   // A multi-line lambda is pulled up as long as the introducer fits on the previous
12014   // line and there are no further args.
12015   verifyFormat("function(1, [this, that] {\n"
12016                "  //\n"
12017                "});\n");
12018   verifyFormat("function([this, that] {\n"
12019                "  //\n"
12020                "});\n");
12021   // FIXME: this format is not ideal and we should consider forcing the first arg
12022   // onto its own line.
12023   verifyFormat("function(a, b, c, //\n"
12024                "         d, [this, that] {\n"
12025                "           //\n"
12026                "         });\n");
12027 
12028   // Multiple lambdas are treated correctly even when there is a short arg0.
12029   verifyFormat("SomeFunction(\n"
12030                "    1,\n"
12031                "    [this] {\n"
12032                "      //\n"
12033                "    },\n"
12034                "    [this] {\n"
12035                "      //\n"
12036                "    },\n"
12037                "    1);\n");
12038 
12039   // More complex introducers.
12040   verifyFormat("return [i, args...] {};");
12041 
12042   // Not lambdas.
12043   verifyFormat("constexpr char hello[]{\"hello\"};");
12044   verifyFormat("double &operator[](int i) { return 0; }\n"
12045                "int i;");
12046   verifyFormat("std::unique_ptr<int[]> foo() {}");
12047   verifyFormat("int i = a[a][a]->f();");
12048   verifyFormat("int i = (*b)[a]->f();");
12049 
12050   // Other corner cases.
12051   verifyFormat("void f() {\n"
12052                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
12053                "  );\n"
12054                "}");
12055 
12056   // Lambdas created through weird macros.
12057   verifyFormat("void f() {\n"
12058                "  MACRO((const AA &a) { return 1; });\n"
12059                "  MACRO((AA &a) { return 1; });\n"
12060                "}");
12061 
12062   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
12063                "      doo_dah();\n"
12064                "      doo_dah();\n"
12065                "    })) {\n"
12066                "}");
12067   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
12068                "                doo_dah();\n"
12069                "                doo_dah();\n"
12070                "              })) {\n"
12071                "}");
12072   verifyFormat("auto lambda = []() {\n"
12073                "  int a = 2\n"
12074                "#if A\n"
12075                "          + 2\n"
12076                "#endif\n"
12077                "      ;\n"
12078                "};");
12079 
12080   // Lambdas with complex multiline introducers.
12081   verifyFormat(
12082       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12083       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
12084       "        -> ::std::unordered_set<\n"
12085       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
12086       "      //\n"
12087       "    });");
12088 }
12089 
12090 TEST_F(FormatTest, EmptyLinesInLambdas) {
12091   verifyFormat("auto lambda = []() {\n"
12092                "  x(); //\n"
12093                "};",
12094                "auto lambda = []() {\n"
12095                "\n"
12096                "  x(); //\n"
12097                "\n"
12098                "};");
12099 }
12100 
12101 TEST_F(FormatTest, FormatsBlocks) {
12102   FormatStyle ShortBlocks = getLLVMStyle();
12103   ShortBlocks.AllowShortBlocksOnASingleLine = true;
12104   verifyFormat("int (^Block)(int, int);", ShortBlocks);
12105   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
12106   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
12107   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
12108   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
12109   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
12110 
12111   verifyFormat("foo(^{ bar(); });", ShortBlocks);
12112   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
12113   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
12114 
12115   verifyFormat("[operation setCompletionBlock:^{\n"
12116                "  [self onOperationDone];\n"
12117                "}];");
12118   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
12119                "  [self onOperationDone];\n"
12120                "}]};");
12121   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
12122                "  f();\n"
12123                "}];");
12124   verifyFormat("int a = [operation block:^int(int *i) {\n"
12125                "  return 1;\n"
12126                "}];");
12127   verifyFormat("[myObject doSomethingWith:arg1\n"
12128                "                      aaa:^int(int *a) {\n"
12129                "                        return 1;\n"
12130                "                      }\n"
12131                "                      bbb:f(a * bbbbbbbb)];");
12132 
12133   verifyFormat("[operation setCompletionBlock:^{\n"
12134                "  [self.delegate newDataAvailable];\n"
12135                "}];",
12136                getLLVMStyleWithColumns(60));
12137   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
12138                "  NSString *path = [self sessionFilePath];\n"
12139                "  if (path) {\n"
12140                "    // ...\n"
12141                "  }\n"
12142                "});");
12143   verifyFormat("[[SessionService sharedService]\n"
12144                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12145                "      if (window) {\n"
12146                "        [self windowDidLoad:window];\n"
12147                "      } else {\n"
12148                "        [self errorLoadingWindow];\n"
12149                "      }\n"
12150                "    }];");
12151   verifyFormat("void (^largeBlock)(void) = ^{\n"
12152                "  // ...\n"
12153                "};\n",
12154                getLLVMStyleWithColumns(40));
12155   verifyFormat("[[SessionService sharedService]\n"
12156                "    loadWindowWithCompletionBlock: //\n"
12157                "        ^(SessionWindow *window) {\n"
12158                "          if (window) {\n"
12159                "            [self windowDidLoad:window];\n"
12160                "          } else {\n"
12161                "            [self errorLoadingWindow];\n"
12162                "          }\n"
12163                "        }];",
12164                getLLVMStyleWithColumns(60));
12165   verifyFormat("[myObject doSomethingWith:arg1\n"
12166                "    firstBlock:^(Foo *a) {\n"
12167                "      // ...\n"
12168                "      int i;\n"
12169                "    }\n"
12170                "    secondBlock:^(Bar *b) {\n"
12171                "      // ...\n"
12172                "      int i;\n"
12173                "    }\n"
12174                "    thirdBlock:^Foo(Bar *b) {\n"
12175                "      // ...\n"
12176                "      int i;\n"
12177                "    }];");
12178   verifyFormat("[myObject doSomethingWith:arg1\n"
12179                "               firstBlock:-1\n"
12180                "              secondBlock:^(Bar *b) {\n"
12181                "                // ...\n"
12182                "                int i;\n"
12183                "              }];");
12184 
12185   verifyFormat("f(^{\n"
12186                "  @autoreleasepool {\n"
12187                "    if (a) {\n"
12188                "      g();\n"
12189                "    }\n"
12190                "  }\n"
12191                "});");
12192   verifyFormat("Block b = ^int *(A *a, B *b) {}");
12193   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
12194                "};");
12195 
12196   FormatStyle FourIndent = getLLVMStyle();
12197   FourIndent.ObjCBlockIndentWidth = 4;
12198   verifyFormat("[operation setCompletionBlock:^{\n"
12199                "    [self onOperationDone];\n"
12200                "}];",
12201                FourIndent);
12202 }
12203 
12204 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
12205   FormatStyle ZeroColumn = getLLVMStyle();
12206   ZeroColumn.ColumnLimit = 0;
12207 
12208   verifyFormat("[[SessionService sharedService] "
12209                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12210                "  if (window) {\n"
12211                "    [self windowDidLoad:window];\n"
12212                "  } else {\n"
12213                "    [self errorLoadingWindow];\n"
12214                "  }\n"
12215                "}];",
12216                ZeroColumn);
12217   EXPECT_EQ("[[SessionService sharedService]\n"
12218             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12219             "      if (window) {\n"
12220             "        [self windowDidLoad:window];\n"
12221             "      } else {\n"
12222             "        [self errorLoadingWindow];\n"
12223             "      }\n"
12224             "    }];",
12225             format("[[SessionService sharedService]\n"
12226                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12227                    "                if (window) {\n"
12228                    "    [self windowDidLoad:window];\n"
12229                    "  } else {\n"
12230                    "    [self errorLoadingWindow];\n"
12231                    "  }\n"
12232                    "}];",
12233                    ZeroColumn));
12234   verifyFormat("[myObject doSomethingWith:arg1\n"
12235                "    firstBlock:^(Foo *a) {\n"
12236                "      // ...\n"
12237                "      int i;\n"
12238                "    }\n"
12239                "    secondBlock:^(Bar *b) {\n"
12240                "      // ...\n"
12241                "      int i;\n"
12242                "    }\n"
12243                "    thirdBlock:^Foo(Bar *b) {\n"
12244                "      // ...\n"
12245                "      int i;\n"
12246                "    }];",
12247                ZeroColumn);
12248   verifyFormat("f(^{\n"
12249                "  @autoreleasepool {\n"
12250                "    if (a) {\n"
12251                "      g();\n"
12252                "    }\n"
12253                "  }\n"
12254                "});",
12255                ZeroColumn);
12256   verifyFormat("void (^largeBlock)(void) = ^{\n"
12257                "  // ...\n"
12258                "};",
12259                ZeroColumn);
12260 
12261   ZeroColumn.AllowShortBlocksOnASingleLine = true;
12262   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
12263             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12264   ZeroColumn.AllowShortBlocksOnASingleLine = false;
12265   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
12266             "  int i;\n"
12267             "};",
12268             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12269 }
12270 
12271 TEST_F(FormatTest, SupportsCRLF) {
12272   EXPECT_EQ("int a;\r\n"
12273             "int b;\r\n"
12274             "int c;\r\n",
12275             format("int a;\r\n"
12276                    "  int b;\r\n"
12277                    "    int c;\r\n",
12278                    getLLVMStyle()));
12279   EXPECT_EQ("int a;\r\n"
12280             "int b;\r\n"
12281             "int c;\r\n",
12282             format("int a;\r\n"
12283                    "  int b;\n"
12284                    "    int c;\r\n",
12285                    getLLVMStyle()));
12286   EXPECT_EQ("int a;\n"
12287             "int b;\n"
12288             "int c;\n",
12289             format("int a;\r\n"
12290                    "  int b;\n"
12291                    "    int c;\n",
12292                    getLLVMStyle()));
12293   EXPECT_EQ("\"aaaaaaa \"\r\n"
12294             "\"bbbbbbb\";\r\n",
12295             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
12296   EXPECT_EQ("#define A \\\r\n"
12297             "  b;      \\\r\n"
12298             "  c;      \\\r\n"
12299             "  d;\r\n",
12300             format("#define A \\\r\n"
12301                    "  b; \\\r\n"
12302                    "  c; d; \r\n",
12303                    getGoogleStyle()));
12304 
12305   EXPECT_EQ("/*\r\n"
12306             "multi line block comments\r\n"
12307             "should not introduce\r\n"
12308             "an extra carriage return\r\n"
12309             "*/\r\n",
12310             format("/*\r\n"
12311                    "multi line block comments\r\n"
12312                    "should not introduce\r\n"
12313                    "an extra carriage return\r\n"
12314                    "*/\r\n"));
12315 }
12316 
12317 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
12318   verifyFormat("MY_CLASS(C) {\n"
12319                "  int i;\n"
12320                "  int j;\n"
12321                "};");
12322 }
12323 
12324 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
12325   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
12326   TwoIndent.ContinuationIndentWidth = 2;
12327 
12328   EXPECT_EQ("int i =\n"
12329             "  longFunction(\n"
12330             "    arg);",
12331             format("int i = longFunction(arg);", TwoIndent));
12332 
12333   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
12334   SixIndent.ContinuationIndentWidth = 6;
12335 
12336   EXPECT_EQ("int i =\n"
12337             "      longFunction(\n"
12338             "            arg);",
12339             format("int i = longFunction(arg);", SixIndent));
12340 }
12341 
12342 TEST_F(FormatTest, SpacesInAngles) {
12343   FormatStyle Spaces = getLLVMStyle();
12344   Spaces.SpacesInAngles = true;
12345 
12346   verifyFormat("static_cast< int >(arg);", Spaces);
12347   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
12348   verifyFormat("f< int, float >();", Spaces);
12349   verifyFormat("template <> g() {}", Spaces);
12350   verifyFormat("template < std::vector< int > > f() {}", Spaces);
12351   verifyFormat("std::function< void(int, int) > fct;", Spaces);
12352   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
12353                Spaces);
12354 
12355   Spaces.Standard = FormatStyle::LS_Cpp03;
12356   Spaces.SpacesInAngles = true;
12357   verifyFormat("A< A< int > >();", Spaces);
12358 
12359   Spaces.SpacesInAngles = false;
12360   verifyFormat("A<A<int> >();", Spaces);
12361 
12362   Spaces.Standard = FormatStyle::LS_Cpp11;
12363   Spaces.SpacesInAngles = true;
12364   verifyFormat("A< A< int > >();", Spaces);
12365 
12366   Spaces.SpacesInAngles = false;
12367   verifyFormat("A<A<int>>();", Spaces);
12368 }
12369 
12370 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
12371   FormatStyle Style = getLLVMStyle();
12372   Style.SpaceAfterTemplateKeyword = false;
12373   verifyFormat("template<int> void foo();", Style);
12374 }
12375 
12376 TEST_F(FormatTest, TripleAngleBrackets) {
12377   verifyFormat("f<<<1, 1>>>();");
12378   verifyFormat("f<<<1, 1, 1, s>>>();");
12379   verifyFormat("f<<<a, b, c, d>>>();");
12380   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
12381   verifyFormat("f<param><<<1, 1>>>();");
12382   verifyFormat("f<1><<<1, 1>>>();");
12383   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
12384   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12385                "aaaaaaaaaaa<<<\n    1, 1>>>();");
12386   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
12387                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
12388 }
12389 
12390 TEST_F(FormatTest, MergeLessLessAtEnd) {
12391   verifyFormat("<<");
12392   EXPECT_EQ("< < <", format("\\\n<<<"));
12393   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12394                "aaallvm::outs() <<");
12395   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12396                "aaaallvm::outs()\n    <<");
12397 }
12398 
12399 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
12400   std::string code = "#if A\n"
12401                      "#if B\n"
12402                      "a.\n"
12403                      "#endif\n"
12404                      "    a = 1;\n"
12405                      "#else\n"
12406                      "#endif\n"
12407                      "#if C\n"
12408                      "#else\n"
12409                      "#endif\n";
12410   EXPECT_EQ(code, format(code));
12411 }
12412 
12413 TEST_F(FormatTest, HandleConflictMarkers) {
12414   // Git/SVN conflict markers.
12415   EXPECT_EQ("int a;\n"
12416             "void f() {\n"
12417             "  callme(some(parameter1,\n"
12418             "<<<<<<< text by the vcs\n"
12419             "              parameter2),\n"
12420             "||||||| text by the vcs\n"
12421             "              parameter2),\n"
12422             "         parameter3,\n"
12423             "======= text by the vcs\n"
12424             "              parameter2, parameter3),\n"
12425             ">>>>>>> text by the vcs\n"
12426             "         otherparameter);\n",
12427             format("int a;\n"
12428                    "void f() {\n"
12429                    "  callme(some(parameter1,\n"
12430                    "<<<<<<< text by the vcs\n"
12431                    "  parameter2),\n"
12432                    "||||||| text by the vcs\n"
12433                    "  parameter2),\n"
12434                    "  parameter3,\n"
12435                    "======= text by the vcs\n"
12436                    "  parameter2,\n"
12437                    "  parameter3),\n"
12438                    ">>>>>>> text by the vcs\n"
12439                    "  otherparameter);\n"));
12440 
12441   // Perforce markers.
12442   EXPECT_EQ("void f() {\n"
12443             "  function(\n"
12444             ">>>> text by the vcs\n"
12445             "      parameter,\n"
12446             "==== text by the vcs\n"
12447             "      parameter,\n"
12448             "==== text by the vcs\n"
12449             "      parameter,\n"
12450             "<<<< text by the vcs\n"
12451             "      parameter);\n",
12452             format("void f() {\n"
12453                    "  function(\n"
12454                    ">>>> text by the vcs\n"
12455                    "  parameter,\n"
12456                    "==== text by the vcs\n"
12457                    "  parameter,\n"
12458                    "==== text by the vcs\n"
12459                    "  parameter,\n"
12460                    "<<<< text by the vcs\n"
12461                    "  parameter);\n"));
12462 
12463   EXPECT_EQ("<<<<<<<\n"
12464             "|||||||\n"
12465             "=======\n"
12466             ">>>>>>>",
12467             format("<<<<<<<\n"
12468                    "|||||||\n"
12469                    "=======\n"
12470                    ">>>>>>>"));
12471 
12472   EXPECT_EQ("<<<<<<<\n"
12473             "|||||||\n"
12474             "int i;\n"
12475             "=======\n"
12476             ">>>>>>>",
12477             format("<<<<<<<\n"
12478                    "|||||||\n"
12479                    "int i;\n"
12480                    "=======\n"
12481                    ">>>>>>>"));
12482 
12483   // FIXME: Handle parsing of macros around conflict markers correctly:
12484   EXPECT_EQ("#define Macro \\\n"
12485             "<<<<<<<\n"
12486             "Something \\\n"
12487             "|||||||\n"
12488             "Else \\\n"
12489             "=======\n"
12490             "Other \\\n"
12491             ">>>>>>>\n"
12492             "    End int i;\n",
12493             format("#define Macro \\\n"
12494                    "<<<<<<<\n"
12495                    "  Something \\\n"
12496                    "|||||||\n"
12497                    "  Else \\\n"
12498                    "=======\n"
12499                    "  Other \\\n"
12500                    ">>>>>>>\n"
12501                    "  End\n"
12502                    "int i;\n"));
12503 }
12504 
12505 TEST_F(FormatTest, DisableRegions) {
12506   EXPECT_EQ("int i;\n"
12507             "// clang-format off\n"
12508             "  int j;\n"
12509             "// clang-format on\n"
12510             "int k;",
12511             format(" int  i;\n"
12512                    "   // clang-format off\n"
12513                    "  int j;\n"
12514                    " // clang-format on\n"
12515                    "   int   k;"));
12516   EXPECT_EQ("int i;\n"
12517             "/* clang-format off */\n"
12518             "  int j;\n"
12519             "/* clang-format on */\n"
12520             "int k;",
12521             format(" int  i;\n"
12522                    "   /* clang-format off */\n"
12523                    "  int j;\n"
12524                    " /* clang-format on */\n"
12525                    "   int   k;"));
12526 
12527   // Don't reflow comments within disabled regions.
12528   EXPECT_EQ(
12529       "// clang-format off\n"
12530       "// long long long long long long line\n"
12531       "/* clang-format on */\n"
12532       "/* long long long\n"
12533       " * long long long\n"
12534       " * line */\n"
12535       "int i;\n"
12536       "/* clang-format off */\n"
12537       "/* long long long long long long line */\n",
12538       format("// clang-format off\n"
12539              "// long long long long long long line\n"
12540              "/* clang-format on */\n"
12541              "/* long long long long long long line */\n"
12542              "int i;\n"
12543              "/* clang-format off */\n"
12544              "/* long long long long long long line */\n",
12545              getLLVMStyleWithColumns(20)));
12546 }
12547 
12548 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12549   format("? ) =");
12550   verifyNoCrash("#define a\\\n /**/}");
12551 }
12552 
12553 TEST_F(FormatTest, FormatsTableGenCode) {
12554   FormatStyle Style = getLLVMStyle();
12555   Style.Language = FormatStyle::LK_TableGen;
12556   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12557 }
12558 
12559 TEST_F(FormatTest, ArrayOfTemplates) {
12560   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12561             format("auto a = new unique_ptr<int > [ 10];"));
12562 
12563   FormatStyle Spaces = getLLVMStyle();
12564   Spaces.SpacesInSquareBrackets = true;
12565   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12566             format("auto a = new unique_ptr<int > [10];", Spaces));
12567 }
12568 
12569 TEST_F(FormatTest, ArrayAsTemplateType) {
12570   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12571             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12572 
12573   FormatStyle Spaces = getLLVMStyle();
12574   Spaces.SpacesInSquareBrackets = true;
12575   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12576             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12577 }
12578 
12579 TEST_F(FormatTest, NoSpaceAfterSuper) {
12580     verifyFormat("__super::FooBar();");
12581 }
12582 
12583 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12584   llvm::vfs::InMemoryFileSystem FS;
12585   auto Style1 = getStyle("file", "", "Google", "", &FS);
12586   ASSERT_TRUE((bool)Style1);
12587   ASSERT_EQ(*Style1, getGoogleStyle());
12588 }
12589 
12590 TEST(FormatStyle, GetStyleOfFile) {
12591   llvm::vfs::InMemoryFileSystem FS;
12592   // Test 1: format file in the same directory.
12593   ASSERT_TRUE(
12594       FS.addFile("/a/.clang-format", 0,
12595                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12596   ASSERT_TRUE(
12597       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12598   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12599   ASSERT_TRUE((bool)Style1);
12600   ASSERT_EQ(*Style1, getLLVMStyle());
12601 
12602   // Test 2.1: fallback to default.
12603   ASSERT_TRUE(
12604       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12605   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12606   ASSERT_TRUE((bool)Style2);
12607   ASSERT_EQ(*Style2, getMozillaStyle());
12608 
12609   // Test 2.2: no format on 'none' fallback style.
12610   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12611   ASSERT_TRUE((bool)Style2);
12612   ASSERT_EQ(*Style2, getNoStyle());
12613 
12614   // Test 2.3: format if config is found with no based style while fallback is
12615   // 'none'.
12616   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12617                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12618   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12619   ASSERT_TRUE((bool)Style2);
12620   ASSERT_EQ(*Style2, getLLVMStyle());
12621 
12622   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12623   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12624   ASSERT_TRUE((bool)Style2);
12625   ASSERT_EQ(*Style2, getLLVMStyle());
12626 
12627   // Test 3: format file in parent directory.
12628   ASSERT_TRUE(
12629       FS.addFile("/c/.clang-format", 0,
12630                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12631   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12632                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12633   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12634   ASSERT_TRUE((bool)Style3);
12635   ASSERT_EQ(*Style3, getGoogleStyle());
12636 
12637   // Test 4: error on invalid fallback style
12638   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12639   ASSERT_FALSE((bool)Style4);
12640   llvm::consumeError(Style4.takeError());
12641 
12642   // Test 5: error on invalid yaml on command line
12643   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12644   ASSERT_FALSE((bool)Style5);
12645   llvm::consumeError(Style5.takeError());
12646 
12647   // Test 6: error on invalid style
12648   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12649   ASSERT_FALSE((bool)Style6);
12650   llvm::consumeError(Style6.takeError());
12651 
12652   // Test 7: found config file, error on parsing it
12653   ASSERT_TRUE(
12654       FS.addFile("/d/.clang-format", 0,
12655                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
12656                                                   "InvalidKey: InvalidValue")));
12657   ASSERT_TRUE(
12658       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12659   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
12660   ASSERT_FALSE((bool)Style7);
12661   llvm::consumeError(Style7.takeError());
12662 }
12663 
12664 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
12665   // Column limit is 20.
12666   std::string Code = "Type *a =\n"
12667                      "    new Type();\n"
12668                      "g(iiiii, 0, jjjjj,\n"
12669                      "  0, kkkkk, 0, mm);\n"
12670                      "int  bad     = format   ;";
12671   std::string Expected = "auto a = new Type();\n"
12672                          "g(iiiii, nullptr,\n"
12673                          "  jjjjj, nullptr,\n"
12674                          "  kkkkk, nullptr,\n"
12675                          "  mm);\n"
12676                          "int  bad     = format   ;";
12677   FileID ID = Context.createInMemoryFile("format.cpp", Code);
12678   tooling::Replacements Replaces = toReplacements(
12679       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
12680                             "auto "),
12681        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12682                             "nullptr"),
12683        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12684                             "nullptr"),
12685        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12686                             "nullptr")});
12687 
12688   format::FormatStyle Style = format::getLLVMStyle();
12689   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12690   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12691   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12692       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12693   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12694   EXPECT_TRUE(static_cast<bool>(Result));
12695   EXPECT_EQ(Expected, *Result);
12696 }
12697 
12698 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12699   std::string Code = "#include \"a.h\"\n"
12700                      "#include \"c.h\"\n"
12701                      "\n"
12702                      "int main() {\n"
12703                      "  return 0;\n"
12704                      "}";
12705   std::string Expected = "#include \"a.h\"\n"
12706                          "#include \"b.h\"\n"
12707                          "#include \"c.h\"\n"
12708                          "\n"
12709                          "int main() {\n"
12710                          "  return 0;\n"
12711                          "}";
12712   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12713   tooling::Replacements Replaces = toReplacements(
12714       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12715                             "#include \"b.h\"\n")});
12716 
12717   format::FormatStyle Style = format::getLLVMStyle();
12718   Style.SortIncludes = true;
12719   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12720   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12721       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12722   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12723   EXPECT_TRUE(static_cast<bool>(Result));
12724   EXPECT_EQ(Expected, *Result);
12725 }
12726 
12727 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12728   EXPECT_EQ("using std::cin;\n"
12729             "using std::cout;",
12730             format("using std::cout;\n"
12731                    "using std::cin;", getGoogleStyle()));
12732 }
12733 
12734 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12735   format::FormatStyle Style = format::getLLVMStyle();
12736   Style.Standard = FormatStyle::LS_Cpp03;
12737   // cpp03 recognize this string as identifier u8 and literal character 'a'
12738   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12739 }
12740 
12741 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12742   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12743   // all modes, including C++11, C++14 and C++17
12744   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12745 }
12746 
12747 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12748   EXPECT_EQ("<!-- ;> -->",
12749             format("<!-- ;> -->", getGoogleStyle()));
12750   EXPECT_EQ(" <!-- >; -->",
12751             format(" <!-- >; -->", getGoogleStyle()));
12752 }
12753 
12754 TEST_F(FormatTest, StructuredBindings) {
12755   // Structured bindings is a C++17 feature.
12756   // all modes, including C++11, C++14 and C++17
12757   verifyFormat("auto [a, b] = f();");
12758   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12759   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12760   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12761   EXPECT_EQ("auto const volatile [a, b] = f();",
12762             format("auto  const   volatile[a, b] = f();"));
12763   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12764   EXPECT_EQ("auto &[a, b, c] = f();",
12765             format("auto   &[  a  ,  b,c   ] = f();"));
12766   EXPECT_EQ("auto &&[a, b, c] = f();",
12767             format("auto   &&[  a  ,  b,c   ] = f();"));
12768   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12769   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12770             format("auto  const  volatile  &&[a, b] = f();"));
12771   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12772   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12773   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12774             format("const  auto   volatile  &&[a, b] = f();"));
12775   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12776             format("volatile  const  auto   &&[a, b] = f();"));
12777   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12778 
12779   // Make sure we don't mistake structured bindings for lambdas.
12780   FormatStyle PointerMiddle = getLLVMStyle();
12781   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12782   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12783   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12784   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12785   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12786   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12787   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12788   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12789   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12790   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12791   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12792   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12793   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12794 
12795   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12796             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12797   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12798             format("for (const auto   &   [a, b] : some_range) {\n}"));
12799   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12800             format("for (const auto[a, b] : some_range) {\n}"));
12801   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12802   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12803   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12804   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12805   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12806   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12807   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12808   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12809 
12810   format::FormatStyle Spaces = format::getLLVMStyle();
12811   Spaces.SpacesInSquareBrackets = true;
12812   verifyFormat("auto [ a, b ] = f();", Spaces);
12813   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12814   verifyFormat("auto &[ a, b ] = f();", Spaces);
12815   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12816   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12817 }
12818 
12819 TEST_F(FormatTest, FileAndCode) {
12820   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12821   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12822   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12823   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12824   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12825   EXPECT_EQ(
12826       FormatStyle::LK_ObjC,
12827       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12828   EXPECT_EQ(FormatStyle::LK_ObjC,
12829             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12830   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12831   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12832   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12833   EXPECT_EQ(FormatStyle::LK_ObjC,
12834             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12835   EXPECT_EQ(
12836       FormatStyle::LK_ObjC,
12837       guessLanguage("foo.h",
12838                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12839   EXPECT_EQ(
12840       FormatStyle::LK_Cpp,
12841       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12842 }
12843 
12844 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12845   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12846   EXPECT_EQ(FormatStyle::LK_ObjC,
12847             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12848   EXPECT_EQ(FormatStyle::LK_Cpp,
12849             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12850   EXPECT_EQ(
12851       FormatStyle::LK_Cpp,
12852       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12853   EXPECT_EQ(FormatStyle::LK_ObjC,
12854             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12855   EXPECT_EQ(FormatStyle::LK_Cpp,
12856             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12857   EXPECT_EQ(FormatStyle::LK_ObjC,
12858             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12859   EXPECT_EQ(FormatStyle::LK_Cpp,
12860             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12861   EXPECT_EQ(FormatStyle::LK_Cpp,
12862             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12863   EXPECT_EQ(FormatStyle::LK_ObjC,
12864             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12865   EXPECT_EQ(FormatStyle::LK_Cpp,
12866             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12867   EXPECT_EQ(
12868       FormatStyle::LK_Cpp,
12869       guessLanguage("foo.h",
12870                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12871   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12872 }
12873 
12874 TEST_F(FormatTest, GuessLanguageWithCaret) {
12875   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12876   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12877   EXPECT_EQ(FormatStyle::LK_ObjC,
12878             guessLanguage("foo.h", "int(^)(char, float);"));
12879   EXPECT_EQ(FormatStyle::LK_ObjC,
12880             guessLanguage("foo.h", "int(^foo)(char, float);"));
12881   EXPECT_EQ(FormatStyle::LK_ObjC,
12882             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12883   EXPECT_EQ(FormatStyle::LK_ObjC,
12884             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12885   EXPECT_EQ(
12886       FormatStyle::LK_ObjC,
12887       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12888 }
12889 
12890 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
12891   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12892                                                "void f() {\n"
12893                                                "  asm (\"mov %[e], %[d]\"\n"
12894                                                "     : [d] \"=rm\" (d)\n"
12895                                                "       [e] \"rm\" (*e));\n"
12896                                                "}"));
12897   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12898                                                "void f() {\n"
12899                                                "  _asm (\"mov %[e], %[d]\"\n"
12900                                                "     : [d] \"=rm\" (d)\n"
12901                                                "       [e] \"rm\" (*e));\n"
12902                                                "}"));
12903   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12904                                                "void f() {\n"
12905                                                "  __asm (\"mov %[e], %[d]\"\n"
12906                                                "     : [d] \"=rm\" (d)\n"
12907                                                "       [e] \"rm\" (*e));\n"
12908                                                "}"));
12909   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12910                                                "void f() {\n"
12911                                                "  __asm__ (\"mov %[e], %[d]\"\n"
12912                                                "     : [d] \"=rm\" (d)\n"
12913                                                "       [e] \"rm\" (*e));\n"
12914                                                "}"));
12915   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
12916                                                "void f() {\n"
12917                                                "  asm (\"mov %[e], %[d]\"\n"
12918                                                "     : [d] \"=rm\" (d),\n"
12919                                                "       [e] \"rm\" (*e));\n"
12920                                                "}"));
12921   EXPECT_EQ(FormatStyle::LK_Cpp,
12922             guessLanguage("foo.h", "void f() {\n"
12923                                    "  asm volatile (\"mov %[e], %[d]\"\n"
12924                                    "     : [d] \"=rm\" (d)\n"
12925                                    "       [e] \"rm\" (*e));\n"
12926                                    "}"));
12927 }
12928 
12929 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12930   EXPECT_EQ(FormatStyle::LK_Cpp,
12931             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12932   EXPECT_EQ(FormatStyle::LK_ObjC,
12933             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12934   EXPECT_EQ(
12935       FormatStyle::LK_Cpp,
12936       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12937   EXPECT_EQ(
12938       FormatStyle::LK_ObjC,
12939       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12940 }
12941 
12942 } // end namespace
12943 } // end namespace format
12944 } // end namespace clang
12945