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 (true)\n"
430                "  f();\ng();");
431   verifyFormat("if constexpr (a)\n"
432                "  if constexpr (b)\n"
433                "    if constexpr (c)\n"
434                "      g();\n"
435                "h();");
436   verifyFormat("if CONSTEXPR (a)\n"
437                "  if CONSTEXPR (b)\n"
438                "    if CONSTEXPR (c)\n"
439                "      g();\n"
440                "h();");
441   verifyFormat("if constexpr (a)\n"
442                "  if constexpr (b) {\n"
443                "    f();\n"
444                "  }\n"
445                "g();");
446   verifyFormat("if CONSTEXPR (a)\n"
447                "  if CONSTEXPR (b) {\n"
448                "    f();\n"
449                "  }\n"
450                "g();");
451 
452   FormatStyle AllowsMergedIf = getLLVMStyle();
453   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
454   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
455       FormatStyle::SIS_WithoutElse;
456   verifyFormat("if (a)\n"
457                "  // comment\n"
458                "  f();",
459                AllowsMergedIf);
460   verifyFormat("{\n"
461                "  if (a)\n"
462                "  label:\n"
463                "    f();\n"
464                "}",
465                AllowsMergedIf);
466   verifyFormat("#define A \\\n"
467                "  if (a)  \\\n"
468                "  label:  \\\n"
469                "    f()",
470                AllowsMergedIf);
471   verifyFormat("if (a)\n"
472                "  ;",
473                AllowsMergedIf);
474   verifyFormat("if (a)\n"
475                "  if (b) return;",
476                AllowsMergedIf);
477 
478   verifyFormat("if (a) // Can't merge this\n"
479                "  f();\n",
480                AllowsMergedIf);
481   verifyFormat("if (a) /* still don't merge */\n"
482                "  f();",
483                AllowsMergedIf);
484   verifyFormat("if (a) { // Never merge this\n"
485                "  f();\n"
486                "}",
487                AllowsMergedIf);
488   verifyFormat("if (a) { /* Never merge this */\n"
489                "  f();\n"
490                "}",
491                AllowsMergedIf);
492 
493   AllowsMergedIf.ColumnLimit = 14;
494   verifyFormat("if (a) return;", AllowsMergedIf);
495   verifyFormat("if (aaaaaaaaa)\n"
496                "  return;",
497                AllowsMergedIf);
498 
499   AllowsMergedIf.ColumnLimit = 13;
500   verifyFormat("if (a)\n  return;", AllowsMergedIf);
501 }
502 
503 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
504   FormatStyle AllowsMergedIf = getLLVMStyle();
505   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
506   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
507       FormatStyle::SIS_WithoutElse;
508   verifyFormat("if (a)\n"
509                "  f();\n"
510                "else {\n"
511                "  g();\n"
512                "}",
513                AllowsMergedIf);
514   verifyFormat("if (a)\n"
515                "  f();\n"
516                "else\n"
517                "  g();\n",
518                AllowsMergedIf);
519 
520   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
521 
522   verifyFormat("if (a) f();\n"
523                "else {\n"
524                "  g();\n"
525                "}",
526                AllowsMergedIf);
527   verifyFormat("if (a) f();\n"
528                "else {\n"
529                "  if (a) f();\n"
530                "  else {\n"
531                "    g();\n"
532                "  }\n"
533                "  g();\n"
534                "}",
535                AllowsMergedIf);
536 }
537 
538 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
539   FormatStyle AllowsMergedLoops = getLLVMStyle();
540   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
541   verifyFormat("while (true) continue;", AllowsMergedLoops);
542   verifyFormat("for (;;) continue;", AllowsMergedLoops);
543   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
544   verifyFormat("while (true)\n"
545                "  ;",
546                AllowsMergedLoops);
547   verifyFormat("for (;;)\n"
548                "  ;",
549                AllowsMergedLoops);
550   verifyFormat("for (;;)\n"
551                "  for (;;) continue;",
552                AllowsMergedLoops);
553   verifyFormat("for (;;) // Can't merge this\n"
554                "  continue;",
555                AllowsMergedLoops);
556   verifyFormat("for (;;) /* still don't merge */\n"
557                "  continue;",
558                AllowsMergedLoops);
559 }
560 
561 TEST_F(FormatTest, FormatShortBracedStatements) {
562   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
563   AllowSimpleBracedStatements.ColumnLimit = 40;
564   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
565 
566   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
567       FormatStyle::SIS_WithoutElse;
568   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
569 
570   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
571   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
572   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
573 
574   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
575   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
576   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
577   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
578   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
579   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
580   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
581   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
582   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
583   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
584   verifyFormat("if (true) {\n"
585                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
586                "}",
587                AllowSimpleBracedStatements);
588   verifyFormat("if (true) { //\n"
589                "  f();\n"
590                "}",
591                AllowSimpleBracedStatements);
592   verifyFormat("if (true) {\n"
593                "  f();\n"
594                "  f();\n"
595                "}",
596                AllowSimpleBracedStatements);
597   verifyFormat("if (true) {\n"
598                "  f();\n"
599                "} else {\n"
600                "  f();\n"
601                "}",
602                AllowSimpleBracedStatements);
603 
604   verifyFormat("struct A2 {\n"
605                "  int X;\n"
606                "};",
607                AllowSimpleBracedStatements);
608   verifyFormat("typedef struct A2 {\n"
609                "  int X;\n"
610                "} A2_t;",
611                AllowSimpleBracedStatements);
612   verifyFormat("template <int> struct A2 {\n"
613                "  struct B {};\n"
614                "};",
615                AllowSimpleBracedStatements);
616 
617   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
618       FormatStyle::SIS_Never;
619   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
620   verifyFormat("if (true) {\n"
621                "  f();\n"
622                "}",
623                AllowSimpleBracedStatements);
624   verifyFormat("if (true) {\n"
625                "  f();\n"
626                "} else {\n"
627                "  f();\n"
628                "}",
629                AllowSimpleBracedStatements);
630 
631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
632   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
633   verifyFormat("while (true) {\n"
634                "  f();\n"
635                "}",
636                AllowSimpleBracedStatements);
637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
638   verifyFormat("for (;;) {\n"
639                "  f();\n"
640                "}",
641                AllowSimpleBracedStatements);
642 
643   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
644       FormatStyle::SIS_WithoutElse;
645   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
646   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
647 
648   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
649   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
650   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
651   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
652   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
653   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
654   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
655   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
656   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
657   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
658   verifyFormat("if (true)\n"
659                "{\n"
660                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
661                "}",
662                AllowSimpleBracedStatements);
663   verifyFormat("if (true)\n"
664                "{ //\n"
665                "  f();\n"
666                "}",
667                AllowSimpleBracedStatements);
668   verifyFormat("if (true)\n"
669                "{\n"
670                "  f();\n"
671                "  f();\n"
672                "}",
673                AllowSimpleBracedStatements);
674   verifyFormat("if (true)\n"
675                "{\n"
676                "  f();\n"
677                "} else\n"
678                "{\n"
679                "  f();\n"
680                "}",
681                AllowSimpleBracedStatements);
682 
683   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
684       FormatStyle::SIS_Never;
685   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
686   verifyFormat("if (true)\n"
687                "{\n"
688                "  f();\n"
689                "}",
690                AllowSimpleBracedStatements);
691   verifyFormat("if (true)\n"
692                "{\n"
693                "  f();\n"
694                "} else\n"
695                "{\n"
696                "  f();\n"
697                "}",
698                AllowSimpleBracedStatements);
699 
700   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
701   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
702   verifyFormat("while (true)\n"
703                "{\n"
704                "  f();\n"
705                "}",
706                AllowSimpleBracedStatements);
707   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
708   verifyFormat("for (;;)\n"
709                "{\n"
710                "  f();\n"
711                "}",
712                AllowSimpleBracedStatements);
713 }
714 
715 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
716   FormatStyle Style = getLLVMStyleWithColumns(60);
717   Style.AllowShortBlocksOnASingleLine = true;
718   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
719   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
720   EXPECT_EQ("#define A                                                  \\\n"
721             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
722             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
723             "X;",
724             format("#define A \\\n"
725                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
726                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
727                    "   }\n"
728                    "X;",
729                    Style));
730 }
731 
732 TEST_F(FormatTest, ParseIfElse) {
733   verifyFormat("if (true)\n"
734                "  if (true)\n"
735                "    if (true)\n"
736                "      f();\n"
737                "    else\n"
738                "      g();\n"
739                "  else\n"
740                "    h();\n"
741                "else\n"
742                "  i();");
743   verifyFormat("if (true)\n"
744                "  if (true)\n"
745                "    if (true) {\n"
746                "      if (true)\n"
747                "        f();\n"
748                "    } else {\n"
749                "      g();\n"
750                "    }\n"
751                "  else\n"
752                "    h();\n"
753                "else {\n"
754                "  i();\n"
755                "}");
756   verifyFormat("if (true)\n"
757                "  if constexpr (true)\n"
758                "    if (true) {\n"
759                "      if constexpr (true)\n"
760                "        f();\n"
761                "    } else {\n"
762                "      g();\n"
763                "    }\n"
764                "  else\n"
765                "    h();\n"
766                "else {\n"
767                "  i();\n"
768                "}");
769   verifyFormat("if (true)\n"
770                "  if CONSTEXPR (true)\n"
771                "    if (true) {\n"
772                "      if CONSTEXPR (true)\n"
773                "        f();\n"
774                "    } else {\n"
775                "      g();\n"
776                "    }\n"
777                "  else\n"
778                "    h();\n"
779                "else {\n"
780                "  i();\n"
781                "}");
782   verifyFormat("void f() {\n"
783                "  if (a) {\n"
784                "  } else {\n"
785                "  }\n"
786                "}");
787 }
788 
789 TEST_F(FormatTest, ElseIf) {
790   verifyFormat("if (a) {\n} else if (b) {\n}");
791   verifyFormat("if (a)\n"
792                "  f();\n"
793                "else if (b)\n"
794                "  g();\n"
795                "else\n"
796                "  h();");
797   verifyFormat("if constexpr (a)\n"
798                "  f();\n"
799                "else if constexpr (b)\n"
800                "  g();\n"
801                "else\n"
802                "  h();");
803   verifyFormat("if CONSTEXPR (a)\n"
804                "  f();\n"
805                "else if CONSTEXPR (b)\n"
806                "  g();\n"
807                "else\n"
808                "  h();");
809   verifyFormat("if (a) {\n"
810                "  f();\n"
811                "}\n"
812                "// or else ..\n"
813                "else {\n"
814                "  g()\n"
815                "}");
816 
817   verifyFormat("if (a) {\n"
818                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
819                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
820                "}");
821   verifyFormat("if (a) {\n"
822                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
823                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
824                "}");
825   verifyFormat("if (a) {\n"
826                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
827                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
828                "}");
829   verifyFormat("if (a) {\n"
830                "} else if (\n"
831                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
832                "}",
833                getLLVMStyleWithColumns(62));
834   verifyFormat("if (a) {\n"
835                "} else if constexpr (\n"
836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
837                "}",
838                getLLVMStyleWithColumns(62));
839   verifyFormat("if (a) {\n"
840                "} else if CONSTEXPR (\n"
841                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
842                "}",
843                getLLVMStyleWithColumns(62));
844 }
845 
846 TEST_F(FormatTest, FormatsForLoop) {
847   verifyFormat(
848       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
849       "     ++VeryVeryLongLoopVariable)\n"
850       "  ;");
851   verifyFormat("for (;;)\n"
852                "  f();");
853   verifyFormat("for (;;) {\n}");
854   verifyFormat("for (;;) {\n"
855                "  f();\n"
856                "}");
857   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
858 
859   verifyFormat(
860       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
861       "                                          E = UnwrappedLines.end();\n"
862       "     I != E; ++I) {\n}");
863 
864   verifyFormat(
865       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
866       "     ++IIIII) {\n}");
867   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
868                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
869                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
870   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
871                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
872                "         E = FD->getDeclsInPrototypeScope().end();\n"
873                "     I != E; ++I) {\n}");
874   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
875                "         I = Container.begin(),\n"
876                "         E = Container.end();\n"
877                "     I != E; ++I) {\n}",
878                getLLVMStyleWithColumns(76));
879 
880   verifyFormat(
881       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
882       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
883       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
884       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
885       "     ++aaaaaaaaaaa) {\n}");
886   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
887                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
888                "     ++i) {\n}");
889   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
890                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
891                "}");
892   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
893                "         aaaaaaaaaa);\n"
894                "     iter; ++iter) {\n"
895                "}");
896   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
897                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
898                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
899                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
900 
901   // These should not be formatted as Objective-C for-in loops.
902   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
903   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
904   verifyFormat("Foo *x;\nfor (x in y) {\n}");
905   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
906 
907   FormatStyle NoBinPacking = getLLVMStyle();
908   NoBinPacking.BinPackParameters = false;
909   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
910                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
911                "                                           aaaaaaaaaaaaaaaa,\n"
912                "                                           aaaaaaaaaaaaaaaa,\n"
913                "                                           aaaaaaaaaaaaaaaa);\n"
914                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
915                "}",
916                NoBinPacking);
917   verifyFormat(
918       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
919       "                                          E = UnwrappedLines.end();\n"
920       "     I != E;\n"
921       "     ++I) {\n}",
922       NoBinPacking);
923 
924   FormatStyle AlignLeft = getLLVMStyle();
925   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
926   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
927 }
928 
929 TEST_F(FormatTest, RangeBasedForLoops) {
930   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
931                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
932   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
933                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
934   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
935                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
936   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
937                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
938 }
939 
940 TEST_F(FormatTest, ForEachLoops) {
941   verifyFormat("void f() {\n"
942                "  foreach (Item *item, itemlist) {}\n"
943                "  Q_FOREACH (Item *item, itemlist) {}\n"
944                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
945                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
946                "}");
947 
948   // As function-like macros.
949   verifyFormat("#define foreach(x, y)\n"
950                "#define Q_FOREACH(x, y)\n"
951                "#define BOOST_FOREACH(x, y)\n"
952                "#define UNKNOWN_FOREACH(x, y)\n");
953 
954   // Not as function-like macros.
955   verifyFormat("#define foreach (x, y)\n"
956                "#define Q_FOREACH (x, y)\n"
957                "#define BOOST_FOREACH (x, y)\n"
958                "#define UNKNOWN_FOREACH (x, y)\n");
959 }
960 
961 TEST_F(FormatTest, FormatsWhileLoop) {
962   verifyFormat("while (true) {\n}");
963   verifyFormat("while (true)\n"
964                "  f();");
965   verifyFormat("while () {\n}");
966   verifyFormat("while () {\n"
967                "  f();\n"
968                "}");
969 }
970 
971 TEST_F(FormatTest, FormatsDoWhile) {
972   verifyFormat("do {\n"
973                "  do_something();\n"
974                "} while (something());");
975   verifyFormat("do\n"
976                "  do_something();\n"
977                "while (something());");
978 }
979 
980 TEST_F(FormatTest, FormatsSwitchStatement) {
981   verifyFormat("switch (x) {\n"
982                "case 1:\n"
983                "  f();\n"
984                "  break;\n"
985                "case kFoo:\n"
986                "case ns::kBar:\n"
987                "case kBaz:\n"
988                "  break;\n"
989                "default:\n"
990                "  g();\n"
991                "  break;\n"
992                "}");
993   verifyFormat("switch (x) {\n"
994                "case 1: {\n"
995                "  f();\n"
996                "  break;\n"
997                "}\n"
998                "case 2: {\n"
999                "  break;\n"
1000                "}\n"
1001                "}");
1002   verifyFormat("switch (x) {\n"
1003                "case 1: {\n"
1004                "  f();\n"
1005                "  {\n"
1006                "    g();\n"
1007                "    h();\n"
1008                "  }\n"
1009                "  break;\n"
1010                "}\n"
1011                "}");
1012   verifyFormat("switch (x) {\n"
1013                "case 1: {\n"
1014                "  f();\n"
1015                "  if (foo) {\n"
1016                "    g();\n"
1017                "    h();\n"
1018                "  }\n"
1019                "  break;\n"
1020                "}\n"
1021                "}");
1022   verifyFormat("switch (x) {\n"
1023                "case 1: {\n"
1024                "  f();\n"
1025                "  g();\n"
1026                "} break;\n"
1027                "}");
1028   verifyFormat("switch (test)\n"
1029                "  ;");
1030   verifyFormat("switch (x) {\n"
1031                "default: {\n"
1032                "  // Do nothing.\n"
1033                "}\n"
1034                "}");
1035   verifyFormat("switch (x) {\n"
1036                "// comment\n"
1037                "// if 1, do f()\n"
1038                "case 1:\n"
1039                "  f();\n"
1040                "}");
1041   verifyFormat("switch (x) {\n"
1042                "case 1:\n"
1043                "  // Do amazing stuff\n"
1044                "  {\n"
1045                "    f();\n"
1046                "    g();\n"
1047                "  }\n"
1048                "  break;\n"
1049                "}");
1050   verifyFormat("#define A          \\\n"
1051                "  switch (x) {     \\\n"
1052                "  case a:          \\\n"
1053                "    foo = b;       \\\n"
1054                "  }",
1055                getLLVMStyleWithColumns(20));
1056   verifyFormat("#define OPERATION_CASE(name)           \\\n"
1057                "  case OP_name:                        \\\n"
1058                "    return operations::Operation##name\n",
1059                getLLVMStyleWithColumns(40));
1060   verifyFormat("switch (x) {\n"
1061                "case 1:;\n"
1062                "default:;\n"
1063                "  int i;\n"
1064                "}");
1065 
1066   verifyGoogleFormat("switch (x) {\n"
1067                      "  case 1:\n"
1068                      "    f();\n"
1069                      "    break;\n"
1070                      "  case kFoo:\n"
1071                      "  case ns::kBar:\n"
1072                      "  case kBaz:\n"
1073                      "    break;\n"
1074                      "  default:\n"
1075                      "    g();\n"
1076                      "    break;\n"
1077                      "}");
1078   verifyGoogleFormat("switch (x) {\n"
1079                      "  case 1: {\n"
1080                      "    f();\n"
1081                      "    break;\n"
1082                      "  }\n"
1083                      "}");
1084   verifyGoogleFormat("switch (test)\n"
1085                      "  ;");
1086 
1087   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1088                      "  case OP_name:              \\\n"
1089                      "    return operations::Operation##name\n");
1090   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1091                      "  // Get the correction operation class.\n"
1092                      "  switch (OpCode) {\n"
1093                      "    CASE(Add);\n"
1094                      "    CASE(Subtract);\n"
1095                      "    default:\n"
1096                      "      return operations::Unknown;\n"
1097                      "  }\n"
1098                      "#undef OPERATION_CASE\n"
1099                      "}");
1100   verifyFormat("DEBUG({\n"
1101                "  switch (x) {\n"
1102                "  case A:\n"
1103                "    f();\n"
1104                "    break;\n"
1105                "    // fallthrough\n"
1106                "  case B:\n"
1107                "    g();\n"
1108                "    break;\n"
1109                "  }\n"
1110                "});");
1111   EXPECT_EQ("DEBUG({\n"
1112             "  switch (x) {\n"
1113             "  case A:\n"
1114             "    f();\n"
1115             "    break;\n"
1116             "  // On B:\n"
1117             "  case B:\n"
1118             "    g();\n"
1119             "    break;\n"
1120             "  }\n"
1121             "});",
1122             format("DEBUG({\n"
1123                    "  switch (x) {\n"
1124                    "  case A:\n"
1125                    "    f();\n"
1126                    "    break;\n"
1127                    "  // On B:\n"
1128                    "  case B:\n"
1129                    "    g();\n"
1130                    "    break;\n"
1131                    "  }\n"
1132                    "});",
1133                    getLLVMStyle()));
1134   EXPECT_EQ("switch (n) {\n"
1135             "case 0: {\n"
1136             "  return false;\n"
1137             "}\n"
1138             "default: {\n"
1139             "  return true;\n"
1140             "}\n"
1141             "}",
1142             format("switch (n)\n"
1143                    "{\n"
1144                    "case 0: {\n"
1145                    "  return false;\n"
1146                    "}\n"
1147                    "default: {\n"
1148                    "  return true;\n"
1149                    "}\n"
1150                    "}",
1151                    getLLVMStyle()));
1152   verifyFormat("switch (a) {\n"
1153                "case (b):\n"
1154                "  return;\n"
1155                "}");
1156 
1157   verifyFormat("switch (a) {\n"
1158                "case some_namespace::\n"
1159                "    some_constant:\n"
1160                "  return;\n"
1161                "}",
1162                getLLVMStyleWithColumns(34));
1163 
1164   FormatStyle Style = getLLVMStyle();
1165   Style.IndentCaseLabels = true;
1166   Style.AllowShortBlocksOnASingleLine = false;
1167   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1168   Style.BraceWrapping.AfterCaseLabel = true;
1169   Style.BraceWrapping.AfterControlStatement = true;
1170   EXPECT_EQ("switch (n)\n"
1171             "{\n"
1172             "  case 0:\n"
1173             "  {\n"
1174             "    return false;\n"
1175             "  }\n"
1176             "  default:\n"
1177             "  {\n"
1178             "    return true;\n"
1179             "  }\n"
1180             "}",
1181             format("switch (n) {\n"
1182                    "  case 0: {\n"
1183                    "    return false;\n"
1184                    "  }\n"
1185                    "  default: {\n"
1186                    "    return true;\n"
1187                    "  }\n"
1188                    "}",
1189                    Style));
1190   Style.BraceWrapping.AfterCaseLabel = false;
1191   EXPECT_EQ("switch (n)\n"
1192             "{\n"
1193             "  case 0: {\n"
1194             "    return false;\n"
1195             "  }\n"
1196             "  default: {\n"
1197             "    return true;\n"
1198             "  }\n"
1199             "}",
1200             format("switch (n) {\n"
1201                    "  case 0:\n"
1202                    "  {\n"
1203                    "    return false;\n"
1204                    "  }\n"
1205                    "  default:\n"
1206                    "  {\n"
1207                    "    return true;\n"
1208                    "  }\n"
1209                    "}",
1210                    Style));
1211 }
1212 
1213 TEST_F(FormatTest, CaseRanges) {
1214   verifyFormat("switch (x) {\n"
1215                "case 'A' ... 'Z':\n"
1216                "case 1 ... 5:\n"
1217                "case a ... b:\n"
1218                "  break;\n"
1219                "}");
1220 }
1221 
1222 TEST_F(FormatTest, ShortCaseLabels) {
1223   FormatStyle Style = getLLVMStyle();
1224   Style.AllowShortCaseLabelsOnASingleLine = true;
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                "case 6: // comment\n"
1232                "  return;\n"
1233                "case 7:\n"
1234                "  // comment\n"
1235                "  return;\n"
1236                "case 8:\n"
1237                "  x = 8; // comment\n"
1238                "  break;\n"
1239                "default: y = 1; break;\n"
1240                "}",
1241                Style);
1242   verifyFormat("switch (a) {\n"
1243                "case 0: return; // comment\n"
1244                "case 1: break;  // comment\n"
1245                "case 2: return;\n"
1246                "// comment\n"
1247                "case 3: return;\n"
1248                "// comment 1\n"
1249                "// comment 2\n"
1250                "// comment 3\n"
1251                "case 4: break; /* comment */\n"
1252                "case 5:\n"
1253                "  // comment\n"
1254                "  break;\n"
1255                "case 6: /* comment */ x = 1; break;\n"
1256                "case 7: x = /* comment */ 1; break;\n"
1257                "case 8:\n"
1258                "  x = 1; /* comment */\n"
1259                "  break;\n"
1260                "case 9:\n"
1261                "  break; // comment line 1\n"
1262                "         // comment line 2\n"
1263                "}",
1264                Style);
1265   EXPECT_EQ("switch (a) {\n"
1266             "case 1:\n"
1267             "  x = 8;\n"
1268             "  // fall through\n"
1269             "case 2: x = 8;\n"
1270             "// comment\n"
1271             "case 3:\n"
1272             "  return; /* comment line 1\n"
1273             "           * comment line 2 */\n"
1274             "case 4: i = 8;\n"
1275             "// something else\n"
1276             "#if FOO\n"
1277             "case 5: break;\n"
1278             "#endif\n"
1279             "}",
1280             format("switch (a) {\n"
1281                    "case 1: x = 8;\n"
1282                    "  // fall through\n"
1283                    "case 2:\n"
1284                    "  x = 8;\n"
1285                    "// comment\n"
1286                    "case 3:\n"
1287                    "  return; /* comment line 1\n"
1288                    "           * comment line 2 */\n"
1289                    "case 4:\n"
1290                    "  i = 8;\n"
1291                    "// something else\n"
1292                    "#if FOO\n"
1293                    "case 5: break;\n"
1294                    "#endif\n"
1295                    "}",
1296                    Style));
1297   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1298             "  return; // long long long long long long long long long long long long comment\n"
1299             "          // line\n" "}",
1300             format("switch (a) {\n"
1301                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1302                    "}",
1303                    Style));
1304   EXPECT_EQ("switch (a) {\n"
1305             "case 0:\n"
1306             "  return; /* long long long long long long long long long long long long comment\n"
1307             "             line */\n"
1308             "}",
1309             format("switch (a) {\n"
1310                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1311                    "}",
1312                    Style));
1313   verifyFormat("switch (a) {\n"
1314                "#if FOO\n"
1315                "case 0: return 0;\n"
1316                "#endif\n"
1317                "}",
1318                Style);
1319   verifyFormat("switch (a) {\n"
1320                "case 1: {\n"
1321                "}\n"
1322                "case 2: {\n"
1323                "  return;\n"
1324                "}\n"
1325                "case 3: {\n"
1326                "  x = 1;\n"
1327                "  return;\n"
1328                "}\n"
1329                "case 4:\n"
1330                "  if (x)\n"
1331                "    return;\n"
1332                "}",
1333                Style);
1334   Style.ColumnLimit = 21;
1335   verifyFormat("switch (a) {\n"
1336                "case 1: x = 1; break;\n"
1337                "case 2: return;\n"
1338                "case 3:\n"
1339                "case 4:\n"
1340                "case 5: return;\n"
1341                "default:\n"
1342                "  y = 1;\n"
1343                "  break;\n"
1344                "}",
1345                Style);
1346   Style.ColumnLimit = 80;
1347   Style.AllowShortCaseLabelsOnASingleLine = false;
1348   Style.IndentCaseLabels = true;
1349   EXPECT_EQ("switch (n) {\n"
1350             "  default /*comments*/:\n"
1351             "    return true;\n"
1352             "  case 0:\n"
1353             "    return false;\n"
1354             "}",
1355             format("switch (n) {\n"
1356                    "default/*comments*/:\n"
1357                    "  return true;\n"
1358                    "case 0:\n"
1359                    "  return false;\n"
1360                    "}",
1361                    Style));
1362   Style.AllowShortCaseLabelsOnASingleLine = true;
1363   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1364   Style.BraceWrapping.AfterCaseLabel = true;
1365   Style.BraceWrapping.AfterControlStatement = true;
1366   EXPECT_EQ("switch (n)\n"
1367             "{\n"
1368             "  case 0:\n"
1369             "  {\n"
1370             "    return false;\n"
1371             "  }\n"
1372             "  default:\n"
1373             "  {\n"
1374             "    return true;\n"
1375             "  }\n"
1376             "}",
1377             format("switch (n) {\n"
1378                    "  case 0: {\n"
1379                    "    return false;\n"
1380                    "  }\n"
1381                    "  default:\n"
1382                    "  {\n"
1383                    "    return true;\n"
1384                    "  }\n"
1385                    "}",
1386                    Style));
1387 }
1388 
1389 TEST_F(FormatTest, FormatsLabels) {
1390   verifyFormat("void f() {\n"
1391                "  some_code();\n"
1392                "test_label:\n"
1393                "  some_other_code();\n"
1394                "  {\n"
1395                "    some_more_code();\n"
1396                "  another_label:\n"
1397                "    some_more_code();\n"
1398                "  }\n"
1399                "}");
1400   verifyFormat("{\n"
1401                "  some_code();\n"
1402                "test_label:\n"
1403                "  some_other_code();\n"
1404                "}");
1405   verifyFormat("{\n"
1406                "  some_code();\n"
1407                "test_label:;\n"
1408                "  int i = 0;\n"
1409                "}");
1410 }
1411 
1412 //===----------------------------------------------------------------------===//
1413 // Tests for classes, namespaces, etc.
1414 //===----------------------------------------------------------------------===//
1415 
1416 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1417   verifyFormat("class A {};");
1418 }
1419 
1420 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1421   verifyFormat("class A {\n"
1422                "public:\n"
1423                "public: // comment\n"
1424                "protected:\n"
1425                "private:\n"
1426                "  void f() {}\n"
1427                "};");
1428   verifyFormat("export class A {\n"
1429                "public:\n"
1430                "public: // comment\n"
1431                "protected:\n"
1432                "private:\n"
1433                "  void f() {}\n"
1434                "};");
1435   verifyGoogleFormat("class A {\n"
1436                      " public:\n"
1437                      " protected:\n"
1438                      " private:\n"
1439                      "  void f() {}\n"
1440                      "};");
1441   verifyGoogleFormat("export class A {\n"
1442                      " public:\n"
1443                      " protected:\n"
1444                      " private:\n"
1445                      "  void f() {}\n"
1446                      "};");
1447   verifyFormat("class A {\n"
1448                "public slots:\n"
1449                "  void f1() {}\n"
1450                "public Q_SLOTS:\n"
1451                "  void f2() {}\n"
1452                "protected slots:\n"
1453                "  void f3() {}\n"
1454                "protected Q_SLOTS:\n"
1455                "  void f4() {}\n"
1456                "private slots:\n"
1457                "  void f5() {}\n"
1458                "private Q_SLOTS:\n"
1459                "  void f6() {}\n"
1460                "signals:\n"
1461                "  void g1();\n"
1462                "Q_SIGNALS:\n"
1463                "  void g2();\n"
1464                "};");
1465 
1466   // Don't interpret 'signals' the wrong way.
1467   verifyFormat("signals.set();");
1468   verifyFormat("for (Signals signals : f()) {\n}");
1469   verifyFormat("{\n"
1470                "  signals.set(); // This needs indentation.\n"
1471                "}");
1472   verifyFormat("void f() {\n"
1473                "label:\n"
1474                "  signals.baz();\n"
1475                "}");
1476 }
1477 
1478 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1479   EXPECT_EQ("class A {\n"
1480             "public:\n"
1481             "  void f();\n"
1482             "\n"
1483             "private:\n"
1484             "  void g() {}\n"
1485             "  // test\n"
1486             "protected:\n"
1487             "  int h;\n"
1488             "};",
1489             format("class A {\n"
1490                    "public:\n"
1491                    "void f();\n"
1492                    "private:\n"
1493                    "void g() {}\n"
1494                    "// test\n"
1495                    "protected:\n"
1496                    "int h;\n"
1497                    "};"));
1498   EXPECT_EQ("class A {\n"
1499             "protected:\n"
1500             "public:\n"
1501             "  void f();\n"
1502             "};",
1503             format("class A {\n"
1504                    "protected:\n"
1505                    "\n"
1506                    "public:\n"
1507                    "\n"
1508                    "  void f();\n"
1509                    "};"));
1510 
1511   // Even ensure proper spacing inside macros.
1512   EXPECT_EQ("#define B     \\\n"
1513             "  class A {   \\\n"
1514             "   protected: \\\n"
1515             "   public:    \\\n"
1516             "    void f(); \\\n"
1517             "  };",
1518             format("#define B     \\\n"
1519                    "  class A {   \\\n"
1520                    "   protected: \\\n"
1521                    "              \\\n"
1522                    "   public:    \\\n"
1523                    "              \\\n"
1524                    "    void f(); \\\n"
1525                    "  };",
1526                    getGoogleStyle()));
1527   // But don't remove empty lines after macros ending in access specifiers.
1528   EXPECT_EQ("#define A private:\n"
1529             "\n"
1530             "int i;",
1531             format("#define A         private:\n"
1532                    "\n"
1533                    "int              i;"));
1534 }
1535 
1536 TEST_F(FormatTest, FormatsClasses) {
1537   verifyFormat("class A : public B {};");
1538   verifyFormat("class A : public ::B {};");
1539 
1540   verifyFormat(
1541       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1542       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1543   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1544                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1545                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1546   verifyFormat(
1547       "class A : public B, public C, public D, public E, public F {};");
1548   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1549                "                     public C,\n"
1550                "                     public D,\n"
1551                "                     public E,\n"
1552                "                     public F,\n"
1553                "                     public G {};");
1554 
1555   verifyFormat("class\n"
1556                "    ReallyReallyLongClassName {\n"
1557                "  int i;\n"
1558                "};",
1559                getLLVMStyleWithColumns(32));
1560   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1561                "                           aaaaaaaaaaaaaaaa> {};");
1562   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1563                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1564                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1565   verifyFormat("template <class R, class C>\n"
1566                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1567                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1568   verifyFormat("class ::A::B {};");
1569 }
1570 
1571 TEST_F(FormatTest, BreakInheritanceStyle) {
1572   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1573   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1574           FormatStyle::BILS_BeforeComma;
1575   verifyFormat("class MyClass : public X {};",
1576                StyleWithInheritanceBreakBeforeComma);
1577   verifyFormat("class MyClass\n"
1578                "    : public X\n"
1579                "    , public Y {};",
1580                StyleWithInheritanceBreakBeforeComma);
1581   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1582                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1583                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1584                StyleWithInheritanceBreakBeforeComma);
1585   verifyFormat("struct aaaaaaaaaaaaa\n"
1586                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1587                "          aaaaaaaaaaaaaaaa> {};",
1588                StyleWithInheritanceBreakBeforeComma);
1589 
1590   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1591   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1592           FormatStyle::BILS_AfterColon;
1593   verifyFormat("class MyClass : public X {};",
1594                StyleWithInheritanceBreakAfterColon);
1595   verifyFormat("class MyClass : public X, public Y {};",
1596                StyleWithInheritanceBreakAfterColon);
1597   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1598                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1599                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1600                StyleWithInheritanceBreakAfterColon);
1601   verifyFormat("struct aaaaaaaaaaaaa :\n"
1602                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1603                "        aaaaaaaaaaaaaaaa> {};",
1604                StyleWithInheritanceBreakAfterColon);
1605 }
1606 
1607 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1608   verifyFormat("class A {\n} a, b;");
1609   verifyFormat("struct A {\n} a, b;");
1610   verifyFormat("union A {\n} a;");
1611 }
1612 
1613 TEST_F(FormatTest, FormatsEnum) {
1614   verifyFormat("enum {\n"
1615                "  Zero,\n"
1616                "  One = 1,\n"
1617                "  Two = One + 1,\n"
1618                "  Three = (One + Two),\n"
1619                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1620                "  Five = (One, Two, Three, Four, 5)\n"
1621                "};");
1622   verifyGoogleFormat("enum {\n"
1623                      "  Zero,\n"
1624                      "  One = 1,\n"
1625                      "  Two = One + 1,\n"
1626                      "  Three = (One + Two),\n"
1627                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1628                      "  Five = (One, Two, Three, Four, 5)\n"
1629                      "};");
1630   verifyFormat("enum Enum {};");
1631   verifyFormat("enum {};");
1632   verifyFormat("enum X E {} d;");
1633   verifyFormat("enum __attribute__((...)) E {} d;");
1634   verifyFormat("enum __declspec__((...)) E {} d;");
1635   verifyFormat("enum {\n"
1636                "  Bar = Foo<int, int>::value\n"
1637                "};",
1638                getLLVMStyleWithColumns(30));
1639 
1640   verifyFormat("enum ShortEnum { A, B, C };");
1641   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1642 
1643   EXPECT_EQ("enum KeepEmptyLines {\n"
1644             "  ONE,\n"
1645             "\n"
1646             "  TWO,\n"
1647             "\n"
1648             "  THREE\n"
1649             "}",
1650             format("enum KeepEmptyLines {\n"
1651                    "  ONE,\n"
1652                    "\n"
1653                    "  TWO,\n"
1654                    "\n"
1655                    "\n"
1656                    "  THREE\n"
1657                    "}"));
1658   verifyFormat("enum E { // comment\n"
1659                "  ONE,\n"
1660                "  TWO\n"
1661                "};\n"
1662                "int i;");
1663   // Not enums.
1664   verifyFormat("enum X f() {\n"
1665                "  a();\n"
1666                "  return 42;\n"
1667                "}");
1668   verifyFormat("enum X Type::f() {\n"
1669                "  a();\n"
1670                "  return 42;\n"
1671                "}");
1672   verifyFormat("enum ::X f() {\n"
1673                "  a();\n"
1674                "  return 42;\n"
1675                "}");
1676   verifyFormat("enum ns::X f() {\n"
1677                "  a();\n"
1678                "  return 42;\n"
1679                "}");
1680 }
1681 
1682 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1683   verifyFormat("enum Type {\n"
1684                "  One = 0; // These semicolons should be commas.\n"
1685                "  Two = 1;\n"
1686                "};");
1687   verifyFormat("namespace n {\n"
1688                "enum Type {\n"
1689                "  One,\n"
1690                "  Two, // missing };\n"
1691                "  int i;\n"
1692                "}\n"
1693                "void g() {}");
1694 }
1695 
1696 TEST_F(FormatTest, FormatsEnumStruct) {
1697   verifyFormat("enum struct {\n"
1698                "  Zero,\n"
1699                "  One = 1,\n"
1700                "  Two = One + 1,\n"
1701                "  Three = (One + Two),\n"
1702                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1703                "  Five = (One, Two, Three, Four, 5)\n"
1704                "};");
1705   verifyFormat("enum struct Enum {};");
1706   verifyFormat("enum struct {};");
1707   verifyFormat("enum struct X E {} d;");
1708   verifyFormat("enum struct __attribute__((...)) E {} d;");
1709   verifyFormat("enum struct __declspec__((...)) E {} d;");
1710   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1711 }
1712 
1713 TEST_F(FormatTest, FormatsEnumClass) {
1714   verifyFormat("enum class {\n"
1715                "  Zero,\n"
1716                "  One = 1,\n"
1717                "  Two = One + 1,\n"
1718                "  Three = (One + Two),\n"
1719                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1720                "  Five = (One, Two, Three, Four, 5)\n"
1721                "};");
1722   verifyFormat("enum class Enum {};");
1723   verifyFormat("enum class {};");
1724   verifyFormat("enum class X E {} d;");
1725   verifyFormat("enum class __attribute__((...)) E {} d;");
1726   verifyFormat("enum class __declspec__((...)) E {} d;");
1727   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1728 }
1729 
1730 TEST_F(FormatTest, FormatsEnumTypes) {
1731   verifyFormat("enum X : int {\n"
1732                "  A, // Force multiple lines.\n"
1733                "  B\n"
1734                "};");
1735   verifyFormat("enum X : int { A, B };");
1736   verifyFormat("enum X : std::uint32_t { A, B };");
1737 }
1738 
1739 TEST_F(FormatTest, FormatsTypedefEnum) {
1740   FormatStyle Style = getLLVMStyle();
1741   Style.ColumnLimit = 40;
1742   verifyFormat("typedef enum {} EmptyEnum;");
1743   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1744   verifyFormat("typedef enum {\n"
1745                "  ZERO = 0,\n"
1746                "  ONE = 1,\n"
1747                "  TWO = 2,\n"
1748                "  THREE = 3\n"
1749                "} LongEnum;",
1750                Style);
1751   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1752   Style.BraceWrapping.AfterEnum = true;
1753   verifyFormat("typedef enum {} EmptyEnum;");
1754   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1755   verifyFormat("typedef enum\n"
1756                "{\n"
1757                "  ZERO = 0,\n"
1758                "  ONE = 1,\n"
1759                "  TWO = 2,\n"
1760                "  THREE = 3\n"
1761                "} LongEnum;",
1762                Style);
1763 }
1764 
1765 TEST_F(FormatTest, FormatsNSEnums) {
1766   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1767   verifyGoogleFormat(
1768       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
1769   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1770                      "  // Information about someDecentlyLongValue.\n"
1771                      "  someDecentlyLongValue,\n"
1772                      "  // Information about anotherDecentlyLongValue.\n"
1773                      "  anotherDecentlyLongValue,\n"
1774                      "  // Information about aThirdDecentlyLongValue.\n"
1775                      "  aThirdDecentlyLongValue\n"
1776                      "};");
1777   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
1778                      "  // Information about someDecentlyLongValue.\n"
1779                      "  someDecentlyLongValue,\n"
1780                      "  // Information about anotherDecentlyLongValue.\n"
1781                      "  anotherDecentlyLongValue,\n"
1782                      "  // Information about aThirdDecentlyLongValue.\n"
1783                      "  aThirdDecentlyLongValue\n"
1784                      "};");
1785   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1786                      "  a = 1,\n"
1787                      "  b = 2,\n"
1788                      "  c = 3,\n"
1789                      "};");
1790   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1791                      "  a = 1,\n"
1792                      "  b = 2,\n"
1793                      "  c = 3,\n"
1794                      "};");
1795   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
1796                      "  a = 1,\n"
1797                      "  b = 2,\n"
1798                      "  c = 3,\n"
1799                      "};");
1800   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1801                      "  a = 1,\n"
1802                      "  b = 2,\n"
1803                      "  c = 3,\n"
1804                      "};");
1805 }
1806 
1807 TEST_F(FormatTest, FormatsBitfields) {
1808   verifyFormat("struct Bitfields {\n"
1809                "  unsigned sClass : 8;\n"
1810                "  unsigned ValueKind : 2;\n"
1811                "};");
1812   verifyFormat("struct A {\n"
1813                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1814                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1815                "};");
1816   verifyFormat("struct MyStruct {\n"
1817                "  uchar data;\n"
1818                "  uchar : 8;\n"
1819                "  uchar : 8;\n"
1820                "  uchar other;\n"
1821                "};");
1822 }
1823 
1824 TEST_F(FormatTest, FormatsNamespaces) {
1825   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1826   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1827 
1828   verifyFormat("namespace some_namespace {\n"
1829                "class A {};\n"
1830                "void f() { f(); }\n"
1831                "}",
1832                LLVMWithNoNamespaceFix);
1833   verifyFormat("namespace N::inline D {\n"
1834                "class A {};\n"
1835                "void f() { f(); }\n"
1836                "}",
1837                LLVMWithNoNamespaceFix);
1838   verifyFormat("namespace N::inline D::E {\n"
1839                "class A {};\n"
1840                "void f() { f(); }\n"
1841                "}",
1842                LLVMWithNoNamespaceFix);
1843   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
1844                "class A {};\n"
1845                "void f() { f(); }\n"
1846                "}",
1847                LLVMWithNoNamespaceFix);
1848   verifyFormat("/* something */ namespace some_namespace {\n"
1849                "class A {};\n"
1850                "void f() { f(); }\n"
1851                "}",
1852                LLVMWithNoNamespaceFix);
1853   verifyFormat("namespace {\n"
1854                "class A {};\n"
1855                "void f() { f(); }\n"
1856                "}",
1857                LLVMWithNoNamespaceFix);
1858   verifyFormat("/* something */ namespace {\n"
1859                "class A {};\n"
1860                "void f() { f(); }\n"
1861                "}",
1862                LLVMWithNoNamespaceFix);
1863   verifyFormat("inline namespace X {\n"
1864                "class A {};\n"
1865                "void f() { f(); }\n"
1866                "}",
1867                LLVMWithNoNamespaceFix);
1868   verifyFormat("/* something */ inline namespace X {\n"
1869                "class A {};\n"
1870                "void f() { f(); }\n"
1871                "}",
1872                LLVMWithNoNamespaceFix);
1873   verifyFormat("export namespace X {\n"
1874                "class A {};\n"
1875                "void f() { f(); }\n"
1876                "}",
1877                LLVMWithNoNamespaceFix);
1878   verifyFormat("using namespace some_namespace;\n"
1879                "class A {};\n"
1880                "void f() { f(); }",
1881                LLVMWithNoNamespaceFix);
1882 
1883   // This code is more common than we thought; if we
1884   // layout this correctly the semicolon will go into
1885   // its own line, which is undesirable.
1886   verifyFormat("namespace {};",
1887                LLVMWithNoNamespaceFix);
1888   verifyFormat("namespace {\n"
1889                "class A {};\n"
1890                "};",
1891                LLVMWithNoNamespaceFix);
1892 
1893   verifyFormat("namespace {\n"
1894                "int SomeVariable = 0; // comment\n"
1895                "} // namespace",
1896                LLVMWithNoNamespaceFix);
1897   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1898             "#define HEADER_GUARD\n"
1899             "namespace my_namespace {\n"
1900             "int i;\n"
1901             "} // my_namespace\n"
1902             "#endif // HEADER_GUARD",
1903             format("#ifndef HEADER_GUARD\n"
1904                    " #define HEADER_GUARD\n"
1905                    "   namespace my_namespace {\n"
1906                    "int i;\n"
1907                    "}    // my_namespace\n"
1908                    "#endif    // HEADER_GUARD",
1909                    LLVMWithNoNamespaceFix));
1910 
1911   EXPECT_EQ("namespace A::B {\n"
1912             "class C {};\n"
1913             "}",
1914             format("namespace A::B {\n"
1915                    "class C {};\n"
1916                    "}",
1917                    LLVMWithNoNamespaceFix));
1918 
1919   FormatStyle Style = getLLVMStyle();
1920   Style.NamespaceIndentation = FormatStyle::NI_All;
1921   EXPECT_EQ("namespace out {\n"
1922             "  int i;\n"
1923             "  namespace in {\n"
1924             "    int i;\n"
1925             "  } // namespace in\n"
1926             "} // namespace out",
1927             format("namespace out {\n"
1928                    "int i;\n"
1929                    "namespace in {\n"
1930                    "int i;\n"
1931                    "} // namespace in\n"
1932                    "} // namespace out",
1933                    Style));
1934 
1935   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1936   EXPECT_EQ("namespace out {\n"
1937             "int i;\n"
1938             "namespace in {\n"
1939             "  int i;\n"
1940             "} // namespace in\n"
1941             "} // namespace out",
1942             format("namespace out {\n"
1943                    "int i;\n"
1944                    "namespace in {\n"
1945                    "int i;\n"
1946                    "} // namespace in\n"
1947                    "} // namespace out",
1948                    Style));
1949 }
1950 
1951 TEST_F(FormatTest, NamespaceMacros) {
1952   FormatStyle Style = getLLVMStyle();
1953   Style.NamespaceMacros.push_back("TESTSUITE");
1954 
1955   verifyFormat("TESTSUITE(A) {\n"
1956                "int foo();\n"
1957                "} // TESTSUITE(A)",
1958                Style);
1959 
1960   verifyFormat("TESTSUITE(A, B) {\n"
1961                "int foo();\n"
1962                "} // TESTSUITE(A)",
1963                Style);
1964 
1965   // Properly indent according to NamespaceIndentation style
1966   Style.NamespaceIndentation = FormatStyle::NI_All;
1967   verifyFormat("TESTSUITE(A) {\n"
1968                "  int foo();\n"
1969                "} // TESTSUITE(A)",
1970                Style);
1971   verifyFormat("TESTSUITE(A) {\n"
1972                "  namespace B {\n"
1973                "    int foo();\n"
1974                "  } // namespace B\n"
1975                "} // TESTSUITE(A)",
1976                Style);
1977   verifyFormat("namespace A {\n"
1978                "  TESTSUITE(B) {\n"
1979                "    int foo();\n"
1980                "  } // TESTSUITE(B)\n"
1981                "} // namespace A",
1982                Style);
1983 
1984   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1985   verifyFormat("TESTSUITE(A) {\n"
1986                "TESTSUITE(B) {\n"
1987                "  int foo();\n"
1988                "} // TESTSUITE(B)\n"
1989                "} // TESTSUITE(A)",
1990                Style);
1991   verifyFormat("TESTSUITE(A) {\n"
1992                "namespace B {\n"
1993                "  int foo();\n"
1994                "} // namespace B\n"
1995                "} // TESTSUITE(A)",
1996                Style);
1997   verifyFormat("namespace A {\n"
1998                "TESTSUITE(B) {\n"
1999                "  int foo();\n"
2000                "} // TESTSUITE(B)\n"
2001                "} // namespace A",
2002                Style);
2003 
2004   // Properly merge namespace-macros blocks in CompactNamespaces mode
2005   Style.NamespaceIndentation = FormatStyle::NI_None;
2006   Style.CompactNamespaces = true;
2007   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
2008                "}} // TESTSUITE(A::B)",
2009                Style);
2010 
2011   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2012             "}} // TESTSUITE(out::in)",
2013             format("TESTSUITE(out) {\n"
2014                    "TESTSUITE(in) {\n"
2015                    "} // TESTSUITE(in)\n"
2016                    "} // TESTSUITE(out)",
2017                    Style));
2018 
2019   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2020             "}} // TESTSUITE(out::in)",
2021             format("TESTSUITE(out) {\n"
2022                    "TESTSUITE(in) {\n"
2023                    "} // TESTSUITE(in)\n"
2024                    "} // TESTSUITE(out)",
2025                    Style));
2026 
2027   // Do not merge different namespaces/macros
2028   EXPECT_EQ("namespace out {\n"
2029             "TESTSUITE(in) {\n"
2030             "} // TESTSUITE(in)\n"
2031             "} // namespace out",
2032             format("namespace out {\n"
2033                    "TESTSUITE(in) {\n"
2034                    "} // TESTSUITE(in)\n"
2035                    "} // namespace out",
2036                    Style));
2037   EXPECT_EQ("TESTSUITE(out) {\n"
2038             "namespace in {\n"
2039             "} // namespace in\n"
2040             "} // TESTSUITE(out)",
2041             format("TESTSUITE(out) {\n"
2042                    "namespace in {\n"
2043                    "} // namespace in\n"
2044                    "} // TESTSUITE(out)",
2045                    Style));
2046   Style.NamespaceMacros.push_back("FOOBAR");
2047   EXPECT_EQ("TESTSUITE(out) {\n"
2048             "FOOBAR(in) {\n"
2049             "} // FOOBAR(in)\n"
2050             "} // TESTSUITE(out)",
2051             format("TESTSUITE(out) {\n"
2052                    "FOOBAR(in) {\n"
2053                    "} // FOOBAR(in)\n"
2054                    "} // TESTSUITE(out)",
2055                    Style));
2056 }
2057 
2058 TEST_F(FormatTest, FormatsCompactNamespaces) {
2059   FormatStyle Style = getLLVMStyle();
2060   Style.CompactNamespaces = true;
2061   Style.NamespaceMacros.push_back("TESTSUITE");
2062 
2063   verifyFormat("namespace A { namespace B {\n"
2064 			   "}} // namespace A::B",
2065 			   Style);
2066 
2067   EXPECT_EQ("namespace out { namespace in {\n"
2068             "}} // namespace out::in",
2069             format("namespace out {\n"
2070                    "namespace in {\n"
2071                    "} // namespace in\n"
2072                    "} // namespace out",
2073                    Style));
2074 
2075   // Only namespaces which have both consecutive opening and end get compacted
2076   EXPECT_EQ("namespace out {\n"
2077             "namespace in1 {\n"
2078             "} // namespace in1\n"
2079             "namespace in2 {\n"
2080             "} // namespace in2\n"
2081             "} // namespace out",
2082             format("namespace out {\n"
2083                    "namespace in1 {\n"
2084                    "} // namespace in1\n"
2085                    "namespace in2 {\n"
2086                    "} // namespace in2\n"
2087                    "} // namespace out",
2088                    Style));
2089 
2090   EXPECT_EQ("namespace out {\n"
2091             "int i;\n"
2092             "namespace in {\n"
2093             "int j;\n"
2094             "} // namespace in\n"
2095             "int k;\n"
2096             "} // namespace out",
2097             format("namespace out { int i;\n"
2098                    "namespace in { int j; } // namespace in\n"
2099                    "int k; } // namespace out",
2100                    Style));
2101 
2102   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
2103             "}}} // namespace A::B::C\n",
2104             format("namespace A { namespace B {\n"
2105                    "namespace C {\n"
2106                    "}} // namespace B::C\n"
2107                    "} // namespace A\n",
2108                    Style));
2109 
2110   Style.ColumnLimit = 40;
2111   EXPECT_EQ("namespace aaaaaaaaaa {\n"
2112             "namespace bbbbbbbbbb {\n"
2113             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
2114             format("namespace aaaaaaaaaa {\n"
2115                    "namespace bbbbbbbbbb {\n"
2116                    "} // namespace bbbbbbbbbb\n"
2117                    "} // namespace aaaaaaaaaa",
2118                    Style));
2119 
2120   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
2121             "namespace cccccc {\n"
2122             "}}} // namespace aaaaaa::bbbbbb::cccccc",
2123             format("namespace aaaaaa {\n"
2124                    "namespace bbbbbb {\n"
2125                    "namespace cccccc {\n"
2126                    "} // namespace cccccc\n"
2127                    "} // namespace bbbbbb\n"
2128                    "} // namespace aaaaaa",
2129                    Style));
2130   Style.ColumnLimit = 80;
2131 
2132   // Extra semicolon after 'inner' closing brace prevents merging
2133   EXPECT_EQ("namespace out { namespace in {\n"
2134             "}; } // namespace out::in",
2135             format("namespace out {\n"
2136                    "namespace in {\n"
2137                    "}; // namespace in\n"
2138                    "} // namespace out",
2139                    Style));
2140 
2141   // Extra semicolon after 'outer' closing brace is conserved
2142   EXPECT_EQ("namespace out { namespace in {\n"
2143             "}}; // namespace out::in",
2144             format("namespace out {\n"
2145                    "namespace in {\n"
2146                    "} // namespace in\n"
2147                    "}; // namespace out",
2148                    Style));
2149 
2150   Style.NamespaceIndentation = FormatStyle::NI_All;
2151   EXPECT_EQ("namespace out { namespace in {\n"
2152             "  int i;\n"
2153             "}} // namespace out::in",
2154             format("namespace out {\n"
2155                    "namespace in {\n"
2156                    "int i;\n"
2157                    "} // namespace in\n"
2158                    "} // namespace out",
2159                    Style));
2160   EXPECT_EQ("namespace out { namespace mid {\n"
2161             "  namespace in {\n"
2162             "    int j;\n"
2163             "  } // namespace in\n"
2164             "  int k;\n"
2165             "}} // namespace out::mid",
2166             format("namespace out { namespace mid {\n"
2167                    "namespace in { int j; } // namespace in\n"
2168                    "int k; }} // namespace out::mid",
2169                    Style));
2170 
2171   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2172   EXPECT_EQ("namespace out { namespace in {\n"
2173             "  int i;\n"
2174             "}} // namespace out::in",
2175             format("namespace out {\n"
2176                    "namespace in {\n"
2177                    "int i;\n"
2178                    "} // namespace in\n"
2179                    "} // namespace out",
2180                    Style));
2181   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
2182             "  int i;\n"
2183             "}}} // namespace out::mid::in",
2184             format("namespace out {\n"
2185                    "namespace mid {\n"
2186                    "namespace in {\n"
2187                    "int i;\n"
2188                    "} // namespace in\n"
2189                    "} // namespace mid\n"
2190                    "} // namespace out",
2191                    Style));
2192 }
2193 
2194 TEST_F(FormatTest, FormatsExternC) {
2195   verifyFormat("extern \"C\" {\nint a;");
2196   verifyFormat("extern \"C\" {}");
2197   verifyFormat("extern \"C\" {\n"
2198                "int foo();\n"
2199                "}");
2200   verifyFormat("extern \"C\" int foo() {}");
2201   verifyFormat("extern \"C\" int foo();");
2202   verifyFormat("extern \"C\" int foo() {\n"
2203                "  int i = 42;\n"
2204                "  return i;\n"
2205                "}");
2206 
2207   FormatStyle Style = getLLVMStyle();
2208   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2209   Style.BraceWrapping.AfterFunction = true;
2210   verifyFormat("extern \"C\" int foo() {}", Style);
2211   verifyFormat("extern \"C\" int foo();", Style);
2212   verifyFormat("extern \"C\" int foo()\n"
2213                "{\n"
2214                "  int i = 42;\n"
2215                "  return i;\n"
2216                "}",
2217                Style);
2218 
2219   Style.BraceWrapping.AfterExternBlock = true;
2220   Style.BraceWrapping.SplitEmptyRecord = false;
2221   verifyFormat("extern \"C\"\n"
2222                "{}",
2223                Style);
2224   verifyFormat("extern \"C\"\n"
2225                "{\n"
2226                "  int foo();\n"
2227                "}",
2228                Style);
2229 }
2230 
2231 TEST_F(FormatTest, FormatsInlineASM) {
2232   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2233   verifyFormat("asm(\"nop\" ::: \"memory\");");
2234   verifyFormat(
2235       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2236       "    \"cpuid\\n\\t\"\n"
2237       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2238       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2239       "    : \"a\"(value));");
2240   EXPECT_EQ(
2241       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2242       "  __asm {\n"
2243       "        mov     edx,[that] // vtable in edx\n"
2244       "        mov     eax,methodIndex\n"
2245       "        call    [edx][eax*4] // stdcall\n"
2246       "  }\n"
2247       "}",
2248       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2249              "    __asm {\n"
2250              "        mov     edx,[that] // vtable in edx\n"
2251              "        mov     eax,methodIndex\n"
2252              "        call    [edx][eax*4] // stdcall\n"
2253              "    }\n"
2254              "}"));
2255   EXPECT_EQ("_asm {\n"
2256             "  xor eax, eax;\n"
2257             "  cpuid;\n"
2258             "}",
2259             format("_asm {\n"
2260                    "  xor eax, eax;\n"
2261                    "  cpuid;\n"
2262                    "}"));
2263   verifyFormat("void function() {\n"
2264                "  // comment\n"
2265                "  asm(\"\");\n"
2266                "}");
2267   EXPECT_EQ("__asm {\n"
2268             "}\n"
2269             "int i;",
2270             format("__asm   {\n"
2271                    "}\n"
2272                    "int   i;"));
2273 }
2274 
2275 TEST_F(FormatTest, FormatTryCatch) {
2276   verifyFormat("try {\n"
2277                "  throw a * b;\n"
2278                "} catch (int a) {\n"
2279                "  // Do nothing.\n"
2280                "} catch (...) {\n"
2281                "  exit(42);\n"
2282                "}");
2283 
2284   // Function-level try statements.
2285   verifyFormat("int f() try { return 4; } catch (...) {\n"
2286                "  return 5;\n"
2287                "}");
2288   verifyFormat("class A {\n"
2289                "  int a;\n"
2290                "  A() try : a(0) {\n"
2291                "  } catch (...) {\n"
2292                "    throw;\n"
2293                "  }\n"
2294                "};\n");
2295 
2296   // Incomplete try-catch blocks.
2297   verifyIncompleteFormat("try {} catch (");
2298 }
2299 
2300 TEST_F(FormatTest, FormatSEHTryCatch) {
2301   verifyFormat("__try {\n"
2302                "  int a = b * c;\n"
2303                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2304                "  // Do nothing.\n"
2305                "}");
2306 
2307   verifyFormat("__try {\n"
2308                "  int a = b * c;\n"
2309                "} __finally {\n"
2310                "  // Do nothing.\n"
2311                "}");
2312 
2313   verifyFormat("DEBUG({\n"
2314                "  __try {\n"
2315                "  } __finally {\n"
2316                "  }\n"
2317                "});\n");
2318 }
2319 
2320 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2321   verifyFormat("try {\n"
2322                "  f();\n"
2323                "} catch {\n"
2324                "  g();\n"
2325                "}");
2326   verifyFormat("try {\n"
2327                "  f();\n"
2328                "} catch (A a) MACRO(x) {\n"
2329                "  g();\n"
2330                "} catch (B b) MACRO(x) {\n"
2331                "  g();\n"
2332                "}");
2333 }
2334 
2335 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2336   FormatStyle Style = getLLVMStyle();
2337   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2338                           FormatStyle::BS_WebKit}) {
2339     Style.BreakBeforeBraces = BraceStyle;
2340     verifyFormat("try {\n"
2341                  "  // something\n"
2342                  "} catch (...) {\n"
2343                  "  // something\n"
2344                  "}",
2345                  Style);
2346   }
2347   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2348   verifyFormat("try {\n"
2349                "  // something\n"
2350                "}\n"
2351                "catch (...) {\n"
2352                "  // something\n"
2353                "}",
2354                Style);
2355   verifyFormat("__try {\n"
2356                "  // something\n"
2357                "}\n"
2358                "__finally {\n"
2359                "  // something\n"
2360                "}",
2361                Style);
2362   verifyFormat("@try {\n"
2363                "  // something\n"
2364                "}\n"
2365                "@finally {\n"
2366                "  // something\n"
2367                "}",
2368                Style);
2369   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2370   verifyFormat("try\n"
2371                "{\n"
2372                "  // something\n"
2373                "}\n"
2374                "catch (...)\n"
2375                "{\n"
2376                "  // something\n"
2377                "}",
2378                Style);
2379   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2380   verifyFormat("try\n"
2381                "  {\n"
2382                "    // something\n"
2383                "  }\n"
2384                "catch (...)\n"
2385                "  {\n"
2386                "    // something\n"
2387                "  }",
2388                Style);
2389   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2390   Style.BraceWrapping.BeforeCatch = true;
2391   verifyFormat("try {\n"
2392                "  // something\n"
2393                "}\n"
2394                "catch (...) {\n"
2395                "  // something\n"
2396                "}",
2397                Style);
2398 }
2399 
2400 TEST_F(FormatTest, StaticInitializers) {
2401   verifyFormat("static SomeClass SC = {1, 'a'};");
2402 
2403   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2404                "    100000000, "
2405                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2406 
2407   // Here, everything other than the "}" would fit on a line.
2408   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2409                "    10000000000000000000000000};");
2410   EXPECT_EQ("S s = {a,\n"
2411             "\n"
2412             "       b};",
2413             format("S s = {\n"
2414                    "  a,\n"
2415                    "\n"
2416                    "  b\n"
2417                    "};"));
2418 
2419   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2420   // line. However, the formatting looks a bit off and this probably doesn't
2421   // happen often in practice.
2422   verifyFormat("static int Variable[1] = {\n"
2423                "    {1000000000000000000000000000000000000}};",
2424                getLLVMStyleWithColumns(40));
2425 }
2426 
2427 TEST_F(FormatTest, DesignatedInitializers) {
2428   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2429   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2430                "                    .bbbbbbbbbb = 2,\n"
2431                "                    .cccccccccc = 3,\n"
2432                "                    .dddddddddd = 4,\n"
2433                "                    .eeeeeeeeee = 5};");
2434   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2435                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2436                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2437                "    .ccccccccccccccccccccccccccc = 3,\n"
2438                "    .ddddddddddddddddddddddddddd = 4,\n"
2439                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2440 
2441   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2442 
2443   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2444   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2445                "                    [2] = bbbbbbbbbb,\n"
2446                "                    [3] = cccccccccc,\n"
2447                "                    [4] = dddddddddd,\n"
2448                "                    [5] = eeeeeeeeee};");
2449   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2450                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2451                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2452                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2453                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2454                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2455 }
2456 
2457 TEST_F(FormatTest, NestedStaticInitializers) {
2458   verifyFormat("static A x = {{{}}};\n");
2459   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2460                "               {init1, init2, init3, init4}}};",
2461                getLLVMStyleWithColumns(50));
2462 
2463   verifyFormat("somes Status::global_reps[3] = {\n"
2464                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2465                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2466                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2467                getLLVMStyleWithColumns(60));
2468   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2469                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2470                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2471                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2472   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2473                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2474                "rect.fTop}};");
2475 
2476   verifyFormat(
2477       "SomeArrayOfSomeType a = {\n"
2478       "    {{1, 2, 3},\n"
2479       "     {1, 2, 3},\n"
2480       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2481       "      333333333333333333333333333333},\n"
2482       "     {1, 2, 3},\n"
2483       "     {1, 2, 3}}};");
2484   verifyFormat(
2485       "SomeArrayOfSomeType a = {\n"
2486       "    {{1, 2, 3}},\n"
2487       "    {{1, 2, 3}},\n"
2488       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2489       "      333333333333333333333333333333}},\n"
2490       "    {{1, 2, 3}},\n"
2491       "    {{1, 2, 3}}};");
2492 
2493   verifyFormat("struct {\n"
2494                "  unsigned bit;\n"
2495                "  const char *const name;\n"
2496                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2497                "                 {kOsWin, \"Windows\"},\n"
2498                "                 {kOsLinux, \"Linux\"},\n"
2499                "                 {kOsCrOS, \"Chrome OS\"}};");
2500   verifyFormat("struct {\n"
2501                "  unsigned bit;\n"
2502                "  const char *const name;\n"
2503                "} kBitsToOs[] = {\n"
2504                "    {kOsMac, \"Mac\"},\n"
2505                "    {kOsWin, \"Windows\"},\n"
2506                "    {kOsLinux, \"Linux\"},\n"
2507                "    {kOsCrOS, \"Chrome OS\"},\n"
2508                "};");
2509 }
2510 
2511 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2512   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2513                "                      \\\n"
2514                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2515 }
2516 
2517 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2518   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2519                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2520 
2521   // Do break defaulted and deleted functions.
2522   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2523                "    default;",
2524                getLLVMStyleWithColumns(40));
2525   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2526                "    delete;",
2527                getLLVMStyleWithColumns(40));
2528 }
2529 
2530 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2531   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2532                getLLVMStyleWithColumns(40));
2533   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2534                getLLVMStyleWithColumns(40));
2535   EXPECT_EQ("#define Q                              \\\n"
2536             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2537             "  \"aaaaaaaa.cpp\"",
2538             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2539                    getLLVMStyleWithColumns(40)));
2540 }
2541 
2542 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2543   EXPECT_EQ("# 123 \"A string literal\"",
2544             format("   #     123    \"A string literal\""));
2545 }
2546 
2547 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2548   EXPECT_EQ("#;", format("#;"));
2549   verifyFormat("#\n;\n;\n;");
2550 }
2551 
2552 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2553   EXPECT_EQ("#line 42 \"test\"\n",
2554             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2555   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2556                                     getLLVMStyleWithColumns(12)));
2557 }
2558 
2559 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2560   EXPECT_EQ("#line 42 \"test\"",
2561             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2562   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2563 }
2564 
2565 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2566   verifyFormat("#define A \\x20");
2567   verifyFormat("#define A \\ x20");
2568   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2569   verifyFormat("#define A ''");
2570   verifyFormat("#define A ''qqq");
2571   verifyFormat("#define A `qqq");
2572   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2573   EXPECT_EQ("const char *c = STRINGIFY(\n"
2574             "\\na : b);",
2575             format("const char * c = STRINGIFY(\n"
2576                    "\\na : b);"));
2577 
2578   verifyFormat("a\r\\");
2579   verifyFormat("a\v\\");
2580   verifyFormat("a\f\\");
2581 }
2582 
2583 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2584   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2585   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2586   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2587   // FIXME: We never break before the macro name.
2588   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2589 
2590   verifyFormat("#define A A\n#define A A");
2591   verifyFormat("#define A(X) A\n#define A A");
2592 
2593   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2594   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2595 }
2596 
2597 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2598   EXPECT_EQ("// somecomment\n"
2599             "#include \"a.h\"\n"
2600             "#define A(  \\\n"
2601             "    A, B)\n"
2602             "#include \"b.h\"\n"
2603             "// somecomment\n",
2604             format("  // somecomment\n"
2605                    "  #include \"a.h\"\n"
2606                    "#define A(A,\\\n"
2607                    "    B)\n"
2608                    "    #include \"b.h\"\n"
2609                    " // somecomment\n",
2610                    getLLVMStyleWithColumns(13)));
2611 }
2612 
2613 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2614 
2615 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2616   EXPECT_EQ("#define A    \\\n"
2617             "  c;         \\\n"
2618             "  e;\n"
2619             "f;",
2620             format("#define A c; e;\n"
2621                    "f;",
2622                    getLLVMStyleWithColumns(14)));
2623 }
2624 
2625 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2626 
2627 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2628   EXPECT_EQ("int x,\n"
2629             "#define A\n"
2630             "    y;",
2631             format("int x,\n#define A\ny;"));
2632 }
2633 
2634 TEST_F(FormatTest, HashInMacroDefinition) {
2635   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2636   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2637   verifyFormat("#define A  \\\n"
2638                "  {        \\\n"
2639                "    f(#c); \\\n"
2640                "  }",
2641                getLLVMStyleWithColumns(11));
2642 
2643   verifyFormat("#define A(X)         \\\n"
2644                "  void function##X()",
2645                getLLVMStyleWithColumns(22));
2646 
2647   verifyFormat("#define A(a, b, c)   \\\n"
2648                "  void a##b##c()",
2649                getLLVMStyleWithColumns(22));
2650 
2651   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2652 }
2653 
2654 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2655   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2656   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2657 
2658   FormatStyle Style = getLLVMStyle();
2659   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
2660   verifyFormat("#define true ((foo)1)", Style);
2661   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
2662   verifyFormat("#define false((foo)0)", Style);
2663 }
2664 
2665 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2666   EXPECT_EQ("#define A b;", format("#define A \\\n"
2667                                    "          \\\n"
2668                                    "  b;",
2669                                    getLLVMStyleWithColumns(25)));
2670   EXPECT_EQ("#define A \\\n"
2671             "          \\\n"
2672             "  a;      \\\n"
2673             "  b;",
2674             format("#define A \\\n"
2675                    "          \\\n"
2676                    "  a;      \\\n"
2677                    "  b;",
2678                    getLLVMStyleWithColumns(11)));
2679   EXPECT_EQ("#define A \\\n"
2680             "  a;      \\\n"
2681             "          \\\n"
2682             "  b;",
2683             format("#define A \\\n"
2684                    "  a;      \\\n"
2685                    "          \\\n"
2686                    "  b;",
2687                    getLLVMStyleWithColumns(11)));
2688 }
2689 
2690 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2691   verifyIncompleteFormat("#define A :");
2692   verifyFormat("#define SOMECASES  \\\n"
2693                "  case 1:          \\\n"
2694                "  case 2\n",
2695                getLLVMStyleWithColumns(20));
2696   verifyFormat("#define MACRO(a) \\\n"
2697                "  if (a)         \\\n"
2698                "    f();         \\\n"
2699                "  else           \\\n"
2700                "    g()",
2701                getLLVMStyleWithColumns(18));
2702   verifyFormat("#define A template <typename T>");
2703   verifyIncompleteFormat("#define STR(x) #x\n"
2704                          "f(STR(this_is_a_string_literal{));");
2705   verifyFormat("#pragma omp threadprivate( \\\n"
2706                "    y)), // expected-warning",
2707                getLLVMStyleWithColumns(28));
2708   verifyFormat("#d, = };");
2709   verifyFormat("#if \"a");
2710   verifyIncompleteFormat("({\n"
2711                          "#define b     \\\n"
2712                          "  }           \\\n"
2713                          "  a\n"
2714                          "a",
2715                          getLLVMStyleWithColumns(15));
2716   verifyFormat("#define A     \\\n"
2717                "  {           \\\n"
2718                "    {\n"
2719                "#define B     \\\n"
2720                "  }           \\\n"
2721                "  }",
2722                getLLVMStyleWithColumns(15));
2723   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2724   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2725   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2726   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2727 }
2728 
2729 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2730   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2731   EXPECT_EQ("class A : public QObject {\n"
2732             "  Q_OBJECT\n"
2733             "\n"
2734             "  A() {}\n"
2735             "};",
2736             format("class A  :  public QObject {\n"
2737                    "     Q_OBJECT\n"
2738                    "\n"
2739                    "  A() {\n}\n"
2740                    "}  ;"));
2741   EXPECT_EQ("MACRO\n"
2742             "/*static*/ int i;",
2743             format("MACRO\n"
2744                    " /*static*/ int   i;"));
2745   EXPECT_EQ("SOME_MACRO\n"
2746             "namespace {\n"
2747             "void f();\n"
2748             "} // namespace",
2749             format("SOME_MACRO\n"
2750                    "  namespace    {\n"
2751                    "void   f(  );\n"
2752                    "} // namespace"));
2753   // Only if the identifier contains at least 5 characters.
2754   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2755   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2756   // Only if everything is upper case.
2757   EXPECT_EQ("class A : public QObject {\n"
2758             "  Q_Object A() {}\n"
2759             "};",
2760             format("class A  :  public QObject {\n"
2761                    "     Q_Object\n"
2762                    "  A() {\n}\n"
2763                    "}  ;"));
2764 
2765   // Only if the next line can actually start an unwrapped line.
2766   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2767             format("SOME_WEIRD_LOG_MACRO\n"
2768                    "<< SomeThing;"));
2769 
2770   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2771                "(n, buffers))\n",
2772                getChromiumStyle(FormatStyle::LK_Cpp));
2773 
2774   // See PR41483
2775   EXPECT_EQ("/**/ FOO(a)\n"
2776             "FOO(b)",
2777             format("/**/ FOO(a)\n"
2778                    "FOO(b)"));
2779 }
2780 
2781 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2782   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2783             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2784             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2785             "class X {};\n"
2786             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2787             "int *createScopDetectionPass() { return 0; }",
2788             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2789                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2790                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2791                    "  class X {};\n"
2792                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2793                    "  int *createScopDetectionPass() { return 0; }"));
2794   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2795   // braces, so that inner block is indented one level more.
2796   EXPECT_EQ("int q() {\n"
2797             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2798             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2799             "  IPC_END_MESSAGE_MAP()\n"
2800             "}",
2801             format("int q() {\n"
2802                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2803                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2804                    "  IPC_END_MESSAGE_MAP()\n"
2805                    "}"));
2806 
2807   // Same inside macros.
2808   EXPECT_EQ("#define LIST(L) \\\n"
2809             "  L(A)          \\\n"
2810             "  L(B)          \\\n"
2811             "  L(C)",
2812             format("#define LIST(L) \\\n"
2813                    "  L(A) \\\n"
2814                    "  L(B) \\\n"
2815                    "  L(C)",
2816                    getGoogleStyle()));
2817 
2818   // These must not be recognized as macros.
2819   EXPECT_EQ("int q() {\n"
2820             "  f(x);\n"
2821             "  f(x) {}\n"
2822             "  f(x)->g();\n"
2823             "  f(x)->*g();\n"
2824             "  f(x).g();\n"
2825             "  f(x) = x;\n"
2826             "  f(x) += x;\n"
2827             "  f(x) -= x;\n"
2828             "  f(x) *= x;\n"
2829             "  f(x) /= x;\n"
2830             "  f(x) %= x;\n"
2831             "  f(x) &= x;\n"
2832             "  f(x) |= x;\n"
2833             "  f(x) ^= x;\n"
2834             "  f(x) >>= x;\n"
2835             "  f(x) <<= x;\n"
2836             "  f(x)[y].z();\n"
2837             "  LOG(INFO) << x;\n"
2838             "  ifstream(x) >> x;\n"
2839             "}\n",
2840             format("int q() {\n"
2841                    "  f(x)\n;\n"
2842                    "  f(x)\n {}\n"
2843                    "  f(x)\n->g();\n"
2844                    "  f(x)\n->*g();\n"
2845                    "  f(x)\n.g();\n"
2846                    "  f(x)\n = x;\n"
2847                    "  f(x)\n += x;\n"
2848                    "  f(x)\n -= x;\n"
2849                    "  f(x)\n *= x;\n"
2850                    "  f(x)\n /= x;\n"
2851                    "  f(x)\n %= x;\n"
2852                    "  f(x)\n &= x;\n"
2853                    "  f(x)\n |= x;\n"
2854                    "  f(x)\n ^= x;\n"
2855                    "  f(x)\n >>= x;\n"
2856                    "  f(x)\n <<= x;\n"
2857                    "  f(x)\n[y].z();\n"
2858                    "  LOG(INFO)\n << x;\n"
2859                    "  ifstream(x)\n >> x;\n"
2860                    "}\n"));
2861   EXPECT_EQ("int q() {\n"
2862             "  F(x)\n"
2863             "  if (1) {\n"
2864             "  }\n"
2865             "  F(x)\n"
2866             "  while (1) {\n"
2867             "  }\n"
2868             "  F(x)\n"
2869             "  G(x);\n"
2870             "  F(x)\n"
2871             "  try {\n"
2872             "    Q();\n"
2873             "  } catch (...) {\n"
2874             "  }\n"
2875             "}\n",
2876             format("int q() {\n"
2877                    "F(x)\n"
2878                    "if (1) {}\n"
2879                    "F(x)\n"
2880                    "while (1) {}\n"
2881                    "F(x)\n"
2882                    "G(x);\n"
2883                    "F(x)\n"
2884                    "try { Q(); } catch (...) {}\n"
2885                    "}\n"));
2886   EXPECT_EQ("class A {\n"
2887             "  A() : t(0) {}\n"
2888             "  A(int i) noexcept() : {}\n"
2889             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2890             "  try : t(0) {\n"
2891             "  } catch (...) {\n"
2892             "  }\n"
2893             "};",
2894             format("class A {\n"
2895                    "  A()\n : t(0) {}\n"
2896                    "  A(int i)\n noexcept() : {}\n"
2897                    "  A(X x)\n"
2898                    "  try : t(0) {} catch (...) {}\n"
2899                    "};"));
2900   FormatStyle Style = getLLVMStyle();
2901   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2902   Style.BraceWrapping.AfterControlStatement = true;
2903   Style.BraceWrapping.AfterFunction = true;
2904   EXPECT_EQ("void f()\n"
2905             "try\n"
2906             "{\n"
2907             "}",
2908             format("void f() try {\n"
2909                    "}", Style));
2910   EXPECT_EQ("class SomeClass {\n"
2911             "public:\n"
2912             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2913             "};",
2914             format("class SomeClass {\n"
2915                    "public:\n"
2916                    "  SomeClass()\n"
2917                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2918                    "};"));
2919   EXPECT_EQ("class SomeClass {\n"
2920             "public:\n"
2921             "  SomeClass()\n"
2922             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2923             "};",
2924             format("class SomeClass {\n"
2925                    "public:\n"
2926                    "  SomeClass()\n"
2927                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2928                    "};",
2929                    getLLVMStyleWithColumns(40)));
2930 
2931   verifyFormat("MACRO(>)");
2932 
2933   // Some macros contain an implicit semicolon.
2934   Style = getLLVMStyle();
2935   Style.StatementMacros.push_back("FOO");
2936   verifyFormat("FOO(a) int b = 0;");
2937   verifyFormat("FOO(a)\n"
2938                "int b = 0;",
2939                Style);
2940   verifyFormat("FOO(a);\n"
2941                "int b = 0;",
2942                Style);
2943   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
2944                "int b = 0;",
2945                Style);
2946   verifyFormat("FOO()\n"
2947                "int b = 0;",
2948                Style);
2949   verifyFormat("FOO\n"
2950                "int b = 0;",
2951                Style);
2952   verifyFormat("void f() {\n"
2953                "  FOO(a)\n"
2954                "  return a;\n"
2955                "}",
2956                Style);
2957   verifyFormat("FOO(a)\n"
2958                "FOO(b)",
2959                Style);
2960   verifyFormat("int a = 0;\n"
2961                "FOO(b)\n"
2962                "int c = 0;",
2963                Style);
2964   verifyFormat("int a = 0;\n"
2965                "int x = FOO(a)\n"
2966                "int b = 0;",
2967                Style);
2968   verifyFormat("void foo(int a) { FOO(a) }\n"
2969                "uint32_t bar() {}",
2970                Style);
2971 }
2972 
2973 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2974   verifyFormat("#define A \\\n"
2975                "  f({     \\\n"
2976                "    g();  \\\n"
2977                "  });",
2978                getLLVMStyleWithColumns(11));
2979 }
2980 
2981 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2982   FormatStyle Style = getLLVMStyle();
2983   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2984   Style.ColumnLimit = 40;
2985   verifyFormat("#ifdef _WIN32\n"
2986                "#define A 0\n"
2987                "#ifdef VAR2\n"
2988                "#define B 1\n"
2989                "#include <someheader.h>\n"
2990                "#define MACRO                          \\\n"
2991                "  some_very_long_func_aaaaaaaaaa();\n"
2992                "#endif\n"
2993                "#else\n"
2994                "#define A 1\n"
2995                "#endif",
2996                Style);
2997   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2998   verifyFormat("#ifdef _WIN32\n"
2999                "#  define A 0\n"
3000                "#  ifdef VAR2\n"
3001                "#    define B 1\n"
3002                "#    include <someheader.h>\n"
3003                "#    define MACRO                      \\\n"
3004                "      some_very_long_func_aaaaaaaaaa();\n"
3005                "#  endif\n"
3006                "#else\n"
3007                "#  define A 1\n"
3008                "#endif",
3009                Style);
3010   verifyFormat("#if A\n"
3011                "#  define MACRO                        \\\n"
3012                "    void a(int x) {                    \\\n"
3013                "      b();                             \\\n"
3014                "      c();                             \\\n"
3015                "      d();                             \\\n"
3016                "      e();                             \\\n"
3017                "      f();                             \\\n"
3018                "    }\n"
3019                "#endif",
3020                Style);
3021   // Comments before include guard.
3022   verifyFormat("// file comment\n"
3023                "// file comment\n"
3024                "#ifndef HEADER_H\n"
3025                "#define HEADER_H\n"
3026                "code();\n"
3027                "#endif",
3028                Style);
3029   // Test with include guards.
3030   verifyFormat("#ifndef HEADER_H\n"
3031                "#define HEADER_H\n"
3032                "code();\n"
3033                "#endif",
3034                Style);
3035   // Include guards must have a #define with the same variable immediately
3036   // after #ifndef.
3037   verifyFormat("#ifndef NOT_GUARD\n"
3038                "#  define FOO\n"
3039                "code();\n"
3040                "#endif",
3041                Style);
3042 
3043   // Include guards must cover the entire file.
3044   verifyFormat("code();\n"
3045                "code();\n"
3046                "#ifndef NOT_GUARD\n"
3047                "#  define NOT_GUARD\n"
3048                "code();\n"
3049                "#endif",
3050                Style);
3051   verifyFormat("#ifndef NOT_GUARD\n"
3052                "#  define NOT_GUARD\n"
3053                "code();\n"
3054                "#endif\n"
3055                "code();",
3056                Style);
3057   // Test with trailing blank lines.
3058   verifyFormat("#ifndef HEADER_H\n"
3059                "#define HEADER_H\n"
3060                "code();\n"
3061                "#endif\n",
3062                Style);
3063   // Include guards don't have #else.
3064   verifyFormat("#ifndef NOT_GUARD\n"
3065                "#  define NOT_GUARD\n"
3066                "code();\n"
3067                "#else\n"
3068                "#endif",
3069                Style);
3070   verifyFormat("#ifndef NOT_GUARD\n"
3071                "#  define NOT_GUARD\n"
3072                "code();\n"
3073                "#elif FOO\n"
3074                "#endif",
3075                Style);
3076   // Non-identifier #define after potential include guard.
3077   verifyFormat("#ifndef FOO\n"
3078                "#  define 1\n"
3079                "#endif\n",
3080                Style);
3081   // #if closes past last non-preprocessor line.
3082   verifyFormat("#ifndef FOO\n"
3083                "#define FOO\n"
3084                "#if 1\n"
3085                "int i;\n"
3086                "#  define A 0\n"
3087                "#endif\n"
3088                "#endif\n",
3089                Style);
3090   // FIXME: This doesn't handle the case where there's code between the
3091   // #ifndef and #define but all other conditions hold. This is because when
3092   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
3093   // previous code line yet, so we can't detect it.
3094   EXPECT_EQ("#ifndef NOT_GUARD\n"
3095             "code();\n"
3096             "#define NOT_GUARD\n"
3097             "code();\n"
3098             "#endif",
3099             format("#ifndef NOT_GUARD\n"
3100                    "code();\n"
3101                    "#  define NOT_GUARD\n"
3102                    "code();\n"
3103                    "#endif",
3104                    Style));
3105   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
3106   // be outside an include guard. Examples are #pragma once and
3107   // #pragma GCC diagnostic, or anything else that does not change the meaning
3108   // of the file if it's included multiple times.
3109   EXPECT_EQ("#ifdef WIN32\n"
3110             "#  pragma once\n"
3111             "#endif\n"
3112             "#ifndef HEADER_H\n"
3113             "#  define HEADER_H\n"
3114             "code();\n"
3115             "#endif",
3116             format("#ifdef WIN32\n"
3117                    "#  pragma once\n"
3118                    "#endif\n"
3119                    "#ifndef HEADER_H\n"
3120                    "#define HEADER_H\n"
3121                    "code();\n"
3122                    "#endif",
3123                    Style));
3124   // FIXME: This does not detect when there is a single non-preprocessor line
3125   // in front of an include-guard-like structure where other conditions hold
3126   // because ScopedLineState hides the line.
3127   EXPECT_EQ("code();\n"
3128             "#ifndef HEADER_H\n"
3129             "#define HEADER_H\n"
3130             "code();\n"
3131             "#endif",
3132             format("code();\n"
3133                    "#ifndef HEADER_H\n"
3134                    "#  define HEADER_H\n"
3135                    "code();\n"
3136                    "#endif",
3137                    Style));
3138   // Keep comments aligned with #, otherwise indent comments normally. These
3139   // tests cannot use verifyFormat because messUp manipulates leading
3140   // whitespace.
3141   {
3142     const char *Expected = ""
3143                            "void f() {\n"
3144                            "#if 1\n"
3145                            "// Preprocessor aligned.\n"
3146                            "#  define A 0\n"
3147                            "  // Code. Separated by blank line.\n"
3148                            "\n"
3149                            "#  define B 0\n"
3150                            "  // Code. Not aligned with #\n"
3151                            "#  define C 0\n"
3152                            "#endif";
3153     const char *ToFormat = ""
3154                            "void f() {\n"
3155                            "#if 1\n"
3156                            "// Preprocessor aligned.\n"
3157                            "#  define A 0\n"
3158                            "// Code. Separated by blank line.\n"
3159                            "\n"
3160                            "#  define B 0\n"
3161                            "   // Code. Not aligned with #\n"
3162                            "#  define C 0\n"
3163                            "#endif";
3164     EXPECT_EQ(Expected, format(ToFormat, Style));
3165     EXPECT_EQ(Expected, format(Expected, Style));
3166   }
3167   // Keep block quotes aligned.
3168   {
3169     const char *Expected = ""
3170                            "void f() {\n"
3171                            "#if 1\n"
3172                            "/* Preprocessor aligned. */\n"
3173                            "#  define A 0\n"
3174                            "  /* Code. Separated by blank line. */\n"
3175                            "\n"
3176                            "#  define B 0\n"
3177                            "  /* Code. Not aligned with # */\n"
3178                            "#  define C 0\n"
3179                            "#endif";
3180     const char *ToFormat = ""
3181                            "void f() {\n"
3182                            "#if 1\n"
3183                            "/* Preprocessor aligned. */\n"
3184                            "#  define A 0\n"
3185                            "/* Code. Separated by blank line. */\n"
3186                            "\n"
3187                            "#  define B 0\n"
3188                            "   /* Code. Not aligned with # */\n"
3189                            "#  define C 0\n"
3190                            "#endif";
3191     EXPECT_EQ(Expected, format(ToFormat, Style));
3192     EXPECT_EQ(Expected, format(Expected, Style));
3193   }
3194   // Keep comments aligned with un-indented directives.
3195   {
3196     const char *Expected = ""
3197                            "void f() {\n"
3198                            "// Preprocessor aligned.\n"
3199                            "#define A 0\n"
3200                            "  // Code. Separated by blank line.\n"
3201                            "\n"
3202                            "#define B 0\n"
3203                            "  // Code. Not aligned with #\n"
3204                            "#define C 0\n";
3205     const char *ToFormat = ""
3206                            "void f() {\n"
3207                            "// Preprocessor aligned.\n"
3208                            "#define A 0\n"
3209                            "// Code. Separated by blank line.\n"
3210                            "\n"
3211                            "#define B 0\n"
3212                            "   // Code. Not aligned with #\n"
3213                            "#define C 0\n";
3214     EXPECT_EQ(Expected, format(ToFormat, Style));
3215     EXPECT_EQ(Expected, format(Expected, Style));
3216   }
3217   // Test AfterHash with tabs.
3218   {
3219     FormatStyle Tabbed = Style;
3220     Tabbed.UseTab = FormatStyle::UT_Always;
3221     Tabbed.IndentWidth = 8;
3222     Tabbed.TabWidth = 8;
3223     verifyFormat("#ifdef _WIN32\n"
3224                  "#\tdefine A 0\n"
3225                  "#\tifdef VAR2\n"
3226                  "#\t\tdefine B 1\n"
3227                  "#\t\tinclude <someheader.h>\n"
3228                  "#\t\tdefine MACRO          \\\n"
3229                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
3230                  "#\tendif\n"
3231                  "#else\n"
3232                  "#\tdefine A 1\n"
3233                  "#endif",
3234                  Tabbed);
3235   }
3236 
3237   // Regression test: Multiline-macro inside include guards.
3238   verifyFormat("#ifndef HEADER_H\n"
3239                "#define HEADER_H\n"
3240                "#define A()        \\\n"
3241                "  int i;           \\\n"
3242                "  int j;\n"
3243                "#endif // HEADER_H",
3244                getLLVMStyleWithColumns(20));
3245 
3246   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
3247   // Basic before hash indent tests
3248   verifyFormat("#ifdef _WIN32\n"
3249                "  #define A 0\n"
3250                "  #ifdef VAR2\n"
3251                "    #define B 1\n"
3252                "    #include <someheader.h>\n"
3253                "    #define MACRO                      \\\n"
3254                "      some_very_long_func_aaaaaaaaaa();\n"
3255                "  #endif\n"
3256                "#else\n"
3257                "  #define A 1\n"
3258                "#endif",
3259                Style);
3260   verifyFormat("#if A\n"
3261                "  #define MACRO                        \\\n"
3262                "    void a(int x) {                    \\\n"
3263                "      b();                             \\\n"
3264                "      c();                             \\\n"
3265                "      d();                             \\\n"
3266                "      e();                             \\\n"
3267                "      f();                             \\\n"
3268                "    }\n"
3269                "#endif",
3270                Style);
3271   // Keep comments aligned with indented directives. These
3272   // tests cannot use verifyFormat because messUp manipulates leading
3273   // whitespace.
3274   {
3275     const char *Expected = "void f() {\n"
3276                            "// Aligned to preprocessor.\n"
3277                            "#if 1\n"
3278                            "  // Aligned to code.\n"
3279                            "  int a;\n"
3280                            "  #if 1\n"
3281                            "    // Aligned to preprocessor.\n"
3282                            "    #define A 0\n"
3283                            "  // Aligned to code.\n"
3284                            "  int b;\n"
3285                            "  #endif\n"
3286                            "#endif\n"
3287                            "}";
3288     const char *ToFormat = "void f() {\n"
3289                            "// Aligned to preprocessor.\n"
3290                            "#if 1\n"
3291                            "// Aligned to code.\n"
3292                            "int a;\n"
3293                            "#if 1\n"
3294                            "// Aligned to preprocessor.\n"
3295                            "#define A 0\n"
3296                            "// Aligned to code.\n"
3297                            "int b;\n"
3298                            "#endif\n"
3299                            "#endif\n"
3300                            "}";
3301     EXPECT_EQ(Expected, format(ToFormat, Style));
3302     EXPECT_EQ(Expected, format(Expected, Style));
3303   }
3304   {
3305     const char *Expected = "void f() {\n"
3306                            "/* Aligned to preprocessor. */\n"
3307                            "#if 1\n"
3308                            "  /* Aligned to code. */\n"
3309                            "  int a;\n"
3310                            "  #if 1\n"
3311                            "    /* Aligned to preprocessor. */\n"
3312                            "    #define A 0\n"
3313                            "  /* Aligned to code. */\n"
3314                            "  int b;\n"
3315                            "  #endif\n"
3316                            "#endif\n"
3317                            "}";
3318     const char *ToFormat = "void f() {\n"
3319                            "/* Aligned to preprocessor. */\n"
3320                            "#if 1\n"
3321                            "/* Aligned to code. */\n"
3322                            "int a;\n"
3323                            "#if 1\n"
3324                            "/* Aligned to preprocessor. */\n"
3325                            "#define A 0\n"
3326                            "/* Aligned to code. */\n"
3327                            "int b;\n"
3328                            "#endif\n"
3329                            "#endif\n"
3330                            "}";
3331     EXPECT_EQ(Expected, format(ToFormat, Style));
3332     EXPECT_EQ(Expected, format(Expected, Style));
3333   }
3334 
3335   // Test single comment before preprocessor
3336   verifyFormat("// Comment\n"
3337                "\n"
3338                "#if 1\n"
3339                "#endif",
3340                Style);
3341 }
3342 
3343 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3344   verifyFormat("{\n  { a #c; }\n}");
3345 }
3346 
3347 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3348   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3349             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3350   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3351             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3352 }
3353 
3354 TEST_F(FormatTest, EscapedNewlines) {
3355   FormatStyle Narrow = getLLVMStyleWithColumns(11);
3356   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
3357             format("#define A \\\nint i;\\\n  int j;", Narrow));
3358   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3359   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3360   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
3361   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3362 
3363   FormatStyle AlignLeft = getLLVMStyle();
3364   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3365   EXPECT_EQ("#define MACRO(x) \\\n"
3366             "private:         \\\n"
3367             "  int x(int a);\n",
3368             format("#define MACRO(x) \\\n"
3369                    "private:         \\\n"
3370                    "  int x(int a);\n",
3371                    AlignLeft));
3372 
3373   // CRLF line endings
3374   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
3375             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
3376   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3377   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3378   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
3379   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3380   EXPECT_EQ("#define MACRO(x) \\\r\n"
3381             "private:         \\\r\n"
3382             "  int x(int a);\r\n",
3383             format("#define MACRO(x) \\\r\n"
3384                    "private:         \\\r\n"
3385                    "  int x(int a);\r\n",
3386                    AlignLeft));
3387 
3388   FormatStyle DontAlign = getLLVMStyle();
3389   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3390   DontAlign.MaxEmptyLinesToKeep = 3;
3391   // FIXME: can't use verifyFormat here because the newline before
3392   // "public:" is not inserted the first time it's reformatted
3393   EXPECT_EQ("#define A \\\n"
3394             "  class Foo { \\\n"
3395             "    void bar(); \\\n"
3396             "\\\n"
3397             "\\\n"
3398             "\\\n"
3399             "  public: \\\n"
3400             "    void baz(); \\\n"
3401             "  };",
3402             format("#define A \\\n"
3403                    "  class Foo { \\\n"
3404                    "    void bar(); \\\n"
3405                    "\\\n"
3406                    "\\\n"
3407                    "\\\n"
3408                    "  public: \\\n"
3409                    "    void baz(); \\\n"
3410                    "  };",
3411                    DontAlign));
3412 }
3413 
3414 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3415   verifyFormat("#define A \\\n"
3416                "  int v(  \\\n"
3417                "      a); \\\n"
3418                "  int i;",
3419                getLLVMStyleWithColumns(11));
3420 }
3421 
3422 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3423   EXPECT_EQ(
3424       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3425       "                      \\\n"
3426       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3427       "\n"
3428       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3429       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3430       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3431              "\\\n"
3432              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3433              "  \n"
3434              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3435              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3436 }
3437 
3438 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3439   EXPECT_EQ("int\n"
3440             "#define A\n"
3441             "    a;",
3442             format("int\n#define A\na;"));
3443   verifyFormat("functionCallTo(\n"
3444                "    someOtherFunction(\n"
3445                "        withSomeParameters, whichInSequence,\n"
3446                "        areLongerThanALine(andAnotherCall,\n"
3447                "#define A B\n"
3448                "                           withMoreParamters,\n"
3449                "                           whichStronglyInfluenceTheLayout),\n"
3450                "        andMoreParameters),\n"
3451                "    trailing);",
3452                getLLVMStyleWithColumns(69));
3453   verifyFormat("Foo::Foo()\n"
3454                "#ifdef BAR\n"
3455                "    : baz(0)\n"
3456                "#endif\n"
3457                "{\n"
3458                "}");
3459   verifyFormat("void f() {\n"
3460                "  if (true)\n"
3461                "#ifdef A\n"
3462                "    f(42);\n"
3463                "  x();\n"
3464                "#else\n"
3465                "    g();\n"
3466                "  x();\n"
3467                "#endif\n"
3468                "}");
3469   verifyFormat("void f(param1, param2,\n"
3470                "       param3,\n"
3471                "#ifdef A\n"
3472                "       param4(param5,\n"
3473                "#ifdef A1\n"
3474                "              param6,\n"
3475                "#ifdef A2\n"
3476                "              param7),\n"
3477                "#else\n"
3478                "              param8),\n"
3479                "       param9,\n"
3480                "#endif\n"
3481                "       param10,\n"
3482                "#endif\n"
3483                "       param11)\n"
3484                "#else\n"
3485                "       param12)\n"
3486                "#endif\n"
3487                "{\n"
3488                "  x();\n"
3489                "}",
3490                getLLVMStyleWithColumns(28));
3491   verifyFormat("#if 1\n"
3492                "int i;");
3493   verifyFormat("#if 1\n"
3494                "#endif\n"
3495                "#if 1\n"
3496                "#else\n"
3497                "#endif\n");
3498   verifyFormat("DEBUG({\n"
3499                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3500                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3501                "});\n"
3502                "#if a\n"
3503                "#else\n"
3504                "#endif");
3505 
3506   verifyIncompleteFormat("void f(\n"
3507                          "#if A\n"
3508                          ");\n"
3509                          "#else\n"
3510                          "#endif");
3511 }
3512 
3513 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3514   verifyFormat("#endif\n"
3515                "#if B");
3516 }
3517 
3518 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3519   FormatStyle SingleLine = getLLVMStyle();
3520   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3521   verifyFormat("#if 0\n"
3522                "#elif 1\n"
3523                "#endif\n"
3524                "void foo() {\n"
3525                "  if (test) foo2();\n"
3526                "}",
3527                SingleLine);
3528 }
3529 
3530 TEST_F(FormatTest, LayoutBlockInsideParens) {
3531   verifyFormat("functionCall({ int i; });");
3532   verifyFormat("functionCall({\n"
3533                "  int i;\n"
3534                "  int j;\n"
3535                "});");
3536   verifyFormat("functionCall(\n"
3537                "    {\n"
3538                "      int i;\n"
3539                "      int j;\n"
3540                "    },\n"
3541                "    aaaa, bbbb, cccc);");
3542   verifyFormat("functionA(functionB({\n"
3543                "            int i;\n"
3544                "            int j;\n"
3545                "          }),\n"
3546                "          aaaa, bbbb, cccc);");
3547   verifyFormat("functionCall(\n"
3548                "    {\n"
3549                "      int i;\n"
3550                "      int j;\n"
3551                "    },\n"
3552                "    aaaa, bbbb, // comment\n"
3553                "    cccc);");
3554   verifyFormat("functionA(functionB({\n"
3555                "            int i;\n"
3556                "            int j;\n"
3557                "          }),\n"
3558                "          aaaa, bbbb, // comment\n"
3559                "          cccc);");
3560   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3561   verifyFormat("functionCall(aaaa, bbbb, {\n"
3562                "  int i;\n"
3563                "  int j;\n"
3564                "});");
3565   verifyFormat(
3566       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3567       "    {\n"
3568       "      int i; // break\n"
3569       "    },\n"
3570       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3571       "                                     ccccccccccccccccc));");
3572   verifyFormat("DEBUG({\n"
3573                "  if (a)\n"
3574                "    f();\n"
3575                "});");
3576 }
3577 
3578 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3579   EXPECT_EQ("SOME_MACRO { int i; }\n"
3580             "int i;",
3581             format("  SOME_MACRO  {int i;}  int i;"));
3582 }
3583 
3584 TEST_F(FormatTest, LayoutNestedBlocks) {
3585   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3586                "  struct s {\n"
3587                "    int i;\n"
3588                "  };\n"
3589                "  s kBitsToOs[] = {{10}};\n"
3590                "  for (int i = 0; i < 10; ++i)\n"
3591                "    return;\n"
3592                "}");
3593   verifyFormat("call(parameter, {\n"
3594                "  something();\n"
3595                "  // Comment using all columns.\n"
3596                "  somethingelse();\n"
3597                "});",
3598                getLLVMStyleWithColumns(40));
3599   verifyFormat("DEBUG( //\n"
3600                "    { f(); }, a);");
3601   verifyFormat("DEBUG( //\n"
3602                "    {\n"
3603                "      f(); //\n"
3604                "    },\n"
3605                "    a);");
3606 
3607   EXPECT_EQ("call(parameter, {\n"
3608             "  something();\n"
3609             "  // Comment too\n"
3610             "  // looooooooooong.\n"
3611             "  somethingElse();\n"
3612             "});",
3613             format("call(parameter, {\n"
3614                    "  something();\n"
3615                    "  // Comment too looooooooooong.\n"
3616                    "  somethingElse();\n"
3617                    "});",
3618                    getLLVMStyleWithColumns(29)));
3619   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3620   EXPECT_EQ("DEBUG({ // comment\n"
3621             "  int i;\n"
3622             "});",
3623             format("DEBUG({ // comment\n"
3624                    "int  i;\n"
3625                    "});"));
3626   EXPECT_EQ("DEBUG({\n"
3627             "  int i;\n"
3628             "\n"
3629             "  // comment\n"
3630             "  int j;\n"
3631             "});",
3632             format("DEBUG({\n"
3633                    "  int  i;\n"
3634                    "\n"
3635                    "  // comment\n"
3636                    "  int  j;\n"
3637                    "});"));
3638 
3639   verifyFormat("DEBUG({\n"
3640                "  if (a)\n"
3641                "    return;\n"
3642                "});");
3643   verifyGoogleFormat("DEBUG({\n"
3644                      "  if (a) return;\n"
3645                      "});");
3646   FormatStyle Style = getGoogleStyle();
3647   Style.ColumnLimit = 45;
3648   verifyFormat("Debug(\n"
3649                "    aaaaa,\n"
3650                "    {\n"
3651                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3652                "    },\n"
3653                "    a);",
3654                Style);
3655 
3656   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3657 
3658   verifyNoCrash("^{v^{a}}");
3659 }
3660 
3661 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3662   EXPECT_EQ("#define MACRO()                     \\\n"
3663             "  Debug(aaa, /* force line break */ \\\n"
3664             "        {                           \\\n"
3665             "          int i;                    \\\n"
3666             "          int j;                    \\\n"
3667             "        })",
3668             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3669                    "          {  int   i;  int  j;   })",
3670                    getGoogleStyle()));
3671 
3672   EXPECT_EQ("#define A                                       \\\n"
3673             "  [] {                                          \\\n"
3674             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3675             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3676             "  }",
3677             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3678                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3679                    getGoogleStyle()));
3680 }
3681 
3682 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3683   EXPECT_EQ("{}", format("{}"));
3684   verifyFormat("enum E {};");
3685   verifyFormat("enum E {}");
3686 }
3687 
3688 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3689   FormatStyle Style = getLLVMStyle();
3690   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3691   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3692   verifyFormat("FOO_BEGIN\n"
3693                "  FOO_ENTRY\n"
3694                "FOO_END", Style);
3695   verifyFormat("FOO_BEGIN\n"
3696                "  NESTED_FOO_BEGIN\n"
3697                "    NESTED_FOO_ENTRY\n"
3698                "  NESTED_FOO_END\n"
3699                "FOO_END", Style);
3700   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3701                "  int x;\n"
3702                "  x = 1;\n"
3703                "FOO_END(Baz)", Style);
3704 }
3705 
3706 //===----------------------------------------------------------------------===//
3707 // Line break tests.
3708 //===----------------------------------------------------------------------===//
3709 
3710 TEST_F(FormatTest, PreventConfusingIndents) {
3711   verifyFormat(
3712       "void f() {\n"
3713       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3714       "                         parameter, parameter, parameter)),\n"
3715       "                     SecondLongCall(parameter));\n"
3716       "}");
3717   verifyFormat(
3718       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3719       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3720       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3721       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3722   verifyFormat(
3723       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3724       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3725       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3726       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3727   verifyFormat(
3728       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3729       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3730       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3731       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3732   verifyFormat("int a = bbbb && ccc &&\n"
3733                "        fffff(\n"
3734                "#define A Just forcing a new line\n"
3735                "            ddd);");
3736 }
3737 
3738 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3739   verifyFormat(
3740       "bool aaaaaaa =\n"
3741       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3742       "    bbbbbbbb();");
3743   verifyFormat(
3744       "bool aaaaaaa =\n"
3745       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3746       "    bbbbbbbb();");
3747 
3748   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3749                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3750                "    ccccccccc == ddddddddddd;");
3751   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3752                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3753                "    ccccccccc == ddddddddddd;");
3754   verifyFormat(
3755       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3756       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3757       "    ccccccccc == ddddddddddd;");
3758 
3759   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3760                "                 aaaaaa) &&\n"
3761                "         bbbbbb && cccccc;");
3762   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3763                "                 aaaaaa) >>\n"
3764                "         bbbbbb;");
3765   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3766                "    SourceMgr.getSpellingColumnNumber(\n"
3767                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3768                "    1);");
3769 
3770   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3771                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3772                "    cccccc) {\n}");
3773   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3774                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3775                "              cccccc) {\n}");
3776   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3777                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3778                "              cccccc) {\n}");
3779   verifyFormat("b = a &&\n"
3780                "    // Comment\n"
3781                "    b.c && d;");
3782 
3783   // If the LHS of a comparison is not a binary expression itself, the
3784   // additional linebreak confuses many people.
3785   verifyFormat(
3786       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3787       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3788       "}");
3789   verifyFormat(
3790       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3791       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3792       "}");
3793   verifyFormat(
3794       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3795       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3796       "}");
3797   verifyFormat(
3798       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3799       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3800       "}");
3801   // Even explicit parentheses stress the precedence enough to make the
3802   // additional break unnecessary.
3803   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3804                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3805                "}");
3806   // This cases is borderline, but with the indentation it is still readable.
3807   verifyFormat(
3808       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3809       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3810       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3811       "}",
3812       getLLVMStyleWithColumns(75));
3813 
3814   // If the LHS is a binary expression, we should still use the additional break
3815   // as otherwise the formatting hides the operator precedence.
3816   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3817                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3818                "    5) {\n"
3819                "}");
3820   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3821                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3822                "    5) {\n"
3823                "}");
3824 
3825   FormatStyle OnePerLine = getLLVMStyle();
3826   OnePerLine.BinPackParameters = false;
3827   verifyFormat(
3828       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3829       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3830       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3831       OnePerLine);
3832 
3833   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3834                "                .aaa(aaaaaaaaaaaaa) *\n"
3835                "            aaaaaaa +\n"
3836                "        aaaaaaa;",
3837                getLLVMStyleWithColumns(40));
3838 }
3839 
3840 TEST_F(FormatTest, ExpressionIndentation) {
3841   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3842                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3843                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3844                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3845                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3846                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3847                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3848                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3849                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3850   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3851                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3852                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3853                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3854   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3855                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3856                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3857                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3858   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3859                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3860                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3861                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3862   verifyFormat("if () {\n"
3863                "} else if (aaaaa && bbbbb > // break\n"
3864                "                        ccccc) {\n"
3865                "}");
3866   verifyFormat("if () {\n"
3867                "} else if constexpr (aaaaa && bbbbb > // break\n"
3868                "                                  ccccc) {\n"
3869                "}");
3870   verifyFormat("if () {\n"
3871                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
3872                "                                  ccccc) {\n"
3873                "}");
3874   verifyFormat("if () {\n"
3875                "} else if (aaaaa &&\n"
3876                "           bbbbb > // break\n"
3877                "               ccccc &&\n"
3878                "           ddddd) {\n"
3879                "}");
3880 
3881   // Presence of a trailing comment used to change indentation of b.
3882   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3883                "       b;\n"
3884                "return aaaaaaaaaaaaaaaaaaa +\n"
3885                "       b; //",
3886                getLLVMStyleWithColumns(30));
3887 }
3888 
3889 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3890   // Not sure what the best system is here. Like this, the LHS can be found
3891   // immediately above an operator (everything with the same or a higher
3892   // indent). The RHS is aligned right of the operator and so compasses
3893   // everything until something with the same indent as the operator is found.
3894   // FIXME: Is this a good system?
3895   FormatStyle Style = getLLVMStyle();
3896   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3897   verifyFormat(
3898       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3899       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3900       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3901       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3902       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3903       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3904       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3905       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3906       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3907       Style);
3908   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3909                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3910                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3911                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3912                Style);
3913   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3914                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3915                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3916                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3917                Style);
3918   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3919                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3920                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3921                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3922                Style);
3923   verifyFormat("if () {\n"
3924                "} else if (aaaaa\n"
3925                "           && bbbbb // break\n"
3926                "                  > ccccc) {\n"
3927                "}",
3928                Style);
3929   verifyFormat("return (a)\n"
3930                "       // comment\n"
3931                "       + b;",
3932                Style);
3933   verifyFormat(
3934       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3935       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3936       "             + cc;",
3937       Style);
3938 
3939   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3940                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3941                Style);
3942 
3943   // Forced by comments.
3944   verifyFormat(
3945       "unsigned ContentSize =\n"
3946       "    sizeof(int16_t)   // DWARF ARange version number\n"
3947       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3948       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3949       "    + sizeof(int8_t); // Segment Size (in bytes)");
3950 
3951   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3952                "       == boost::fusion::at_c<1>(iiii).second;",
3953                Style);
3954 
3955   Style.ColumnLimit = 60;
3956   verifyFormat("zzzzzzzzzz\n"
3957                "    = bbbbbbbbbbbbbbbbb\n"
3958                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3959                Style);
3960 
3961   Style.ColumnLimit = 80;
3962   Style.IndentWidth = 4;
3963   Style.TabWidth = 4;
3964   Style.UseTab = FormatStyle::UT_Always;
3965   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3966   Style.AlignOperands = false;
3967   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3968             "\t&& (someOtherLongishConditionPart1\n"
3969             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3970             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3971                    Style));
3972 }
3973 
3974 TEST_F(FormatTest, EnforcedOperatorWraps) {
3975   // Here we'd like to wrap after the || operators, but a comment is forcing an
3976   // earlier wrap.
3977   verifyFormat("bool x = aaaaa //\n"
3978                "         || bbbbb\n"
3979                "         //\n"
3980                "         || cccc;");
3981 }
3982 
3983 TEST_F(FormatTest, NoOperandAlignment) {
3984   FormatStyle Style = getLLVMStyle();
3985   Style.AlignOperands = false;
3986   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3987                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3988                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3989                Style);
3990   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3991   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3992                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3993                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3994                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3995                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3996                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3997                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3998                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3999                "        > ccccccccccccccccccccccccccccccccccccccccc;",
4000                Style);
4001 
4002   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4003                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4004                "    + cc;",
4005                Style);
4006   verifyFormat("int a = aa\n"
4007                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4008                "        * cccccccccccccccccccccccccccccccccccc;\n",
4009                Style);
4010 
4011   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4012   verifyFormat("return (a > b\n"
4013                "    // comment1\n"
4014                "    // comment2\n"
4015                "    || c);",
4016                Style);
4017 }
4018 
4019 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
4020   FormatStyle Style = getLLVMStyle();
4021   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4022   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4023                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4024                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4025                Style);
4026 }
4027 
4028 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
4029   FormatStyle Style = getLLVMStyle();
4030   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4031   Style.BinPackArguments = false;
4032   Style.ColumnLimit = 40;
4033   verifyFormat("void test() {\n"
4034                "  someFunction(\n"
4035                "      this + argument + is + quite\n"
4036                "      + long + so + it + gets + wrapped\n"
4037                "      + but + remains + bin - packed);\n"
4038                "}",
4039                Style);
4040   verifyFormat("void test() {\n"
4041                "  someFunction(arg1,\n"
4042                "               this + argument + is\n"
4043                "                   + quite + long + so\n"
4044                "                   + it + gets + wrapped\n"
4045                "                   + but + remains + bin\n"
4046                "                   - packed,\n"
4047                "               arg3);\n"
4048                "}",
4049                Style);
4050   verifyFormat("void test() {\n"
4051                "  someFunction(\n"
4052                "      arg1,\n"
4053                "      this + argument + has\n"
4054                "          + anotherFunc(nested,\n"
4055                "                        calls + whose\n"
4056                "                            + arguments\n"
4057                "                            + are + also\n"
4058                "                            + wrapped,\n"
4059                "                        in + addition)\n"
4060                "          + to + being + bin - packed,\n"
4061                "      arg3);\n"
4062                "}",
4063                Style);
4064 
4065   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4066   verifyFormat("void test() {\n"
4067                "  someFunction(\n"
4068                "      arg1,\n"
4069                "      this + argument + has +\n"
4070                "          anotherFunc(nested,\n"
4071                "                      calls + whose +\n"
4072                "                          arguments +\n"
4073                "                          are + also +\n"
4074                "                          wrapped,\n"
4075                "                      in + addition) +\n"
4076                "          to + being + bin - packed,\n"
4077                "      arg3);\n"
4078                "}",
4079                Style);
4080 }
4081 
4082 TEST_F(FormatTest, ConstructorInitializers) {
4083   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
4084   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
4085                getLLVMStyleWithColumns(45));
4086   verifyFormat("Constructor()\n"
4087                "    : Inttializer(FitsOnTheLine) {}",
4088                getLLVMStyleWithColumns(44));
4089   verifyFormat("Constructor()\n"
4090                "    : Inttializer(FitsOnTheLine) {}",
4091                getLLVMStyleWithColumns(43));
4092 
4093   verifyFormat("template <typename T>\n"
4094                "Constructor() : Initializer(FitsOnTheLine) {}",
4095                getLLVMStyleWithColumns(45));
4096 
4097   verifyFormat(
4098       "SomeClass::Constructor()\n"
4099       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
4100 
4101   verifyFormat(
4102       "SomeClass::Constructor()\n"
4103       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4104       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
4105   verifyFormat(
4106       "SomeClass::Constructor()\n"
4107       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4108       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
4109   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4110                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4111                "    : aaaaaaaaaa(aaaaaa) {}");
4112 
4113   verifyFormat("Constructor()\n"
4114                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4115                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4116                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4117                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
4118 
4119   verifyFormat("Constructor()\n"
4120                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4121                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4122 
4123   verifyFormat("Constructor(int Parameter = 0)\n"
4124                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
4125                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
4126   verifyFormat("Constructor()\n"
4127                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
4128                "}",
4129                getLLVMStyleWithColumns(60));
4130   verifyFormat("Constructor()\n"
4131                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4132                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
4133 
4134   // Here a line could be saved by splitting the second initializer onto two
4135   // lines, but that is not desirable.
4136   verifyFormat("Constructor()\n"
4137                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
4138                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
4139                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4140 
4141   FormatStyle OnePerLine = getLLVMStyle();
4142   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4143   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
4144   verifyFormat("SomeClass::Constructor()\n"
4145                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4146                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4147                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4148                OnePerLine);
4149   verifyFormat("SomeClass::Constructor()\n"
4150                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
4151                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4152                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4153                OnePerLine);
4154   verifyFormat("MyClass::MyClass(int var)\n"
4155                "    : some_var_(var),            // 4 space indent\n"
4156                "      some_other_var_(var + 1) { // lined up\n"
4157                "}",
4158                OnePerLine);
4159   verifyFormat("Constructor()\n"
4160                "    : aaaaa(aaaaaa),\n"
4161                "      aaaaa(aaaaaa),\n"
4162                "      aaaaa(aaaaaa),\n"
4163                "      aaaaa(aaaaaa),\n"
4164                "      aaaaa(aaaaaa) {}",
4165                OnePerLine);
4166   verifyFormat("Constructor()\n"
4167                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4168                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
4169                OnePerLine);
4170   OnePerLine.BinPackParameters = false;
4171   verifyFormat(
4172       "Constructor()\n"
4173       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4174       "          aaaaaaaaaaa().aaa(),\n"
4175       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4176       OnePerLine);
4177   OnePerLine.ColumnLimit = 60;
4178   verifyFormat("Constructor()\n"
4179                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4180                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
4181                OnePerLine);
4182 
4183   EXPECT_EQ("Constructor()\n"
4184             "    : // Comment forcing unwanted break.\n"
4185             "      aaaa(aaaa) {}",
4186             format("Constructor() :\n"
4187                    "    // Comment forcing unwanted break.\n"
4188                    "    aaaa(aaaa) {}"));
4189 }
4190 
4191 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
4192   FormatStyle Style = getLLVMStyle();
4193   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4194   Style.ColumnLimit = 60;
4195   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4196   Style.AllowAllConstructorInitializersOnNextLine = true;
4197   Style.BinPackParameters = false;
4198 
4199   for (int i = 0; i < 4; ++i) {
4200     // Test all combinations of parameters that should not have an effect.
4201     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
4202     Style.AllowAllArgumentsOnNextLine = i & 2;
4203 
4204     Style.AllowAllConstructorInitializersOnNextLine = true;
4205     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4206     verifyFormat("Constructor()\n"
4207                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4208                  Style);
4209     verifyFormat("Constructor() : a(a), b(b) {}", Style);
4210 
4211     Style.AllowAllConstructorInitializersOnNextLine = false;
4212     verifyFormat("Constructor()\n"
4213                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
4214                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4215                  Style);
4216     verifyFormat("Constructor() : a(a), b(b) {}", Style);
4217 
4218     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
4219     Style.AllowAllConstructorInitializersOnNextLine = true;
4220     verifyFormat("Constructor()\n"
4221                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4222                  Style);
4223 
4224     Style.AllowAllConstructorInitializersOnNextLine = false;
4225     verifyFormat("Constructor()\n"
4226                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4227                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
4228                  Style);
4229 
4230     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4231     Style.AllowAllConstructorInitializersOnNextLine = true;
4232     verifyFormat("Constructor() :\n"
4233                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4234                  Style);
4235 
4236     Style.AllowAllConstructorInitializersOnNextLine = false;
4237     verifyFormat("Constructor() :\n"
4238                  "    aaaaaaaaaaaaaaaaaa(a),\n"
4239                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4240                  Style);
4241   }
4242 
4243   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
4244   // AllowAllConstructorInitializersOnNextLine in all
4245   // BreakConstructorInitializers modes
4246   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4247   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4248   Style.AllowAllConstructorInitializersOnNextLine = false;
4249   verifyFormat("SomeClassWithALongName::Constructor(\n"
4250                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4251                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
4252                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4253                Style);
4254 
4255   Style.AllowAllConstructorInitializersOnNextLine = true;
4256   verifyFormat("SomeClassWithALongName::Constructor(\n"
4257                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4258                "    int bbbbbbbbbbbbb,\n"
4259                "    int cccccccccccccccc)\n"
4260                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4261                Style);
4262 
4263   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4264   Style.AllowAllConstructorInitializersOnNextLine = false;
4265   verifyFormat("SomeClassWithALongName::Constructor(\n"
4266                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4267                "    int bbbbbbbbbbbbb)\n"
4268                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
4269                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4270                Style);
4271 
4272   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
4273 
4274   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4275   verifyFormat("SomeClassWithALongName::Constructor(\n"
4276                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4277                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4278                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
4279                Style);
4280 
4281   Style.AllowAllConstructorInitializersOnNextLine = true;
4282   verifyFormat("SomeClassWithALongName::Constructor(\n"
4283                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4284                "    int bbbbbbbbbbbbb,\n"
4285                "    int cccccccccccccccc)\n"
4286                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4287                Style);
4288 
4289   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4290   Style.AllowAllConstructorInitializersOnNextLine = false;
4291   verifyFormat("SomeClassWithALongName::Constructor(\n"
4292                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4293                "    int bbbbbbbbbbbbb)\n"
4294                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4295                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
4296                Style);
4297 
4298   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4299   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4300   verifyFormat("SomeClassWithALongName::Constructor(\n"
4301                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
4302                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4303                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4304                Style);
4305 
4306   Style.AllowAllConstructorInitializersOnNextLine = true;
4307   verifyFormat("SomeClassWithALongName::Constructor(\n"
4308                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4309                "    int bbbbbbbbbbbbb,\n"
4310                "    int cccccccccccccccc) :\n"
4311                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4312                Style);
4313 
4314   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4315   Style.AllowAllConstructorInitializersOnNextLine = false;
4316   verifyFormat("SomeClassWithALongName::Constructor(\n"
4317                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4318                "    int bbbbbbbbbbbbb) :\n"
4319                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4320                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4321                Style);
4322 }
4323 
4324 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
4325   FormatStyle Style = getLLVMStyle();
4326   Style.ColumnLimit = 60;
4327   Style.BinPackArguments = false;
4328   for (int i = 0; i < 4; ++i) {
4329     // Test all combinations of parameters that should not have an effect.
4330     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
4331     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
4332 
4333     Style.AllowAllArgumentsOnNextLine = true;
4334     verifyFormat("void foo() {\n"
4335                  "  FunctionCallWithReallyLongName(\n"
4336                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
4337                  "}",
4338                  Style);
4339     Style.AllowAllArgumentsOnNextLine = false;
4340     verifyFormat("void foo() {\n"
4341                  "  FunctionCallWithReallyLongName(\n"
4342                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4343                  "      bbbbbbbbbbbb);\n"
4344                  "}",
4345                  Style);
4346 
4347     Style.AllowAllArgumentsOnNextLine = true;
4348     verifyFormat("void foo() {\n"
4349                  "  auto VariableWithReallyLongName = {\n"
4350                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
4351                  "}",
4352                  Style);
4353     Style.AllowAllArgumentsOnNextLine = false;
4354     verifyFormat("void foo() {\n"
4355                  "  auto VariableWithReallyLongName = {\n"
4356                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4357                  "      bbbbbbbbbbbb};\n"
4358                  "}",
4359                  Style);
4360   }
4361 
4362   // This parameter should not affect declarations.
4363   Style.BinPackParameters = false;
4364   Style.AllowAllArgumentsOnNextLine = false;
4365   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4366   verifyFormat("void FunctionCallWithReallyLongName(\n"
4367                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
4368                Style);
4369   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4370   verifyFormat("void FunctionCallWithReallyLongName(\n"
4371                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
4372                "    int bbbbbbbbbbbb);",
4373                Style);
4374 }
4375 
4376 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
4377   FormatStyle Style = getLLVMStyle();
4378   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4379 
4380   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
4381   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
4382                getStyleWithColumns(Style, 45));
4383   verifyFormat("Constructor() :\n"
4384                "    Initializer(FitsOnTheLine) {}",
4385                getStyleWithColumns(Style, 44));
4386   verifyFormat("Constructor() :\n"
4387                "    Initializer(FitsOnTheLine) {}",
4388                getStyleWithColumns(Style, 43));
4389 
4390   verifyFormat("template <typename T>\n"
4391                "Constructor() : Initializer(FitsOnTheLine) {}",
4392                getStyleWithColumns(Style, 50));
4393   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4394   verifyFormat(
4395       "SomeClass::Constructor() :\n"
4396       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4397       Style);
4398 
4399   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
4400   verifyFormat(
4401       "SomeClass::Constructor() :\n"
4402       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4403       Style);
4404 
4405   verifyFormat(
4406       "SomeClass::Constructor() :\n"
4407       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4408       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4409       Style);
4410   verifyFormat(
4411       "SomeClass::Constructor() :\n"
4412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4413       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4414 	  Style);
4415   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4416                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4417                "    aaaaaaaaaa(aaaaaa) {}",
4418 			   Style);
4419 
4420   verifyFormat("Constructor() :\n"
4421                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4422                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4423                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4424                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
4425 			   Style);
4426 
4427   verifyFormat("Constructor() :\n"
4428                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4429                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4430 			   Style);
4431 
4432   verifyFormat("Constructor(int Parameter = 0) :\n"
4433                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
4434                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
4435 			   Style);
4436   verifyFormat("Constructor() :\n"
4437                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
4438                "}",
4439                getStyleWithColumns(Style, 60));
4440   verifyFormat("Constructor() :\n"
4441                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4442                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
4443 			   Style);
4444 
4445   // Here a line could be saved by splitting the second initializer onto two
4446   // lines, but that is not desirable.
4447   verifyFormat("Constructor() :\n"
4448                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
4449                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
4450                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4451 			   Style);
4452 
4453   FormatStyle OnePerLine = Style;
4454   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4455   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
4456   verifyFormat("SomeClass::Constructor() :\n"
4457                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4458                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4459                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4460                OnePerLine);
4461   verifyFormat("SomeClass::Constructor() :\n"
4462                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
4463                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4464                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4465                OnePerLine);
4466   verifyFormat("MyClass::MyClass(int var) :\n"
4467                "    some_var_(var),            // 4 space indent\n"
4468                "    some_other_var_(var + 1) { // lined up\n"
4469                "}",
4470                OnePerLine);
4471   verifyFormat("Constructor() :\n"
4472                "    aaaaa(aaaaaa),\n"
4473                "    aaaaa(aaaaaa),\n"
4474                "    aaaaa(aaaaaa),\n"
4475                "    aaaaa(aaaaaa),\n"
4476                "    aaaaa(aaaaaa) {}",
4477                OnePerLine);
4478   verifyFormat("Constructor() :\n"
4479                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4480                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
4481                OnePerLine);
4482   OnePerLine.BinPackParameters = false;
4483   verifyFormat(
4484       "Constructor() :\n"
4485       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4486       "        aaaaaaaaaaa().aaa(),\n"
4487       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4488       OnePerLine);
4489   OnePerLine.ColumnLimit = 60;
4490   verifyFormat("Constructor() :\n"
4491                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4492                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
4493                OnePerLine);
4494 
4495   EXPECT_EQ("Constructor() :\n"
4496             "    // Comment forcing unwanted break.\n"
4497             "    aaaa(aaaa) {}",
4498             format("Constructor() :\n"
4499                    "    // Comment forcing unwanted break.\n"
4500                    "    aaaa(aaaa) {}",
4501 				   Style));
4502 
4503   Style.ColumnLimit = 0;
4504   verifyFormat("SomeClass::Constructor() :\n"
4505                "    a(a) {}",
4506                Style);
4507   verifyFormat("SomeClass::Constructor() noexcept :\n"
4508                "    a(a) {}",
4509                Style);
4510   verifyFormat("SomeClass::Constructor() :\n"
4511 			   "    a(a), b(b), c(c) {}",
4512                Style);
4513   verifyFormat("SomeClass::Constructor() :\n"
4514                "    a(a) {\n"
4515                "  foo();\n"
4516                "  bar();\n"
4517                "}",
4518                Style);
4519 
4520   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
4521   verifyFormat("SomeClass::Constructor() :\n"
4522 			   "    a(a), b(b), c(c) {\n"
4523 			   "}",
4524                Style);
4525   verifyFormat("SomeClass::Constructor() :\n"
4526                "    a(a) {\n"
4527 			   "}",
4528                Style);
4529 
4530   Style.ColumnLimit = 80;
4531   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
4532   Style.ConstructorInitializerIndentWidth = 2;
4533   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
4534                Style);
4535   verifyFormat("SomeClass::Constructor() :\n"
4536                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4537                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
4538                Style);
4539 
4540   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
4541   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
4542   verifyFormat("class SomeClass\n"
4543                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4544                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4545                Style);
4546   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
4547   verifyFormat("class SomeClass\n"
4548                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4549                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4550                Style);
4551   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
4552   verifyFormat("class SomeClass :\n"
4553                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4554                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4555                Style);
4556 }
4557 
4558 #ifndef EXPENSIVE_CHECKS
4559 // Expensive checks enables libstdc++ checking which includes validating the
4560 // state of ranges used in std::priority_queue - this blows out the
4561 // runtime/scalability of the function and makes this test unacceptably slow.
4562 TEST_F(FormatTest, MemoizationTests) {
4563   // This breaks if the memoization lookup does not take \c Indent and
4564   // \c LastSpace into account.
4565   verifyFormat(
4566       "extern CFRunLoopTimerRef\n"
4567       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
4568       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
4569       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
4570       "                     CFRunLoopTimerContext *context) {}");
4571 
4572   // Deep nesting somewhat works around our memoization.
4573   verifyFormat(
4574       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4575       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4576       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4577       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4578       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
4579       getLLVMStyleWithColumns(65));
4580   verifyFormat(
4581       "aaaaa(\n"
4582       "    aaaaa,\n"
4583       "    aaaaa(\n"
4584       "        aaaaa,\n"
4585       "        aaaaa(\n"
4586       "            aaaaa,\n"
4587       "            aaaaa(\n"
4588       "                aaaaa,\n"
4589       "                aaaaa(\n"
4590       "                    aaaaa,\n"
4591       "                    aaaaa(\n"
4592       "                        aaaaa,\n"
4593       "                        aaaaa(\n"
4594       "                            aaaaa,\n"
4595       "                            aaaaa(\n"
4596       "                                aaaaa,\n"
4597       "                                aaaaa(\n"
4598       "                                    aaaaa,\n"
4599       "                                    aaaaa(\n"
4600       "                                        aaaaa,\n"
4601       "                                        aaaaa(\n"
4602       "                                            aaaaa,\n"
4603       "                                            aaaaa(\n"
4604       "                                                aaaaa,\n"
4605       "                                                aaaaa))))))))))));",
4606       getLLVMStyleWithColumns(65));
4607   verifyFormat(
4608       "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"
4609       "                                  a),\n"
4610       "                                a),\n"
4611       "                              a),\n"
4612       "                            a),\n"
4613       "                          a),\n"
4614       "                        a),\n"
4615       "                      a),\n"
4616       "                    a),\n"
4617       "                  a),\n"
4618       "                a),\n"
4619       "              a),\n"
4620       "            a),\n"
4621       "          a),\n"
4622       "        a),\n"
4623       "      a),\n"
4624       "    a),\n"
4625       "  a)",
4626       getLLVMStyleWithColumns(65));
4627 
4628   // This test takes VERY long when memoization is broken.
4629   FormatStyle OnePerLine = getLLVMStyle();
4630   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4631   OnePerLine.BinPackParameters = false;
4632   std::string input = "Constructor()\n"
4633                       "    : aaaa(a,\n";
4634   for (unsigned i = 0, e = 80; i != e; ++i) {
4635     input += "           a,\n";
4636   }
4637   input += "           a) {}";
4638   verifyFormat(input, OnePerLine);
4639 }
4640 #endif
4641 
4642 TEST_F(FormatTest, BreaksAsHighAsPossible) {
4643   verifyFormat(
4644       "void f() {\n"
4645       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
4646       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
4647       "    f();\n"
4648       "}");
4649   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
4650                "    Intervals[i - 1].getRange().getLast()) {\n}");
4651 }
4652 
4653 TEST_F(FormatTest, BreaksFunctionDeclarations) {
4654   // Principially, we break function declarations in a certain order:
4655   // 1) break amongst arguments.
4656   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
4657                "                              Cccccccccccccc cccccccccccccc);");
4658   verifyFormat("template <class TemplateIt>\n"
4659                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
4660                "                            TemplateIt *stop) {}");
4661 
4662   // 2) break after return type.
4663   verifyFormat(
4664       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4665       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
4666       getGoogleStyle());
4667 
4668   // 3) break after (.
4669   verifyFormat(
4670       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
4671       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
4672       getGoogleStyle());
4673 
4674   // 4) break before after nested name specifiers.
4675   verifyFormat(
4676       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4677       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
4678       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
4679       getGoogleStyle());
4680 
4681   // However, there are exceptions, if a sufficient amount of lines can be
4682   // saved.
4683   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
4684   // more adjusting.
4685   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4686                "                                  Cccccccccccccc cccccccccc,\n"
4687                "                                  Cccccccccccccc cccccccccc,\n"
4688                "                                  Cccccccccccccc cccccccccc,\n"
4689                "                                  Cccccccccccccc cccccccccc);");
4690   verifyFormat(
4691       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4692       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4693       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4694       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
4695       getGoogleStyle());
4696   verifyFormat(
4697       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4698       "                                          Cccccccccccccc cccccccccc,\n"
4699       "                                          Cccccccccccccc cccccccccc,\n"
4700       "                                          Cccccccccccccc cccccccccc,\n"
4701       "                                          Cccccccccccccc cccccccccc,\n"
4702       "                                          Cccccccccccccc cccccccccc,\n"
4703       "                                          Cccccccccccccc cccccccccc);");
4704   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4705                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4706                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4707                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4708                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
4709 
4710   // Break after multi-line parameters.
4711   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4712                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4713                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4714                "    bbbb bbbb);");
4715   verifyFormat("void SomeLoooooooooooongFunction(\n"
4716                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4717                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4718                "    int bbbbbbbbbbbbb);");
4719 
4720   // Treat overloaded operators like other functions.
4721   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4722                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
4723   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4724                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
4725   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4726                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
4727   verifyGoogleFormat(
4728       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
4729       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4730   verifyGoogleFormat(
4731       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
4732       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4733   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4734                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4735   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
4736                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4737   verifyGoogleFormat(
4738       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
4739       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4740       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
4741   verifyGoogleFormat(
4742       "template <typename T>\n"
4743       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4744       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
4745       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
4746 
4747   FormatStyle Style = getLLVMStyle();
4748   Style.PointerAlignment = FormatStyle::PAS_Left;
4749   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4750                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
4751                Style);
4752   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4753                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4754                Style);
4755 }
4756 
4757 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
4758   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
4759   // Prefer keeping `::` followed by `operator` together.
4760   EXPECT_EQ("const aaaa::bbbbbbb &\n"
4761             "ccccccccc::operator++() {\n"
4762             "  stuff();\n"
4763             "}",
4764             format("const aaaa::bbbbbbb\n"
4765                    "&ccccccccc::operator++() { stuff(); }",
4766                    getLLVMStyleWithColumns(40)));
4767 }
4768 
4769 TEST_F(FormatTest, TrailingReturnType) {
4770   verifyFormat("auto foo() -> int;\n");
4771   verifyFormat("struct S {\n"
4772                "  auto bar() const -> int;\n"
4773                "};");
4774   verifyFormat("template <size_t Order, typename T>\n"
4775                "auto load_img(const std::string &filename)\n"
4776                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4777   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4778                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4779   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4780   verifyFormat("template <typename T>\n"
4781                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4782                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4783 
4784   // Not trailing return types.
4785   verifyFormat("void f() { auto a = b->c(); }");
4786 }
4787 
4788 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4789   // Avoid breaking before trailing 'const' or other trailing annotations, if
4790   // they are not function-like.
4791   FormatStyle Style = getGoogleStyle();
4792   Style.ColumnLimit = 47;
4793   verifyFormat("void someLongFunction(\n"
4794                "    int someLoooooooooooooongParameter) const {\n}",
4795                getLLVMStyleWithColumns(47));
4796   verifyFormat("LoooooongReturnType\n"
4797                "someLoooooooongFunction() const {}",
4798                getLLVMStyleWithColumns(47));
4799   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4800                "    const {}",
4801                Style);
4802   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4803                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4804   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4805                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4806   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4807                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4808   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4809                "                   aaaaaaaaaaa aaaaa) const override;");
4810   verifyGoogleFormat(
4811       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4812       "    const override;");
4813 
4814   // Even if the first parameter has to be wrapped.
4815   verifyFormat("void someLongFunction(\n"
4816                "    int someLongParameter) const {}",
4817                getLLVMStyleWithColumns(46));
4818   verifyFormat("void someLongFunction(\n"
4819                "    int someLongParameter) const {}",
4820                Style);
4821   verifyFormat("void someLongFunction(\n"
4822                "    int someLongParameter) override {}",
4823                Style);
4824   verifyFormat("void someLongFunction(\n"
4825                "    int someLongParameter) OVERRIDE {}",
4826                Style);
4827   verifyFormat("void someLongFunction(\n"
4828                "    int someLongParameter) final {}",
4829                Style);
4830   verifyFormat("void someLongFunction(\n"
4831                "    int someLongParameter) FINAL {}",
4832                Style);
4833   verifyFormat("void someLongFunction(\n"
4834                "    int parameter) const override {}",
4835                Style);
4836 
4837   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4838   verifyFormat("void someLongFunction(\n"
4839                "    int someLongParameter) const\n"
4840                "{\n"
4841                "}",
4842                Style);
4843 
4844   // Unless these are unknown annotations.
4845   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4846                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4847                "    LONG_AND_UGLY_ANNOTATION;");
4848 
4849   // Breaking before function-like trailing annotations is fine to keep them
4850   // close to their arguments.
4851   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4852                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4853   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4854                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4855   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4856                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4857   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4858                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4859   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4860 
4861   verifyFormat(
4862       "void aaaaaaaaaaaaaaaaaa()\n"
4863       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4864       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4865   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4866                "    __attribute__((unused));");
4867   verifyGoogleFormat(
4868       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4869       "    GUARDED_BY(aaaaaaaaaaaa);");
4870   verifyGoogleFormat(
4871       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4872       "    GUARDED_BY(aaaaaaaaaaaa);");
4873   verifyGoogleFormat(
4874       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4875       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4876   verifyGoogleFormat(
4877       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4878       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4879 }
4880 
4881 TEST_F(FormatTest, FunctionAnnotations) {
4882   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4883                "int OldFunction(const string &parameter) {}");
4884   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4885                "string OldFunction(const string &parameter) {}");
4886   verifyFormat("template <typename T>\n"
4887                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4888                "string OldFunction(const string &parameter) {}");
4889 
4890   // Not function annotations.
4891   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4892                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4893   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4894                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4895   verifyFormat("MACRO(abc).function() // wrap\n"
4896                "    << abc;");
4897   verifyFormat("MACRO(abc)->function() // wrap\n"
4898                "    << abc;");
4899   verifyFormat("MACRO(abc)::function() // wrap\n"
4900                "    << abc;");
4901 }
4902 
4903 TEST_F(FormatTest, BreaksDesireably) {
4904   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4905                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4906                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4907   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4908                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4909                "}");
4910 
4911   verifyFormat(
4912       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4913       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4914 
4915   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4916                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4917                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4918 
4919   verifyFormat(
4920       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4921       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4922       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4923       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4924       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4925 
4926   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4927                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4928 
4929   verifyFormat(
4930       "void f() {\n"
4931       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4932       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4933       "}");
4934   verifyFormat(
4935       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4936       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4937   verifyFormat(
4938       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4939       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4940   verifyFormat(
4941       "aaaaaa(aaa,\n"
4942       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4943       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4944       "       aaaa);");
4945   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4946                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4947                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4948 
4949   // Indent consistently independent of call expression and unary operator.
4950   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4951                "    dddddddddddddddddddddddddddddd));");
4952   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4953                "    dddddddddddddddddddddddddddddd));");
4954   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4955                "    dddddddddddddddddddddddddddddd));");
4956 
4957   // This test case breaks on an incorrect memoization, i.e. an optimization not
4958   // taking into account the StopAt value.
4959   verifyFormat(
4960       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4961       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4962       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4963       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4964 
4965   verifyFormat("{\n  {\n    {\n"
4966                "      Annotation.SpaceRequiredBefore =\n"
4967                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4968                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4969                "    }\n  }\n}");
4970 
4971   // Break on an outer level if there was a break on an inner level.
4972   EXPECT_EQ("f(g(h(a, // comment\n"
4973             "      b, c),\n"
4974             "    d, e),\n"
4975             "  x, y);",
4976             format("f(g(h(a, // comment\n"
4977                    "    b, c), d, e), x, y);"));
4978 
4979   // Prefer breaking similar line breaks.
4980   verifyFormat(
4981       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4982       "                             NSTrackingMouseEnteredAndExited |\n"
4983       "                             NSTrackingActiveAlways;");
4984 }
4985 
4986 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4987   FormatStyle NoBinPacking = getGoogleStyle();
4988   NoBinPacking.BinPackParameters = false;
4989   NoBinPacking.BinPackArguments = true;
4990   verifyFormat("void f() {\n"
4991                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4992                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4993                "}",
4994                NoBinPacking);
4995   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4996                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4997                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4998                NoBinPacking);
4999 
5000   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
5001   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5002                "                        vector<int> bbbbbbbbbbbbbbb);",
5003                NoBinPacking);
5004   // FIXME: This behavior difference is probably not wanted. However, currently
5005   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
5006   // template arguments from BreakBeforeParameter being set because of the
5007   // one-per-line formatting.
5008   verifyFormat(
5009       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
5010       "                                             aaaaaaaaaa> aaaaaaaaaa);",
5011       NoBinPacking);
5012   verifyFormat(
5013       "void fffffffffff(\n"
5014       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
5015       "        aaaaaaaaaa);");
5016 }
5017 
5018 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
5019   FormatStyle NoBinPacking = getGoogleStyle();
5020   NoBinPacking.BinPackParameters = false;
5021   NoBinPacking.BinPackArguments = false;
5022   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
5023                "  aaaaaaaaaaaaaaaaaaaa,\n"
5024                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
5025                NoBinPacking);
5026   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
5027                "        aaaaaaaaaaaaa,\n"
5028                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
5029                NoBinPacking);
5030   verifyFormat(
5031       "aaaaaaaa(aaaaaaaaaaaaa,\n"
5032       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5033       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
5034       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5035       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
5036       NoBinPacking);
5037   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
5038                "    .aaaaaaaaaaaaaaaaaa();",
5039                NoBinPacking);
5040   verifyFormat("void f() {\n"
5041                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5042                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
5043                "}",
5044                NoBinPacking);
5045 
5046   verifyFormat(
5047       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5048       "             aaaaaaaaaaaa,\n"
5049       "             aaaaaaaaaaaa);",
5050       NoBinPacking);
5051   verifyFormat(
5052       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
5053       "                               ddddddddddddddddddddddddddddd),\n"
5054       "             test);",
5055       NoBinPacking);
5056 
5057   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
5058                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
5059                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
5060                "    aaaaaaaaaaaaaaaaaa;",
5061                NoBinPacking);
5062   verifyFormat("a(\"a\"\n"
5063                "  \"a\",\n"
5064                "  a);");
5065 
5066   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
5067   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
5068                "                aaaaaaaaa,\n"
5069                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5070                NoBinPacking);
5071   verifyFormat(
5072       "void f() {\n"
5073       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
5074       "      .aaaaaaa();\n"
5075       "}",
5076       NoBinPacking);
5077   verifyFormat(
5078       "template <class SomeType, class SomeOtherType>\n"
5079       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
5080       NoBinPacking);
5081 }
5082 
5083 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
5084   FormatStyle Style = getLLVMStyleWithColumns(15);
5085   Style.ExperimentalAutoDetectBinPacking = true;
5086   EXPECT_EQ("aaa(aaaa,\n"
5087             "    aaaa,\n"
5088             "    aaaa);\n"
5089             "aaa(aaaa,\n"
5090             "    aaaa,\n"
5091             "    aaaa);",
5092             format("aaa(aaaa,\n" // one-per-line
5093                    "  aaaa,\n"
5094                    "    aaaa  );\n"
5095                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
5096                    Style));
5097   EXPECT_EQ("aaa(aaaa, aaaa,\n"
5098             "    aaaa);\n"
5099             "aaa(aaaa, aaaa,\n"
5100             "    aaaa);",
5101             format("aaa(aaaa,  aaaa,\n" // bin-packed
5102                    "    aaaa  );\n"
5103                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
5104                    Style));
5105 }
5106 
5107 TEST_F(FormatTest, FormatsBuilderPattern) {
5108   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
5109                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
5110                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
5111                "    .StartsWith(\".init\", ORDER_INIT)\n"
5112                "    .StartsWith(\".fini\", ORDER_FINI)\n"
5113                "    .StartsWith(\".hash\", ORDER_HASH)\n"
5114                "    .Default(ORDER_TEXT);\n");
5115 
5116   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
5117                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
5118   verifyFormat(
5119       "aaaaaaa->aaaaaaa\n"
5120       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5121       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5122       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
5123   verifyFormat(
5124       "aaaaaaa->aaaaaaa\n"
5125       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5126       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
5127   verifyFormat(
5128       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
5129       "    aaaaaaaaaaaaaa);");
5130   verifyFormat(
5131       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
5132       "    aaaaaa->aaaaaaaaaaaa()\n"
5133       "        ->aaaaaaaaaaaaaaaa(\n"
5134       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5135       "        ->aaaaaaaaaaaaaaaaa();");
5136   verifyGoogleFormat(
5137       "void f() {\n"
5138       "  someo->Add((new util::filetools::Handler(dir))\n"
5139       "                 ->OnEvent1(NewPermanentCallback(\n"
5140       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
5141       "                 ->OnEvent2(NewPermanentCallback(\n"
5142       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
5143       "                 ->OnEvent3(NewPermanentCallback(\n"
5144       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
5145       "                 ->OnEvent5(NewPermanentCallback(\n"
5146       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
5147       "                 ->OnEvent6(NewPermanentCallback(\n"
5148       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
5149       "}");
5150 
5151   verifyFormat(
5152       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
5153   verifyFormat("aaaaaaaaaaaaaaa()\n"
5154                "    .aaaaaaaaaaaaaaa()\n"
5155                "    .aaaaaaaaaaaaaaa()\n"
5156                "    .aaaaaaaaaaaaaaa()\n"
5157                "    .aaaaaaaaaaaaaaa();");
5158   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5159                "    .aaaaaaaaaaaaaaa()\n"
5160                "    .aaaaaaaaaaaaaaa()\n"
5161                "    .aaaaaaaaaaaaaaa();");
5162   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5163                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5164                "    .aaaaaaaaaaaaaaa();");
5165   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
5166                "    ->aaaaaaaaaaaaaae(0)\n"
5167                "    ->aaaaaaaaaaaaaaa();");
5168 
5169   // Don't linewrap after very short segments.
5170   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5171                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5172                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5173   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5174                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5175                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5176   verifyFormat("aaa()\n"
5177                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5178                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5179                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5180 
5181   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
5182                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5183                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
5184   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
5185                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5186                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
5187 
5188   // Prefer not to break after empty parentheses.
5189   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
5190                "    First->LastNewlineOffset);");
5191 
5192   // Prefer not to create "hanging" indents.
5193   verifyFormat(
5194       "return !soooooooooooooome_map\n"
5195       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5196       "            .second;");
5197   verifyFormat(
5198       "return aaaaaaaaaaaaaaaa\n"
5199       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
5200       "    .aaaa(aaaaaaaaaaaaaa);");
5201   // No hanging indent here.
5202   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
5203                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5204   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
5205                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5206   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
5207                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5208                getLLVMStyleWithColumns(60));
5209   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
5210                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
5211                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5212                getLLVMStyleWithColumns(59));
5213   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5214                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5215                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5216 
5217   // Dont break if only closing statements before member call
5218   verifyFormat("test() {\n"
5219                "  ([]() -> {\n"
5220                "    int b = 32;\n"
5221                "    return 3;\n"
5222                "  }).foo();\n"
5223                "}");
5224   verifyFormat("test() {\n"
5225                "  (\n"
5226                "      []() -> {\n"
5227                "        int b = 32;\n"
5228                "        return 3;\n"
5229                "      },\n"
5230                "      foo, bar)\n"
5231                "      .foo();\n"
5232                "}");
5233   verifyFormat("test() {\n"
5234                "  ([]() -> {\n"
5235                "    int b = 32;\n"
5236                "    return 3;\n"
5237                "  })\n"
5238                "      .foo()\n"
5239                "      .bar();\n"
5240                "}");
5241   verifyFormat("test() {\n"
5242                "  ([]() -> {\n"
5243                "    int b = 32;\n"
5244                "    return 3;\n"
5245                "  })\n"
5246                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
5247                "           \"bbbb\");\n"
5248                "}",
5249                getLLVMStyleWithColumns(30));
5250 }
5251 
5252 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
5253   verifyFormat(
5254       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5255       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
5256   verifyFormat(
5257       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
5258       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
5259 
5260   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5261                "    ccccccccccccccccccccccccc) {\n}");
5262   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
5263                "    ccccccccccccccccccccccccc) {\n}");
5264 
5265   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5266                "    ccccccccccccccccccccccccc) {\n}");
5267   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
5268                "    ccccccccccccccccccccccccc) {\n}");
5269 
5270   verifyFormat(
5271       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
5272       "    ccccccccccccccccccccccccc) {\n}");
5273   verifyFormat(
5274       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
5275       "    ccccccccccccccccccccccccc) {\n}");
5276 
5277   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
5278                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
5279                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
5280                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5281   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
5282                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
5283                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
5284                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5285 
5286   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
5287                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
5288                "    aaaaaaaaaaaaaaa != aa) {\n}");
5289   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
5290                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
5291                "    aaaaaaaaaaaaaaa != aa) {\n}");
5292 }
5293 
5294 TEST_F(FormatTest, BreaksAfterAssignments) {
5295   verifyFormat(
5296       "unsigned Cost =\n"
5297       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
5298       "                        SI->getPointerAddressSpaceee());\n");
5299   verifyFormat(
5300       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
5301       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
5302 
5303   verifyFormat(
5304       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
5305       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
5306   verifyFormat("unsigned OriginalStartColumn =\n"
5307                "    SourceMgr.getSpellingColumnNumber(\n"
5308                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
5309                "    1;");
5310 }
5311 
5312 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
5313   FormatStyle Style = getLLVMStyle();
5314   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5315                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
5316                Style);
5317 
5318   Style.PenaltyBreakAssignment = 20;
5319   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5320                "                                 cccccccccccccccccccccccccc;",
5321                Style);
5322 }
5323 
5324 TEST_F(FormatTest, AlignsAfterAssignments) {
5325   verifyFormat(
5326       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5327       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
5328   verifyFormat(
5329       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5330       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
5331   verifyFormat(
5332       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5333       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
5334   verifyFormat(
5335       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5336       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
5337   verifyFormat(
5338       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5339       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5340       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
5341 }
5342 
5343 TEST_F(FormatTest, AlignsAfterReturn) {
5344   verifyFormat(
5345       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5346       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
5347   verifyFormat(
5348       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5349       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
5350   verifyFormat(
5351       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5352       "       aaaaaaaaaaaaaaaaaaaaaa();");
5353   verifyFormat(
5354       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5355       "        aaaaaaaaaaaaaaaaaaaaaa());");
5356   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5357                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5358   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5359                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
5360                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5361   verifyFormat("return\n"
5362                "    // true if code is one of a or b.\n"
5363                "    code == a || code == b;");
5364 }
5365 
5366 TEST_F(FormatTest, AlignsAfterOpenBracket) {
5367   verifyFormat(
5368       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5369       "                                                aaaaaaaaa aaaaaaa) {}");
5370   verifyFormat(
5371       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5372       "                                               aaaaaaaaaaa aaaaaaaaa);");
5373   verifyFormat(
5374       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5375       "                                             aaaaaaaaaaaaaaaaaaaaa));");
5376   FormatStyle Style = getLLVMStyle();
5377   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5378   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5379                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
5380                Style);
5381   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5382                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
5383                Style);
5384   verifyFormat("SomeLongVariableName->someFunction(\n"
5385                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
5386                Style);
5387   verifyFormat(
5388       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5389       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5390       Style);
5391   verifyFormat(
5392       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5393       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5394       Style);
5395   verifyFormat(
5396       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5397       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5398       Style);
5399 
5400   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
5401                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
5402                "        b));",
5403                Style);
5404 
5405   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5406   Style.BinPackArguments = false;
5407   Style.BinPackParameters = false;
5408   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5409                "    aaaaaaaaaaa aaaaaaaa,\n"
5410                "    aaaaaaaaa aaaaaaa,\n"
5411                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5412                Style);
5413   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5414                "    aaaaaaaaaaa aaaaaaaaa,\n"
5415                "    aaaaaaaaaaa aaaaaaaaa,\n"
5416                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5417                Style);
5418   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
5419                "    aaaaaaaaaaaaaaa,\n"
5420                "    aaaaaaaaaaaaaaaaaaaaa,\n"
5421                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5422                Style);
5423   verifyFormat(
5424       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
5425       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5426       Style);
5427   verifyFormat(
5428       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
5429       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5430       Style);
5431   verifyFormat(
5432       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5433       "    aaaaaaaaaaaaaaaaaaaaa(\n"
5434       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
5435       "    aaaaaaaaaaaaaaaa);",
5436       Style);
5437   verifyFormat(
5438       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5439       "    aaaaaaaaaaaaaaaaaaaaa(\n"
5440       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
5441       "    aaaaaaaaaaaaaaaa);",
5442       Style);
5443 }
5444 
5445 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
5446   FormatStyle Style = getLLVMStyleWithColumns(40);
5447   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5448                "          bbbbbbbbbbbbbbbbbbbbbb);",
5449                Style);
5450   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5451   Style.AlignOperands = false;
5452   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5453                "          bbbbbbbbbbbbbbbbbbbbbb);",
5454                Style);
5455   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5456   Style.AlignOperands = true;
5457   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5458                "          bbbbbbbbbbbbbbbbbbbbbb);",
5459                Style);
5460   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5461   Style.AlignOperands = false;
5462   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5463                "    bbbbbbbbbbbbbbbbbbbbbb);",
5464                Style);
5465 }
5466 
5467 TEST_F(FormatTest, BreaksConditionalExpressions) {
5468   verifyFormat(
5469       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5470       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5471       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5472   verifyFormat(
5473       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5474       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5475       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5476   verifyFormat(
5477       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5478       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5479   verifyFormat(
5480       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
5481       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5482       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5483   verifyFormat(
5484       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
5485       "                                                    : aaaaaaaaaaaaa);");
5486   verifyFormat(
5487       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5488       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5489       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5490       "                   aaaaaaaaaaaaa);");
5491   verifyFormat(
5492       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5493       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5494       "                   aaaaaaaaaaaaa);");
5495   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5496                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5497                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5498                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5499                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5500   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5501                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5502                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5503                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5504                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5505                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5506                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5507   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5508                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5509                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5510                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5511                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5512   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5513                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5514                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5515   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5516                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5517                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5518                "        : aaaaaaaaaaaaaaaa;");
5519   verifyFormat(
5520       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5521       "    ? aaaaaaaaaaaaaaa\n"
5522       "    : aaaaaaaaaaaaaaa;");
5523   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5524                "          aaaaaaaaa\n"
5525                "      ? b\n"
5526                "      : c);");
5527   verifyFormat("return aaaa == bbbb\n"
5528                "           // comment\n"
5529                "           ? aaaa\n"
5530                "           : bbbb;");
5531   verifyFormat("unsigned Indent =\n"
5532                "    format(TheLine.First,\n"
5533                "           IndentForLevel[TheLine.Level] >= 0\n"
5534                "               ? IndentForLevel[TheLine.Level]\n"
5535                "               : TheLine * 2,\n"
5536                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5537                getLLVMStyleWithColumns(60));
5538   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5539                "                  ? aaaaaaaaaaaaaaa\n"
5540                "                  : bbbbbbbbbbbbbbb //\n"
5541                "                        ? ccccccccccccccc\n"
5542                "                        : ddddddddddddddd;");
5543   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5544                "                  ? aaaaaaaaaaaaaaa\n"
5545                "                  : (bbbbbbbbbbbbbbb //\n"
5546                "                         ? ccccccccccccccc\n"
5547                "                         : ddddddddddddddd);");
5548   verifyFormat(
5549       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5550       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5551       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
5552       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
5553       "                                      : aaaaaaaaaa;");
5554   verifyFormat(
5555       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5556       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
5557       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5558 
5559   FormatStyle NoBinPacking = getLLVMStyle();
5560   NoBinPacking.BinPackArguments = false;
5561   verifyFormat(
5562       "void f() {\n"
5563       "  g(aaa,\n"
5564       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5565       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5566       "        ? aaaaaaaaaaaaaaa\n"
5567       "        : aaaaaaaaaaaaaaa);\n"
5568       "}",
5569       NoBinPacking);
5570   verifyFormat(
5571       "void f() {\n"
5572       "  g(aaa,\n"
5573       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5574       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5575       "        ?: aaaaaaaaaaaaaaa);\n"
5576       "}",
5577       NoBinPacking);
5578 
5579   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
5580                "             // comment.\n"
5581                "             ccccccccccccccccccccccccccccccccccccccc\n"
5582                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5583                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
5584 
5585   // Assignments in conditional expressions. Apparently not uncommon :-(.
5586   verifyFormat("return a != b\n"
5587                "           // comment\n"
5588                "           ? a = b\n"
5589                "           : a = b;");
5590   verifyFormat("return a != b\n"
5591                "           // comment\n"
5592                "           ? a = a != b\n"
5593                "                     // comment\n"
5594                "                     ? a = b\n"
5595                "                     : a\n"
5596                "           : a;\n");
5597   verifyFormat("return a != b\n"
5598                "           // comment\n"
5599                "           ? a\n"
5600                "           : a = a != b\n"
5601                "                     // comment\n"
5602                "                     ? a = b\n"
5603                "                     : a;");
5604 }
5605 
5606 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
5607   FormatStyle Style = getLLVMStyle();
5608   Style.BreakBeforeTernaryOperators = false;
5609   Style.ColumnLimit = 70;
5610   verifyFormat(
5611       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5612       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5613       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5614       Style);
5615   verifyFormat(
5616       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5617       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5618       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5619       Style);
5620   verifyFormat(
5621       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5622       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5623       Style);
5624   verifyFormat(
5625       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
5626       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5627       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5628       Style);
5629   verifyFormat(
5630       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
5631       "                                                      aaaaaaaaaaaaa);",
5632       Style);
5633   verifyFormat(
5634       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5635       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5636       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5637       "                   aaaaaaaaaaaaa);",
5638       Style);
5639   verifyFormat(
5640       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5641       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5642       "                   aaaaaaaaaaaaa);",
5643       Style);
5644   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5645                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5646                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5647                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5648                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5649                Style);
5650   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5651                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5652                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5653                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5654                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5655                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5656                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5657                Style);
5658   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5659                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
5660                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5661                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5662                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5663                Style);
5664   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5665                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5666                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5667                Style);
5668   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5669                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5670                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5671                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5672                Style);
5673   verifyFormat(
5674       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5675       "    aaaaaaaaaaaaaaa :\n"
5676       "    aaaaaaaaaaaaaaa;",
5677       Style);
5678   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5679                "          aaaaaaaaa ?\n"
5680                "      b :\n"
5681                "      c);",
5682                Style);
5683   verifyFormat("unsigned Indent =\n"
5684                "    format(TheLine.First,\n"
5685                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
5686                "               IndentForLevel[TheLine.Level] :\n"
5687                "               TheLine * 2,\n"
5688                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5689                Style);
5690   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5691                "                  aaaaaaaaaaaaaaa :\n"
5692                "                  bbbbbbbbbbbbbbb ? //\n"
5693                "                      ccccccccccccccc :\n"
5694                "                      ddddddddddddddd;",
5695                Style);
5696   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5697                "                  aaaaaaaaaaaaaaa :\n"
5698                "                  (bbbbbbbbbbbbbbb ? //\n"
5699                "                       ccccccccccccccc :\n"
5700                "                       ddddddddddddddd);",
5701                Style);
5702   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5703                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
5704                "            ccccccccccccccccccccccccccc;",
5705                Style);
5706   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5707                "           aaaaa :\n"
5708                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
5709                Style);
5710 }
5711 
5712 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
5713   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
5714                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
5715   verifyFormat("bool a = true, b = false;");
5716 
5717   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5718                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
5719                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
5720                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
5721   verifyFormat(
5722       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5723       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
5724       "     d = e && f;");
5725   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
5726                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
5727   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5728                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
5729   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
5730                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
5731 
5732   FormatStyle Style = getGoogleStyle();
5733   Style.PointerAlignment = FormatStyle::PAS_Left;
5734   Style.DerivePointerAlignment = false;
5735   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5736                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
5737                "    *b = bbbbbbbbbbbbbbbbbbb;",
5738                Style);
5739   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5740                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
5741                Style);
5742   verifyFormat("vector<int*> a, b;", Style);
5743   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
5744 }
5745 
5746 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
5747   verifyFormat("arr[foo ? bar : baz];");
5748   verifyFormat("f()[foo ? bar : baz];");
5749   verifyFormat("(a + b)[foo ? bar : baz];");
5750   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
5751 }
5752 
5753 TEST_F(FormatTest, AlignsStringLiterals) {
5754   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
5755                "                                      \"short literal\");");
5756   verifyFormat(
5757       "looooooooooooooooooooooooongFunction(\n"
5758       "    \"short literal\"\n"
5759       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
5760   verifyFormat("someFunction(\"Always break between multi-line\"\n"
5761                "             \" string literals\",\n"
5762                "             and, other, parameters);");
5763   EXPECT_EQ("fun + \"1243\" /* comment */\n"
5764             "      \"5678\";",
5765             format("fun + \"1243\" /* comment */\n"
5766                    "    \"5678\";",
5767                    getLLVMStyleWithColumns(28)));
5768   EXPECT_EQ(
5769       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
5770       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
5771       "         \"aaaaaaaaaaaaaaaa\";",
5772       format("aaaaaa ="
5773              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
5774              "aaaaaaaaaaaaaaaaaaaaa\" "
5775              "\"aaaaaaaaaaaaaaaa\";"));
5776   verifyFormat("a = a + \"a\"\n"
5777                "        \"a\"\n"
5778                "        \"a\";");
5779   verifyFormat("f(\"a\", \"b\"\n"
5780                "       \"c\");");
5781 
5782   verifyFormat(
5783       "#define LL_FORMAT \"ll\"\n"
5784       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
5785       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
5786 
5787   verifyFormat("#define A(X)          \\\n"
5788                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
5789                "  \"ccccc\"",
5790                getLLVMStyleWithColumns(23));
5791   verifyFormat("#define A \"def\"\n"
5792                "f(\"abc\" A \"ghi\"\n"
5793                "  \"jkl\");");
5794 
5795   verifyFormat("f(L\"a\"\n"
5796                "  L\"b\");");
5797   verifyFormat("#define A(X)            \\\n"
5798                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
5799                "  L\"ccccc\"",
5800                getLLVMStyleWithColumns(25));
5801 
5802   verifyFormat("f(@\"a\"\n"
5803                "  @\"b\");");
5804   verifyFormat("NSString s = @\"a\"\n"
5805                "             @\"b\"\n"
5806                "             @\"c\";");
5807   verifyFormat("NSString s = @\"a\"\n"
5808                "              \"b\"\n"
5809                "              \"c\";");
5810 }
5811 
5812 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5813   FormatStyle Style = getLLVMStyle();
5814   // No declarations or definitions should be moved to own line.
5815   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5816   verifyFormat("class A {\n"
5817                "  int f() { return 1; }\n"
5818                "  int g();\n"
5819                "};\n"
5820                "int f() { return 1; }\n"
5821                "int g();\n",
5822                Style);
5823 
5824   // All declarations and definitions should have the return type moved to its
5825   // own
5826   // line.
5827   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5828   verifyFormat("class E {\n"
5829                "  int\n"
5830                "  f() {\n"
5831                "    return 1;\n"
5832                "  }\n"
5833                "  int\n"
5834                "  g();\n"
5835                "};\n"
5836                "int\n"
5837                "f() {\n"
5838                "  return 1;\n"
5839                "}\n"
5840                "int\n"
5841                "g();\n",
5842                Style);
5843 
5844   // Top-level definitions, and no kinds of declarations should have the
5845   // return type moved to its own line.
5846   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5847   verifyFormat("class B {\n"
5848                "  int f() { return 1; }\n"
5849                "  int g();\n"
5850                "};\n"
5851                "int\n"
5852                "f() {\n"
5853                "  return 1;\n"
5854                "}\n"
5855                "int g();\n",
5856                Style);
5857 
5858   // Top-level definitions and declarations should have the return type moved
5859   // to its own line.
5860   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5861   verifyFormat("class C {\n"
5862                "  int f() { return 1; }\n"
5863                "  int g();\n"
5864                "};\n"
5865                "int\n"
5866                "f() {\n"
5867                "  return 1;\n"
5868                "}\n"
5869                "int\n"
5870                "g();\n",
5871                Style);
5872 
5873   // All definitions should have the return type moved to its own line, but no
5874   // kinds of declarations.
5875   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5876   verifyFormat("class D {\n"
5877                "  int\n"
5878                "  f() {\n"
5879                "    return 1;\n"
5880                "  }\n"
5881                "  int g();\n"
5882                "};\n"
5883                "int\n"
5884                "f() {\n"
5885                "  return 1;\n"
5886                "}\n"
5887                "int g();\n",
5888                Style);
5889   verifyFormat("const char *\n"
5890                "f(void) {\n" // Break here.
5891                "  return \"\";\n"
5892                "}\n"
5893                "const char *bar(void);\n", // No break here.
5894                Style);
5895   verifyFormat("template <class T>\n"
5896                "T *\n"
5897                "f(T &c) {\n" // Break here.
5898                "  return NULL;\n"
5899                "}\n"
5900                "template <class T> T *f(T &c);\n", // No break here.
5901                Style);
5902   verifyFormat("class C {\n"
5903                "  int\n"
5904                "  operator+() {\n"
5905                "    return 1;\n"
5906                "  }\n"
5907                "  int\n"
5908                "  operator()() {\n"
5909                "    return 1;\n"
5910                "  }\n"
5911                "};\n",
5912                Style);
5913   verifyFormat("void\n"
5914                "A::operator()() {}\n"
5915                "void\n"
5916                "A::operator>>() {}\n"
5917                "void\n"
5918                "A::operator+() {}\n",
5919                Style);
5920   verifyFormat("void *operator new(std::size_t s);", // No break here.
5921                Style);
5922   verifyFormat("void *\n"
5923                "operator new(std::size_t s) {}",
5924                Style);
5925   verifyFormat("void *\n"
5926                "operator delete[](void *ptr) {}",
5927                Style);
5928   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5929   verifyFormat("const char *\n"
5930                "f(void)\n" // Break here.
5931                "{\n"
5932                "  return \"\";\n"
5933                "}\n"
5934                "const char *bar(void);\n", // No break here.
5935                Style);
5936   verifyFormat("template <class T>\n"
5937                "T *\n"     // Problem here: no line break
5938                "f(T &c)\n" // Break here.
5939                "{\n"
5940                "  return NULL;\n"
5941                "}\n"
5942                "template <class T> T *f(T &c);\n", // No break here.
5943                Style);
5944   verifyFormat("int\n"
5945                "foo(A<bool> a)\n"
5946                "{\n"
5947                "  return a;\n"
5948                "}\n",
5949                Style);
5950   verifyFormat("int\n"
5951                "foo(A<8> a)\n"
5952                "{\n"
5953                "  return a;\n"
5954                "}\n",
5955                Style);
5956   verifyFormat("int\n"
5957                "foo(A<B<bool>, 8> a)\n"
5958                "{\n"
5959                "  return a;\n"
5960                "}\n",
5961                Style);
5962   verifyFormat("int\n"
5963                "foo(A<B<8>, bool> a)\n"
5964                "{\n"
5965                "  return a;\n"
5966                "}\n",
5967                Style);
5968   verifyFormat("int\n"
5969                "foo(A<B<bool>, bool> a)\n"
5970                "{\n"
5971                "  return a;\n"
5972                "}\n",
5973                Style);
5974   verifyFormat("int\n"
5975                "foo(A<B<8>, 8> a)\n"
5976                "{\n"
5977                "  return a;\n"
5978                "}\n",
5979                Style);
5980 
5981   Style = getGNUStyle();
5982 
5983   // Test for comments at the end of function declarations.
5984   verifyFormat("void\n"
5985                "foo (int a, /*abc*/ int b) // def\n"
5986                "{\n"
5987                "}\n",
5988                Style);
5989 
5990   verifyFormat("void\n"
5991                "foo (int a, /* abc */ int b) /* def */\n"
5992                "{\n"
5993                "}\n",
5994                Style);
5995 
5996   // Definitions that should not break after return type
5997   verifyFormat("void foo (int a, int b); // def\n", Style);
5998   verifyFormat("void foo (int a, int b); /* def */\n", Style);
5999   verifyFormat("void foo (int a, int b);\n", Style);
6000 }
6001 
6002 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
6003   FormatStyle NoBreak = getLLVMStyle();
6004   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
6005   FormatStyle Break = getLLVMStyle();
6006   Break.AlwaysBreakBeforeMultilineStrings = true;
6007   verifyFormat("aaaa = \"bbbb\"\n"
6008                "       \"cccc\";",
6009                NoBreak);
6010   verifyFormat("aaaa =\n"
6011                "    \"bbbb\"\n"
6012                "    \"cccc\";",
6013                Break);
6014   verifyFormat("aaaa(\"bbbb\"\n"
6015                "     \"cccc\");",
6016                NoBreak);
6017   verifyFormat("aaaa(\n"
6018                "    \"bbbb\"\n"
6019                "    \"cccc\");",
6020                Break);
6021   verifyFormat("aaaa(qqq, \"bbbb\"\n"
6022                "          \"cccc\");",
6023                NoBreak);
6024   verifyFormat("aaaa(qqq,\n"
6025                "     \"bbbb\"\n"
6026                "     \"cccc\");",
6027                Break);
6028   verifyFormat("aaaa(qqq,\n"
6029                "     L\"bbbb\"\n"
6030                "     L\"cccc\");",
6031                Break);
6032   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
6033                "                      \"bbbb\"));",
6034                Break);
6035   verifyFormat("string s = someFunction(\n"
6036                "    \"abc\"\n"
6037                "    \"abc\");",
6038                Break);
6039 
6040   // As we break before unary operators, breaking right after them is bad.
6041   verifyFormat("string foo = abc ? \"x\"\n"
6042                "                   \"blah blah blah blah blah blah\"\n"
6043                "                 : \"y\";",
6044                Break);
6045 
6046   // Don't break if there is no column gain.
6047   verifyFormat("f(\"aaaa\"\n"
6048                "  \"bbbb\");",
6049                Break);
6050 
6051   // Treat literals with escaped newlines like multi-line string literals.
6052   EXPECT_EQ("x = \"a\\\n"
6053             "b\\\n"
6054             "c\";",
6055             format("x = \"a\\\n"
6056                    "b\\\n"
6057                    "c\";",
6058                    NoBreak));
6059   EXPECT_EQ("xxxx =\n"
6060             "    \"a\\\n"
6061             "b\\\n"
6062             "c\";",
6063             format("xxxx = \"a\\\n"
6064                    "b\\\n"
6065                    "c\";",
6066                    Break));
6067 
6068   EXPECT_EQ("NSString *const kString =\n"
6069             "    @\"aaaa\"\n"
6070             "    @\"bbbb\";",
6071             format("NSString *const kString = @\"aaaa\"\n"
6072                    "@\"bbbb\";",
6073                    Break));
6074 
6075   Break.ColumnLimit = 0;
6076   verifyFormat("const char *hello = \"hello llvm\";", Break);
6077 }
6078 
6079 TEST_F(FormatTest, AlignsPipes) {
6080   verifyFormat(
6081       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6082       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6083       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6084   verifyFormat(
6085       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
6086       "                     << aaaaaaaaaaaaaaaaaaaa;");
6087   verifyFormat(
6088       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6089       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6090   verifyFormat(
6091       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6092       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6093   verifyFormat(
6094       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
6095       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
6096       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
6097   verifyFormat(
6098       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6099       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6100       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6101   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6102                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6103                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6104                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6105   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
6106                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
6107   verifyFormat(
6108       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6109       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6110   verifyFormat(
6111       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
6112       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
6113 
6114   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
6115                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
6116   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6117                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6118                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
6119                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
6120   verifyFormat("LOG_IF(aaa == //\n"
6121                "       bbb)\n"
6122                "    << a << b;");
6123 
6124   // But sometimes, breaking before the first "<<" is desirable.
6125   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6126                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
6127   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
6128                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6129                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6130   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
6131                "    << BEF << IsTemplate << Description << E->getType();");
6132   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6133                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6134                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6135   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6136                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6137                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6138                "    << aaa;");
6139 
6140   verifyFormat(
6141       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6142       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6143 
6144   // Incomplete string literal.
6145   EXPECT_EQ("llvm::errs() << \"\n"
6146             "             << a;",
6147             format("llvm::errs() << \"\n<<a;"));
6148 
6149   verifyFormat("void f() {\n"
6150                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
6151                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
6152                "}");
6153 
6154   // Handle 'endl'.
6155   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
6156                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
6157   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
6158 
6159   // Handle '\n'.
6160   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
6161                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
6162   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
6163                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
6164   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
6165                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
6166   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
6167 }
6168 
6169 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
6170   verifyFormat("return out << \"somepacket = {\\n\"\n"
6171                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
6172                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
6173                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
6174                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
6175                "           << \"}\";");
6176 
6177   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
6178                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
6179                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
6180   verifyFormat(
6181       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
6182       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
6183       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
6184       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
6185       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
6186   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
6187                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6188   verifyFormat(
6189       "void f() {\n"
6190       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
6191       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6192       "}");
6193 
6194   // Breaking before the first "<<" is generally not desirable.
6195   verifyFormat(
6196       "llvm::errs()\n"
6197       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6198       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6199       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6200       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6201       getLLVMStyleWithColumns(70));
6202   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
6203                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6204                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
6205                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6206                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
6207                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6208                getLLVMStyleWithColumns(70));
6209 
6210   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
6211                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
6212                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
6213   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
6214                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
6215                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
6216   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
6217                "           (aaaa + aaaa);",
6218                getLLVMStyleWithColumns(40));
6219   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
6220                "                  (aaaaaaa + aaaaa));",
6221                getLLVMStyleWithColumns(40));
6222   verifyFormat(
6223       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
6224       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
6225       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
6226 }
6227 
6228 TEST_F(FormatTest, UnderstandsEquals) {
6229   verifyFormat(
6230       "aaaaaaaaaaaaaaaaa =\n"
6231       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6232   verifyFormat(
6233       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6234       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
6235   verifyFormat(
6236       "if (a) {\n"
6237       "  f();\n"
6238       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6239       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
6240       "}");
6241 
6242   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6243                "        100000000 + 10000000) {\n}");
6244 }
6245 
6246 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
6247   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
6248                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
6249 
6250   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
6251                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
6252 
6253   verifyFormat(
6254       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
6255       "                                                          Parameter2);");
6256 
6257   verifyFormat(
6258       "ShortObject->shortFunction(\n"
6259       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
6260       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
6261 
6262   verifyFormat("loooooooooooooongFunction(\n"
6263                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
6264 
6265   verifyFormat(
6266       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
6267       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
6268 
6269   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
6270                "    .WillRepeatedly(Return(SomeValue));");
6271   verifyFormat("void f() {\n"
6272                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
6273                "      .Times(2)\n"
6274                "      .WillRepeatedly(Return(SomeValue));\n"
6275                "}");
6276   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
6277                "    ccccccccccccccccccccccc);");
6278   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6279                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6280                "          .aaaaa(aaaaa),\n"
6281                "      aaaaaaaaaaaaaaaaaaaaa);");
6282   verifyFormat("void f() {\n"
6283                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6284                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
6285                "}");
6286   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6287                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6288                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6289                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6290                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6291   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6292                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6293                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6294                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
6295                "}");
6296 
6297   // Here, it is not necessary to wrap at "." or "->".
6298   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
6299                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
6300   verifyFormat(
6301       "aaaaaaaaaaa->aaaaaaaaa(\n"
6302       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6303       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
6304 
6305   verifyFormat(
6306       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6307       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
6308   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
6309                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
6310   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
6311                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
6312 
6313   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6314                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6315                "    .a();");
6316 
6317   FormatStyle NoBinPacking = getLLVMStyle();
6318   NoBinPacking.BinPackParameters = false;
6319   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
6320                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
6321                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
6322                "                         aaaaaaaaaaaaaaaaaaa,\n"
6323                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6324                NoBinPacking);
6325 
6326   // If there is a subsequent call, change to hanging indentation.
6327   verifyFormat(
6328       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6329       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
6330       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6331   verifyFormat(
6332       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6333       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
6334   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6335                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6336                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6337   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6338                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6339                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
6340 }
6341 
6342 TEST_F(FormatTest, WrapsTemplateDeclarations) {
6343   verifyFormat("template <typename T>\n"
6344                "virtual void loooooooooooongFunction(int Param1, int Param2);");
6345   verifyFormat("template <typename T>\n"
6346                "// T should be one of {A, B}.\n"
6347                "virtual void loooooooooooongFunction(int Param1, int Param2);");
6348   verifyFormat(
6349       "template <typename T>\n"
6350       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
6351   verifyFormat("template <typename T>\n"
6352                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
6353                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
6354   verifyFormat(
6355       "template <typename T>\n"
6356       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
6357       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
6358   verifyFormat(
6359       "template <typename T>\n"
6360       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
6361       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
6362       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6363   verifyFormat("template <typename T>\n"
6364                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6365                "    int aaaaaaaaaaaaaaaaaaaaaa);");
6366   verifyFormat(
6367       "template <typename T1, typename T2 = char, typename T3 = char,\n"
6368       "          typename T4 = char>\n"
6369       "void f();");
6370   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
6371                "          template <typename> class cccccccccccccccccccccc,\n"
6372                "          typename ddddddddddddd>\n"
6373                "class C {};");
6374   verifyFormat(
6375       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
6376       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6377 
6378   verifyFormat("void f() {\n"
6379                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
6380                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
6381                "}");
6382 
6383   verifyFormat("template <typename T> class C {};");
6384   verifyFormat("template <typename T> void f();");
6385   verifyFormat("template <typename T> void f() {}");
6386   verifyFormat(
6387       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
6388       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6389       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
6390       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
6391       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6392       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
6393       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
6394       getLLVMStyleWithColumns(72));
6395   EXPECT_EQ("static_cast<A< //\n"
6396             "    B> *>(\n"
6397             "\n"
6398             ");",
6399             format("static_cast<A<//\n"
6400                    "    B>*>(\n"
6401                    "\n"
6402                    "    );"));
6403   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6404                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
6405 
6406   FormatStyle AlwaysBreak = getLLVMStyle();
6407   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
6408   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
6409   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
6410   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
6411   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6412                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
6413                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
6414   verifyFormat("template <template <typename> class Fooooooo,\n"
6415                "          template <typename> class Baaaaaaar>\n"
6416                "struct C {};",
6417                AlwaysBreak);
6418   verifyFormat("template <typename T> // T can be A, B or C.\n"
6419                "struct C {};",
6420                AlwaysBreak);
6421   verifyFormat("template <enum E> class A {\n"
6422                "public:\n"
6423                "  E *f();\n"
6424                "};");
6425 
6426   FormatStyle NeverBreak = getLLVMStyle();
6427   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
6428   verifyFormat("template <typename T> class C {};", NeverBreak);
6429   verifyFormat("template <typename T> void f();", NeverBreak);
6430   verifyFormat("template <typename T> void f() {}", NeverBreak);
6431   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
6432                NeverBreak);
6433   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6434                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
6435                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
6436                NeverBreak);
6437   verifyFormat("template <template <typename> class Fooooooo,\n"
6438                "          template <typename> class Baaaaaaar>\n"
6439                "struct C {};",
6440                NeverBreak);
6441   verifyFormat("template <typename T> // T can be A, B or C.\n"
6442                "struct C {};",
6443                NeverBreak);
6444   verifyFormat("template <enum E> class A {\n"
6445                "public:\n"
6446                "  E *f();\n"
6447                "};", NeverBreak);
6448   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
6449   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
6450                NeverBreak);
6451 }
6452 
6453 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
6454   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6455   Style.ColumnLimit = 60;
6456   EXPECT_EQ("// Baseline - no comments.\n"
6457             "template <\n"
6458             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
6459             "void f() {}",
6460             format("// Baseline - no comments.\n"
6461                    "template <\n"
6462                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
6463                    "void f() {}",
6464                    Style));
6465 
6466   EXPECT_EQ("template <\n"
6467             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
6468             "void f() {}",
6469             format("template <\n"
6470                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
6471                    "void f() {}",
6472                    Style));
6473 
6474   EXPECT_EQ(
6475       "template <\n"
6476       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
6477       "void f() {}",
6478       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
6479              "void f() {}",
6480              Style));
6481 
6482   EXPECT_EQ(
6483       "template <\n"
6484       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
6485       "                                               // multiline\n"
6486       "void f() {}",
6487       format("template <\n"
6488              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
6489              "                                              // multiline\n"
6490              "void f() {}",
6491              Style));
6492 
6493   EXPECT_EQ(
6494       "template <typename aaaaaaaaaa<\n"
6495       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
6496       "void f() {}",
6497       format(
6498           "template <\n"
6499           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
6500           "void f() {}",
6501           Style));
6502 }
6503 
6504 TEST_F(FormatTest, WrapsTemplateParameters) {
6505   FormatStyle Style = getLLVMStyle();
6506   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6507   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6508   verifyFormat(
6509       "template <typename... a> struct q {};\n"
6510       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
6511       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
6512       "    y;",
6513       Style);
6514   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6515   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6516   verifyFormat(
6517       "template <typename... a> struct r {};\n"
6518       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
6519       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
6520       "    y;",
6521       Style);
6522   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6523   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6524   verifyFormat(
6525       "template <typename... a> struct s {};\n"
6526       "extern s<\n"
6527       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6528       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
6529       "    y;",
6530       Style);
6531   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6532   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6533   verifyFormat(
6534       "template <typename... a> struct t {};\n"
6535       "extern t<\n"
6536       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6537       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
6538       "    y;",
6539       Style);
6540 }
6541 
6542 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
6543   verifyFormat(
6544       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6545       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6546   verifyFormat(
6547       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6548       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6549       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
6550 
6551   // FIXME: Should we have the extra indent after the second break?
6552   verifyFormat(
6553       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6554       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6555       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6556 
6557   verifyFormat(
6558       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
6559       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
6560 
6561   // Breaking at nested name specifiers is generally not desirable.
6562   verifyFormat(
6563       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6564       "    aaaaaaaaaaaaaaaaaaaaaaa);");
6565 
6566   verifyFormat(
6567       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
6568       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6569       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6570       "                   aaaaaaaaaaaaaaaaaaaaa);",
6571       getLLVMStyleWithColumns(74));
6572 
6573   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6574                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6575                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6576 }
6577 
6578 TEST_F(FormatTest, UnderstandsTemplateParameters) {
6579   verifyFormat("A<int> a;");
6580   verifyFormat("A<A<A<int>>> a;");
6581   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
6582   verifyFormat("bool x = a < 1 || 2 > a;");
6583   verifyFormat("bool x = 5 < f<int>();");
6584   verifyFormat("bool x = f<int>() > 5;");
6585   verifyFormat("bool x = 5 < a<int>::x;");
6586   verifyFormat("bool x = a < 4 ? a > 2 : false;");
6587   verifyFormat("bool x = f() ? a < 2 : a > 2;");
6588 
6589   verifyGoogleFormat("A<A<int>> a;");
6590   verifyGoogleFormat("A<A<A<int>>> a;");
6591   verifyGoogleFormat("A<A<A<A<int>>>> a;");
6592   verifyGoogleFormat("A<A<int> > a;");
6593   verifyGoogleFormat("A<A<A<int> > > a;");
6594   verifyGoogleFormat("A<A<A<A<int> > > > a;");
6595   verifyGoogleFormat("A<::A<int>> a;");
6596   verifyGoogleFormat("A<::A> a;");
6597   verifyGoogleFormat("A< ::A> a;");
6598   verifyGoogleFormat("A< ::A<int> > a;");
6599   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
6600   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
6601   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
6602   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
6603   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
6604             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
6605 
6606   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
6607 
6608   verifyFormat("test >> a >> b;");
6609   verifyFormat("test << a >> b;");
6610 
6611   verifyFormat("f<int>();");
6612   verifyFormat("template <typename T> void f() {}");
6613   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
6614   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
6615                "sizeof(char)>::type>;");
6616   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
6617   verifyFormat("f(a.operator()<A>());");
6618   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6619                "      .template operator()<A>());",
6620                getLLVMStyleWithColumns(35));
6621 
6622   // Not template parameters.
6623   verifyFormat("return a < b && c > d;");
6624   verifyFormat("void f() {\n"
6625                "  while (a < b && c > d) {\n"
6626                "  }\n"
6627                "}");
6628   verifyFormat("template <typename... Types>\n"
6629                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
6630 
6631   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6632                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
6633                getLLVMStyleWithColumns(60));
6634   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
6635   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
6636   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
6637 }
6638 
6639 TEST_F(FormatTest, BitshiftOperatorWidth) {
6640   EXPECT_EQ("int a = 1 << 2; /* foo\n"
6641             "                   bar */",
6642             format("int    a=1<<2;  /* foo\n"
6643                    "                   bar */"));
6644 
6645   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
6646             "                     bar */",
6647             format("int  b  =256>>1 ;  /* foo\n"
6648                    "                      bar */"));
6649 }
6650 
6651 TEST_F(FormatTest, UnderstandsBinaryOperators) {
6652   verifyFormat("COMPARE(a, ==, b);");
6653   verifyFormat("auto s = sizeof...(Ts) - 1;");
6654 }
6655 
6656 TEST_F(FormatTest, UnderstandsPointersToMembers) {
6657   verifyFormat("int A::*x;");
6658   verifyFormat("int (S::*func)(void *);");
6659   verifyFormat("void f() { int (S::*func)(void *); }");
6660   verifyFormat("typedef bool *(Class::*Member)() const;");
6661   verifyFormat("void f() {\n"
6662                "  (a->*f)();\n"
6663                "  a->*x;\n"
6664                "  (a.*f)();\n"
6665                "  ((*a).*f)();\n"
6666                "  a.*x;\n"
6667                "}");
6668   verifyFormat("void f() {\n"
6669                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6670                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
6671                "}");
6672   verifyFormat(
6673       "(aaaaaaaaaa->*bbbbbbb)(\n"
6674       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6675   FormatStyle Style = getLLVMStyle();
6676   Style.PointerAlignment = FormatStyle::PAS_Left;
6677   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
6678 }
6679 
6680 TEST_F(FormatTest, UnderstandsUnaryOperators) {
6681   verifyFormat("int a = -2;");
6682   verifyFormat("f(-1, -2, -3);");
6683   verifyFormat("a[-1] = 5;");
6684   verifyFormat("int a = 5 + -2;");
6685   verifyFormat("if (i == -1) {\n}");
6686   verifyFormat("if (i != -1) {\n}");
6687   verifyFormat("if (i > -1) {\n}");
6688   verifyFormat("if (i < -1) {\n}");
6689   verifyFormat("++(a->f());");
6690   verifyFormat("--(a->f());");
6691   verifyFormat("(a->f())++;");
6692   verifyFormat("a[42]++;");
6693   verifyFormat("if (!(a->f())) {\n}");
6694   verifyFormat("if (!+i) {\n}");
6695   verifyFormat("~&a;");
6696 
6697   verifyFormat("a-- > b;");
6698   verifyFormat("b ? -a : c;");
6699   verifyFormat("n * sizeof char16;");
6700   verifyFormat("n * alignof char16;", getGoogleStyle());
6701   verifyFormat("sizeof(char);");
6702   verifyFormat("alignof(char);", getGoogleStyle());
6703 
6704   verifyFormat("return -1;");
6705   verifyFormat("switch (a) {\n"
6706                "case -1:\n"
6707                "  break;\n"
6708                "}");
6709   verifyFormat("#define X -1");
6710   verifyFormat("#define X -kConstant");
6711 
6712   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
6713   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
6714 
6715   verifyFormat("int a = /* confusing comment */ -1;");
6716   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
6717   verifyFormat("int a = i /* confusing comment */++;");
6718 }
6719 
6720 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
6721   verifyFormat("if (!aaaaaaaaaa( // break\n"
6722                "        aaaaa)) {\n"
6723                "}");
6724   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
6725                "    aaaaa));");
6726   verifyFormat("*aaa = aaaaaaa( // break\n"
6727                "    bbbbbb);");
6728 }
6729 
6730 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
6731   verifyFormat("bool operator<();");
6732   verifyFormat("bool operator>();");
6733   verifyFormat("bool operator=();");
6734   verifyFormat("bool operator==();");
6735   verifyFormat("bool operator!=();");
6736   verifyFormat("int operator+();");
6737   verifyFormat("int operator++();");
6738   verifyFormat("int operator++(int) volatile noexcept;");
6739   verifyFormat("bool operator,();");
6740   verifyFormat("bool operator();");
6741   verifyFormat("bool operator()();");
6742   verifyFormat("bool operator[]();");
6743   verifyFormat("operator bool();");
6744   verifyFormat("operator int();");
6745   verifyFormat("operator void *();");
6746   verifyFormat("operator SomeType<int>();");
6747   verifyFormat("operator SomeType<int, int>();");
6748   verifyFormat("operator SomeType<SomeType<int>>();");
6749   verifyFormat("void *operator new(std::size_t size);");
6750   verifyFormat("void *operator new[](std::size_t size);");
6751   verifyFormat("void operator delete(void *ptr);");
6752   verifyFormat("void operator delete[](void *ptr);");
6753   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
6754                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
6755   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
6756                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
6757 
6758   verifyFormat(
6759       "ostream &operator<<(ostream &OutputStream,\n"
6760       "                    SomeReallyLongType WithSomeReallyLongValue);");
6761   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
6762                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
6763                "  return left.group < right.group;\n"
6764                "}");
6765   verifyFormat("SomeType &operator=(const SomeType &S);");
6766   verifyFormat("f.template operator()<int>();");
6767 
6768   verifyGoogleFormat("operator void*();");
6769   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
6770   verifyGoogleFormat("operator ::A();");
6771 
6772   verifyFormat("using A::operator+;");
6773   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
6774                "int i;");
6775 }
6776 
6777 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
6778   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
6779   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
6780   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
6781   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
6782   verifyFormat("Deleted &operator=(const Deleted &) &;");
6783   verifyFormat("Deleted &operator=(const Deleted &) &&;");
6784   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
6785   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
6786   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
6787   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
6788   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
6789   verifyFormat("void Fn(T const &) const &;");
6790   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
6791   verifyFormat("template <typename T>\n"
6792                "void F(T) && = delete;",
6793                getGoogleStyle());
6794 
6795   FormatStyle AlignLeft = getLLVMStyle();
6796   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
6797   verifyFormat("void A::b() && {}", AlignLeft);
6798   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
6799   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
6800                AlignLeft);
6801   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
6802   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
6803   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
6804   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
6805   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
6806   verifyFormat("auto Function(T) & -> void;", AlignLeft);
6807   verifyFormat("void Fn(T const&) const&;", AlignLeft);
6808   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
6809 
6810   FormatStyle Spaces = getLLVMStyle();
6811   Spaces.SpacesInCStyleCastParentheses = true;
6812   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
6813   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
6814   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
6815   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
6816 
6817   Spaces.SpacesInCStyleCastParentheses = false;
6818   Spaces.SpacesInParentheses = true;
6819   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
6820   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
6821   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
6822   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
6823 }
6824 
6825 TEST_F(FormatTest, UnderstandsNewAndDelete) {
6826   verifyFormat("void f() {\n"
6827                "  A *a = new A;\n"
6828                "  A *a = new (placement) A;\n"
6829                "  delete a;\n"
6830                "  delete (A *)a;\n"
6831                "}");
6832   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6833                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6834   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6835                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6836                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6837   verifyFormat("delete[] h->p;");
6838 }
6839 
6840 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
6841   verifyFormat("int *f(int *a) {}");
6842   verifyFormat("int main(int argc, char **argv) {}");
6843   verifyFormat("Test::Test(int b) : a(b * b) {}");
6844   verifyIndependentOfContext("f(a, *a);");
6845   verifyFormat("void g() { f(*a); }");
6846   verifyIndependentOfContext("int a = b * 10;");
6847   verifyIndependentOfContext("int a = 10 * b;");
6848   verifyIndependentOfContext("int a = b * c;");
6849   verifyIndependentOfContext("int a += b * c;");
6850   verifyIndependentOfContext("int a -= b * c;");
6851   verifyIndependentOfContext("int a *= b * c;");
6852   verifyIndependentOfContext("int a /= b * c;");
6853   verifyIndependentOfContext("int a = *b;");
6854   verifyIndependentOfContext("int a = *b * c;");
6855   verifyIndependentOfContext("int a = b * *c;");
6856   verifyIndependentOfContext("int a = b * (10);");
6857   verifyIndependentOfContext("S << b * (10);");
6858   verifyIndependentOfContext("return 10 * b;");
6859   verifyIndependentOfContext("return *b * *c;");
6860   verifyIndependentOfContext("return a & ~b;");
6861   verifyIndependentOfContext("f(b ? *c : *d);");
6862   verifyIndependentOfContext("int a = b ? *c : *d;");
6863   verifyIndependentOfContext("*b = a;");
6864   verifyIndependentOfContext("a * ~b;");
6865   verifyIndependentOfContext("a * !b;");
6866   verifyIndependentOfContext("a * +b;");
6867   verifyIndependentOfContext("a * -b;");
6868   verifyIndependentOfContext("a * ++b;");
6869   verifyIndependentOfContext("a * --b;");
6870   verifyIndependentOfContext("a[4] * b;");
6871   verifyIndependentOfContext("a[a * a] = 1;");
6872   verifyIndependentOfContext("f() * b;");
6873   verifyIndependentOfContext("a * [self dostuff];");
6874   verifyIndependentOfContext("int x = a * (a + b);");
6875   verifyIndependentOfContext("(a *)(a + b);");
6876   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6877   verifyIndependentOfContext("int *pa = (int *)&a;");
6878   verifyIndependentOfContext("return sizeof(int **);");
6879   verifyIndependentOfContext("return sizeof(int ******);");
6880   verifyIndependentOfContext("return (int **&)a;");
6881   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6882   verifyFormat("void f(Type (*parameter)[10]) {}");
6883   verifyFormat("void f(Type (&parameter)[10]) {}");
6884   verifyGoogleFormat("return sizeof(int**);");
6885   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6886   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6887   verifyFormat("auto a = [](int **&, int ***) {};");
6888   verifyFormat("auto PointerBinding = [](const char *S) {};");
6889   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6890   verifyFormat("[](const decltype(*a) &value) {}");
6891   verifyFormat("decltype(a * b) F();");
6892   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6893   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6894   verifyIndependentOfContext("typedef void (*f)(int *a);");
6895   verifyIndependentOfContext("int i{a * b};");
6896   verifyIndependentOfContext("aaa && aaa->f();");
6897   verifyIndependentOfContext("int x = ~*p;");
6898   verifyFormat("Constructor() : a(a), area(width * height) {}");
6899   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6900   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6901   verifyFormat("void f() { f(a, c * d); }");
6902   verifyFormat("void f() { f(new a(), c * d); }");
6903   verifyFormat("void f(const MyOverride &override);");
6904   verifyFormat("void f(const MyFinal &final);");
6905   verifyIndependentOfContext("bool a = f() && override.f();");
6906   verifyIndependentOfContext("bool a = f() && final.f();");
6907 
6908   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6909 
6910   verifyIndependentOfContext("A<int *> a;");
6911   verifyIndependentOfContext("A<int **> a;");
6912   verifyIndependentOfContext("A<int *, int *> a;");
6913   verifyIndependentOfContext("A<int *[]> a;");
6914   verifyIndependentOfContext(
6915       "const char *const p = reinterpret_cast<const char *const>(q);");
6916   verifyIndependentOfContext("A<int **, int **> a;");
6917   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6918   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6919   verifyFormat("for (; a && b;) {\n}");
6920   verifyFormat("bool foo = true && [] { return false; }();");
6921 
6922   verifyFormat(
6923       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6924       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6925 
6926   verifyGoogleFormat("int const* a = &b;");
6927   verifyGoogleFormat("**outparam = 1;");
6928   verifyGoogleFormat("*outparam = a * b;");
6929   verifyGoogleFormat("int main(int argc, char** argv) {}");
6930   verifyGoogleFormat("A<int*> a;");
6931   verifyGoogleFormat("A<int**> a;");
6932   verifyGoogleFormat("A<int*, int*> a;");
6933   verifyGoogleFormat("A<int**, int**> a;");
6934   verifyGoogleFormat("f(b ? *c : *d);");
6935   verifyGoogleFormat("int a = b ? *c : *d;");
6936   verifyGoogleFormat("Type* t = **x;");
6937   verifyGoogleFormat("Type* t = *++*x;");
6938   verifyGoogleFormat("*++*x;");
6939   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6940   verifyGoogleFormat("Type* t = x++ * y;");
6941   verifyGoogleFormat(
6942       "const char* const p = reinterpret_cast<const char* const>(q);");
6943   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6944   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6945   verifyGoogleFormat("template <typename T>\n"
6946                      "void f(int i = 0, SomeType** temps = NULL);");
6947 
6948   FormatStyle Left = getLLVMStyle();
6949   Left.PointerAlignment = FormatStyle::PAS_Left;
6950   verifyFormat("x = *a(x) = *a(y);", Left);
6951   verifyFormat("for (;; *a = b) {\n}", Left);
6952   verifyFormat("return *this += 1;", Left);
6953   verifyFormat("throw *x;", Left);
6954   verifyFormat("delete *x;", Left);
6955   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6956   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6957   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6958 
6959   verifyIndependentOfContext("a = *(x + y);");
6960   verifyIndependentOfContext("a = &(x + y);");
6961   verifyIndependentOfContext("*(x + y).call();");
6962   verifyIndependentOfContext("&(x + y)->call();");
6963   verifyFormat("void f() { &(*I).first; }");
6964 
6965   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6966   verifyFormat(
6967       "int *MyValues = {\n"
6968       "    *A, // Operator detection might be confused by the '{'\n"
6969       "    *BB // Operator detection might be confused by previous comment\n"
6970       "};");
6971 
6972   verifyIndependentOfContext("if (int *a = &b)");
6973   verifyIndependentOfContext("if (int &a = *b)");
6974   verifyIndependentOfContext("if (a & b[i])");
6975   verifyIndependentOfContext("if constexpr (a & b[i])");
6976   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
6977   verifyIndependentOfContext("if (a * (b * c))");
6978   verifyIndependentOfContext("if constexpr (a * (b * c))");
6979   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
6980   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6981   verifyIndependentOfContext("if (*b[i])");
6982   verifyIndependentOfContext("if (int *a = (&b))");
6983   verifyIndependentOfContext("while (int *a = &b)");
6984   verifyIndependentOfContext("while (a * (b * c))");
6985   verifyIndependentOfContext("size = sizeof *a;");
6986   verifyIndependentOfContext("if (a && (b = c))");
6987   verifyFormat("void f() {\n"
6988                "  for (const int &v : Values) {\n"
6989                "  }\n"
6990                "}");
6991   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6992   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6993   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6994 
6995   verifyFormat("#define A (!a * b)");
6996   verifyFormat("#define MACRO     \\\n"
6997                "  int *i = a * b; \\\n"
6998                "  void f(a *b);",
6999                getLLVMStyleWithColumns(19));
7000 
7001   verifyIndependentOfContext("A = new SomeType *[Length];");
7002   verifyIndependentOfContext("A = new SomeType *[Length]();");
7003   verifyIndependentOfContext("T **t = new T *;");
7004   verifyIndependentOfContext("T **t = new T *();");
7005   verifyGoogleFormat("A = new SomeType*[Length]();");
7006   verifyGoogleFormat("A = new SomeType*[Length];");
7007   verifyGoogleFormat("T** t = new T*;");
7008   verifyGoogleFormat("T** t = new T*();");
7009 
7010   verifyFormat("STATIC_ASSERT((a & b) == 0);");
7011   verifyFormat("STATIC_ASSERT(0 == (a & b));");
7012   verifyFormat("template <bool a, bool b> "
7013                "typename t::if<x && y>::type f() {}");
7014   verifyFormat("template <int *y> f() {}");
7015   verifyFormat("vector<int *> v;");
7016   verifyFormat("vector<int *const> v;");
7017   verifyFormat("vector<int *const **const *> v;");
7018   verifyFormat("vector<int *volatile> v;");
7019   verifyFormat("vector<a * b> v;");
7020   verifyFormat("foo<b && false>();");
7021   verifyFormat("foo<b & 1>();");
7022   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
7023   verifyFormat(
7024       "template <class T, class = typename std::enable_if<\n"
7025       "                       std::is_integral<T>::value &&\n"
7026       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
7027       "void F();",
7028       getLLVMStyleWithColumns(70));
7029   verifyFormat(
7030       "template <class T,\n"
7031       "          class = typename std::enable_if<\n"
7032       "              std::is_integral<T>::value &&\n"
7033       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
7034       "          class U>\n"
7035       "void F();",
7036       getLLVMStyleWithColumns(70));
7037   verifyFormat(
7038       "template <class T,\n"
7039       "          class = typename ::std::enable_if<\n"
7040       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
7041       "void F();",
7042       getGoogleStyleWithColumns(68));
7043 
7044   verifyIndependentOfContext("MACRO(int *i);");
7045   verifyIndependentOfContext("MACRO(auto *a);");
7046   verifyIndependentOfContext("MACRO(const A *a);");
7047   verifyIndependentOfContext("MACRO(A *const a);");
7048   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
7049   verifyFormat("void f() { f(float{1}, a * a); }");
7050   // FIXME: Is there a way to make this work?
7051   // verifyIndependentOfContext("MACRO(A *a);");
7052 
7053   verifyFormat("DatumHandle const *operator->() const { return input_; }");
7054   verifyFormat("return options != nullptr && operator==(*options);");
7055 
7056   EXPECT_EQ("#define OP(x)                                    \\\n"
7057             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
7058             "    return s << a.DebugString();                 \\\n"
7059             "  }",
7060             format("#define OP(x) \\\n"
7061                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
7062                    "    return s << a.DebugString(); \\\n"
7063                    "  }",
7064                    getLLVMStyleWithColumns(50)));
7065 
7066   // FIXME: We cannot handle this case yet; we might be able to figure out that
7067   // foo<x> d > v; doesn't make sense.
7068   verifyFormat("foo<a<b && c> d> v;");
7069 
7070   FormatStyle PointerMiddle = getLLVMStyle();
7071   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
7072   verifyFormat("delete *x;", PointerMiddle);
7073   verifyFormat("int * x;", PointerMiddle);
7074   verifyFormat("int *[] x;", PointerMiddle);
7075   verifyFormat("template <int * y> f() {}", PointerMiddle);
7076   verifyFormat("int * f(int * a) {}", PointerMiddle);
7077   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
7078   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
7079   verifyFormat("A<int *> a;", PointerMiddle);
7080   verifyFormat("A<int **> a;", PointerMiddle);
7081   verifyFormat("A<int *, int *> a;", PointerMiddle);
7082   verifyFormat("A<int *[]> a;", PointerMiddle);
7083   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
7084   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
7085   verifyFormat("T ** t = new T *;", PointerMiddle);
7086 
7087   // Member function reference qualifiers aren't binary operators.
7088   verifyFormat("string // break\n"
7089                "operator()() & {}");
7090   verifyFormat("string // break\n"
7091                "operator()() && {}");
7092   verifyGoogleFormat("template <typename T>\n"
7093                      "auto x() & -> int {}");
7094 }
7095 
7096 TEST_F(FormatTest, UnderstandsAttributes) {
7097   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
7098   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
7099                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
7100   FormatStyle AfterType = getLLVMStyle();
7101   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
7102   verifyFormat("__attribute__((nodebug)) void\n"
7103                "foo() {}\n",
7104                AfterType);
7105 }
7106 
7107 TEST_F(FormatTest, UnderstandsSquareAttributes) {
7108   verifyFormat("SomeType s [[unused]] (InitValue);");
7109   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
7110   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
7111   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
7112   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
7113   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7114                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
7115 
7116   // Make sure we do not mistake attributes for array subscripts.
7117   verifyFormat("int a() {}\n"
7118                "[[unused]] int b() {}\n");
7119   verifyFormat("NSArray *arr;\n"
7120                "arr[[Foo() bar]];");
7121 
7122   // On the other hand, we still need to correctly find array subscripts.
7123   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
7124 
7125   // Make sure that we do not mistake Objective-C method inside array literals
7126   // as attributes, even if those method names are also keywords.
7127   verifyFormat("@[ [foo bar] ];");
7128   verifyFormat("@[ [NSArray class] ];");
7129   verifyFormat("@[ [foo enum] ];");
7130 
7131   // Make sure we do not parse attributes as lambda introducers.
7132   FormatStyle MultiLineFunctions = getLLVMStyle();
7133   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7134   verifyFormat("[[unused]] int b() {\n"
7135                "  return 42;\n"
7136                "}\n",
7137                MultiLineFunctions);
7138 }
7139 
7140 TEST_F(FormatTest, UnderstandsEllipsis) {
7141   verifyFormat("int printf(const char *fmt, ...);");
7142   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
7143   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
7144 
7145   FormatStyle PointersLeft = getLLVMStyle();
7146   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
7147   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
7148 }
7149 
7150 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
7151   EXPECT_EQ("int *a;\n"
7152             "int *a;\n"
7153             "int *a;",
7154             format("int *a;\n"
7155                    "int* a;\n"
7156                    "int *a;",
7157                    getGoogleStyle()));
7158   EXPECT_EQ("int* a;\n"
7159             "int* a;\n"
7160             "int* a;",
7161             format("int* a;\n"
7162                    "int* a;\n"
7163                    "int *a;",
7164                    getGoogleStyle()));
7165   EXPECT_EQ("int *a;\n"
7166             "int *a;\n"
7167             "int *a;",
7168             format("int *a;\n"
7169                    "int * a;\n"
7170                    "int *  a;",
7171                    getGoogleStyle()));
7172   EXPECT_EQ("auto x = [] {\n"
7173             "  int *a;\n"
7174             "  int *a;\n"
7175             "  int *a;\n"
7176             "};",
7177             format("auto x=[]{int *a;\n"
7178                    "int * a;\n"
7179                    "int *  a;};",
7180                    getGoogleStyle()));
7181 }
7182 
7183 TEST_F(FormatTest, UnderstandsRvalueReferences) {
7184   verifyFormat("int f(int &&a) {}");
7185   verifyFormat("int f(int a, char &&b) {}");
7186   verifyFormat("void f() { int &&a = b; }");
7187   verifyGoogleFormat("int f(int a, char&& b) {}");
7188   verifyGoogleFormat("void f() { int&& a = b; }");
7189 
7190   verifyIndependentOfContext("A<int &&> a;");
7191   verifyIndependentOfContext("A<int &&, int &&> a;");
7192   verifyGoogleFormat("A<int&&> a;");
7193   verifyGoogleFormat("A<int&&, int&&> a;");
7194 
7195   // Not rvalue references:
7196   verifyFormat("template <bool B, bool C> class A {\n"
7197                "  static_assert(B && C, \"Something is wrong\");\n"
7198                "};");
7199   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
7200   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
7201   verifyFormat("#define A(a, b) (a && b)");
7202 }
7203 
7204 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
7205   verifyFormat("void f() {\n"
7206                "  x[aaaaaaaaa -\n"
7207                "    b] = 23;\n"
7208                "}",
7209                getLLVMStyleWithColumns(15));
7210 }
7211 
7212 TEST_F(FormatTest, FormatsCasts) {
7213   verifyFormat("Type *A = static_cast<Type *>(P);");
7214   verifyFormat("Type *A = (Type *)P;");
7215   verifyFormat("Type *A = (vector<Type *, int *>)P;");
7216   verifyFormat("int a = (int)(2.0f);");
7217   verifyFormat("int a = (int)2.0f;");
7218   verifyFormat("x[(int32)y];");
7219   verifyFormat("x = (int32)y;");
7220   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
7221   verifyFormat("int a = (int)*b;");
7222   verifyFormat("int a = (int)2.0f;");
7223   verifyFormat("int a = (int)~0;");
7224   verifyFormat("int a = (int)++a;");
7225   verifyFormat("int a = (int)sizeof(int);");
7226   verifyFormat("int a = (int)+2;");
7227   verifyFormat("my_int a = (my_int)2.0f;");
7228   verifyFormat("my_int a = (my_int)sizeof(int);");
7229   verifyFormat("return (my_int)aaa;");
7230   verifyFormat("#define x ((int)-1)");
7231   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
7232   verifyFormat("#define p(q) ((int *)&q)");
7233   verifyFormat("fn(a)(b) + 1;");
7234 
7235   verifyFormat("void f() { my_int a = (my_int)*b; }");
7236   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
7237   verifyFormat("my_int a = (my_int)~0;");
7238   verifyFormat("my_int a = (my_int)++a;");
7239   verifyFormat("my_int a = (my_int)-2;");
7240   verifyFormat("my_int a = (my_int)1;");
7241   verifyFormat("my_int a = (my_int *)1;");
7242   verifyFormat("my_int a = (const my_int)-1;");
7243   verifyFormat("my_int a = (const my_int *)-1;");
7244   verifyFormat("my_int a = (my_int)(my_int)-1;");
7245   verifyFormat("my_int a = (ns::my_int)-2;");
7246   verifyFormat("case (my_int)ONE:");
7247   verifyFormat("auto x = (X)this;");
7248 
7249   // FIXME: single value wrapped with paren will be treated as cast.
7250   verifyFormat("void f(int i = (kValue)*kMask) {}");
7251 
7252   verifyFormat("{ (void)F; }");
7253 
7254   // Don't break after a cast's
7255   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7256                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
7257                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
7258 
7259   // These are not casts.
7260   verifyFormat("void f(int *) {}");
7261   verifyFormat("f(foo)->b;");
7262   verifyFormat("f(foo).b;");
7263   verifyFormat("f(foo)(b);");
7264   verifyFormat("f(foo)[b];");
7265   verifyFormat("[](foo) { return 4; }(bar);");
7266   verifyFormat("(*funptr)(foo)[4];");
7267   verifyFormat("funptrs[4](foo)[4];");
7268   verifyFormat("void f(int *);");
7269   verifyFormat("void f(int *) = 0;");
7270   verifyFormat("void f(SmallVector<int>) {}");
7271   verifyFormat("void f(SmallVector<int>);");
7272   verifyFormat("void f(SmallVector<int>) = 0;");
7273   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
7274   verifyFormat("int a = sizeof(int) * b;");
7275   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
7276   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
7277   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
7278   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
7279 
7280   // These are not casts, but at some point were confused with casts.
7281   verifyFormat("virtual void foo(int *) override;");
7282   verifyFormat("virtual void foo(char &) const;");
7283   verifyFormat("virtual void foo(int *a, char *) const;");
7284   verifyFormat("int a = sizeof(int *) + b;");
7285   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
7286   verifyFormat("bool b = f(g<int>) && c;");
7287   verifyFormat("typedef void (*f)(int i) func;");
7288 
7289   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
7290                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7291   // FIXME: The indentation here is not ideal.
7292   verifyFormat(
7293       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7294       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
7295       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
7296 }
7297 
7298 TEST_F(FormatTest, FormatsFunctionTypes) {
7299   verifyFormat("A<bool()> a;");
7300   verifyFormat("A<SomeType()> a;");
7301   verifyFormat("A<void (*)(int, std::string)> a;");
7302   verifyFormat("A<void *(int)>;");
7303   verifyFormat("void *(*a)(int *, SomeType *);");
7304   verifyFormat("int (*func)(void *);");
7305   verifyFormat("void f() { int (*func)(void *); }");
7306   verifyFormat("template <class CallbackClass>\n"
7307                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
7308 
7309   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
7310   verifyGoogleFormat("void* (*a)(int);");
7311   verifyGoogleFormat(
7312       "template <class CallbackClass>\n"
7313       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
7314 
7315   // Other constructs can look somewhat like function types:
7316   verifyFormat("A<sizeof(*x)> a;");
7317   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
7318   verifyFormat("some_var = function(*some_pointer_var)[0];");
7319   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
7320   verifyFormat("int x = f(&h)();");
7321   verifyFormat("returnsFunction(&param1, &param2)(param);");
7322   verifyFormat("std::function<\n"
7323                "    LooooooooooongTemplatedType<\n"
7324                "        SomeType>*(\n"
7325                "        LooooooooooooooooongType type)>\n"
7326                "    function;",
7327                getGoogleStyleWithColumns(40));
7328 }
7329 
7330 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
7331   verifyFormat("A (*foo_)[6];");
7332   verifyFormat("vector<int> (*foo_)[6];");
7333 }
7334 
7335 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
7336   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7337                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
7338   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
7339                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
7340   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7341                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
7342 
7343   // Different ways of ()-initializiation.
7344   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7345                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
7346   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7347                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
7348   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7349                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
7350   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7351                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
7352 
7353   // Lambdas should not confuse the variable declaration heuristic.
7354   verifyFormat("LooooooooooooooooongType\n"
7355                "    variable(nullptr, [](A *a) {});",
7356                getLLVMStyleWithColumns(40));
7357 }
7358 
7359 TEST_F(FormatTest, BreaksLongDeclarations) {
7360   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
7361                "    AnotherNameForTheLongType;");
7362   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
7363                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7364   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7365                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
7366   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
7367                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
7368   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7369                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7370   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
7371                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7372   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
7373                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7374   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
7375                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7376   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7377                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
7378   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7379                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
7380   FormatStyle Indented = getLLVMStyle();
7381   Indented.IndentWrappedFunctionNames = true;
7382   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7383                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
7384                Indented);
7385   verifyFormat(
7386       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7387       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7388       Indented);
7389   verifyFormat(
7390       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
7391       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7392       Indented);
7393   verifyFormat(
7394       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
7395       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7396       Indented);
7397 
7398   // FIXME: Without the comment, this breaks after "(".
7399   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
7400                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
7401                getGoogleStyle());
7402 
7403   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
7404                "                  int LoooooooooooooooooooongParam2) {}");
7405   verifyFormat(
7406       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
7407       "                                   SourceLocation L, IdentifierIn *II,\n"
7408       "                                   Type *T) {}");
7409   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
7410                "ReallyReaaallyLongFunctionName(\n"
7411                "    const std::string &SomeParameter,\n"
7412                "    const SomeType<string, SomeOtherTemplateParameter>\n"
7413                "        &ReallyReallyLongParameterName,\n"
7414                "    const SomeType<string, SomeOtherTemplateParameter>\n"
7415                "        &AnotherLongParameterName) {}");
7416   verifyFormat("template <typename A>\n"
7417                "SomeLoooooooooooooooooooooongType<\n"
7418                "    typename some_namespace::SomeOtherType<A>::Type>\n"
7419                "Function() {}");
7420 
7421   verifyGoogleFormat(
7422       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
7423       "    aaaaaaaaaaaaaaaaaaaaaaa;");
7424   verifyGoogleFormat(
7425       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
7426       "                                   SourceLocation L) {}");
7427   verifyGoogleFormat(
7428       "some_namespace::LongReturnType\n"
7429       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
7430       "    int first_long_parameter, int second_parameter) {}");
7431 
7432   verifyGoogleFormat("template <typename T>\n"
7433                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7434                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
7435   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7436                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
7437 
7438   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
7439                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7440                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7441   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7442                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7443                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
7444   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7445                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7446                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
7447                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7448 
7449   verifyFormat("template <typename T> // Templates on own line.\n"
7450                "static int            // Some comment.\n"
7451                "MyFunction(int a);",
7452                getLLVMStyle());
7453 }
7454 
7455 TEST_F(FormatTest, FormatsArrays) {
7456   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7457                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
7458   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
7459                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
7460   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7461                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
7462   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7463                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
7464   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7465                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
7466   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7467                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7468                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
7469   verifyFormat(
7470       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
7471       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7472       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7473   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
7474                "    .aaaaaaaaaaaaaaaaaaaaaa();");
7475 
7476   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
7477                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
7478   verifyFormat(
7479       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
7480       "                                  .aaaaaaa[0]\n"
7481       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
7482   verifyFormat("a[::b::c];");
7483 
7484   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
7485 
7486   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
7487   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
7488 }
7489 
7490 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
7491   verifyFormat("(a)->b();");
7492   verifyFormat("--a;");
7493 }
7494 
7495 TEST_F(FormatTest, HandlesIncludeDirectives) {
7496   verifyFormat("#include <string>\n"
7497                "#include <a/b/c.h>\n"
7498                "#include \"a/b/string\"\n"
7499                "#include \"string.h\"\n"
7500                "#include \"string.h\"\n"
7501                "#include <a-a>\n"
7502                "#include < path with space >\n"
7503                "#include_next <test.h>"
7504                "#include \"abc.h\" // this is included for ABC\n"
7505                "#include \"some long include\" // with a comment\n"
7506                "#include \"some very long include path\"\n"
7507                "#include <some/very/long/include/path>\n",
7508                getLLVMStyleWithColumns(35));
7509   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
7510   EXPECT_EQ("#include <a>", format("#include<a>"));
7511 
7512   verifyFormat("#import <string>");
7513   verifyFormat("#import <a/b/c.h>");
7514   verifyFormat("#import \"a/b/string\"");
7515   verifyFormat("#import \"string.h\"");
7516   verifyFormat("#import \"string.h\"");
7517   verifyFormat("#if __has_include(<strstream>)\n"
7518                "#include <strstream>\n"
7519                "#endif");
7520 
7521   verifyFormat("#define MY_IMPORT <a/b>");
7522 
7523   verifyFormat("#if __has_include(<a/b>)");
7524   verifyFormat("#if __has_include_next(<a/b>)");
7525   verifyFormat("#define F __has_include(<a/b>)");
7526   verifyFormat("#define F __has_include_next(<a/b>)");
7527 
7528   // Protocol buffer definition or missing "#".
7529   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
7530                getLLVMStyleWithColumns(30));
7531 
7532   FormatStyle Style = getLLVMStyle();
7533   Style.AlwaysBreakBeforeMultilineStrings = true;
7534   Style.ColumnLimit = 0;
7535   verifyFormat("#import \"abc.h\"", Style);
7536 
7537   // But 'import' might also be a regular C++ namespace.
7538   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7539                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7540 }
7541 
7542 //===----------------------------------------------------------------------===//
7543 // Error recovery tests.
7544 //===----------------------------------------------------------------------===//
7545 
7546 TEST_F(FormatTest, IncompleteParameterLists) {
7547   FormatStyle NoBinPacking = getLLVMStyle();
7548   NoBinPacking.BinPackParameters = false;
7549   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
7550                "                        double *min_x,\n"
7551                "                        double *max_x,\n"
7552                "                        double *min_y,\n"
7553                "                        double *max_y,\n"
7554                "                        double *min_z,\n"
7555                "                        double *max_z, ) {}",
7556                NoBinPacking);
7557 }
7558 
7559 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
7560   verifyFormat("void f() { return; }\n42");
7561   verifyFormat("void f() {\n"
7562                "  if (0)\n"
7563                "    return;\n"
7564                "}\n"
7565                "42");
7566   verifyFormat("void f() { return }\n42");
7567   verifyFormat("void f() {\n"
7568                "  if (0)\n"
7569                "    return\n"
7570                "}\n"
7571                "42");
7572 }
7573 
7574 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
7575   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
7576   EXPECT_EQ("void f() {\n"
7577             "  if (a)\n"
7578             "    return\n"
7579             "}",
7580             format("void  f  (  )  {  if  ( a )  return  }"));
7581   EXPECT_EQ("namespace N {\n"
7582             "void f()\n"
7583             "}",
7584             format("namespace  N  {  void f()  }"));
7585   EXPECT_EQ("namespace N {\n"
7586             "void f() {}\n"
7587             "void g()\n"
7588             "} // namespace N",
7589             format("namespace N  { void f( ) { } void g( ) }"));
7590 }
7591 
7592 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
7593   verifyFormat("int aaaaaaaa =\n"
7594                "    // Overlylongcomment\n"
7595                "    b;",
7596                getLLVMStyleWithColumns(20));
7597   verifyFormat("function(\n"
7598                "    ShortArgument,\n"
7599                "    LoooooooooooongArgument);\n",
7600                getLLVMStyleWithColumns(20));
7601 }
7602 
7603 TEST_F(FormatTest, IncorrectAccessSpecifier) {
7604   verifyFormat("public:");
7605   verifyFormat("class A {\n"
7606                "public\n"
7607                "  void f() {}\n"
7608                "};");
7609   verifyFormat("public\n"
7610                "int qwerty;");
7611   verifyFormat("public\n"
7612                "B {}");
7613   verifyFormat("public\n"
7614                "{}");
7615   verifyFormat("public\n"
7616                "B { int x; }");
7617 }
7618 
7619 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
7620   verifyFormat("{");
7621   verifyFormat("#})");
7622   verifyNoCrash("(/**/[:!] ?[).");
7623 }
7624 
7625 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
7626   // Found by oss-fuzz:
7627   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
7628   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7629   Style.ColumnLimit = 60;
7630   verifyNoCrash(
7631       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
7632       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
7633       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
7634       Style);
7635 }
7636 
7637 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
7638   verifyFormat("do {\n}");
7639   verifyFormat("do {\n}\n"
7640                "f();");
7641   verifyFormat("do {\n}\n"
7642                "wheeee(fun);");
7643   verifyFormat("do {\n"
7644                "  f();\n"
7645                "}");
7646 }
7647 
7648 TEST_F(FormatTest, IncorrectCodeMissingParens) {
7649   verifyFormat("if {\n  foo;\n  foo();\n}");
7650   verifyFormat("switch {\n  foo;\n  foo();\n}");
7651   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
7652   verifyFormat("while {\n  foo;\n  foo();\n}");
7653   verifyFormat("do {\n  foo;\n  foo();\n} while;");
7654 }
7655 
7656 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
7657   verifyIncompleteFormat("namespace {\n"
7658                          "class Foo { Foo (\n"
7659                          "};\n"
7660                          "} // namespace");
7661 }
7662 
7663 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
7664   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
7665   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
7666   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
7667   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
7668 
7669   EXPECT_EQ("{\n"
7670             "  {\n"
7671             "    breakme(\n"
7672             "        qwe);\n"
7673             "  }\n",
7674             format("{\n"
7675                    "    {\n"
7676                    " breakme(qwe);\n"
7677                    "}\n",
7678                    getLLVMStyleWithColumns(10)));
7679 }
7680 
7681 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
7682   verifyFormat("int x = {\n"
7683                "    avariable,\n"
7684                "    b(alongervariable)};",
7685                getLLVMStyleWithColumns(25));
7686 }
7687 
7688 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
7689   verifyFormat("return (a)(b){1, 2, 3};");
7690 }
7691 
7692 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
7693   verifyFormat("vector<int> x{1, 2, 3, 4};");
7694   verifyFormat("vector<int> x{\n"
7695                "    1,\n"
7696                "    2,\n"
7697                "    3,\n"
7698                "    4,\n"
7699                "};");
7700   verifyFormat("vector<T> x{{}, {}, {}, {}};");
7701   verifyFormat("f({1, 2});");
7702   verifyFormat("auto v = Foo{-1};");
7703   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
7704   verifyFormat("Class::Class : member{1, 2, 3} {}");
7705   verifyFormat("new vector<int>{1, 2, 3};");
7706   verifyFormat("new int[3]{1, 2, 3};");
7707   verifyFormat("new int{1};");
7708   verifyFormat("return {arg1, arg2};");
7709   verifyFormat("return {arg1, SomeType{parameter}};");
7710   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
7711   verifyFormat("new T{arg1, arg2};");
7712   verifyFormat("f(MyMap[{composite, key}]);");
7713   verifyFormat("class Class {\n"
7714                "  T member = {arg1, arg2};\n"
7715                "};");
7716   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
7717   verifyFormat("const struct A a = {.a = 1, .b = 2};");
7718   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
7719   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
7720   verifyFormat("int a = std::is_integral<int>{} + 0;");
7721 
7722   verifyFormat("int foo(int i) { return fo1{}(i); }");
7723   verifyFormat("int foo(int i) { return fo1{}(i); }");
7724   verifyFormat("auto i = decltype(x){};");
7725   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
7726   verifyFormat("Node n{1, Node{1000}, //\n"
7727                "       2};");
7728   verifyFormat("Aaaa aaaaaaa{\n"
7729                "    {\n"
7730                "        aaaa,\n"
7731                "    },\n"
7732                "};");
7733   verifyFormat("class C : public D {\n"
7734                "  SomeClass SC{2};\n"
7735                "};");
7736   verifyFormat("class C : public A {\n"
7737                "  class D : public B {\n"
7738                "    void f() { int i{2}; }\n"
7739                "  };\n"
7740                "};");
7741   verifyFormat("#define A {a, a},");
7742 
7743   // Avoid breaking between equal sign and opening brace
7744   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
7745   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
7746   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
7747                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
7748                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
7749                "     {\"ccccccccccccccccccccc\", 2}};",
7750                AvoidBreakingFirstArgument);
7751 
7752   // Binpacking only if there is no trailing comma
7753   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
7754                "                      cccccccccc, dddddddddd};",
7755 			   getLLVMStyleWithColumns(50));
7756   verifyFormat("const Aaaaaa aaaaa = {\n"
7757                "    aaaaaaaaaaa,\n"
7758                "    bbbbbbbbbbb,\n"
7759                "    ccccccccccc,\n"
7760                "    ddddddddddd,\n"
7761                "};", getLLVMStyleWithColumns(50));
7762 
7763   // Cases where distinguising braced lists and blocks is hard.
7764   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
7765   verifyFormat("void f() {\n"
7766                "  return; // comment\n"
7767                "}\n"
7768                "SomeType t;");
7769   verifyFormat("void f() {\n"
7770                "  if (a) {\n"
7771                "    f();\n"
7772                "  }\n"
7773                "}\n"
7774                "SomeType t;");
7775 
7776   // In combination with BinPackArguments = false.
7777   FormatStyle NoBinPacking = getLLVMStyle();
7778   NoBinPacking.BinPackArguments = false;
7779   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
7780                "                      bbbbb,\n"
7781                "                      ccccc,\n"
7782                "                      ddddd,\n"
7783                "                      eeeee,\n"
7784                "                      ffffff,\n"
7785                "                      ggggg,\n"
7786                "                      hhhhhh,\n"
7787                "                      iiiiii,\n"
7788                "                      jjjjjj,\n"
7789                "                      kkkkkk};",
7790                NoBinPacking);
7791   verifyFormat("const Aaaaaa aaaaa = {\n"
7792                "    aaaaa,\n"
7793                "    bbbbb,\n"
7794                "    ccccc,\n"
7795                "    ddddd,\n"
7796                "    eeeee,\n"
7797                "    ffffff,\n"
7798                "    ggggg,\n"
7799                "    hhhhhh,\n"
7800                "    iiiiii,\n"
7801                "    jjjjjj,\n"
7802                "    kkkkkk,\n"
7803                "};",
7804                NoBinPacking);
7805   verifyFormat(
7806       "const Aaaaaa aaaaa = {\n"
7807       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
7808       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
7809       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
7810       "};",
7811       NoBinPacking);
7812 
7813   // FIXME: The alignment of these trailing comments might be bad. Then again,
7814   // this might be utterly useless in real code.
7815   verifyFormat("Constructor::Constructor()\n"
7816                "    : some_value{         //\n"
7817                "                 aaaaaaa, //\n"
7818                "                 bbbbbbb} {}");
7819 
7820   // In braced lists, the first comment is always assumed to belong to the
7821   // first element. Thus, it can be moved to the next or previous line as
7822   // appropriate.
7823   EXPECT_EQ("function({// First element:\n"
7824             "          1,\n"
7825             "          // Second element:\n"
7826             "          2});",
7827             format("function({\n"
7828                    "    // First element:\n"
7829                    "    1,\n"
7830                    "    // Second element:\n"
7831                    "    2});"));
7832   EXPECT_EQ("std::vector<int> MyNumbers{\n"
7833             "    // First element:\n"
7834             "    1,\n"
7835             "    // Second element:\n"
7836             "    2};",
7837             format("std::vector<int> MyNumbers{// First element:\n"
7838                    "                           1,\n"
7839                    "                           // Second element:\n"
7840                    "                           2};",
7841                    getLLVMStyleWithColumns(30)));
7842   // A trailing comma should still lead to an enforced line break and no
7843   // binpacking.
7844   EXPECT_EQ("vector<int> SomeVector = {\n"
7845             "    // aaa\n"
7846             "    1,\n"
7847             "    2,\n"
7848             "};",
7849             format("vector<int> SomeVector = { // aaa\n"
7850                    "    1, 2, };"));
7851 
7852   FormatStyle ExtraSpaces = getLLVMStyle();
7853   ExtraSpaces.Cpp11BracedListStyle = false;
7854   ExtraSpaces.ColumnLimit = 75;
7855   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
7856   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
7857   verifyFormat("f({ 1, 2 });", ExtraSpaces);
7858   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
7859   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
7860   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
7861   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
7862   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
7863   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
7864   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
7865   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
7866   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
7867   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
7868   verifyFormat("class Class {\n"
7869                "  T member = { arg1, arg2 };\n"
7870                "};",
7871                ExtraSpaces);
7872   verifyFormat(
7873       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7874       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7875       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7876       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7877       ExtraSpaces);
7878   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7879   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7880                ExtraSpaces);
7881   verifyFormat(
7882       "someFunction(OtherParam,\n"
7883       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7884       "                         param1, param2,\n"
7885       "                         // comment 2\n"
7886       "                         param3, param4 });",
7887       ExtraSpaces);
7888   verifyFormat(
7889       "std::this_thread::sleep_for(\n"
7890       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7891       ExtraSpaces);
7892   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7893                "    aaaaaaa,\n"
7894                "    aaaaaaaaaa,\n"
7895                "    aaaaa,\n"
7896                "    aaaaaaaaaaaaaaa,\n"
7897                "    aaa,\n"
7898                "    aaaaaaaaaa,\n"
7899                "    a,\n"
7900                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7901                "    aaaaaaaaaaaa,\n"
7902                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7903                "    aaaaaaa,\n"
7904                "    a};");
7905   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7906   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7907   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7908 
7909   // Avoid breaking between initializer/equal sign and opening brace
7910   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7911   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7912                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7913                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7914                "  { \"ccccccccccccccccccccc\", 2 }\n"
7915                "};",
7916                ExtraSpaces);
7917   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7918                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7919                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7920                "  { \"ccccccccccccccccccccc\", 2 }\n"
7921                "};",
7922                ExtraSpaces);
7923 
7924   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7925   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7926   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7927   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7928 }
7929 
7930 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7931   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7932                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7933                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7934                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7935                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7936                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7937   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7938                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7939                "                 1, 22, 333, 4444, 55555, //\n"
7940                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7941                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7942   verifyFormat(
7943       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7944       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7945       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7946       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7947       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7948       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7949       "                 7777777};");
7950   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7951                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7952                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7953   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7954                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7955                "    // Separating comment.\n"
7956                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7957   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7958                "    // Leading comment\n"
7959                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7960                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7961   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7962                "                 1, 1, 1, 1};",
7963                getLLVMStyleWithColumns(39));
7964   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7965                "                 1, 1, 1, 1};",
7966                getLLVMStyleWithColumns(38));
7967   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7968                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7969                getLLVMStyleWithColumns(43));
7970   verifyFormat(
7971       "static unsigned SomeValues[10][3] = {\n"
7972       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7973       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7974   verifyFormat("static auto fields = new vector<string>{\n"
7975                "    \"aaaaaaaaaaaaa\",\n"
7976                "    \"aaaaaaaaaaaaa\",\n"
7977                "    \"aaaaaaaaaaaa\",\n"
7978                "    \"aaaaaaaaaaaaaa\",\n"
7979                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7980                "    \"aaaaaaaaaaaa\",\n"
7981                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7982                "};");
7983   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7984   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7985                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7986                "                 3, cccccccccccccccccccccc};",
7987                getLLVMStyleWithColumns(60));
7988 
7989   // Trailing commas.
7990   verifyFormat("vector<int> x = {\n"
7991                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7992                "};",
7993                getLLVMStyleWithColumns(39));
7994   verifyFormat("vector<int> x = {\n"
7995                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7996                "};",
7997                getLLVMStyleWithColumns(39));
7998   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7999                "                 1, 1, 1, 1,\n"
8000                "                 /**/ /**/};",
8001                getLLVMStyleWithColumns(39));
8002 
8003   // Trailing comment in the first line.
8004   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
8005                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
8006                "    111111111,  222222222,  3333333333,  444444444,  //\n"
8007                "    11111111,   22222222,   333333333,   44444444};");
8008   // Trailing comment in the last line.
8009   verifyFormat("int aaaaa[] = {\n"
8010                "    1, 2, 3, // comment\n"
8011                "    4, 5, 6  // comment\n"
8012                "};");
8013 
8014   // With nested lists, we should either format one item per line or all nested
8015   // lists one on line.
8016   // FIXME: For some nested lists, we can do better.
8017   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
8018                "        {aaaaaaaaaaaaaaaaaaa},\n"
8019                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
8020                "        {aaaaaaaaaaaaaaaaa}};",
8021                getLLVMStyleWithColumns(60));
8022   verifyFormat(
8023       "SomeStruct my_struct_array = {\n"
8024       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
8025       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
8026       "    {aaa, aaa},\n"
8027       "    {aaa, aaa},\n"
8028       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
8029       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8030       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
8031 
8032   // No column layout should be used here.
8033   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
8034                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
8035 
8036   verifyNoCrash("a<,");
8037 
8038   // No braced initializer here.
8039   verifyFormat("void f() {\n"
8040                "  struct Dummy {};\n"
8041                "  f(v);\n"
8042                "}");
8043 
8044   // Long lists should be formatted in columns even if they are nested.
8045   verifyFormat(
8046       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
8047       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
8048       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
8049       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
8050       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
8051       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
8052 
8053   // Allow "single-column" layout even if that violates the column limit. There
8054   // isn't going to be a better way.
8055   verifyFormat("std::vector<int> a = {\n"
8056                "    aaaaaaaa,\n"
8057                "    aaaaaaaa,\n"
8058                "    aaaaaaaa,\n"
8059                "    aaaaaaaa,\n"
8060                "    aaaaaaaaaa,\n"
8061                "    aaaaaaaa,\n"
8062                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
8063                getLLVMStyleWithColumns(30));
8064   verifyFormat("vector<int> aaaa = {\n"
8065                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8066                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8067                "    aaaaaa.aaaaaaa,\n"
8068                "    aaaaaa.aaaaaaa,\n"
8069                "    aaaaaa.aaaaaaa,\n"
8070                "    aaaaaa.aaaaaaa,\n"
8071                "};");
8072 
8073   // Don't create hanging lists.
8074   verifyFormat("someFunction(Param, {List1, List2,\n"
8075                "                     List3});",
8076                getLLVMStyleWithColumns(35));
8077   verifyFormat("someFunction(Param, Param,\n"
8078                "             {List1, List2,\n"
8079                "              List3});",
8080                getLLVMStyleWithColumns(35));
8081   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
8082                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
8083 }
8084 
8085 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
8086   FormatStyle DoNotMerge = getLLVMStyle();
8087   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8088 
8089   verifyFormat("void f() { return 42; }");
8090   verifyFormat("void f() {\n"
8091                "  return 42;\n"
8092                "}",
8093                DoNotMerge);
8094   verifyFormat("void f() {\n"
8095                "  // Comment\n"
8096                "}");
8097   verifyFormat("{\n"
8098                "#error {\n"
8099                "  int a;\n"
8100                "}");
8101   verifyFormat("{\n"
8102                "  int a;\n"
8103                "#error {\n"
8104                "}");
8105   verifyFormat("void f() {} // comment");
8106   verifyFormat("void f() { int a; } // comment");
8107   verifyFormat("void f() {\n"
8108                "} // comment",
8109                DoNotMerge);
8110   verifyFormat("void f() {\n"
8111                "  int a;\n"
8112                "} // comment",
8113                DoNotMerge);
8114   verifyFormat("void f() {\n"
8115                "} // comment",
8116                getLLVMStyleWithColumns(15));
8117 
8118   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
8119   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
8120 
8121   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
8122   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
8123   verifyFormat("class C {\n"
8124                "  C()\n"
8125                "      : iiiiiiii(nullptr),\n"
8126                "        kkkkkkk(nullptr),\n"
8127                "        mmmmmmm(nullptr),\n"
8128                "        nnnnnnn(nullptr) {}\n"
8129                "};",
8130                getGoogleStyle());
8131 
8132   FormatStyle NoColumnLimit = getLLVMStyle();
8133   NoColumnLimit.ColumnLimit = 0;
8134   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
8135   EXPECT_EQ("class C {\n"
8136             "  A() : b(0) {}\n"
8137             "};",
8138             format("class C{A():b(0){}};", NoColumnLimit));
8139   EXPECT_EQ("A()\n"
8140             "    : b(0) {\n"
8141             "}",
8142             format("A()\n:b(0)\n{\n}", NoColumnLimit));
8143 
8144   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
8145   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
8146       FormatStyle::SFS_None;
8147   EXPECT_EQ("A()\n"
8148             "    : b(0) {\n"
8149             "}",
8150             format("A():b(0){}", DoNotMergeNoColumnLimit));
8151   EXPECT_EQ("A()\n"
8152             "    : b(0) {\n"
8153             "}",
8154             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
8155 
8156   verifyFormat("#define A          \\\n"
8157                "  void f() {       \\\n"
8158                "    int i;         \\\n"
8159                "  }",
8160                getLLVMStyleWithColumns(20));
8161   verifyFormat("#define A           \\\n"
8162                "  void f() { int i; }",
8163                getLLVMStyleWithColumns(21));
8164   verifyFormat("#define A            \\\n"
8165                "  void f() {         \\\n"
8166                "    int i;           \\\n"
8167                "  }                  \\\n"
8168                "  int j;",
8169                getLLVMStyleWithColumns(22));
8170   verifyFormat("#define A             \\\n"
8171                "  void f() { int i; } \\\n"
8172                "  int j;",
8173                getLLVMStyleWithColumns(23));
8174 }
8175 
8176 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
8177   FormatStyle MergeEmptyOnly = getLLVMStyle();
8178   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
8179   verifyFormat("class C {\n"
8180                "  int f() {}\n"
8181                "};",
8182                MergeEmptyOnly);
8183   verifyFormat("class C {\n"
8184                "  int f() {\n"
8185                "    return 42;\n"
8186                "  }\n"
8187                "};",
8188                MergeEmptyOnly);
8189   verifyFormat("int f() {}", MergeEmptyOnly);
8190   verifyFormat("int f() {\n"
8191                "  return 42;\n"
8192                "}",
8193                MergeEmptyOnly);
8194 
8195   // Also verify behavior when BraceWrapping.AfterFunction = true
8196   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
8197   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
8198   verifyFormat("int f() {}", MergeEmptyOnly);
8199   verifyFormat("class C {\n"
8200                "  int f() {}\n"
8201                "};",
8202                MergeEmptyOnly);
8203 }
8204 
8205 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
8206   FormatStyle MergeInlineOnly = getLLVMStyle();
8207   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8208   verifyFormat("class C {\n"
8209                "  int f() { return 42; }\n"
8210                "};",
8211                MergeInlineOnly);
8212   verifyFormat("int f() {\n"
8213                "  return 42;\n"
8214                "}",
8215                MergeInlineOnly);
8216 
8217   // SFS_Inline implies SFS_Empty
8218   verifyFormat("class C {\n"
8219                "  int f() {}\n"
8220                "};",
8221                MergeInlineOnly);
8222   verifyFormat("int f() {}", MergeInlineOnly);
8223 
8224   // Also verify behavior when BraceWrapping.AfterFunction = true
8225   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
8226   MergeInlineOnly.BraceWrapping.AfterFunction = true;
8227   verifyFormat("class C {\n"
8228                "  int f() { return 42; }\n"
8229                "};",
8230                MergeInlineOnly);
8231   verifyFormat("int f()\n"
8232                "{\n"
8233                "  return 42;\n"
8234                "}",
8235                MergeInlineOnly);
8236 
8237   // SFS_Inline implies SFS_Empty
8238   verifyFormat("int f() {}", MergeInlineOnly);
8239   verifyFormat("class C {\n"
8240                "  int f() {}\n"
8241                "};",
8242                MergeInlineOnly);
8243 }
8244 
8245 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
8246   FormatStyle MergeInlineOnly = getLLVMStyle();
8247   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
8248       FormatStyle::SFS_InlineOnly;
8249   verifyFormat("class C {\n"
8250                "  int f() { return 42; }\n"
8251                "};",
8252                MergeInlineOnly);
8253   verifyFormat("int f() {\n"
8254                "  return 42;\n"
8255                "}",
8256                MergeInlineOnly);
8257 
8258   // SFS_InlineOnly does not imply SFS_Empty
8259   verifyFormat("class C {\n"
8260                "  int f() {}\n"
8261                "};",
8262                MergeInlineOnly);
8263   verifyFormat("int f() {\n"
8264                "}",
8265                MergeInlineOnly);
8266 
8267   // Also verify behavior when BraceWrapping.AfterFunction = true
8268   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
8269   MergeInlineOnly.BraceWrapping.AfterFunction = true;
8270   verifyFormat("class C {\n"
8271                "  int f() { return 42; }\n"
8272                "};",
8273                MergeInlineOnly);
8274   verifyFormat("int f()\n"
8275                "{\n"
8276                "  return 42;\n"
8277                "}",
8278                MergeInlineOnly);
8279 
8280   // SFS_InlineOnly does not imply SFS_Empty
8281   verifyFormat("int f()\n"
8282                "{\n"
8283                "}",
8284                MergeInlineOnly);
8285   verifyFormat("class C {\n"
8286                "  int f() {}\n"
8287                "};",
8288                MergeInlineOnly);
8289 }
8290 
8291 TEST_F(FormatTest, SplitEmptyFunction) {
8292   FormatStyle Style = getLLVMStyle();
8293   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8294   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8295   Style.BraceWrapping.AfterFunction = true;
8296   Style.BraceWrapping.SplitEmptyFunction = false;
8297   Style.ColumnLimit = 40;
8298 
8299   verifyFormat("int f()\n"
8300                "{}",
8301                Style);
8302   verifyFormat("int f()\n"
8303                "{\n"
8304                "  return 42;\n"
8305                "}",
8306                Style);
8307   verifyFormat("int f()\n"
8308                "{\n"
8309                "  // some comment\n"
8310                "}",
8311                Style);
8312 
8313   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
8314   verifyFormat("int f() {}", Style);
8315   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8316                "{}",
8317                Style);
8318   verifyFormat("int f()\n"
8319                "{\n"
8320                "  return 0;\n"
8321                "}",
8322                Style);
8323 
8324   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8325   verifyFormat("class Foo {\n"
8326                "  int f() {}\n"
8327                "};\n",
8328                Style);
8329   verifyFormat("class Foo {\n"
8330                "  int f() { return 0; }\n"
8331                "};\n",
8332                Style);
8333   verifyFormat("class Foo {\n"
8334                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8335                "  {}\n"
8336                "};\n",
8337                Style);
8338   verifyFormat("class Foo {\n"
8339                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8340                "  {\n"
8341                "    return 0;\n"
8342                "  }\n"
8343                "};\n",
8344                Style);
8345 
8346   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8347   verifyFormat("int f() {}", Style);
8348   verifyFormat("int f() { return 0; }", Style);
8349   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8350                "{}",
8351                Style);
8352   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8353                "{\n"
8354                "  return 0;\n"
8355                "}",
8356                Style);
8357 }
8358 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
8359   FormatStyle Style = getLLVMStyle();
8360   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8361   verifyFormat("#ifdef A\n"
8362                "int f() {}\n"
8363                "#else\n"
8364                "int g() {}\n"
8365                "#endif",
8366                Style);
8367 }
8368 
8369 TEST_F(FormatTest, SplitEmptyClass) {
8370   FormatStyle Style = getLLVMStyle();
8371   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8372   Style.BraceWrapping.AfterClass = true;
8373   Style.BraceWrapping.SplitEmptyRecord = false;
8374 
8375   verifyFormat("class Foo\n"
8376                "{};",
8377                Style);
8378   verifyFormat("/* something */ class Foo\n"
8379                "{};",
8380                Style);
8381   verifyFormat("template <typename X> class Foo\n"
8382                "{};",
8383                Style);
8384   verifyFormat("class Foo\n"
8385                "{\n"
8386                "  Foo();\n"
8387                "};",
8388                Style);
8389   verifyFormat("typedef class Foo\n"
8390                "{\n"
8391                "} Foo_t;",
8392                Style);
8393 }
8394 
8395 TEST_F(FormatTest, SplitEmptyStruct) {
8396   FormatStyle Style = getLLVMStyle();
8397   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8398   Style.BraceWrapping.AfterStruct = true;
8399   Style.BraceWrapping.SplitEmptyRecord = false;
8400 
8401   verifyFormat("struct Foo\n"
8402                "{};",
8403                Style);
8404   verifyFormat("/* something */ struct Foo\n"
8405                "{};",
8406                Style);
8407   verifyFormat("template <typename X> struct Foo\n"
8408                "{};",
8409                Style);
8410   verifyFormat("struct Foo\n"
8411                "{\n"
8412                "  Foo();\n"
8413                "};",
8414                Style);
8415   verifyFormat("typedef struct Foo\n"
8416                "{\n"
8417                "} Foo_t;",
8418                Style);
8419   //typedef struct Bar {} Bar_t;
8420 }
8421 
8422 TEST_F(FormatTest, SplitEmptyUnion) {
8423   FormatStyle Style = getLLVMStyle();
8424   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8425   Style.BraceWrapping.AfterUnion = true;
8426   Style.BraceWrapping.SplitEmptyRecord = false;
8427 
8428   verifyFormat("union Foo\n"
8429                "{};",
8430                Style);
8431   verifyFormat("/* something */ union Foo\n"
8432                "{};",
8433                Style);
8434   verifyFormat("union Foo\n"
8435                "{\n"
8436                "  A,\n"
8437                "};",
8438                Style);
8439   verifyFormat("typedef union Foo\n"
8440                "{\n"
8441                "} Foo_t;",
8442                Style);
8443 }
8444 
8445 TEST_F(FormatTest, SplitEmptyNamespace) {
8446   FormatStyle Style = getLLVMStyle();
8447   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8448   Style.BraceWrapping.AfterNamespace = true;
8449   Style.BraceWrapping.SplitEmptyNamespace = false;
8450 
8451   verifyFormat("namespace Foo\n"
8452                "{};",
8453                Style);
8454   verifyFormat("/* something */ namespace Foo\n"
8455                "{};",
8456                Style);
8457   verifyFormat("inline namespace Foo\n"
8458                "{};",
8459                Style);
8460   verifyFormat("/* something */ inline namespace Foo\n"
8461                "{};",
8462                Style);
8463   verifyFormat("export namespace Foo\n"
8464                "{};",
8465                Style);
8466   verifyFormat("namespace Foo\n"
8467                "{\n"
8468                "void Bar();\n"
8469                "};",
8470                Style);
8471 }
8472 
8473 TEST_F(FormatTest, NeverMergeShortRecords) {
8474   FormatStyle Style = getLLVMStyle();
8475 
8476   verifyFormat("class Foo {\n"
8477                "  Foo();\n"
8478                "};",
8479                Style);
8480   verifyFormat("typedef class Foo {\n"
8481                "  Foo();\n"
8482                "} Foo_t;",
8483                Style);
8484   verifyFormat("struct Foo {\n"
8485                "  Foo();\n"
8486                "};",
8487                Style);
8488   verifyFormat("typedef struct Foo {\n"
8489                "  Foo();\n"
8490                "} Foo_t;",
8491                Style);
8492   verifyFormat("union Foo {\n"
8493                "  A,\n"
8494                "};",
8495                Style);
8496   verifyFormat("typedef union Foo {\n"
8497                "  A,\n"
8498                "} Foo_t;",
8499                Style);
8500   verifyFormat("namespace Foo {\n"
8501                "void Bar();\n"
8502                "};",
8503                Style);
8504 
8505   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8506   Style.BraceWrapping.AfterClass = true;
8507   Style.BraceWrapping.AfterStruct = true;
8508   Style.BraceWrapping.AfterUnion = true;
8509   Style.BraceWrapping.AfterNamespace = true;
8510   verifyFormat("class Foo\n"
8511                "{\n"
8512                "  Foo();\n"
8513                "};",
8514                Style);
8515   verifyFormat("typedef class Foo\n"
8516                "{\n"
8517                "  Foo();\n"
8518                "} Foo_t;",
8519                Style);
8520   verifyFormat("struct Foo\n"
8521                "{\n"
8522                "  Foo();\n"
8523                "};",
8524                Style);
8525   verifyFormat("typedef struct Foo\n"
8526                "{\n"
8527                "  Foo();\n"
8528                "} Foo_t;",
8529                Style);
8530   verifyFormat("union Foo\n"
8531                "{\n"
8532                "  A,\n"
8533                "};",
8534                Style);
8535   verifyFormat("typedef union Foo\n"
8536                "{\n"
8537                "  A,\n"
8538                "} Foo_t;",
8539                Style);
8540   verifyFormat("namespace Foo\n"
8541                "{\n"
8542                "void Bar();\n"
8543                "};",
8544                Style);
8545 }
8546 
8547 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
8548   // Elaborate type variable declarations.
8549   verifyFormat("struct foo a = {bar};\nint n;");
8550   verifyFormat("class foo a = {bar};\nint n;");
8551   verifyFormat("union foo a = {bar};\nint n;");
8552 
8553   // Elaborate types inside function definitions.
8554   verifyFormat("struct foo f() {}\nint n;");
8555   verifyFormat("class foo f() {}\nint n;");
8556   verifyFormat("union foo f() {}\nint n;");
8557 
8558   // Templates.
8559   verifyFormat("template <class X> void f() {}\nint n;");
8560   verifyFormat("template <struct X> void f() {}\nint n;");
8561   verifyFormat("template <union X> void f() {}\nint n;");
8562 
8563   // Actual definitions...
8564   verifyFormat("struct {\n} n;");
8565   verifyFormat(
8566       "template <template <class T, class Y>, class Z> class X {\n} n;");
8567   verifyFormat("union Z {\n  int n;\n} x;");
8568   verifyFormat("class MACRO Z {\n} n;");
8569   verifyFormat("class MACRO(X) Z {\n} n;");
8570   verifyFormat("class __attribute__(X) Z {\n} n;");
8571   verifyFormat("class __declspec(X) Z {\n} n;");
8572   verifyFormat("class A##B##C {\n} n;");
8573   verifyFormat("class alignas(16) Z {\n} n;");
8574   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
8575   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
8576 
8577   // Redefinition from nested context:
8578   verifyFormat("class A::B::C {\n} n;");
8579 
8580   // Template definitions.
8581   verifyFormat(
8582       "template <typename F>\n"
8583       "Matcher(const Matcher<F> &Other,\n"
8584       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
8585       "                             !is_same<F, T>::value>::type * = 0)\n"
8586       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
8587 
8588   // FIXME: This is still incorrectly handled at the formatter side.
8589   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
8590   verifyFormat("int i = SomeFunction(a<b, a> b);");
8591 
8592   // FIXME:
8593   // This now gets parsed incorrectly as class definition.
8594   // verifyFormat("class A<int> f() {\n}\nint n;");
8595 
8596   // Elaborate types where incorrectly parsing the structural element would
8597   // break the indent.
8598   verifyFormat("if (true)\n"
8599                "  class X x;\n"
8600                "else\n"
8601                "  f();\n");
8602 
8603   // This is simply incomplete. Formatting is not important, but must not crash.
8604   verifyFormat("class A:");
8605 }
8606 
8607 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
8608   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
8609             format("#error Leave     all         white!!!!! space* alone!\n"));
8610   EXPECT_EQ(
8611       "#warning Leave     all         white!!!!! space* alone!\n",
8612       format("#warning Leave     all         white!!!!! space* alone!\n"));
8613   EXPECT_EQ("#error 1", format("  #  error   1"));
8614   EXPECT_EQ("#warning 1", format("  #  warning 1"));
8615 }
8616 
8617 TEST_F(FormatTest, FormatHashIfExpressions) {
8618   verifyFormat("#if AAAA && BBBB");
8619   verifyFormat("#if (AAAA && BBBB)");
8620   verifyFormat("#elif (AAAA && BBBB)");
8621   // FIXME: Come up with a better indentation for #elif.
8622   verifyFormat(
8623       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
8624       "    defined(BBBBBBBB)\n"
8625       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
8626       "    defined(BBBBBBBB)\n"
8627       "#endif",
8628       getLLVMStyleWithColumns(65));
8629 }
8630 
8631 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
8632   FormatStyle AllowsMergedIf = getGoogleStyle();
8633   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
8634       FormatStyle::SIS_WithoutElse;
8635   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
8636   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
8637   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
8638   EXPECT_EQ("if (true) return 42;",
8639             format("if (true)\nreturn 42;", AllowsMergedIf));
8640   FormatStyle ShortMergedIf = AllowsMergedIf;
8641   ShortMergedIf.ColumnLimit = 25;
8642   verifyFormat("#define A \\\n"
8643                "  if (true) return 42;",
8644                ShortMergedIf);
8645   verifyFormat("#define A \\\n"
8646                "  f();    \\\n"
8647                "  if (true)\n"
8648                "#define B",
8649                ShortMergedIf);
8650   verifyFormat("#define A \\\n"
8651                "  f();    \\\n"
8652                "  if (true)\n"
8653                "g();",
8654                ShortMergedIf);
8655   verifyFormat("{\n"
8656                "#ifdef A\n"
8657                "  // Comment\n"
8658                "  if (true) continue;\n"
8659                "#endif\n"
8660                "  // Comment\n"
8661                "  if (true) continue;\n"
8662                "}",
8663                ShortMergedIf);
8664   ShortMergedIf.ColumnLimit = 33;
8665   verifyFormat("#define A \\\n"
8666                "  if constexpr (true) return 42;",
8667                ShortMergedIf);
8668   verifyFormat("#define A \\\n"
8669                "  if CONSTEXPR (true) return 42;",
8670                ShortMergedIf);
8671   ShortMergedIf.ColumnLimit = 29;
8672   verifyFormat("#define A                   \\\n"
8673                "  if (aaaaaaaaaa) return 1; \\\n"
8674                "  return 2;",
8675                ShortMergedIf);
8676   ShortMergedIf.ColumnLimit = 28;
8677   verifyFormat("#define A         \\\n"
8678                "  if (aaaaaaaaaa) \\\n"
8679                "    return 1;     \\\n"
8680                "  return 2;",
8681                ShortMergedIf);
8682   verifyFormat("#define A                \\\n"
8683                "  if constexpr (aaaaaaa) \\\n"
8684                "    return 1;            \\\n"
8685                "  return 2;",
8686                ShortMergedIf);
8687   verifyFormat("#define A                \\\n"
8688                "  if CONSTEXPR (aaaaaaa) \\\n"
8689                "    return 1;            \\\n"
8690                "  return 2;",
8691                ShortMergedIf);
8692 }
8693 
8694 TEST_F(FormatTest, FormatStarDependingOnContext) {
8695   verifyFormat("void f(int *a);");
8696   verifyFormat("void f() { f(fint * b); }");
8697   verifyFormat("class A {\n  void f(int *a);\n};");
8698   verifyFormat("class A {\n  int *a;\n};");
8699   verifyFormat("namespace a {\n"
8700                "namespace b {\n"
8701                "class A {\n"
8702                "  void f() {}\n"
8703                "  int *a;\n"
8704                "};\n"
8705                "} // namespace b\n"
8706                "} // namespace a");
8707 }
8708 
8709 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
8710   verifyFormat("while");
8711   verifyFormat("operator");
8712 }
8713 
8714 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
8715   // This code would be painfully slow to format if we didn't skip it.
8716   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
8717                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8718                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8719                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8720                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8721                    "A(1, 1)\n"
8722                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
8723                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8724                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8725                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8726                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8727                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8728                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8729                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8730                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8731                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
8732   // Deeply nested part is untouched, rest is formatted.
8733   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
8734             format(std::string("int    i;\n") + Code + "int    j;\n",
8735                    getLLVMStyle(), SC_ExpectIncomplete));
8736 }
8737 
8738 //===----------------------------------------------------------------------===//
8739 // Objective-C tests.
8740 //===----------------------------------------------------------------------===//
8741 
8742 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
8743   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
8744   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
8745             format("-(NSUInteger)indexOfObject:(id)anObject;"));
8746   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
8747   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
8748   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
8749             format("-(NSInteger)Method3:(id)anObject;"));
8750   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
8751             format("-(NSInteger)Method4:(id)anObject;"));
8752   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
8753             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
8754   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
8755             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
8756   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8757             "forAllCells:(BOOL)flag;",
8758             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8759                    "forAllCells:(BOOL)flag;"));
8760 
8761   // Very long objectiveC method declaration.
8762   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
8763                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
8764   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
8765                "                    inRange:(NSRange)range\n"
8766                "                   outRange:(NSRange)out_range\n"
8767                "                  outRange1:(NSRange)out_range1\n"
8768                "                  outRange2:(NSRange)out_range2\n"
8769                "                  outRange3:(NSRange)out_range3\n"
8770                "                  outRange4:(NSRange)out_range4\n"
8771                "                  outRange5:(NSRange)out_range5\n"
8772                "                  outRange6:(NSRange)out_range6\n"
8773                "                  outRange7:(NSRange)out_range7\n"
8774                "                  outRange8:(NSRange)out_range8\n"
8775                "                  outRange9:(NSRange)out_range9;");
8776 
8777   // When the function name has to be wrapped.
8778   FormatStyle Style = getLLVMStyle();
8779   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
8780   // and always indents instead.
8781   Style.IndentWrappedFunctionNames = false;
8782   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8783                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
8784                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
8785                "}",
8786                Style);
8787   Style.IndentWrappedFunctionNames = true;
8788   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8789                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
8790                "               anotherName:(NSString)dddddddddddddd {\n"
8791                "}",
8792                Style);
8793 
8794   verifyFormat("- (int)sum:(vector<int>)numbers;");
8795   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
8796   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
8797   // protocol lists (but not for template classes):
8798   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
8799 
8800   verifyFormat("- (int (*)())foo:(int (*)())f;");
8801   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
8802 
8803   // If there's no return type (very rare in practice!), LLVM and Google style
8804   // agree.
8805   verifyFormat("- foo;");
8806   verifyFormat("- foo:(int)f;");
8807   verifyGoogleFormat("- foo:(int)foo;");
8808 }
8809 
8810 
8811 TEST_F(FormatTest, BreaksStringLiterals) {
8812   EXPECT_EQ("\"some text \"\n"
8813             "\"other\";",
8814             format("\"some text other\";", getLLVMStyleWithColumns(12)));
8815   EXPECT_EQ("\"some text \"\n"
8816             "\"other\";",
8817             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
8818   EXPECT_EQ(
8819       "#define A  \\\n"
8820       "  \"some \"  \\\n"
8821       "  \"text \"  \\\n"
8822       "  \"other\";",
8823       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
8824   EXPECT_EQ(
8825       "#define A  \\\n"
8826       "  \"so \"    \\\n"
8827       "  \"text \"  \\\n"
8828       "  \"other\";",
8829       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
8830 
8831   EXPECT_EQ("\"some text\"",
8832             format("\"some text\"", getLLVMStyleWithColumns(1)));
8833   EXPECT_EQ("\"some text\"",
8834             format("\"some text\"", getLLVMStyleWithColumns(11)));
8835   EXPECT_EQ("\"some \"\n"
8836             "\"text\"",
8837             format("\"some text\"", getLLVMStyleWithColumns(10)));
8838   EXPECT_EQ("\"some \"\n"
8839             "\"text\"",
8840             format("\"some text\"", getLLVMStyleWithColumns(7)));
8841   EXPECT_EQ("\"some\"\n"
8842             "\" tex\"\n"
8843             "\"t\"",
8844             format("\"some text\"", getLLVMStyleWithColumns(6)));
8845   EXPECT_EQ("\"some\"\n"
8846             "\" tex\"\n"
8847             "\" and\"",
8848             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
8849   EXPECT_EQ("\"some\"\n"
8850             "\"/tex\"\n"
8851             "\"/and\"",
8852             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
8853 
8854   EXPECT_EQ("variable =\n"
8855             "    \"long string \"\n"
8856             "    \"literal\";",
8857             format("variable = \"long string literal\";",
8858                    getLLVMStyleWithColumns(20)));
8859 
8860   EXPECT_EQ("variable = f(\n"
8861             "    \"long string \"\n"
8862             "    \"literal\",\n"
8863             "    short,\n"
8864             "    loooooooooooooooooooong);",
8865             format("variable = f(\"long string literal\", short, "
8866                    "loooooooooooooooooooong);",
8867                    getLLVMStyleWithColumns(20)));
8868 
8869   EXPECT_EQ(
8870       "f(g(\"long string \"\n"
8871       "    \"literal\"),\n"
8872       "  b);",
8873       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
8874   EXPECT_EQ("f(g(\"long string \"\n"
8875             "    \"literal\",\n"
8876             "    a),\n"
8877             "  b);",
8878             format("f(g(\"long string literal\", a), b);",
8879                    getLLVMStyleWithColumns(20)));
8880   EXPECT_EQ(
8881       "f(\"one two\".split(\n"
8882       "    variable));",
8883       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8884   EXPECT_EQ("f(\"one two three four five six \"\n"
8885             "  \"seven\".split(\n"
8886             "      really_looooong_variable));",
8887             format("f(\"one two three four five six seven\"."
8888                    "split(really_looooong_variable));",
8889                    getLLVMStyleWithColumns(33)));
8890 
8891   EXPECT_EQ("f(\"some \"\n"
8892             "  \"text\",\n"
8893             "  other);",
8894             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8895 
8896   // Only break as a last resort.
8897   verifyFormat(
8898       "aaaaaaaaaaaaaaaaaaaa(\n"
8899       "    aaaaaaaaaaaaaaaaaaaa,\n"
8900       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8901 
8902   EXPECT_EQ("\"splitmea\"\n"
8903             "\"trandomp\"\n"
8904             "\"oint\"",
8905             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8906 
8907   EXPECT_EQ("\"split/\"\n"
8908             "\"pathat/\"\n"
8909             "\"slashes\"",
8910             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8911 
8912   EXPECT_EQ("\"split/\"\n"
8913             "\"pathat/\"\n"
8914             "\"slashes\"",
8915             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8916   EXPECT_EQ("\"split at \"\n"
8917             "\"spaces/at/\"\n"
8918             "\"slashes.at.any$\"\n"
8919             "\"non-alphanumeric%\"\n"
8920             "\"1111111111characte\"\n"
8921             "\"rs\"",
8922             format("\"split at "
8923                    "spaces/at/"
8924                    "slashes.at."
8925                    "any$non-"
8926                    "alphanumeric%"
8927                    "1111111111characte"
8928                    "rs\"",
8929                    getLLVMStyleWithColumns(20)));
8930 
8931   // Verify that splitting the strings understands
8932   // Style::AlwaysBreakBeforeMultilineStrings.
8933   EXPECT_EQ(
8934       "aaaaaaaaaaaa(\n"
8935       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8936       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8937       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8938              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8939              "aaaaaaaaaaaaaaaaaaaaaa\");",
8940              getGoogleStyle()));
8941   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8942             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8943             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8944                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8945                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8946                    getGoogleStyle()));
8947   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8948             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8949             format("llvm::outs() << "
8950                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8951                    "aaaaaaaaaaaaaaaaaaa\";"));
8952   EXPECT_EQ("ffff(\n"
8953             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8954             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8955             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8956                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8957                    getGoogleStyle()));
8958 
8959   FormatStyle Style = getLLVMStyleWithColumns(12);
8960   Style.BreakStringLiterals = false;
8961   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8962 
8963   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8964   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8965   EXPECT_EQ("#define A \\\n"
8966             "  \"some \" \\\n"
8967             "  \"text \" \\\n"
8968             "  \"other\";",
8969             format("#define A \"some text other\";", AlignLeft));
8970 }
8971 
8972 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8973   EXPECT_EQ("C a = \"some more \"\n"
8974             "      \"text\";",
8975             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8976 }
8977 
8978 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8979   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8980   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8981   EXPECT_EQ("int i = a(b());",
8982             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8983 }
8984 
8985 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8986   EXPECT_EQ(
8987       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8988       "(\n"
8989       "    \"x\t\");",
8990       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8991              "aaaaaaa("
8992              "\"x\t\");"));
8993 }
8994 
8995 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8996   EXPECT_EQ(
8997       "u8\"utf8 string \"\n"
8998       "u8\"literal\";",
8999       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
9000   EXPECT_EQ(
9001       "u\"utf16 string \"\n"
9002       "u\"literal\";",
9003       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
9004   EXPECT_EQ(
9005       "U\"utf32 string \"\n"
9006       "U\"literal\";",
9007       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
9008   EXPECT_EQ("L\"wide string \"\n"
9009             "L\"literal\";",
9010             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
9011   EXPECT_EQ("@\"NSString \"\n"
9012             "@\"literal\";",
9013             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
9014   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
9015 
9016   // This input makes clang-format try to split the incomplete unicode escape
9017   // sequence, which used to lead to a crasher.
9018   verifyNoCrash(
9019       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
9020       getLLVMStyleWithColumns(60));
9021 }
9022 
9023 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
9024   FormatStyle Style = getGoogleStyleWithColumns(15);
9025   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
9026   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
9027   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
9028   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
9029   EXPECT_EQ("u8R\"x(raw literal)x\";",
9030             format("u8R\"x(raw literal)x\";", Style));
9031 }
9032 
9033 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
9034   FormatStyle Style = getLLVMStyleWithColumns(20);
9035   EXPECT_EQ(
9036       "_T(\"aaaaaaaaaaaaaa\")\n"
9037       "_T(\"aaaaaaaaaaaaaa\")\n"
9038       "_T(\"aaaaaaaaaaaa\")",
9039       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
9040   EXPECT_EQ("f(x,\n"
9041             "  _T(\"aaaaaaaaaaaa\")\n"
9042             "  _T(\"aaa\"),\n"
9043             "  z);",
9044             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
9045 
9046   // FIXME: Handle embedded spaces in one iteration.
9047   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
9048   //            "_T(\"aaaaaaaaaaaaa\")\n"
9049   //            "_T(\"aaaaaaaaaaaaa\")\n"
9050   //            "_T(\"a\")",
9051   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
9052   //                   getLLVMStyleWithColumns(20)));
9053   EXPECT_EQ(
9054       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
9055       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
9056   EXPECT_EQ("f(\n"
9057             "#if !TEST\n"
9058             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
9059             "#endif\n"
9060             ");",
9061             format("f(\n"
9062                    "#if !TEST\n"
9063                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
9064                    "#endif\n"
9065                    ");"));
9066   EXPECT_EQ("f(\n"
9067             "\n"
9068             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
9069             format("f(\n"
9070                    "\n"
9071                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
9072 }
9073 
9074 TEST_F(FormatTest, BreaksStringLiteralOperands) {
9075   // In a function call with two operands, the second can be broken with no line
9076   // break before it.
9077   EXPECT_EQ("func(a, \"long long \"\n"
9078             "        \"long long\");",
9079             format("func(a, \"long long long long\");",
9080                    getLLVMStyleWithColumns(24)));
9081   // In a function call with three operands, the second must be broken with a
9082   // line break before it.
9083   EXPECT_EQ("func(a,\n"
9084             "     \"long long long \"\n"
9085             "     \"long\",\n"
9086             "     c);",
9087             format("func(a, \"long long long long\", c);",
9088                    getLLVMStyleWithColumns(24)));
9089   // In a function call with three operands, the third must be broken with a
9090   // line break before it.
9091   EXPECT_EQ("func(a, b,\n"
9092             "     \"long long long \"\n"
9093             "     \"long\");",
9094             format("func(a, b, \"long long long long\");",
9095                    getLLVMStyleWithColumns(24)));
9096   // In a function call with three operands, both the second and the third must
9097   // be broken with a line break before them.
9098   EXPECT_EQ("func(a,\n"
9099             "     \"long long long \"\n"
9100             "     \"long\",\n"
9101             "     \"long long long \"\n"
9102             "     \"long\");",
9103             format("func(a, \"long long long long\", \"long long long long\");",
9104                    getLLVMStyleWithColumns(24)));
9105   // In a chain of << with two operands, the second can be broken with no line
9106   // break before it.
9107   EXPECT_EQ("a << \"line line \"\n"
9108             "     \"line\";",
9109             format("a << \"line line line\";",
9110                    getLLVMStyleWithColumns(20)));
9111   // In a chain of << with three operands, the second can be broken with no line
9112   // break before it.
9113   EXPECT_EQ("abcde << \"line \"\n"
9114             "         \"line line\"\n"
9115             "      << c;",
9116             format("abcde << \"line line line\" << c;",
9117                    getLLVMStyleWithColumns(20)));
9118   // In a chain of << with three operands, the third must be broken with a line
9119   // break before it.
9120   EXPECT_EQ("a << b\n"
9121             "  << \"line line \"\n"
9122             "     \"line\";",
9123             format("a << b << \"line line line\";",
9124                    getLLVMStyleWithColumns(20)));
9125   // In a chain of << with three operands, the second can be broken with no line
9126   // break before it and the third must be broken with a line break before it.
9127   EXPECT_EQ("abcd << \"line line \"\n"
9128             "        \"line\"\n"
9129             "     << \"line line \"\n"
9130             "        \"line\";",
9131             format("abcd << \"line line line\" << \"line line line\";",
9132                    getLLVMStyleWithColumns(20)));
9133   // In a chain of binary operators with two operands, the second can be broken
9134   // with no line break before it.
9135   EXPECT_EQ("abcd + \"line line \"\n"
9136             "       \"line line\";",
9137             format("abcd + \"line line line line\";",
9138                    getLLVMStyleWithColumns(20)));
9139   // In a chain of binary operators with three operands, the second must be
9140   // broken with a line break before it.
9141   EXPECT_EQ("abcd +\n"
9142             "    \"line line \"\n"
9143             "    \"line line\" +\n"
9144             "    e;",
9145             format("abcd + \"line line line line\" + e;",
9146                    getLLVMStyleWithColumns(20)));
9147   // In a function call with two operands, with AlignAfterOpenBracket enabled,
9148   // the first must be broken with a line break before it.
9149   FormatStyle Style = getLLVMStyleWithColumns(25);
9150   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9151   EXPECT_EQ("someFunction(\n"
9152             "    \"long long long \"\n"
9153             "    \"long\",\n"
9154             "    a);",
9155             format("someFunction(\"long long long long\", a);", Style));
9156 }
9157 
9158 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
9159   EXPECT_EQ(
9160       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
9161       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
9162       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
9163       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
9164              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
9165              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
9166 }
9167 
9168 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
9169   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
9170             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
9171   EXPECT_EQ("fffffffffff(g(R\"x(\n"
9172             "multiline raw string literal xxxxxxxxxxxxxx\n"
9173             ")x\",\n"
9174             "              a),\n"
9175             "            b);",
9176             format("fffffffffff(g(R\"x(\n"
9177                    "multiline raw string literal xxxxxxxxxxxxxx\n"
9178                    ")x\", a), b);",
9179                    getGoogleStyleWithColumns(20)));
9180   EXPECT_EQ("fffffffffff(\n"
9181             "    g(R\"x(qqq\n"
9182             "multiline raw string literal xxxxxxxxxxxxxx\n"
9183             ")x\",\n"
9184             "      a),\n"
9185             "    b);",
9186             format("fffffffffff(g(R\"x(qqq\n"
9187                    "multiline raw string literal xxxxxxxxxxxxxx\n"
9188                    ")x\", a), b);",
9189                    getGoogleStyleWithColumns(20)));
9190 
9191   EXPECT_EQ("fffffffffff(R\"x(\n"
9192             "multiline raw string literal xxxxxxxxxxxxxx\n"
9193             ")x\");",
9194             format("fffffffffff(R\"x(\n"
9195                    "multiline raw string literal xxxxxxxxxxxxxx\n"
9196                    ")x\");",
9197                    getGoogleStyleWithColumns(20)));
9198   EXPECT_EQ("fffffffffff(R\"x(\n"
9199             "multiline raw string literal xxxxxxxxxxxxxx\n"
9200             ")x\" + bbbbbb);",
9201             format("fffffffffff(R\"x(\n"
9202                    "multiline raw string literal xxxxxxxxxxxxxx\n"
9203                    ")x\" +   bbbbbb);",
9204                    getGoogleStyleWithColumns(20)));
9205   EXPECT_EQ("fffffffffff(\n"
9206             "    R\"x(\n"
9207             "multiline raw string literal xxxxxxxxxxxxxx\n"
9208             ")x\" +\n"
9209             "    bbbbbb);",
9210             format("fffffffffff(\n"
9211                    " R\"x(\n"
9212                    "multiline raw string literal xxxxxxxxxxxxxx\n"
9213                    ")x\" + bbbbbb);",
9214                    getGoogleStyleWithColumns(20)));
9215   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
9216             format("fffffffffff(\n"
9217                    " R\"(single line raw string)\" + bbbbbb);"));
9218 }
9219 
9220 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
9221   verifyFormat("string a = \"unterminated;");
9222   EXPECT_EQ("function(\"unterminated,\n"
9223             "         OtherParameter);",
9224             format("function(  \"unterminated,\n"
9225                    "    OtherParameter);"));
9226 }
9227 
9228 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
9229   FormatStyle Style = getLLVMStyle();
9230   Style.Standard = FormatStyle::LS_Cpp03;
9231   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
9232             format("#define x(_a) printf(\"foo\"_a);", Style));
9233 }
9234 
9235 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
9236 
9237 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
9238   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
9239             "             \"ddeeefff\");",
9240             format("someFunction(\"aaabbbcccdddeeefff\");",
9241                    getLLVMStyleWithColumns(25)));
9242   EXPECT_EQ("someFunction1234567890(\n"
9243             "    \"aaabbbcccdddeeefff\");",
9244             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
9245                    getLLVMStyleWithColumns(26)));
9246   EXPECT_EQ("someFunction1234567890(\n"
9247             "    \"aaabbbcccdddeeeff\"\n"
9248             "    \"f\");",
9249             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
9250                    getLLVMStyleWithColumns(25)));
9251   EXPECT_EQ("someFunction1234567890(\n"
9252             "    \"aaabbbcccdddeeeff\"\n"
9253             "    \"f\");",
9254             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
9255                    getLLVMStyleWithColumns(24)));
9256   EXPECT_EQ("someFunction(\n"
9257             "    \"aaabbbcc ddde \"\n"
9258             "    \"efff\");",
9259             format("someFunction(\"aaabbbcc ddde efff\");",
9260                    getLLVMStyleWithColumns(25)));
9261   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
9262             "             \"ddeeefff\");",
9263             format("someFunction(\"aaabbbccc ddeeefff\");",
9264                    getLLVMStyleWithColumns(25)));
9265   EXPECT_EQ("someFunction1234567890(\n"
9266             "    \"aaabb \"\n"
9267             "    \"cccdddeeefff\");",
9268             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
9269                    getLLVMStyleWithColumns(25)));
9270   EXPECT_EQ("#define A          \\\n"
9271             "  string s =       \\\n"
9272             "      \"123456789\"  \\\n"
9273             "      \"0\";         \\\n"
9274             "  int i;",
9275             format("#define A string s = \"1234567890\"; int i;",
9276                    getLLVMStyleWithColumns(20)));
9277   EXPECT_EQ("someFunction(\n"
9278             "    \"aaabbbcc \"\n"
9279             "    \"dddeeefff\");",
9280             format("someFunction(\"aaabbbcc dddeeefff\");",
9281                    getLLVMStyleWithColumns(25)));
9282 }
9283 
9284 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
9285   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
9286   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
9287   EXPECT_EQ("\"test\"\n"
9288             "\"\\n\"",
9289             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
9290   EXPECT_EQ("\"tes\\\\\"\n"
9291             "\"n\"",
9292             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
9293   EXPECT_EQ("\"\\\\\\\\\"\n"
9294             "\"\\n\"",
9295             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
9296   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
9297   EXPECT_EQ("\"\\uff01\"\n"
9298             "\"test\"",
9299             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
9300   EXPECT_EQ("\"\\Uff01ff02\"",
9301             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
9302   EXPECT_EQ("\"\\x000000000001\"\n"
9303             "\"next\"",
9304             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
9305   EXPECT_EQ("\"\\x000000000001next\"",
9306             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
9307   EXPECT_EQ("\"\\x000000000001\"",
9308             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
9309   EXPECT_EQ("\"test\"\n"
9310             "\"\\000000\"\n"
9311             "\"000001\"",
9312             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
9313   EXPECT_EQ("\"test\\000\"\n"
9314             "\"00000000\"\n"
9315             "\"1\"",
9316             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
9317 }
9318 
9319 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
9320   verifyFormat("void f() {\n"
9321                "  return g() {}\n"
9322                "  void h() {}");
9323   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
9324                "g();\n"
9325                "}");
9326 }
9327 
9328 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
9329   verifyFormat(
9330       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
9331 }
9332 
9333 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
9334   verifyFormat("class X {\n"
9335                "  void f() {\n"
9336                "  }\n"
9337                "};",
9338                getLLVMStyleWithColumns(12));
9339 }
9340 
9341 TEST_F(FormatTest, ConfigurableIndentWidth) {
9342   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
9343   EightIndent.IndentWidth = 8;
9344   EightIndent.ContinuationIndentWidth = 8;
9345   verifyFormat("void f() {\n"
9346                "        someFunction();\n"
9347                "        if (true) {\n"
9348                "                f();\n"
9349                "        }\n"
9350                "}",
9351                EightIndent);
9352   verifyFormat("class X {\n"
9353                "        void f() {\n"
9354                "        }\n"
9355                "};",
9356                EightIndent);
9357   verifyFormat("int x[] = {\n"
9358                "        call(),\n"
9359                "        call()};",
9360                EightIndent);
9361 }
9362 
9363 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
9364   verifyFormat("double\n"
9365                "f();",
9366                getLLVMStyleWithColumns(8));
9367 }
9368 
9369 TEST_F(FormatTest, ConfigurableUseOfTab) {
9370   FormatStyle Tab = getLLVMStyleWithColumns(42);
9371   Tab.IndentWidth = 8;
9372   Tab.UseTab = FormatStyle::UT_Always;
9373   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9374 
9375   EXPECT_EQ("if (aaaaaaaa && // q\n"
9376             "    bb)\t\t// w\n"
9377             "\t;",
9378             format("if (aaaaaaaa &&// q\n"
9379                    "bb)// w\n"
9380                    ";",
9381                    Tab));
9382   EXPECT_EQ("if (aaa && bbb) // w\n"
9383             "\t;",
9384             format("if(aaa&&bbb)// w\n"
9385                    ";",
9386                    Tab));
9387 
9388   verifyFormat("class X {\n"
9389                "\tvoid f() {\n"
9390                "\t\tsomeFunction(parameter1,\n"
9391                "\t\t\t     parameter2);\n"
9392                "\t}\n"
9393                "};",
9394                Tab);
9395   verifyFormat("#define A                        \\\n"
9396                "\tvoid f() {               \\\n"
9397                "\t\tsomeFunction(    \\\n"
9398                "\t\t    parameter1,  \\\n"
9399                "\t\t    parameter2); \\\n"
9400                "\t}",
9401                Tab);
9402   verifyFormat("int a;\t      // x\n"
9403                "int bbbbbbbb; // x\n",
9404                Tab);
9405 
9406   Tab.TabWidth = 4;
9407   Tab.IndentWidth = 8;
9408   verifyFormat("class TabWidth4Indent8 {\n"
9409                "\t\tvoid f() {\n"
9410                "\t\t\t\tsomeFunction(parameter1,\n"
9411                "\t\t\t\t\t\t\t parameter2);\n"
9412                "\t\t}\n"
9413                "};",
9414                Tab);
9415 
9416   Tab.TabWidth = 4;
9417   Tab.IndentWidth = 4;
9418   verifyFormat("class TabWidth4Indent4 {\n"
9419                "\tvoid f() {\n"
9420                "\t\tsomeFunction(parameter1,\n"
9421                "\t\t\t\t\t parameter2);\n"
9422                "\t}\n"
9423                "};",
9424                Tab);
9425 
9426   Tab.TabWidth = 8;
9427   Tab.IndentWidth = 4;
9428   verifyFormat("class TabWidth8Indent4 {\n"
9429                "    void f() {\n"
9430                "\tsomeFunction(parameter1,\n"
9431                "\t\t     parameter2);\n"
9432                "    }\n"
9433                "};",
9434                Tab);
9435 
9436   Tab.TabWidth = 8;
9437   Tab.IndentWidth = 8;
9438   EXPECT_EQ("/*\n"
9439             "\t      a\t\tcomment\n"
9440             "\t      in multiple lines\n"
9441             "       */",
9442             format("   /*\t \t \n"
9443                    " \t \t a\t\tcomment\t \t\n"
9444                    " \t \t in multiple lines\t\n"
9445                    " \t  */",
9446                    Tab));
9447 
9448   Tab.UseTab = FormatStyle::UT_ForIndentation;
9449   verifyFormat("{\n"
9450                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9451                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9452                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9453                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9454                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9455                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9456                "};",
9457                Tab);
9458   verifyFormat("enum AA {\n"
9459                "\ta1, // Force multiple lines\n"
9460                "\ta2,\n"
9461                "\ta3\n"
9462                "};",
9463                Tab);
9464   EXPECT_EQ("if (aaaaaaaa && // q\n"
9465             "    bb)         // w\n"
9466             "\t;",
9467             format("if (aaaaaaaa &&// q\n"
9468                    "bb)// w\n"
9469                    ";",
9470                    Tab));
9471   verifyFormat("class X {\n"
9472                "\tvoid f() {\n"
9473                "\t\tsomeFunction(parameter1,\n"
9474                "\t\t             parameter2);\n"
9475                "\t}\n"
9476                "};",
9477                Tab);
9478   verifyFormat("{\n"
9479                "\tQ(\n"
9480                "\t    {\n"
9481                "\t\t    int a;\n"
9482                "\t\t    someFunction(aaaaaaaa,\n"
9483                "\t\t                 bbbbbbb);\n"
9484                "\t    },\n"
9485                "\t    p);\n"
9486                "}",
9487                Tab);
9488   EXPECT_EQ("{\n"
9489             "\t/* aaaa\n"
9490             "\t   bbbb */\n"
9491             "}",
9492             format("{\n"
9493                    "/* aaaa\n"
9494                    "   bbbb */\n"
9495                    "}",
9496                    Tab));
9497   EXPECT_EQ("{\n"
9498             "\t/*\n"
9499             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9500             "\t  bbbbbbbbbbbbb\n"
9501             "\t*/\n"
9502             "}",
9503             format("{\n"
9504                    "/*\n"
9505                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9506                    "*/\n"
9507                    "}",
9508                    Tab));
9509   EXPECT_EQ("{\n"
9510             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9511             "\t// bbbbbbbbbbbbb\n"
9512             "}",
9513             format("{\n"
9514                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9515                    "}",
9516                    Tab));
9517   EXPECT_EQ("{\n"
9518             "\t/*\n"
9519             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9520             "\t  bbbbbbbbbbbbb\n"
9521             "\t*/\n"
9522             "}",
9523             format("{\n"
9524                    "\t/*\n"
9525                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9526                    "\t*/\n"
9527                    "}",
9528                    Tab));
9529   EXPECT_EQ("{\n"
9530             "\t/*\n"
9531             "\n"
9532             "\t*/\n"
9533             "}",
9534             format("{\n"
9535                    "\t/*\n"
9536                    "\n"
9537                    "\t*/\n"
9538                    "}",
9539                    Tab));
9540   EXPECT_EQ("{\n"
9541             "\t/*\n"
9542             " asdf\n"
9543             "\t*/\n"
9544             "}",
9545             format("{\n"
9546                    "\t/*\n"
9547                    " asdf\n"
9548                    "\t*/\n"
9549                    "}",
9550                    Tab));
9551 
9552   Tab.UseTab = FormatStyle::UT_Never;
9553   EXPECT_EQ("/*\n"
9554             "              a\t\tcomment\n"
9555             "              in multiple lines\n"
9556             "       */",
9557             format("   /*\t \t \n"
9558                    " \t \t a\t\tcomment\t \t\n"
9559                    " \t \t in multiple lines\t\n"
9560                    " \t  */",
9561                    Tab));
9562   EXPECT_EQ("/* some\n"
9563             "   comment */",
9564             format(" \t \t /* some\n"
9565                    " \t \t    comment */",
9566                    Tab));
9567   EXPECT_EQ("int a; /* some\n"
9568             "   comment */",
9569             format(" \t \t int a; /* some\n"
9570                    " \t \t    comment */",
9571                    Tab));
9572 
9573   EXPECT_EQ("int a; /* some\n"
9574             "comment */",
9575             format(" \t \t int\ta; /* some\n"
9576                    " \t \t    comment */",
9577                    Tab));
9578   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9579             "    comment */",
9580             format(" \t \t f(\"\t\t\"); /* some\n"
9581                    " \t \t    comment */",
9582                    Tab));
9583   EXPECT_EQ("{\n"
9584             "  /*\n"
9585             "   * Comment\n"
9586             "   */\n"
9587             "  int i;\n"
9588             "}",
9589             format("{\n"
9590                    "\t/*\n"
9591                    "\t * Comment\n"
9592                    "\t */\n"
9593                    "\t int i;\n"
9594                    "}"));
9595 
9596   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
9597   Tab.TabWidth = 8;
9598   Tab.IndentWidth = 8;
9599   EXPECT_EQ("if (aaaaaaaa && // q\n"
9600             "    bb)         // w\n"
9601             "\t;",
9602             format("if (aaaaaaaa &&// q\n"
9603                    "bb)// w\n"
9604                    ";",
9605                    Tab));
9606   EXPECT_EQ("if (aaa && bbb) // w\n"
9607             "\t;",
9608             format("if(aaa&&bbb)// w\n"
9609                    ";",
9610                    Tab));
9611   verifyFormat("class X {\n"
9612                "\tvoid f() {\n"
9613                "\t\tsomeFunction(parameter1,\n"
9614                "\t\t\t     parameter2);\n"
9615                "\t}\n"
9616                "};",
9617                Tab);
9618   verifyFormat("#define A                        \\\n"
9619                "\tvoid f() {               \\\n"
9620                "\t\tsomeFunction(    \\\n"
9621                "\t\t    parameter1,  \\\n"
9622                "\t\t    parameter2); \\\n"
9623                "\t}",
9624                Tab);
9625   Tab.TabWidth = 4;
9626   Tab.IndentWidth = 8;
9627   verifyFormat("class TabWidth4Indent8 {\n"
9628                "\t\tvoid f() {\n"
9629                "\t\t\t\tsomeFunction(parameter1,\n"
9630                "\t\t\t\t\t\t\t parameter2);\n"
9631                "\t\t}\n"
9632                "};",
9633                Tab);
9634   Tab.TabWidth = 4;
9635   Tab.IndentWidth = 4;
9636   verifyFormat("class TabWidth4Indent4 {\n"
9637                "\tvoid f() {\n"
9638                "\t\tsomeFunction(parameter1,\n"
9639                "\t\t\t\t\t parameter2);\n"
9640                "\t}\n"
9641                "};",
9642                Tab);
9643   Tab.TabWidth = 8;
9644   Tab.IndentWidth = 4;
9645   verifyFormat("class TabWidth8Indent4 {\n"
9646                "    void f() {\n"
9647                "\tsomeFunction(parameter1,\n"
9648                "\t\t     parameter2);\n"
9649                "    }\n"
9650                "};",
9651                Tab);
9652   Tab.TabWidth = 8;
9653   Tab.IndentWidth = 8;
9654   EXPECT_EQ("/*\n"
9655             "\t      a\t\tcomment\n"
9656             "\t      in multiple lines\n"
9657             "       */",
9658             format("   /*\t \t \n"
9659                    " \t \t a\t\tcomment\t \t\n"
9660                    " \t \t in multiple lines\t\n"
9661                    " \t  */",
9662                    Tab));
9663   verifyFormat("{\n"
9664                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9665                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9666                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9667                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9668                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9669                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9670                "};",
9671                Tab);
9672   verifyFormat("enum AA {\n"
9673                "\ta1, // Force multiple lines\n"
9674                "\ta2,\n"
9675                "\ta3\n"
9676                "};",
9677                Tab);
9678   EXPECT_EQ("if (aaaaaaaa && // q\n"
9679             "    bb)         // w\n"
9680             "\t;",
9681             format("if (aaaaaaaa &&// q\n"
9682                    "bb)// w\n"
9683                    ";",
9684                    Tab));
9685   verifyFormat("class X {\n"
9686                "\tvoid f() {\n"
9687                "\t\tsomeFunction(parameter1,\n"
9688                "\t\t\t     parameter2);\n"
9689                "\t}\n"
9690                "};",
9691                Tab);
9692   verifyFormat("{\n"
9693                "\tQ(\n"
9694                "\t    {\n"
9695                "\t\t    int a;\n"
9696                "\t\t    someFunction(aaaaaaaa,\n"
9697                "\t\t\t\t bbbbbbb);\n"
9698                "\t    },\n"
9699                "\t    p);\n"
9700                "}",
9701                Tab);
9702   EXPECT_EQ("{\n"
9703             "\t/* aaaa\n"
9704             "\t   bbbb */\n"
9705             "}",
9706             format("{\n"
9707                    "/* aaaa\n"
9708                    "   bbbb */\n"
9709                    "}",
9710                    Tab));
9711   EXPECT_EQ("{\n"
9712             "\t/*\n"
9713             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9714             "\t  bbbbbbbbbbbbb\n"
9715             "\t*/\n"
9716             "}",
9717             format("{\n"
9718                    "/*\n"
9719                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9720                    "*/\n"
9721                    "}",
9722                    Tab));
9723   EXPECT_EQ("{\n"
9724             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9725             "\t// bbbbbbbbbbbbb\n"
9726             "}",
9727             format("{\n"
9728                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9729                    "}",
9730                    Tab));
9731   EXPECT_EQ("{\n"
9732             "\t/*\n"
9733             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9734             "\t  bbbbbbbbbbbbb\n"
9735             "\t*/\n"
9736             "}",
9737             format("{\n"
9738                    "\t/*\n"
9739                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9740                    "\t*/\n"
9741                    "}",
9742                    Tab));
9743   EXPECT_EQ("{\n"
9744             "\t/*\n"
9745             "\n"
9746             "\t*/\n"
9747             "}",
9748             format("{\n"
9749                    "\t/*\n"
9750                    "\n"
9751                    "\t*/\n"
9752                    "}",
9753                    Tab));
9754   EXPECT_EQ("{\n"
9755             "\t/*\n"
9756             " asdf\n"
9757             "\t*/\n"
9758             "}",
9759             format("{\n"
9760                    "\t/*\n"
9761                    " asdf\n"
9762                    "\t*/\n"
9763                    "}",
9764                    Tab));
9765   EXPECT_EQ("/*\n"
9766             "\t      a\t\tcomment\n"
9767             "\t      in multiple lines\n"
9768             "       */",
9769             format("   /*\t \t \n"
9770                    " \t \t a\t\tcomment\t \t\n"
9771                    " \t \t in multiple lines\t\n"
9772                    " \t  */",
9773                    Tab));
9774   EXPECT_EQ("/* some\n"
9775             "   comment */",
9776             format(" \t \t /* some\n"
9777                    " \t \t    comment */",
9778                    Tab));
9779   EXPECT_EQ("int a; /* some\n"
9780             "   comment */",
9781             format(" \t \t int a; /* some\n"
9782                    " \t \t    comment */",
9783                    Tab));
9784   EXPECT_EQ("int a; /* some\n"
9785             "comment */",
9786             format(" \t \t int\ta; /* some\n"
9787                    " \t \t    comment */",
9788                    Tab));
9789   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9790             "    comment */",
9791             format(" \t \t f(\"\t\t\"); /* some\n"
9792                    " \t \t    comment */",
9793                    Tab));
9794   EXPECT_EQ("{\n"
9795             "  /*\n"
9796             "   * Comment\n"
9797             "   */\n"
9798             "  int i;\n"
9799             "}",
9800             format("{\n"
9801                    "\t/*\n"
9802                    "\t * Comment\n"
9803                    "\t */\n"
9804                    "\t int i;\n"
9805                    "}"));
9806   Tab.AlignConsecutiveAssignments = true;
9807   Tab.AlignConsecutiveDeclarations = true;
9808   Tab.TabWidth = 4;
9809   Tab.IndentWidth = 4;
9810   verifyFormat("class Assign {\n"
9811                "\tvoid f() {\n"
9812                "\t\tint         x      = 123;\n"
9813                "\t\tint         random = 4;\n"
9814                "\t\tstd::string alphabet =\n"
9815                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
9816                "\t}\n"
9817                "};",
9818                Tab);
9819 }
9820 
9821 TEST_F(FormatTest, CalculatesOriginalColumn) {
9822   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9823             "q\"; /* some\n"
9824             "       comment */",
9825             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9826                    "q\"; /* some\n"
9827                    "       comment */",
9828                    getLLVMStyle()));
9829   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9830             "/* some\n"
9831             "   comment */",
9832             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9833                    " /* some\n"
9834                    "    comment */",
9835                    getLLVMStyle()));
9836   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9837             "qqq\n"
9838             "/* some\n"
9839             "   comment */",
9840             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9841                    "qqq\n"
9842                    " /* some\n"
9843                    "    comment */",
9844                    getLLVMStyle()));
9845   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9846             "wwww; /* some\n"
9847             "         comment */",
9848             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9849                    "wwww; /* some\n"
9850                    "         comment */",
9851                    getLLVMStyle()));
9852 }
9853 
9854 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
9855   FormatStyle NoSpace = getLLVMStyle();
9856   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
9857 
9858   verifyFormat("while(true)\n"
9859                "  continue;",
9860                NoSpace);
9861   verifyFormat("for(;;)\n"
9862                "  continue;",
9863                NoSpace);
9864   verifyFormat("if(true)\n"
9865                "  f();\n"
9866                "else if(true)\n"
9867                "  f();",
9868                NoSpace);
9869   verifyFormat("do {\n"
9870                "  do_something();\n"
9871                "} while(something());",
9872                NoSpace);
9873   verifyFormat("switch(x) {\n"
9874                "default:\n"
9875                "  break;\n"
9876                "}",
9877                NoSpace);
9878   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
9879   verifyFormat("size_t x = sizeof(x);", NoSpace);
9880   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
9881   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
9882   verifyFormat("alignas(128) char a[128];", NoSpace);
9883   verifyFormat("size_t x = alignof(MyType);", NoSpace);
9884   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
9885   verifyFormat("int f() throw(Deprecated);", NoSpace);
9886   verifyFormat("typedef void (*cb)(int);", NoSpace);
9887   verifyFormat("T A::operator()();", NoSpace);
9888   verifyFormat("X A::operator++(T);", NoSpace);
9889   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
9890 
9891   FormatStyle Space = getLLVMStyle();
9892   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9893 
9894   verifyFormat("int f ();", Space);
9895   verifyFormat("void f (int a, T b) {\n"
9896                "  while (true)\n"
9897                "    continue;\n"
9898                "}",
9899                Space);
9900   verifyFormat("if (true)\n"
9901                "  f ();\n"
9902                "else if (true)\n"
9903                "  f ();",
9904                Space);
9905   verifyFormat("do {\n"
9906                "  do_something ();\n"
9907                "} while (something ());",
9908                Space);
9909   verifyFormat("switch (x) {\n"
9910                "default:\n"
9911                "  break;\n"
9912                "}",
9913                Space);
9914   verifyFormat("A::A () : a (1) {}", Space);
9915   verifyFormat("void f () __attribute__ ((asdf));", Space);
9916   verifyFormat("*(&a + 1);\n"
9917                "&((&a)[1]);\n"
9918                "a[(b + c) * d];\n"
9919                "(((a + 1) * 2) + 3) * 4;",
9920                Space);
9921   verifyFormat("#define A(x) x", Space);
9922   verifyFormat("#define A (x) x", Space);
9923   verifyFormat("#if defined(x)\n"
9924                "#endif",
9925                Space);
9926   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9927   verifyFormat("size_t x = sizeof (x);", Space);
9928   verifyFormat("auto f (int x) -> decltype (x);", Space);
9929   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9930   verifyFormat("alignas (128) char a[128];", Space);
9931   verifyFormat("size_t x = alignof (MyType);", Space);
9932   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9933   verifyFormat("int f () throw (Deprecated);", Space);
9934   verifyFormat("typedef void (*cb) (int);", Space);
9935   verifyFormat("T A::operator() ();", Space);
9936   verifyFormat("X A::operator++ (T);", Space);
9937   verifyFormat("auto lambda = [] () { return 0; };", Space);
9938   verifyFormat("int x = int (y);", Space);
9939 
9940   FormatStyle SomeSpace = getLLVMStyle();
9941   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
9942 
9943   verifyFormat("[]() -> float {}", SomeSpace);
9944   verifyFormat("[] (auto foo) {}", SomeSpace);
9945   verifyFormat("[foo]() -> int {}", SomeSpace);
9946   verifyFormat("int f();", SomeSpace);
9947   verifyFormat("void f (int a, T b) {\n"
9948                "  while (true)\n"
9949                "    continue;\n"
9950                "}",
9951                SomeSpace);
9952   verifyFormat("if (true)\n"
9953                "  f();\n"
9954                "else if (true)\n"
9955                "  f();",
9956                SomeSpace);
9957   verifyFormat("do {\n"
9958                "  do_something();\n"
9959                "} while (something());",
9960                SomeSpace);
9961   verifyFormat("switch (x) {\n"
9962                "default:\n"
9963                "  break;\n"
9964                "}",
9965                SomeSpace);
9966   verifyFormat("A::A() : a (1) {}", SomeSpace);
9967   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
9968   verifyFormat("*(&a + 1);\n"
9969                "&((&a)[1]);\n"
9970                "a[(b + c) * d];\n"
9971                "(((a + 1) * 2) + 3) * 4;",
9972                SomeSpace);
9973   verifyFormat("#define A(x) x", SomeSpace);
9974   verifyFormat("#define A (x) x", SomeSpace);
9975   verifyFormat("#if defined(x)\n"
9976                "#endif",
9977                SomeSpace);
9978   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
9979   verifyFormat("size_t x = sizeof (x);", SomeSpace);
9980   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
9981   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
9982   verifyFormat("alignas (128) char a[128];", SomeSpace);
9983   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
9984   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
9985                SomeSpace);
9986   verifyFormat("int f() throw (Deprecated);", SomeSpace);
9987   verifyFormat("typedef void (*cb) (int);", SomeSpace);
9988   verifyFormat("T A::operator()();", SomeSpace);
9989   verifyFormat("X A::operator++ (T);", SomeSpace);
9990   verifyFormat("int x = int (y);", SomeSpace);
9991   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
9992 }
9993 
9994 TEST_F(FormatTest, SpaceAfterLogicalNot) {
9995   FormatStyle Spaces = getLLVMStyle();
9996   Spaces.SpaceAfterLogicalNot = true;
9997 
9998   verifyFormat("bool x = ! y", Spaces);
9999   verifyFormat("if (! isFailure())", Spaces);
10000   verifyFormat("if (! (a && b))", Spaces);
10001   verifyFormat("\"Error!\"", Spaces);
10002   verifyFormat("! ! x", Spaces);
10003 }
10004 
10005 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
10006   FormatStyle Spaces = getLLVMStyle();
10007 
10008   Spaces.SpacesInParentheses = true;
10009   verifyFormat("do_something( ::globalVar );", Spaces);
10010   verifyFormat("call( x, y, z );", Spaces);
10011   verifyFormat("call();", Spaces);
10012   verifyFormat("std::function<void( int, int )> callback;", Spaces);
10013   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
10014                Spaces);
10015   verifyFormat("while ( (bool)1 )\n"
10016                "  continue;",
10017                Spaces);
10018   verifyFormat("for ( ;; )\n"
10019                "  continue;",
10020                Spaces);
10021   verifyFormat("if ( true )\n"
10022                "  f();\n"
10023                "else if ( true )\n"
10024                "  f();",
10025                Spaces);
10026   verifyFormat("do {\n"
10027                "  do_something( (int)i );\n"
10028                "} while ( something() );",
10029                Spaces);
10030   verifyFormat("switch ( x ) {\n"
10031                "default:\n"
10032                "  break;\n"
10033                "}",
10034                Spaces);
10035 
10036   Spaces.SpacesInParentheses = false;
10037   Spaces.SpacesInCStyleCastParentheses = true;
10038   verifyFormat("Type *A = ( Type * )P;", Spaces);
10039   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
10040   verifyFormat("x = ( int32 )y;", Spaces);
10041   verifyFormat("int a = ( int )(2.0f);", Spaces);
10042   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
10043   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
10044   verifyFormat("#define x (( int )-1)", Spaces);
10045 
10046   // Run the first set of tests again with:
10047   Spaces.SpacesInParentheses = false;
10048   Spaces.SpaceInEmptyParentheses = true;
10049   Spaces.SpacesInCStyleCastParentheses = true;
10050   verifyFormat("call(x, y, z);", Spaces);
10051   verifyFormat("call( );", Spaces);
10052   verifyFormat("std::function<void(int, int)> callback;", Spaces);
10053   verifyFormat("while (( bool )1)\n"
10054                "  continue;",
10055                Spaces);
10056   verifyFormat("for (;;)\n"
10057                "  continue;",
10058                Spaces);
10059   verifyFormat("if (true)\n"
10060                "  f( );\n"
10061                "else if (true)\n"
10062                "  f( );",
10063                Spaces);
10064   verifyFormat("do {\n"
10065                "  do_something(( int )i);\n"
10066                "} while (something( ));",
10067                Spaces);
10068   verifyFormat("switch (x) {\n"
10069                "default:\n"
10070                "  break;\n"
10071                "}",
10072                Spaces);
10073 
10074   // Run the first set of tests again with:
10075   Spaces.SpaceAfterCStyleCast = true;
10076   verifyFormat("call(x, y, z);", Spaces);
10077   verifyFormat("call( );", Spaces);
10078   verifyFormat("std::function<void(int, int)> callback;", Spaces);
10079   verifyFormat("while (( bool ) 1)\n"
10080                "  continue;",
10081                Spaces);
10082   verifyFormat("for (;;)\n"
10083                "  continue;",
10084                Spaces);
10085   verifyFormat("if (true)\n"
10086                "  f( );\n"
10087                "else if (true)\n"
10088                "  f( );",
10089                Spaces);
10090   verifyFormat("do {\n"
10091                "  do_something(( int ) i);\n"
10092                "} while (something( ));",
10093                Spaces);
10094   verifyFormat("switch (x) {\n"
10095                "default:\n"
10096                "  break;\n"
10097                "}",
10098                Spaces);
10099 
10100   // Run subset of tests again with:
10101   Spaces.SpacesInCStyleCastParentheses = false;
10102   Spaces.SpaceAfterCStyleCast = true;
10103   verifyFormat("while ((bool) 1)\n"
10104                "  continue;",
10105                Spaces);
10106   verifyFormat("do {\n"
10107                "  do_something((int) i);\n"
10108                "} while (something( ));",
10109                Spaces);
10110 }
10111 
10112 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
10113   verifyFormat("int a[5];");
10114   verifyFormat("a[3] += 42;");
10115 
10116   FormatStyle Spaces = getLLVMStyle();
10117   Spaces.SpacesInSquareBrackets = true;
10118   // Lambdas unchanged.
10119   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
10120   verifyFormat("return [i, args...] {};", Spaces);
10121 
10122   // Not lambdas.
10123   verifyFormat("int a[ 5 ];", Spaces);
10124   verifyFormat("a[ 3 ] += 42;", Spaces);
10125   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
10126   verifyFormat("double &operator[](int i) { return 0; }\n"
10127                "int i;",
10128                Spaces);
10129   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
10130   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
10131   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
10132 }
10133 
10134 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
10135   verifyFormat("int a = 5;");
10136   verifyFormat("a += 42;");
10137   verifyFormat("a or_eq 8;");
10138 
10139   FormatStyle Spaces = getLLVMStyle();
10140   Spaces.SpaceBeforeAssignmentOperators = false;
10141   verifyFormat("int a= 5;", Spaces);
10142   verifyFormat("a+= 42;", Spaces);
10143   verifyFormat("a or_eq 8;", Spaces);
10144 }
10145 
10146 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
10147   verifyFormat("class Foo : public Bar {};");
10148   verifyFormat("Foo::Foo() : foo(1) {}");
10149   verifyFormat("for (auto a : b) {\n}");
10150   verifyFormat("int x = a ? b : c;");
10151   verifyFormat("{\n"
10152                "label0:\n"
10153                "  int x = 0;\n"
10154                "}");
10155   verifyFormat("switch (x) {\n"
10156                "case 1:\n"
10157                "default:\n"
10158                "}");
10159 
10160   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
10161   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
10162   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
10163   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
10164   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
10165   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
10166   verifyFormat("{\n"
10167                "label1:\n"
10168                "  int x = 0;\n"
10169                "}",
10170                CtorInitializerStyle);
10171   verifyFormat("switch (x) {\n"
10172                "case 1:\n"
10173                "default:\n"
10174                "}",
10175                CtorInitializerStyle);
10176   CtorInitializerStyle.BreakConstructorInitializers =
10177       FormatStyle::BCIS_AfterColon;
10178   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
10179                "    aaaaaaaaaaaaaaaa(1),\n"
10180                "    bbbbbbbbbbbbbbbb(2) {}",
10181                CtorInitializerStyle);
10182   CtorInitializerStyle.BreakConstructorInitializers =
10183       FormatStyle::BCIS_BeforeComma;
10184   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
10185                "    : aaaaaaaaaaaaaaaa(1)\n"
10186                "    , bbbbbbbbbbbbbbbb(2) {}",
10187                CtorInitializerStyle);
10188   CtorInitializerStyle.BreakConstructorInitializers =
10189       FormatStyle::BCIS_BeforeColon;
10190   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
10191                "    : aaaaaaaaaaaaaaaa(1),\n"
10192                "      bbbbbbbbbbbbbbbb(2) {}",
10193                CtorInitializerStyle);
10194   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
10195   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
10196                ": aaaaaaaaaaaaaaaa(1),\n"
10197                "  bbbbbbbbbbbbbbbb(2) {}",
10198                CtorInitializerStyle);
10199 
10200   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
10201   InheritanceStyle.SpaceBeforeInheritanceColon = false;
10202   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
10203   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
10204   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
10205   verifyFormat("int x = a ? b : c;", InheritanceStyle);
10206   verifyFormat("{\n"
10207                "label2:\n"
10208                "  int x = 0;\n"
10209                "}",
10210                InheritanceStyle);
10211   verifyFormat("switch (x) {\n"
10212                "case 1:\n"
10213                "default:\n"
10214                "}",
10215                InheritanceStyle);
10216   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
10217   verifyFormat("class Foooooooooooooooooooooo:\n"
10218                "    public aaaaaaaaaaaaaaaaaa,\n"
10219                "    public bbbbbbbbbbbbbbbbbb {\n"
10220                "}",
10221                InheritanceStyle);
10222   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
10223   verifyFormat("class Foooooooooooooooooooooo\n"
10224                "    : public aaaaaaaaaaaaaaaaaa\n"
10225                "    , public bbbbbbbbbbbbbbbbbb {\n"
10226                "}",
10227                InheritanceStyle);
10228   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
10229   verifyFormat("class Foooooooooooooooooooooo\n"
10230                "    : public aaaaaaaaaaaaaaaaaa,\n"
10231                "      public bbbbbbbbbbbbbbbbbb {\n"
10232                "}",
10233                InheritanceStyle);
10234   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
10235   verifyFormat("class Foooooooooooooooooooooo\n"
10236                ": public aaaaaaaaaaaaaaaaaa,\n"
10237                "  public bbbbbbbbbbbbbbbbbb {}",
10238                InheritanceStyle);
10239 
10240   FormatStyle ForLoopStyle = getLLVMStyle();
10241   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
10242   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
10243   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
10244   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
10245   verifyFormat("int x = a ? b : c;", ForLoopStyle);
10246   verifyFormat("{\n"
10247                "label2:\n"
10248                "  int x = 0;\n"
10249                "}",
10250                ForLoopStyle);
10251   verifyFormat("switch (x) {\n"
10252                "case 1:\n"
10253                "default:\n"
10254                "}",
10255                ForLoopStyle);
10256 
10257   FormatStyle NoSpaceStyle = getLLVMStyle();
10258   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
10259   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
10260   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
10261   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
10262   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
10263   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
10264   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
10265   verifyFormat("{\n"
10266                "label3:\n"
10267                "  int x = 0;\n"
10268                "}",
10269                NoSpaceStyle);
10270   verifyFormat("switch (x) {\n"
10271                "case 1:\n"
10272                "default:\n"
10273                "}",
10274                NoSpaceStyle);
10275 }
10276 
10277 TEST_F(FormatTest, AlignConsecutiveMacros) {
10278   FormatStyle Style = getLLVMStyle();
10279   Style.AlignConsecutiveAssignments = true;
10280   Style.AlignConsecutiveDeclarations = true;
10281   Style.AlignConsecutiveMacros = false;
10282 
10283   verifyFormat("#define a 3\n"
10284                "#define bbbb 4\n"
10285                "#define ccc (5)",
10286                Style);
10287 
10288   verifyFormat("#define f(x) (x * x)\n"
10289                "#define fff(x, y, z) (x * y + z)\n"
10290                "#define ffff(x, y) (x - y)",
10291                Style);
10292 
10293   verifyFormat("#define foo(x, y) (x + y)\n"
10294                "#define bar (5, 6)(2 + 2)",
10295                Style);
10296 
10297   verifyFormat("#define a 3\n"
10298                "#define bbbb 4\n"
10299                "#define ccc (5)\n"
10300                "#define f(x) (x * x)\n"
10301                "#define fff(x, y, z) (x * y + z)\n"
10302                "#define ffff(x, y) (x - y)",
10303                Style);
10304 
10305   Style.AlignConsecutiveMacros = true;
10306   verifyFormat("#define a    3\n"
10307                "#define bbbb 4\n"
10308                "#define ccc  (5)",
10309                Style);
10310 
10311   verifyFormat("#define f(x)         (x * x)\n"
10312                "#define fff(x, y, z) (x * y + z)\n"
10313                "#define ffff(x, y)   (x - y)",
10314                Style);
10315 
10316   verifyFormat("#define foo(x, y) (x + y)\n"
10317                "#define bar       (5, 6)(2 + 2)",
10318                Style);
10319 
10320   verifyFormat("#define a            3\n"
10321                "#define bbbb         4\n"
10322                "#define ccc          (5)\n"
10323                "#define f(x)         (x * x)\n"
10324                "#define fff(x, y, z) (x * y + z)\n"
10325                "#define ffff(x, y)   (x - y)",
10326                Style);
10327 
10328   verifyFormat("#define a         5\n"
10329                "#define foo(x, y) (x + y)\n"
10330                "#define CCC       (6)\n"
10331                "auto lambda = []() {\n"
10332                "  auto  ii = 0;\n"
10333                "  float j  = 0;\n"
10334                "  return 0;\n"
10335                "};\n"
10336                "int   i  = 0;\n"
10337                "float i2 = 0;\n"
10338                "auto  v  = type{\n"
10339                "    i = 1,   //\n"
10340                "    (i = 2), //\n"
10341                "    i = 3    //\n"
10342                "};",
10343                Style);
10344 
10345   Style.AlignConsecutiveMacros = false;
10346   Style.ColumnLimit = 20;
10347 
10348   verifyFormat("#define a          \\\n"
10349                "  \"aabbbbbbbbbbbb\"\n"
10350                "#define D          \\\n"
10351                "  \"aabbbbbbbbbbbb\" \\\n"
10352                "  \"ccddeeeeeeeee\"\n"
10353                "#define B          \\\n"
10354                "  \"QQQQQQQQQQQQQ\"  \\\n"
10355                "  \"FFFFFFFFFFFFF\"  \\\n"
10356                "  \"LLLLLLLL\"\n",
10357                Style);
10358 
10359   Style.AlignConsecutiveMacros = true;
10360   verifyFormat("#define a          \\\n"
10361                "  \"aabbbbbbbbbbbb\"\n"
10362                "#define D          \\\n"
10363                "  \"aabbbbbbbbbbbb\" \\\n"
10364                "  \"ccddeeeeeeeee\"\n"
10365                "#define B          \\\n"
10366                "  \"QQQQQQQQQQQQQ\"  \\\n"
10367                "  \"FFFFFFFFFFFFF\"  \\\n"
10368                "  \"LLLLLLLL\"\n",
10369                Style);
10370 }
10371 
10372 TEST_F(FormatTest, AlignConsecutiveAssignments) {
10373   FormatStyle Alignment = getLLVMStyle();
10374   Alignment.AlignConsecutiveMacros = true;
10375   Alignment.AlignConsecutiveAssignments = false;
10376   verifyFormat("int a = 5;\n"
10377                "int oneTwoThree = 123;",
10378                Alignment);
10379   verifyFormat("int a = 5;\n"
10380                "int oneTwoThree = 123;",
10381                Alignment);
10382 
10383   Alignment.AlignConsecutiveAssignments = true;
10384   verifyFormat("int a           = 5;\n"
10385                "int oneTwoThree = 123;",
10386                Alignment);
10387   verifyFormat("int a           = method();\n"
10388                "int oneTwoThree = 133;",
10389                Alignment);
10390   verifyFormat("a &= 5;\n"
10391                "bcd *= 5;\n"
10392                "ghtyf += 5;\n"
10393                "dvfvdb -= 5;\n"
10394                "a /= 5;\n"
10395                "vdsvsv %= 5;\n"
10396                "sfdbddfbdfbb ^= 5;\n"
10397                "dvsdsv |= 5;\n"
10398                "int dsvvdvsdvvv = 123;",
10399                Alignment);
10400   verifyFormat("int i = 1, j = 10;\n"
10401                "something = 2000;",
10402                Alignment);
10403   verifyFormat("something = 2000;\n"
10404                "int i = 1, j = 10;\n",
10405                Alignment);
10406   verifyFormat("something = 2000;\n"
10407                "another   = 911;\n"
10408                "int i = 1, j = 10;\n"
10409                "oneMore = 1;\n"
10410                "i       = 2;",
10411                Alignment);
10412   verifyFormat("int a   = 5;\n"
10413                "int one = 1;\n"
10414                "method();\n"
10415                "int oneTwoThree = 123;\n"
10416                "int oneTwo      = 12;",
10417                Alignment);
10418   verifyFormat("int oneTwoThree = 123;\n"
10419                "int oneTwo      = 12;\n"
10420                "method();\n",
10421                Alignment);
10422   verifyFormat("int oneTwoThree = 123; // comment\n"
10423                "int oneTwo      = 12;  // comment",
10424                Alignment);
10425   EXPECT_EQ("int a = 5;\n"
10426             "\n"
10427             "int oneTwoThree = 123;",
10428             format("int a       = 5;\n"
10429                    "\n"
10430                    "int oneTwoThree= 123;",
10431                    Alignment));
10432   EXPECT_EQ("int a   = 5;\n"
10433             "int one = 1;\n"
10434             "\n"
10435             "int oneTwoThree = 123;",
10436             format("int a = 5;\n"
10437                    "int one = 1;\n"
10438                    "\n"
10439                    "int oneTwoThree = 123;",
10440                    Alignment));
10441   EXPECT_EQ("int a   = 5;\n"
10442             "int one = 1;\n"
10443             "\n"
10444             "int oneTwoThree = 123;\n"
10445             "int oneTwo      = 12;",
10446             format("int a = 5;\n"
10447                    "int one = 1;\n"
10448                    "\n"
10449                    "int oneTwoThree = 123;\n"
10450                    "int oneTwo = 12;",
10451                    Alignment));
10452   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
10453   verifyFormat("#define A \\\n"
10454                "  int aaaa       = 12; \\\n"
10455                "  int b          = 23; \\\n"
10456                "  int ccc        = 234; \\\n"
10457                "  int dddddddddd = 2345;",
10458                Alignment);
10459   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10460   verifyFormat("#define A               \\\n"
10461                "  int aaaa       = 12;  \\\n"
10462                "  int b          = 23;  \\\n"
10463                "  int ccc        = 234; \\\n"
10464                "  int dddddddddd = 2345;",
10465                Alignment);
10466   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
10467   verifyFormat("#define A                                                      "
10468                "                \\\n"
10469                "  int aaaa       = 12;                                         "
10470                "                \\\n"
10471                "  int b          = 23;                                         "
10472                "                \\\n"
10473                "  int ccc        = 234;                                        "
10474                "                \\\n"
10475                "  int dddddddddd = 2345;",
10476                Alignment);
10477   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
10478                "k = 4, int l = 5,\n"
10479                "                  int m = 6) {\n"
10480                "  int j      = 10;\n"
10481                "  otherThing = 1;\n"
10482                "}",
10483                Alignment);
10484   verifyFormat("void SomeFunction(int parameter = 0) {\n"
10485                "  int i   = 1;\n"
10486                "  int j   = 2;\n"
10487                "  int big = 10000;\n"
10488                "}",
10489                Alignment);
10490   verifyFormat("class C {\n"
10491                "public:\n"
10492                "  int i            = 1;\n"
10493                "  virtual void f() = 0;\n"
10494                "};",
10495                Alignment);
10496   verifyFormat("int i = 1;\n"
10497                "if (SomeType t = getSomething()) {\n"
10498                "}\n"
10499                "int j   = 2;\n"
10500                "int big = 10000;",
10501                Alignment);
10502   verifyFormat("int j = 7;\n"
10503                "for (int k = 0; k < N; ++k) {\n"
10504                "}\n"
10505                "int j   = 2;\n"
10506                "int big = 10000;\n"
10507                "}",
10508                Alignment);
10509   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10510   verifyFormat("int i = 1;\n"
10511                "LooooooooooongType loooooooooooooooooooooongVariable\n"
10512                "    = someLooooooooooooooooongFunction();\n"
10513                "int j = 2;",
10514                Alignment);
10515   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10516   verifyFormat("int i = 1;\n"
10517                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
10518                "    someLooooooooooooooooongFunction();\n"
10519                "int j = 2;",
10520                Alignment);
10521 
10522   verifyFormat("auto lambda = []() {\n"
10523                "  auto i = 0;\n"
10524                "  return 0;\n"
10525                "};\n"
10526                "int i  = 0;\n"
10527                "auto v = type{\n"
10528                "    i = 1,   //\n"
10529                "    (i = 2), //\n"
10530                "    i = 3    //\n"
10531                "};",
10532                Alignment);
10533 
10534   verifyFormat(
10535       "int i      = 1;\n"
10536       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10537       "                          loooooooooooooooooooooongParameterB);\n"
10538       "int j      = 2;",
10539       Alignment);
10540 
10541   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
10542                "          typename B   = very_long_type_name_1,\n"
10543                "          typename T_2 = very_long_type_name_2>\n"
10544                "auto foo() {}\n",
10545                Alignment);
10546   verifyFormat("int a, b = 1;\n"
10547                "int c  = 2;\n"
10548                "int dd = 3;\n",
10549                Alignment);
10550   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
10551                "float b[1][] = {{3.f}};\n",
10552                Alignment);
10553   verifyFormat("for (int i = 0; i < 1; i++)\n"
10554                "  int x = 1;\n",
10555                Alignment);
10556   verifyFormat("for (i = 0; i < 1; i++)\n"
10557                "  x = 1;\n"
10558                "y = 1;\n",
10559                Alignment);
10560 }
10561 
10562 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
10563   FormatStyle Alignment = getLLVMStyle();
10564   Alignment.AlignConsecutiveMacros = true;
10565   Alignment.AlignConsecutiveDeclarations = false;
10566   verifyFormat("float const a = 5;\n"
10567                "int oneTwoThree = 123;",
10568                Alignment);
10569   verifyFormat("int a = 5;\n"
10570                "float const oneTwoThree = 123;",
10571                Alignment);
10572 
10573   Alignment.AlignConsecutiveDeclarations = true;
10574   verifyFormat("float const a = 5;\n"
10575                "int         oneTwoThree = 123;",
10576                Alignment);
10577   verifyFormat("int         a = method();\n"
10578                "float const oneTwoThree = 133;",
10579                Alignment);
10580   verifyFormat("int i = 1, j = 10;\n"
10581                "something = 2000;",
10582                Alignment);
10583   verifyFormat("something = 2000;\n"
10584                "int i = 1, j = 10;\n",
10585                Alignment);
10586   verifyFormat("float      something = 2000;\n"
10587                "double     another = 911;\n"
10588                "int        i = 1, j = 10;\n"
10589                "const int *oneMore = 1;\n"
10590                "unsigned   i = 2;",
10591                Alignment);
10592   verifyFormat("float a = 5;\n"
10593                "int   one = 1;\n"
10594                "method();\n"
10595                "const double       oneTwoThree = 123;\n"
10596                "const unsigned int oneTwo = 12;",
10597                Alignment);
10598   verifyFormat("int      oneTwoThree{0}; // comment\n"
10599                "unsigned oneTwo;         // comment",
10600                Alignment);
10601   EXPECT_EQ("float const a = 5;\n"
10602             "\n"
10603             "int oneTwoThree = 123;",
10604             format("float const   a = 5;\n"
10605                    "\n"
10606                    "int           oneTwoThree= 123;",
10607                    Alignment));
10608   EXPECT_EQ("float a = 5;\n"
10609             "int   one = 1;\n"
10610             "\n"
10611             "unsigned oneTwoThree = 123;",
10612             format("float    a = 5;\n"
10613                    "int      one = 1;\n"
10614                    "\n"
10615                    "unsigned oneTwoThree = 123;",
10616                    Alignment));
10617   EXPECT_EQ("float a = 5;\n"
10618             "int   one = 1;\n"
10619             "\n"
10620             "unsigned oneTwoThree = 123;\n"
10621             "int      oneTwo = 12;",
10622             format("float    a = 5;\n"
10623                    "int one = 1;\n"
10624                    "\n"
10625                    "unsigned oneTwoThree = 123;\n"
10626                    "int oneTwo = 12;",
10627                    Alignment));
10628   // Function prototype alignment
10629   verifyFormat("int    a();\n"
10630                "double b();",
10631                Alignment);
10632   verifyFormat("int    a(int x);\n"
10633                "double b();",
10634                Alignment);
10635   unsigned OldColumnLimit = Alignment.ColumnLimit;
10636   // We need to set ColumnLimit to zero, in order to stress nested alignments,
10637   // otherwise the function parameters will be re-flowed onto a single line.
10638   Alignment.ColumnLimit = 0;
10639   EXPECT_EQ("int    a(int   x,\n"
10640             "         float y);\n"
10641             "double b(int    x,\n"
10642             "         double y);",
10643             format("int a(int x,\n"
10644                    " float y);\n"
10645                    "double b(int x,\n"
10646                    " double y);",
10647                    Alignment));
10648   // This ensures that function parameters of function declarations are
10649   // correctly indented when their owning functions are indented.
10650   // The failure case here is for 'double y' to not be indented enough.
10651   EXPECT_EQ("double a(int x);\n"
10652             "int    b(int    y,\n"
10653             "         double z);",
10654             format("double a(int x);\n"
10655                    "int b(int y,\n"
10656                    " double z);",
10657                    Alignment));
10658   // Set ColumnLimit low so that we induce wrapping immediately after
10659   // the function name and opening paren.
10660   Alignment.ColumnLimit = 13;
10661   verifyFormat("int function(\n"
10662                "    int  x,\n"
10663                "    bool y);",
10664                Alignment);
10665   Alignment.ColumnLimit = OldColumnLimit;
10666   // Ensure function pointers don't screw up recursive alignment
10667   verifyFormat("int    a(int x, void (*fp)(int y));\n"
10668                "double b();",
10669                Alignment);
10670   Alignment.AlignConsecutiveAssignments = true;
10671   // Ensure recursive alignment is broken by function braces, so that the
10672   // "a = 1" does not align with subsequent assignments inside the function
10673   // body.
10674   verifyFormat("int func(int a = 1) {\n"
10675                "  int b  = 2;\n"
10676                "  int cc = 3;\n"
10677                "}",
10678                Alignment);
10679   verifyFormat("float      something = 2000;\n"
10680                "double     another   = 911;\n"
10681                "int        i = 1, j = 10;\n"
10682                "const int *oneMore = 1;\n"
10683                "unsigned   i       = 2;",
10684                Alignment);
10685   verifyFormat("int      oneTwoThree = {0}; // comment\n"
10686                "unsigned oneTwo      = 0;   // comment",
10687                Alignment);
10688   // Make sure that scope is correctly tracked, in the absence of braces
10689   verifyFormat("for (int i = 0; i < n; i++)\n"
10690                "  j = i;\n"
10691                "double x = 1;\n",
10692                Alignment);
10693   verifyFormat("if (int i = 0)\n"
10694                "  j = i;\n"
10695                "double x = 1;\n",
10696                Alignment);
10697   // Ensure operator[] and operator() are comprehended
10698   verifyFormat("struct test {\n"
10699                "  long long int foo();\n"
10700                "  int           operator[](int a);\n"
10701                "  double        bar();\n"
10702                "};\n",
10703                Alignment);
10704   verifyFormat("struct test {\n"
10705                "  long long int foo();\n"
10706                "  int           operator()(int a);\n"
10707                "  double        bar();\n"
10708                "};\n",
10709                Alignment);
10710   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
10711             "  int const i   = 1;\n"
10712             "  int *     j   = 2;\n"
10713             "  int       big = 10000;\n"
10714             "\n"
10715             "  unsigned oneTwoThree = 123;\n"
10716             "  int      oneTwo      = 12;\n"
10717             "  method();\n"
10718             "  float k  = 2;\n"
10719             "  int   ll = 10000;\n"
10720             "}",
10721             format("void SomeFunction(int parameter= 0) {\n"
10722                    " int const  i= 1;\n"
10723                    "  int *j=2;\n"
10724                    " int big  =  10000;\n"
10725                    "\n"
10726                    "unsigned oneTwoThree  =123;\n"
10727                    "int oneTwo = 12;\n"
10728                    "  method();\n"
10729                    "float k= 2;\n"
10730                    "int ll=10000;\n"
10731                    "}",
10732                    Alignment));
10733   Alignment.AlignConsecutiveAssignments = false;
10734   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
10735   verifyFormat("#define A \\\n"
10736                "  int       aaaa = 12; \\\n"
10737                "  float     b = 23; \\\n"
10738                "  const int ccc = 234; \\\n"
10739                "  unsigned  dddddddddd = 2345;",
10740                Alignment);
10741   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10742   verifyFormat("#define A              \\\n"
10743                "  int       aaaa = 12; \\\n"
10744                "  float     b = 23;    \\\n"
10745                "  const int ccc = 234; \\\n"
10746                "  unsigned  dddddddddd = 2345;",
10747                Alignment);
10748   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
10749   Alignment.ColumnLimit = 30;
10750   verifyFormat("#define A                    \\\n"
10751                "  int       aaaa = 12;       \\\n"
10752                "  float     b = 23;          \\\n"
10753                "  const int ccc = 234;       \\\n"
10754                "  int       dddddddddd = 2345;",
10755                Alignment);
10756   Alignment.ColumnLimit = 80;
10757   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
10758                "k = 4, int l = 5,\n"
10759                "                  int m = 6) {\n"
10760                "  const int j = 10;\n"
10761                "  otherThing = 1;\n"
10762                "}",
10763                Alignment);
10764   verifyFormat("void SomeFunction(int parameter = 0) {\n"
10765                "  int const i = 1;\n"
10766                "  int *     j = 2;\n"
10767                "  int       big = 10000;\n"
10768                "}",
10769                Alignment);
10770   verifyFormat("class C {\n"
10771                "public:\n"
10772                "  int          i = 1;\n"
10773                "  virtual void f() = 0;\n"
10774                "};",
10775                Alignment);
10776   verifyFormat("float i = 1;\n"
10777                "if (SomeType t = getSomething()) {\n"
10778                "}\n"
10779                "const unsigned j = 2;\n"
10780                "int            big = 10000;",
10781                Alignment);
10782   verifyFormat("float j = 7;\n"
10783                "for (int k = 0; k < N; ++k) {\n"
10784                "}\n"
10785                "unsigned j = 2;\n"
10786                "int      big = 10000;\n"
10787                "}",
10788                Alignment);
10789   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10790   verifyFormat("float              i = 1;\n"
10791                "LooooooooooongType loooooooooooooooooooooongVariable\n"
10792                "    = someLooooooooooooooooongFunction();\n"
10793                "int j = 2;",
10794                Alignment);
10795   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10796   verifyFormat("int                i = 1;\n"
10797                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
10798                "    someLooooooooooooooooongFunction();\n"
10799                "int j = 2;",
10800                Alignment);
10801 
10802   Alignment.AlignConsecutiveAssignments = true;
10803   verifyFormat("auto lambda = []() {\n"
10804                "  auto  ii = 0;\n"
10805                "  float j  = 0;\n"
10806                "  return 0;\n"
10807                "};\n"
10808                "int   i  = 0;\n"
10809                "float i2 = 0;\n"
10810                "auto  v  = type{\n"
10811                "    i = 1,   //\n"
10812                "    (i = 2), //\n"
10813                "    i = 3    //\n"
10814                "};",
10815                Alignment);
10816   Alignment.AlignConsecutiveAssignments = false;
10817 
10818   verifyFormat(
10819       "int      i = 1;\n"
10820       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10821       "                          loooooooooooooooooooooongParameterB);\n"
10822       "int      j = 2;",
10823       Alignment);
10824 
10825   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
10826   // We expect declarations and assignments to align, as long as it doesn't
10827   // exceed the column limit, starting a new alignment sequence whenever it
10828   // happens.
10829   Alignment.AlignConsecutiveAssignments = true;
10830   Alignment.ColumnLimit = 30;
10831   verifyFormat("float    ii              = 1;\n"
10832                "unsigned j               = 2;\n"
10833                "int someVerylongVariable = 1;\n"
10834                "AnotherLongType  ll = 123456;\n"
10835                "VeryVeryLongType k  = 2;\n"
10836                "int              myvar = 1;",
10837                Alignment);
10838   Alignment.ColumnLimit = 80;
10839   Alignment.AlignConsecutiveAssignments = false;
10840 
10841   verifyFormat(
10842       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
10843       "          typename LongType, typename B>\n"
10844       "auto foo() {}\n",
10845       Alignment);
10846   verifyFormat("float a, b = 1;\n"
10847                "int   c = 2;\n"
10848                "int   dd = 3;\n",
10849                Alignment);
10850   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
10851                "float b[1][] = {{3.f}};\n",
10852                Alignment);
10853   Alignment.AlignConsecutiveAssignments = true;
10854   verifyFormat("float a, b = 1;\n"
10855                "int   c  = 2;\n"
10856                "int   dd = 3;\n",
10857                Alignment);
10858   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
10859                "float b[1][] = {{3.f}};\n",
10860                Alignment);
10861   Alignment.AlignConsecutiveAssignments = false;
10862 
10863   Alignment.ColumnLimit = 30;
10864   Alignment.BinPackParameters = false;
10865   verifyFormat("void foo(float     a,\n"
10866                "         float     b,\n"
10867                "         int       c,\n"
10868                "         uint32_t *d) {\n"
10869                "  int *  e = 0;\n"
10870                "  float  f = 0;\n"
10871                "  double g = 0;\n"
10872                "}\n"
10873                "void bar(ino_t     a,\n"
10874                "         int       b,\n"
10875                "         uint32_t *c,\n"
10876                "         bool      d) {}\n",
10877                Alignment);
10878   Alignment.BinPackParameters = true;
10879   Alignment.ColumnLimit = 80;
10880 
10881   // Bug 33507
10882   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
10883   verifyFormat(
10884       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
10885       "  static const Version verVs2017;\n"
10886       "  return true;\n"
10887       "});\n",
10888       Alignment);
10889   Alignment.PointerAlignment = FormatStyle::PAS_Right;
10890 
10891   // See llvm.org/PR35641
10892   Alignment.AlignConsecutiveDeclarations = true;
10893   verifyFormat("int func() { //\n"
10894                "  int      b;\n"
10895                "  unsigned c;\n"
10896                "}",
10897                Alignment);
10898 
10899   // See PR37175
10900   FormatStyle Style = getMozillaStyle();
10901   Style.AlignConsecutiveDeclarations = true;
10902   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
10903             "foo(int a);",
10904             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
10905 }
10906 
10907 TEST_F(FormatTest, LinuxBraceBreaking) {
10908   FormatStyle LinuxBraceStyle = getLLVMStyle();
10909   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
10910   verifyFormat("namespace a\n"
10911                "{\n"
10912                "class A\n"
10913                "{\n"
10914                "  void f()\n"
10915                "  {\n"
10916                "    if (true) {\n"
10917                "      a();\n"
10918                "      b();\n"
10919                "    } else {\n"
10920                "      a();\n"
10921                "    }\n"
10922                "  }\n"
10923                "  void g() { return; }\n"
10924                "};\n"
10925                "struct B {\n"
10926                "  int x;\n"
10927                "};\n"
10928                "} // namespace a\n",
10929                LinuxBraceStyle);
10930   verifyFormat("enum X {\n"
10931                "  Y = 0,\n"
10932                "}\n",
10933                LinuxBraceStyle);
10934   verifyFormat("struct S {\n"
10935                "  int Type;\n"
10936                "  union {\n"
10937                "    int x;\n"
10938                "    double y;\n"
10939                "  } Value;\n"
10940                "  class C\n"
10941                "  {\n"
10942                "    MyFavoriteType Value;\n"
10943                "  } Class;\n"
10944                "}\n",
10945                LinuxBraceStyle);
10946 }
10947 
10948 TEST_F(FormatTest, MozillaBraceBreaking) {
10949   FormatStyle MozillaBraceStyle = getLLVMStyle();
10950   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
10951   MozillaBraceStyle.FixNamespaceComments = false;
10952   verifyFormat("namespace a {\n"
10953                "class A\n"
10954                "{\n"
10955                "  void f()\n"
10956                "  {\n"
10957                "    if (true) {\n"
10958                "      a();\n"
10959                "      b();\n"
10960                "    }\n"
10961                "  }\n"
10962                "  void g() { return; }\n"
10963                "};\n"
10964                "enum E\n"
10965                "{\n"
10966                "  A,\n"
10967                "  // foo\n"
10968                "  B,\n"
10969                "  C\n"
10970                "};\n"
10971                "struct B\n"
10972                "{\n"
10973                "  int x;\n"
10974                "};\n"
10975                "}\n",
10976                MozillaBraceStyle);
10977   verifyFormat("struct S\n"
10978                "{\n"
10979                "  int Type;\n"
10980                "  union\n"
10981                "  {\n"
10982                "    int x;\n"
10983                "    double y;\n"
10984                "  } Value;\n"
10985                "  class C\n"
10986                "  {\n"
10987                "    MyFavoriteType Value;\n"
10988                "  } Class;\n"
10989                "}\n",
10990                MozillaBraceStyle);
10991 }
10992 
10993 TEST_F(FormatTest, StroustrupBraceBreaking) {
10994   FormatStyle StroustrupBraceStyle = getLLVMStyle();
10995   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10996   verifyFormat("namespace a {\n"
10997                "class A {\n"
10998                "  void f()\n"
10999                "  {\n"
11000                "    if (true) {\n"
11001                "      a();\n"
11002                "      b();\n"
11003                "    }\n"
11004                "  }\n"
11005                "  void g() { return; }\n"
11006                "};\n"
11007                "struct B {\n"
11008                "  int x;\n"
11009                "};\n"
11010                "} // namespace a\n",
11011                StroustrupBraceStyle);
11012 
11013   verifyFormat("void foo()\n"
11014                "{\n"
11015                "  if (a) {\n"
11016                "    a();\n"
11017                "  }\n"
11018                "  else {\n"
11019                "    b();\n"
11020                "  }\n"
11021                "}\n",
11022                StroustrupBraceStyle);
11023 
11024   verifyFormat("#ifdef _DEBUG\n"
11025                "int foo(int i = 0)\n"
11026                "#else\n"
11027                "int foo(int i = 5)\n"
11028                "#endif\n"
11029                "{\n"
11030                "  return i;\n"
11031                "}",
11032                StroustrupBraceStyle);
11033 
11034   verifyFormat("void foo() {}\n"
11035                "void bar()\n"
11036                "#ifdef _DEBUG\n"
11037                "{\n"
11038                "  foo();\n"
11039                "}\n"
11040                "#else\n"
11041                "{\n"
11042                "}\n"
11043                "#endif",
11044                StroustrupBraceStyle);
11045 
11046   verifyFormat("void foobar() { int i = 5; }\n"
11047                "#ifdef _DEBUG\n"
11048                "void bar() {}\n"
11049                "#else\n"
11050                "void bar() { foobar(); }\n"
11051                "#endif",
11052                StroustrupBraceStyle);
11053 }
11054 
11055 TEST_F(FormatTest, AllmanBraceBreaking) {
11056   FormatStyle AllmanBraceStyle = getLLVMStyle();
11057   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
11058 
11059   EXPECT_EQ("namespace a\n"
11060             "{\n"
11061             "void f();\n"
11062             "void g();\n"
11063             "} // namespace a\n",
11064             format("namespace a\n"
11065                    "{\n"
11066                    "void f();\n"
11067                    "void g();\n"
11068                    "}\n",
11069                    AllmanBraceStyle));
11070 
11071   verifyFormat("namespace a\n"
11072                "{\n"
11073                "class A\n"
11074                "{\n"
11075                "  void f()\n"
11076                "  {\n"
11077                "    if (true)\n"
11078                "    {\n"
11079                "      a();\n"
11080                "      b();\n"
11081                "    }\n"
11082                "  }\n"
11083                "  void g() { return; }\n"
11084                "};\n"
11085                "struct B\n"
11086                "{\n"
11087                "  int x;\n"
11088                "};\n"
11089                "} // namespace a",
11090                AllmanBraceStyle);
11091 
11092   verifyFormat("void f()\n"
11093                "{\n"
11094                "  if (true)\n"
11095                "  {\n"
11096                "    a();\n"
11097                "  }\n"
11098                "  else if (false)\n"
11099                "  {\n"
11100                "    b();\n"
11101                "  }\n"
11102                "  else\n"
11103                "  {\n"
11104                "    c();\n"
11105                "  }\n"
11106                "}\n",
11107                AllmanBraceStyle);
11108 
11109   verifyFormat("void f()\n"
11110                "{\n"
11111                "  for (int i = 0; i < 10; ++i)\n"
11112                "  {\n"
11113                "    a();\n"
11114                "  }\n"
11115                "  while (false)\n"
11116                "  {\n"
11117                "    b();\n"
11118                "  }\n"
11119                "  do\n"
11120                "  {\n"
11121                "    c();\n"
11122                "  } while (false)\n"
11123                "}\n",
11124                AllmanBraceStyle);
11125 
11126   verifyFormat("void f(int a)\n"
11127                "{\n"
11128                "  switch (a)\n"
11129                "  {\n"
11130                "  case 0:\n"
11131                "    break;\n"
11132                "  case 1:\n"
11133                "  {\n"
11134                "    break;\n"
11135                "  }\n"
11136                "  case 2:\n"
11137                "  {\n"
11138                "  }\n"
11139                "  break;\n"
11140                "  default:\n"
11141                "    break;\n"
11142                "  }\n"
11143                "}\n",
11144                AllmanBraceStyle);
11145 
11146   verifyFormat("enum X\n"
11147                "{\n"
11148                "  Y = 0,\n"
11149                "}\n",
11150                AllmanBraceStyle);
11151   verifyFormat("enum X\n"
11152                "{\n"
11153                "  Y = 0\n"
11154                "}\n",
11155                AllmanBraceStyle);
11156 
11157   verifyFormat("@interface BSApplicationController ()\n"
11158                "{\n"
11159                "@private\n"
11160                "  id _extraIvar;\n"
11161                "}\n"
11162                "@end\n",
11163                AllmanBraceStyle);
11164 
11165   verifyFormat("#ifdef _DEBUG\n"
11166                "int foo(int i = 0)\n"
11167                "#else\n"
11168                "int foo(int i = 5)\n"
11169                "#endif\n"
11170                "{\n"
11171                "  return i;\n"
11172                "}",
11173                AllmanBraceStyle);
11174 
11175   verifyFormat("void foo() {}\n"
11176                "void bar()\n"
11177                "#ifdef _DEBUG\n"
11178                "{\n"
11179                "  foo();\n"
11180                "}\n"
11181                "#else\n"
11182                "{\n"
11183                "}\n"
11184                "#endif",
11185                AllmanBraceStyle);
11186 
11187   verifyFormat("void foobar() { int i = 5; }\n"
11188                "#ifdef _DEBUG\n"
11189                "void bar() {}\n"
11190                "#else\n"
11191                "void bar() { foobar(); }\n"
11192                "#endif",
11193                AllmanBraceStyle);
11194 
11195   // This shouldn't affect ObjC blocks..
11196   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
11197                "  // ...\n"
11198                "  int i;\n"
11199                "}];",
11200                AllmanBraceStyle);
11201   verifyFormat("void (^block)(void) = ^{\n"
11202                "  // ...\n"
11203                "  int i;\n"
11204                "};",
11205                AllmanBraceStyle);
11206   // .. or dict literals.
11207   verifyFormat("void f()\n"
11208                "{\n"
11209                "  // ...\n"
11210                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
11211                "}",
11212                AllmanBraceStyle);
11213   verifyFormat("void f()\n"
11214                "{\n"
11215                "  // ...\n"
11216                "  [object someMethod:@{a : @\"b\"}];\n"
11217                "}",
11218                AllmanBraceStyle);
11219   verifyFormat("int f()\n"
11220                "{ // comment\n"
11221                "  return 42;\n"
11222                "}",
11223                AllmanBraceStyle);
11224 
11225   AllmanBraceStyle.ColumnLimit = 19;
11226   verifyFormat("void f() { int i; }", AllmanBraceStyle);
11227   AllmanBraceStyle.ColumnLimit = 18;
11228   verifyFormat("void f()\n"
11229                "{\n"
11230                "  int i;\n"
11231                "}",
11232                AllmanBraceStyle);
11233   AllmanBraceStyle.ColumnLimit = 80;
11234 
11235   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
11236   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
11237       FormatStyle::SIS_WithoutElse;
11238   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
11239   verifyFormat("void f(bool b)\n"
11240                "{\n"
11241                "  if (b)\n"
11242                "  {\n"
11243                "    return;\n"
11244                "  }\n"
11245                "}\n",
11246                BreakBeforeBraceShortIfs);
11247   verifyFormat("void f(bool b)\n"
11248                "{\n"
11249                "  if constexpr (b)\n"
11250                "  {\n"
11251                "    return;\n"
11252                "  }\n"
11253                "}\n",
11254                BreakBeforeBraceShortIfs);
11255   verifyFormat("void f(bool b)\n"
11256                "{\n"
11257                "  if CONSTEXPR (b)\n"
11258                "  {\n"
11259                "    return;\n"
11260                "  }\n"
11261                "}\n",
11262                BreakBeforeBraceShortIfs);
11263   verifyFormat("void f(bool b)\n"
11264                "{\n"
11265                "  if (b) return;\n"
11266                "}\n",
11267                BreakBeforeBraceShortIfs);
11268   verifyFormat("void f(bool b)\n"
11269                "{\n"
11270                "  if constexpr (b) return;\n"
11271                "}\n",
11272                BreakBeforeBraceShortIfs);
11273   verifyFormat("void f(bool b)\n"
11274                "{\n"
11275                "  if CONSTEXPR (b) return;\n"
11276                "}\n",
11277                BreakBeforeBraceShortIfs);
11278   verifyFormat("void f(bool b)\n"
11279                "{\n"
11280                "  while (b)\n"
11281                "  {\n"
11282                "    return;\n"
11283                "  }\n"
11284                "}\n",
11285                BreakBeforeBraceShortIfs);
11286 }
11287 
11288 TEST_F(FormatTest, GNUBraceBreaking) {
11289   FormatStyle GNUBraceStyle = getLLVMStyle();
11290   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
11291   verifyFormat("namespace a\n"
11292                "{\n"
11293                "class A\n"
11294                "{\n"
11295                "  void f()\n"
11296                "  {\n"
11297                "    int a;\n"
11298                "    {\n"
11299                "      int b;\n"
11300                "    }\n"
11301                "    if (true)\n"
11302                "      {\n"
11303                "        a();\n"
11304                "        b();\n"
11305                "      }\n"
11306                "  }\n"
11307                "  void g() { return; }\n"
11308                "}\n"
11309                "} // namespace a",
11310                GNUBraceStyle);
11311 
11312   verifyFormat("void f()\n"
11313                "{\n"
11314                "  if (true)\n"
11315                "    {\n"
11316                "      a();\n"
11317                "    }\n"
11318                "  else if (false)\n"
11319                "    {\n"
11320                "      b();\n"
11321                "    }\n"
11322                "  else\n"
11323                "    {\n"
11324                "      c();\n"
11325                "    }\n"
11326                "}\n",
11327                GNUBraceStyle);
11328 
11329   verifyFormat("void f()\n"
11330                "{\n"
11331                "  for (int i = 0; i < 10; ++i)\n"
11332                "    {\n"
11333                "      a();\n"
11334                "    }\n"
11335                "  while (false)\n"
11336                "    {\n"
11337                "      b();\n"
11338                "    }\n"
11339                "  do\n"
11340                "    {\n"
11341                "      c();\n"
11342                "    }\n"
11343                "  while (false);\n"
11344                "}\n",
11345                GNUBraceStyle);
11346 
11347   verifyFormat("void f(int a)\n"
11348                "{\n"
11349                "  switch (a)\n"
11350                "    {\n"
11351                "    case 0:\n"
11352                "      break;\n"
11353                "    case 1:\n"
11354                "      {\n"
11355                "        break;\n"
11356                "      }\n"
11357                "    case 2:\n"
11358                "      {\n"
11359                "      }\n"
11360                "      break;\n"
11361                "    default:\n"
11362                "      break;\n"
11363                "    }\n"
11364                "}\n",
11365                GNUBraceStyle);
11366 
11367   verifyFormat("enum X\n"
11368                "{\n"
11369                "  Y = 0,\n"
11370                "}\n",
11371                GNUBraceStyle);
11372 
11373   verifyFormat("@interface BSApplicationController ()\n"
11374                "{\n"
11375                "@private\n"
11376                "  id _extraIvar;\n"
11377                "}\n"
11378                "@end\n",
11379                GNUBraceStyle);
11380 
11381   verifyFormat("#ifdef _DEBUG\n"
11382                "int foo(int i = 0)\n"
11383                "#else\n"
11384                "int foo(int i = 5)\n"
11385                "#endif\n"
11386                "{\n"
11387                "  return i;\n"
11388                "}",
11389                GNUBraceStyle);
11390 
11391   verifyFormat("void foo() {}\n"
11392                "void bar()\n"
11393                "#ifdef _DEBUG\n"
11394                "{\n"
11395                "  foo();\n"
11396                "}\n"
11397                "#else\n"
11398                "{\n"
11399                "}\n"
11400                "#endif",
11401                GNUBraceStyle);
11402 
11403   verifyFormat("void foobar() { int i = 5; }\n"
11404                "#ifdef _DEBUG\n"
11405                "void bar() {}\n"
11406                "#else\n"
11407                "void bar() { foobar(); }\n"
11408                "#endif",
11409                GNUBraceStyle);
11410 }
11411 
11412 TEST_F(FormatTest, WebKitBraceBreaking) {
11413   FormatStyle WebKitBraceStyle = getLLVMStyle();
11414   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
11415   WebKitBraceStyle.FixNamespaceComments = false;
11416   verifyFormat("namespace a {\n"
11417                "class A {\n"
11418                "  void f()\n"
11419                "  {\n"
11420                "    if (true) {\n"
11421                "      a();\n"
11422                "      b();\n"
11423                "    }\n"
11424                "  }\n"
11425                "  void g() { return; }\n"
11426                "};\n"
11427                "enum E {\n"
11428                "  A,\n"
11429                "  // foo\n"
11430                "  B,\n"
11431                "  C\n"
11432                "};\n"
11433                "struct B {\n"
11434                "  int x;\n"
11435                "};\n"
11436                "}\n",
11437                WebKitBraceStyle);
11438   verifyFormat("struct S {\n"
11439                "  int Type;\n"
11440                "  union {\n"
11441                "    int x;\n"
11442                "    double y;\n"
11443                "  } Value;\n"
11444                "  class C {\n"
11445                "    MyFavoriteType Value;\n"
11446                "  } Class;\n"
11447                "};\n",
11448                WebKitBraceStyle);
11449 }
11450 
11451 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
11452   verifyFormat("void f() {\n"
11453                "  try {\n"
11454                "  } catch (const Exception &e) {\n"
11455                "  }\n"
11456                "}\n",
11457                getLLVMStyle());
11458 }
11459 
11460 TEST_F(FormatTest, UnderstandsPragmas) {
11461   verifyFormat("#pragma omp reduction(| : var)");
11462   verifyFormat("#pragma omp reduction(+ : var)");
11463 
11464   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
11465             "(including parentheses).",
11466             format("#pragma    mark   Any non-hyphenated or hyphenated string "
11467                    "(including parentheses)."));
11468 }
11469 
11470 TEST_F(FormatTest, UnderstandPragmaOption) {
11471   verifyFormat("#pragma option -C -A");
11472 
11473   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
11474 }
11475 
11476 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
11477   FormatStyle Style = getLLVMStyle();
11478   Style.ColumnLimit = 20;
11479 
11480   // See PR41213
11481   EXPECT_EQ("/*\n"
11482             " *\t9012345\n"
11483             " * /8901\n"
11484             " */",
11485             format("/*\n"
11486                    " *\t9012345 /8901\n"
11487                    " */",
11488                    Style));
11489   EXPECT_EQ("/*\n"
11490             " *345678\n"
11491             " *\t/8901\n"
11492             " */",
11493             format("/*\n"
11494                    " *345678\t/8901\n"
11495                    " */",
11496                    Style));
11497 
11498   verifyFormat("int a; // the\n"
11499                "       // comment", Style);
11500   EXPECT_EQ("int a; /* first line\n"
11501             "        * second\n"
11502             "        * line third\n"
11503             "        * line\n"
11504             "        */",
11505             format("int a; /* first line\n"
11506                    "        * second\n"
11507                    "        * line third\n"
11508                    "        * line\n"
11509                    "        */",
11510                    Style));
11511   EXPECT_EQ("int a; // first line\n"
11512             "       // second\n"
11513             "       // line third\n"
11514             "       // line",
11515             format("int a; // first line\n"
11516                    "       // second line\n"
11517                    "       // third line",
11518                    Style));
11519 
11520   Style.PenaltyExcessCharacter = 90;
11521   verifyFormat("int a; // the comment", Style);
11522   EXPECT_EQ("int a; // the comment\n"
11523             "       // aaa",
11524             format("int a; // the comment aaa", Style));
11525   EXPECT_EQ("int a; /* first line\n"
11526             "        * second line\n"
11527             "        * third line\n"
11528             "        */",
11529             format("int a; /* first line\n"
11530                    "        * second line\n"
11531                    "        * third line\n"
11532                    "        */",
11533                    Style));
11534   EXPECT_EQ("int a; // first line\n"
11535             "       // second line\n"
11536             "       // third line",
11537             format("int a; // first line\n"
11538                    "       // second line\n"
11539                    "       // third line",
11540                    Style));
11541   // FIXME: Investigate why this is not getting the same layout as the test
11542   // above.
11543   EXPECT_EQ("int a; /* first line\n"
11544             "        * second line\n"
11545             "        * third line\n"
11546             "        */",
11547             format("int a; /* first line second line third line"
11548                    "\n*/",
11549                    Style));
11550 
11551   EXPECT_EQ("// foo bar baz bazfoo\n"
11552             "// foo bar foo bar\n",
11553             format("// foo bar baz bazfoo\n"
11554                    "// foo bar foo           bar\n",
11555                    Style));
11556   EXPECT_EQ("// foo bar baz bazfoo\n"
11557             "// foo bar foo bar\n",
11558             format("// foo bar baz      bazfoo\n"
11559                    "// foo            bar foo bar\n",
11560                    Style));
11561 
11562   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
11563   // next one.
11564   EXPECT_EQ("// foo bar baz bazfoo\n"
11565             "// bar foo bar\n",
11566             format("// foo bar baz      bazfoo bar\n"
11567                    "// foo            bar\n",
11568                    Style));
11569 
11570   EXPECT_EQ("// foo bar baz bazfoo\n"
11571             "// foo bar baz bazfoo\n"
11572             "// bar foo bar\n",
11573             format("// foo bar baz      bazfoo\n"
11574                    "// foo bar baz      bazfoo bar\n"
11575                    "// foo bar\n",
11576                    Style));
11577 
11578   EXPECT_EQ("// foo bar baz bazfoo\n"
11579             "// foo bar baz bazfoo\n"
11580             "// bar foo bar\n",
11581             format("// foo bar baz      bazfoo\n"
11582                    "// foo bar baz      bazfoo bar\n"
11583                    "// foo           bar\n",
11584                    Style));
11585 
11586   // Make sure we do not keep protruding characters if strict mode reflow is
11587   // cheaper than keeping protruding characters.
11588   Style.ColumnLimit = 21;
11589   EXPECT_EQ("// foo foo foo foo\n"
11590             "// foo foo foo foo\n"
11591             "// foo foo foo foo\n",
11592             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
11593                            Style));
11594 
11595   EXPECT_EQ("int a = /* long block\n"
11596             "           comment */\n"
11597             "    42;",
11598             format("int a = /* long block comment */ 42;", Style));
11599 }
11600 
11601 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
11602   for (size_t i = 1; i < Styles.size(); ++i)                                   \
11603   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
11604                                   << " differs from Style #0"
11605 
11606 TEST_F(FormatTest, GetsPredefinedStyleByName) {
11607   SmallVector<FormatStyle, 3> Styles;
11608   Styles.resize(3);
11609 
11610   Styles[0] = getLLVMStyle();
11611   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
11612   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
11613   EXPECT_ALL_STYLES_EQUAL(Styles);
11614 
11615   Styles[0] = getGoogleStyle();
11616   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
11617   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
11618   EXPECT_ALL_STYLES_EQUAL(Styles);
11619 
11620   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
11621   EXPECT_TRUE(
11622       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
11623   EXPECT_TRUE(
11624       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
11625   EXPECT_ALL_STYLES_EQUAL(Styles);
11626 
11627   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
11628   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
11629   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
11630   EXPECT_ALL_STYLES_EQUAL(Styles);
11631 
11632   Styles[0] = getMozillaStyle();
11633   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
11634   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
11635   EXPECT_ALL_STYLES_EQUAL(Styles);
11636 
11637   Styles[0] = getWebKitStyle();
11638   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
11639   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
11640   EXPECT_ALL_STYLES_EQUAL(Styles);
11641 
11642   Styles[0] = getGNUStyle();
11643   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
11644   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
11645   EXPECT_ALL_STYLES_EQUAL(Styles);
11646 
11647   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
11648 }
11649 
11650 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
11651   SmallVector<FormatStyle, 8> Styles;
11652   Styles.resize(2);
11653 
11654   Styles[0] = getGoogleStyle();
11655   Styles[1] = getLLVMStyle();
11656   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
11657   EXPECT_ALL_STYLES_EQUAL(Styles);
11658 
11659   Styles.resize(5);
11660   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
11661   Styles[1] = getLLVMStyle();
11662   Styles[1].Language = FormatStyle::LK_JavaScript;
11663   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
11664 
11665   Styles[2] = getLLVMStyle();
11666   Styles[2].Language = FormatStyle::LK_JavaScript;
11667   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
11668                                   "BasedOnStyle: Google",
11669                                   &Styles[2])
11670                    .value());
11671 
11672   Styles[3] = getLLVMStyle();
11673   Styles[3].Language = FormatStyle::LK_JavaScript;
11674   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
11675                                   "Language: JavaScript",
11676                                   &Styles[3])
11677                    .value());
11678 
11679   Styles[4] = getLLVMStyle();
11680   Styles[4].Language = FormatStyle::LK_JavaScript;
11681   EXPECT_EQ(0, parseConfiguration("---\n"
11682                                   "BasedOnStyle: LLVM\n"
11683                                   "IndentWidth: 123\n"
11684                                   "---\n"
11685                                   "BasedOnStyle: Google\n"
11686                                   "Language: JavaScript",
11687                                   &Styles[4])
11688                    .value());
11689   EXPECT_ALL_STYLES_EQUAL(Styles);
11690 }
11691 
11692 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
11693   Style.FIELD = false;                                                         \
11694   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
11695   EXPECT_TRUE(Style.FIELD);                                                    \
11696   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
11697   EXPECT_FALSE(Style.FIELD);
11698 
11699 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
11700 
11701 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
11702   Style.STRUCT.FIELD = false;                                                  \
11703   EXPECT_EQ(0,                                                                 \
11704             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
11705                 .value());                                                     \
11706   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
11707   EXPECT_EQ(0,                                                                 \
11708             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
11709                 .value());                                                     \
11710   EXPECT_FALSE(Style.STRUCT.FIELD);
11711 
11712 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
11713   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
11714 
11715 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
11716   EXPECT_NE(VALUE, Style.FIELD);                                               \
11717   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
11718   EXPECT_EQ(VALUE, Style.FIELD)
11719 
11720 TEST_F(FormatTest, ParsesConfigurationBools) {
11721   FormatStyle Style = {};
11722   Style.Language = FormatStyle::LK_Cpp;
11723   CHECK_PARSE_BOOL(AlignOperands);
11724   CHECK_PARSE_BOOL(AlignTrailingComments);
11725   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
11726   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
11727   CHECK_PARSE_BOOL(AlignConsecutiveMacros);
11728   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
11729   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
11730   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
11731   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
11732   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
11733   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
11734   CHECK_PARSE_BOOL(BinPackArguments);
11735   CHECK_PARSE_BOOL(BinPackParameters);
11736   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
11737   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
11738   CHECK_PARSE_BOOL(BreakStringLiterals);
11739   CHECK_PARSE_BOOL(CompactNamespaces);
11740   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
11741   CHECK_PARSE_BOOL(DerivePointerAlignment);
11742   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
11743   CHECK_PARSE_BOOL(DisableFormat);
11744   CHECK_PARSE_BOOL(IndentCaseLabels);
11745   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
11746   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
11747   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
11748   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
11749   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
11750   CHECK_PARSE_BOOL(ReflowComments);
11751   CHECK_PARSE_BOOL(SortIncludes);
11752   CHECK_PARSE_BOOL(SortUsingDeclarations);
11753   CHECK_PARSE_BOOL(SpacesInParentheses);
11754   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
11755   CHECK_PARSE_BOOL(SpacesInAngles);
11756   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
11757   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
11758   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
11759   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
11760   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
11761   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
11762   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
11763   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
11764   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
11765   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
11766   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
11767 
11768   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
11769   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
11770   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
11771   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
11772   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
11773   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
11774   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
11775   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
11776   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
11777   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
11778   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
11779   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
11780   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
11781   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
11782   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
11783   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
11784 }
11785 
11786 #undef CHECK_PARSE_BOOL
11787 
11788 TEST_F(FormatTest, ParsesConfiguration) {
11789   FormatStyle Style = {};
11790   Style.Language = FormatStyle::LK_Cpp;
11791   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
11792   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
11793               ConstructorInitializerIndentWidth, 1234u);
11794   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
11795   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
11796   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
11797   CHECK_PARSE("PenaltyBreakAssignment: 1234",
11798               PenaltyBreakAssignment, 1234u);
11799   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
11800               PenaltyBreakBeforeFirstCallParameter, 1234u);
11801   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
11802               PenaltyBreakTemplateDeclaration, 1234u);
11803   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
11804   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
11805               PenaltyReturnTypeOnItsOwnLine, 1234u);
11806   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
11807               SpacesBeforeTrailingComments, 1234u);
11808   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
11809   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
11810   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
11811 
11812   Style.PointerAlignment = FormatStyle::PAS_Middle;
11813   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
11814               FormatStyle::PAS_Left);
11815   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
11816               FormatStyle::PAS_Right);
11817   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
11818               FormatStyle::PAS_Middle);
11819   // For backward compatibility:
11820   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
11821               FormatStyle::PAS_Left);
11822   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
11823               FormatStyle::PAS_Right);
11824   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
11825               FormatStyle::PAS_Middle);
11826 
11827   Style.Standard = FormatStyle::LS_Auto;
11828   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
11829   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
11830   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
11831   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
11832   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
11833 
11834   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11835   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
11836               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
11837   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
11838               FormatStyle::BOS_None);
11839   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
11840               FormatStyle::BOS_All);
11841   // For backward compatibility:
11842   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
11843               FormatStyle::BOS_None);
11844   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
11845               FormatStyle::BOS_All);
11846 
11847   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
11848   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
11849               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11850   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
11851               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
11852   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
11853               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
11854   // For backward compatibility:
11855   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
11856               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11857 
11858   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
11859   CHECK_PARSE("BreakInheritanceList: BeforeComma",
11860               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11861   CHECK_PARSE("BreakInheritanceList: AfterColon",
11862               BreakInheritanceList, FormatStyle::BILS_AfterColon);
11863   CHECK_PARSE("BreakInheritanceList: BeforeColon",
11864               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
11865   // For backward compatibility:
11866   CHECK_PARSE("BreakBeforeInheritanceComma: true",
11867               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11868 
11869   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11870   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
11871               FormatStyle::BAS_Align);
11872   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
11873               FormatStyle::BAS_DontAlign);
11874   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
11875               FormatStyle::BAS_AlwaysBreak);
11876   // For backward compatibility:
11877   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
11878               FormatStyle::BAS_DontAlign);
11879   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
11880               FormatStyle::BAS_Align);
11881 
11882   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11883   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
11884               FormatStyle::ENAS_DontAlign);
11885   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
11886               FormatStyle::ENAS_Left);
11887   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
11888               FormatStyle::ENAS_Right);
11889   // For backward compatibility:
11890   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
11891               FormatStyle::ENAS_Left);
11892   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
11893               FormatStyle::ENAS_Right);
11894 
11895   Style.UseTab = FormatStyle::UT_ForIndentation;
11896   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
11897   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
11898   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
11899   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
11900               FormatStyle::UT_ForContinuationAndIndentation);
11901   // For backward compatibility:
11902   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
11903   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
11904 
11905   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11906   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
11907               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11908   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
11909               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
11910   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
11911               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
11912   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
11913               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11914   // For backward compatibility:
11915   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
11916               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11917   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
11918               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11919 
11920   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
11921   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
11922               FormatStyle::SBPO_Never);
11923   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
11924               FormatStyle::SBPO_Always);
11925   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
11926               FormatStyle::SBPO_ControlStatements);
11927   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
11928               FormatStyle::SBPO_NonEmptyParentheses);
11929   // For backward compatibility:
11930   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
11931               FormatStyle::SBPO_Never);
11932   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
11933               FormatStyle::SBPO_ControlStatements);
11934 
11935   Style.ColumnLimit = 123;
11936   FormatStyle BaseStyle = getLLVMStyle();
11937   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
11938   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
11939 
11940   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
11941   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
11942               FormatStyle::BS_Attach);
11943   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
11944               FormatStyle::BS_Linux);
11945   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
11946               FormatStyle::BS_Mozilla);
11947   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
11948               FormatStyle::BS_Stroustrup);
11949   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
11950               FormatStyle::BS_Allman);
11951   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
11952   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
11953               FormatStyle::BS_WebKit);
11954   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
11955               FormatStyle::BS_Custom);
11956 
11957   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11958   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
11959               FormatStyle::RTBS_None);
11960   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
11961               FormatStyle::RTBS_All);
11962   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
11963               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
11964   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
11965               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
11966   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
11967               AlwaysBreakAfterReturnType,
11968               FormatStyle::RTBS_TopLevelDefinitions);
11969 
11970   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11971   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
11972               FormatStyle::BTDS_No);
11973   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
11974               FormatStyle::BTDS_MultiLine);
11975   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
11976               FormatStyle::BTDS_Yes);
11977   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
11978               FormatStyle::BTDS_MultiLine);
11979   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
11980               FormatStyle::BTDS_Yes);
11981 
11982   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
11983   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
11984               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
11985   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
11986               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
11987   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
11988               AlwaysBreakAfterDefinitionReturnType,
11989               FormatStyle::DRTBS_TopLevel);
11990 
11991   Style.NamespaceIndentation = FormatStyle::NI_All;
11992   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
11993               FormatStyle::NI_None);
11994   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
11995               FormatStyle::NI_Inner);
11996   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
11997               FormatStyle::NI_All);
11998 
11999   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
12000   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
12001               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
12002   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
12003               AllowShortIfStatementsOnASingleLine,
12004               FormatStyle::SIS_WithoutElse);
12005   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
12006               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always);
12007   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
12008               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
12009   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
12010               AllowShortIfStatementsOnASingleLine,
12011               FormatStyle::SIS_WithoutElse);
12012 
12013   // FIXME: This is required because parsing a configuration simply overwrites
12014   // the first N elements of the list instead of resetting it.
12015   Style.ForEachMacros.clear();
12016   std::vector<std::string> BoostForeach;
12017   BoostForeach.push_back("BOOST_FOREACH");
12018   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
12019   std::vector<std::string> BoostAndQForeach;
12020   BoostAndQForeach.push_back("BOOST_FOREACH");
12021   BoostAndQForeach.push_back("Q_FOREACH");
12022   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
12023               BoostAndQForeach);
12024 
12025   Style.StatementMacros.clear();
12026   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
12027               std::vector<std::string>{"QUNUSED"});
12028   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
12029               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
12030 
12031   Style.NamespaceMacros.clear();
12032   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
12033               std::vector<std::string>{"TESTSUITE"});
12034   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
12035               std::vector<std::string>({"TESTSUITE", "SUITE"}));
12036 
12037   Style.IncludeStyle.IncludeCategories.clear();
12038   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
12039       {"abc/.*", 2}, {".*", 1}};
12040   CHECK_PARSE("IncludeCategories:\n"
12041               "  - Regex: abc/.*\n"
12042               "    Priority: 2\n"
12043               "  - Regex: .*\n"
12044               "    Priority: 1",
12045               IncludeStyle.IncludeCategories, ExpectedCategories);
12046   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
12047               "abc$");
12048 
12049   Style.RawStringFormats.clear();
12050   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
12051       {
12052           FormatStyle::LK_TextProto,
12053           {"pb", "proto"},
12054           {"PARSE_TEXT_PROTO"},
12055           /*CanonicalDelimiter=*/"",
12056           "llvm",
12057       },
12058       {
12059           FormatStyle::LK_Cpp,
12060           {"cc", "cpp"},
12061           {"C_CODEBLOCK", "CPPEVAL"},
12062           /*CanonicalDelimiter=*/"cc",
12063           /*BasedOnStyle=*/"",
12064       },
12065   };
12066 
12067   CHECK_PARSE("RawStringFormats:\n"
12068               "  - Language: TextProto\n"
12069               "    Delimiters:\n"
12070               "      - 'pb'\n"
12071               "      - 'proto'\n"
12072               "    EnclosingFunctions:\n"
12073               "      - 'PARSE_TEXT_PROTO'\n"
12074               "    BasedOnStyle: llvm\n"
12075               "  - Language: Cpp\n"
12076               "    Delimiters:\n"
12077               "      - 'cc'\n"
12078               "      - 'cpp'\n"
12079               "    EnclosingFunctions:\n"
12080               "      - 'C_CODEBLOCK'\n"
12081               "      - 'CPPEVAL'\n"
12082               "    CanonicalDelimiter: 'cc'",
12083               RawStringFormats, ExpectedRawStringFormats);
12084 }
12085 
12086 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
12087   FormatStyle Style = {};
12088   Style.Language = FormatStyle::LK_Cpp;
12089   CHECK_PARSE("Language: Cpp\n"
12090               "IndentWidth: 12",
12091               IndentWidth, 12u);
12092   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
12093                                "IndentWidth: 34",
12094                                &Style),
12095             ParseError::Unsuitable);
12096   EXPECT_EQ(12u, Style.IndentWidth);
12097   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
12098   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
12099 
12100   Style.Language = FormatStyle::LK_JavaScript;
12101   CHECK_PARSE("Language: JavaScript\n"
12102               "IndentWidth: 12",
12103               IndentWidth, 12u);
12104   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
12105   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
12106                                "IndentWidth: 34",
12107                                &Style),
12108             ParseError::Unsuitable);
12109   EXPECT_EQ(23u, Style.IndentWidth);
12110   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
12111   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
12112 
12113   CHECK_PARSE("BasedOnStyle: LLVM\n"
12114               "IndentWidth: 67",
12115               IndentWidth, 67u);
12116 
12117   CHECK_PARSE("---\n"
12118               "Language: JavaScript\n"
12119               "IndentWidth: 12\n"
12120               "---\n"
12121               "Language: Cpp\n"
12122               "IndentWidth: 34\n"
12123               "...\n",
12124               IndentWidth, 12u);
12125 
12126   Style.Language = FormatStyle::LK_Cpp;
12127   CHECK_PARSE("---\n"
12128               "Language: JavaScript\n"
12129               "IndentWidth: 12\n"
12130               "---\n"
12131               "Language: Cpp\n"
12132               "IndentWidth: 34\n"
12133               "...\n",
12134               IndentWidth, 34u);
12135   CHECK_PARSE("---\n"
12136               "IndentWidth: 78\n"
12137               "---\n"
12138               "Language: JavaScript\n"
12139               "IndentWidth: 56\n"
12140               "...\n",
12141               IndentWidth, 78u);
12142 
12143   Style.ColumnLimit = 123;
12144   Style.IndentWidth = 234;
12145   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
12146   Style.TabWidth = 345;
12147   EXPECT_FALSE(parseConfiguration("---\n"
12148                                   "IndentWidth: 456\n"
12149                                   "BreakBeforeBraces: Allman\n"
12150                                   "---\n"
12151                                   "Language: JavaScript\n"
12152                                   "IndentWidth: 111\n"
12153                                   "TabWidth: 111\n"
12154                                   "---\n"
12155                                   "Language: Cpp\n"
12156                                   "BreakBeforeBraces: Stroustrup\n"
12157                                   "TabWidth: 789\n"
12158                                   "...\n",
12159                                   &Style));
12160   EXPECT_EQ(123u, Style.ColumnLimit);
12161   EXPECT_EQ(456u, Style.IndentWidth);
12162   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
12163   EXPECT_EQ(789u, Style.TabWidth);
12164 
12165   EXPECT_EQ(parseConfiguration("---\n"
12166                                "Language: JavaScript\n"
12167                                "IndentWidth: 56\n"
12168                                "---\n"
12169                                "IndentWidth: 78\n"
12170                                "...\n",
12171                                &Style),
12172             ParseError::Error);
12173   EXPECT_EQ(parseConfiguration("---\n"
12174                                "Language: JavaScript\n"
12175                                "IndentWidth: 56\n"
12176                                "---\n"
12177                                "Language: JavaScript\n"
12178                                "IndentWidth: 78\n"
12179                                "...\n",
12180                                &Style),
12181             ParseError::Error);
12182 
12183   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
12184 }
12185 
12186 #undef CHECK_PARSE
12187 
12188 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
12189   FormatStyle Style = {};
12190   Style.Language = FormatStyle::LK_JavaScript;
12191   Style.BreakBeforeTernaryOperators = true;
12192   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
12193   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
12194 
12195   Style.BreakBeforeTernaryOperators = true;
12196   EXPECT_EQ(0, parseConfiguration("---\n"
12197                                   "BasedOnStyle: Google\n"
12198                                   "---\n"
12199                                   "Language: JavaScript\n"
12200                                   "IndentWidth: 76\n"
12201                                   "...\n",
12202                                   &Style)
12203                    .value());
12204   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
12205   EXPECT_EQ(76u, Style.IndentWidth);
12206   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
12207 }
12208 
12209 TEST_F(FormatTest, ConfigurationRoundTripTest) {
12210   FormatStyle Style = getLLVMStyle();
12211   std::string YAML = configurationAsText(Style);
12212   FormatStyle ParsedStyle = {};
12213   ParsedStyle.Language = FormatStyle::LK_Cpp;
12214   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
12215   EXPECT_EQ(Style, ParsedStyle);
12216 }
12217 
12218 TEST_F(FormatTest, WorksFor8bitEncodings) {
12219   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
12220             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
12221             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
12222             "\"\xef\xee\xf0\xf3...\"",
12223             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
12224                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
12225                    "\xef\xee\xf0\xf3...\"",
12226                    getLLVMStyleWithColumns(12)));
12227 }
12228 
12229 TEST_F(FormatTest, HandlesUTF8BOM) {
12230   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
12231   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
12232             format("\xef\xbb\xbf#include <iostream>"));
12233   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
12234             format("\xef\xbb\xbf\n#include <iostream>"));
12235 }
12236 
12237 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
12238 #if !defined(_MSC_VER)
12239 
12240 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
12241   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
12242                getLLVMStyleWithColumns(35));
12243   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
12244                getLLVMStyleWithColumns(31));
12245   verifyFormat("// Однажды в студёную зимнюю пору...",
12246                getLLVMStyleWithColumns(36));
12247   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
12248   verifyFormat("/* Однажды в студёную зимнюю пору... */",
12249                getLLVMStyleWithColumns(39));
12250   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
12251                getLLVMStyleWithColumns(35));
12252 }
12253 
12254 TEST_F(FormatTest, SplitsUTF8Strings) {
12255   // Non-printable characters' width is currently considered to be the length in
12256   // bytes in UTF8. The characters can be displayed in very different manner
12257   // (zero-width, single width with a substitution glyph, expanded to their code
12258   // (e.g. "<8d>"), so there's no single correct way to handle them.
12259   EXPECT_EQ("\"aaaaÄ\"\n"
12260             "\"\xc2\x8d\";",
12261             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
12262   EXPECT_EQ("\"aaaaaaaÄ\"\n"
12263             "\"\xc2\x8d\";",
12264             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
12265   EXPECT_EQ("\"Однажды, в \"\n"
12266             "\"студёную \"\n"
12267             "\"зимнюю \"\n"
12268             "\"пору,\"",
12269             format("\"Однажды, в студёную зимнюю пору,\"",
12270                    getLLVMStyleWithColumns(13)));
12271   EXPECT_EQ(
12272       "\"一 二 三 \"\n"
12273       "\"四 五六 \"\n"
12274       "\"七 八 九 \"\n"
12275       "\"十\"",
12276       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
12277   EXPECT_EQ("\"一\t\"\n"
12278             "\"二 \t\"\n"
12279             "\"三 四 \"\n"
12280             "\"五\t\"\n"
12281             "\"六 \t\"\n"
12282             "\"七 \"\n"
12283             "\"八九十\tqq\"",
12284             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
12285                    getLLVMStyleWithColumns(11)));
12286 
12287   // UTF8 character in an escape sequence.
12288   EXPECT_EQ("\"aaaaaa\"\n"
12289             "\"\\\xC2\x8D\"",
12290             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
12291 }
12292 
12293 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
12294   EXPECT_EQ("const char *sssss =\n"
12295             "    \"一二三四五六七八\\\n"
12296             " 九 十\";",
12297             format("const char *sssss = \"一二三四五六七八\\\n"
12298                    " 九 十\";",
12299                    getLLVMStyleWithColumns(30)));
12300 }
12301 
12302 TEST_F(FormatTest, SplitsUTF8LineComments) {
12303   EXPECT_EQ("// aaaaÄ\xc2\x8d",
12304             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
12305   EXPECT_EQ("// Я из лесу\n"
12306             "// вышел; был\n"
12307             "// сильный\n"
12308             "// мороз.",
12309             format("// Я из лесу вышел; был сильный мороз.",
12310                    getLLVMStyleWithColumns(13)));
12311   EXPECT_EQ("// 一二三\n"
12312             "// 四五六七\n"
12313             "// 八  九\n"
12314             "// 十",
12315             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
12316 }
12317 
12318 TEST_F(FormatTest, SplitsUTF8BlockComments) {
12319   EXPECT_EQ("/* Гляжу,\n"
12320             " * поднимается\n"
12321             " * медленно в\n"
12322             " * гору\n"
12323             " * Лошадка,\n"
12324             " * везущая\n"
12325             " * хворосту\n"
12326             " * воз. */",
12327             format("/* Гляжу, поднимается медленно в гору\n"
12328                    " * Лошадка, везущая хворосту воз. */",
12329                    getLLVMStyleWithColumns(13)));
12330   EXPECT_EQ(
12331       "/* 一二三\n"
12332       " * 四五六七\n"
12333       " * 八  九\n"
12334       " * 十  */",
12335       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
12336   EXPECT_EQ("/* �������� ��������\n"
12337             " * ��������\n"
12338             " * ������-�� */",
12339             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
12340 }
12341 
12342 #endif // _MSC_VER
12343 
12344 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
12345   FormatStyle Style = getLLVMStyle();
12346 
12347   Style.ConstructorInitializerIndentWidth = 4;
12348   verifyFormat(
12349       "SomeClass::Constructor()\n"
12350       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
12351       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
12352       Style);
12353 
12354   Style.ConstructorInitializerIndentWidth = 2;
12355   verifyFormat(
12356       "SomeClass::Constructor()\n"
12357       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
12358       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
12359       Style);
12360 
12361   Style.ConstructorInitializerIndentWidth = 0;
12362   verifyFormat(
12363       "SomeClass::Constructor()\n"
12364       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
12365       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
12366       Style);
12367   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12368   verifyFormat(
12369       "SomeLongTemplateVariableName<\n"
12370       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
12371       Style);
12372   verifyFormat(
12373       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
12374       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
12375       Style);
12376 
12377   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
12378   verifyFormat(
12379       "SomeClass::Constructor() :\n"
12380       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
12381       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
12382       Style);
12383 }
12384 
12385 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
12386   FormatStyle Style = getLLVMStyle();
12387   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
12388   Style.ConstructorInitializerIndentWidth = 4;
12389   verifyFormat("SomeClass::Constructor()\n"
12390                "    : a(a)\n"
12391                "    , b(b)\n"
12392                "    , c(c) {}",
12393                Style);
12394   verifyFormat("SomeClass::Constructor()\n"
12395                "    : a(a) {}",
12396                Style);
12397 
12398   Style.ColumnLimit = 0;
12399   verifyFormat("SomeClass::Constructor()\n"
12400                "    : a(a) {}",
12401                Style);
12402   verifyFormat("SomeClass::Constructor() noexcept\n"
12403                "    : a(a) {}",
12404                Style);
12405   verifyFormat("SomeClass::Constructor()\n"
12406                "    : a(a)\n"
12407                "    , b(b)\n"
12408                "    , c(c) {}",
12409                Style);
12410   verifyFormat("SomeClass::Constructor()\n"
12411                "    : a(a) {\n"
12412                "  foo();\n"
12413                "  bar();\n"
12414                "}",
12415                Style);
12416 
12417   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12418   verifyFormat("SomeClass::Constructor()\n"
12419                "    : a(a)\n"
12420                "    , b(b)\n"
12421                "    , c(c) {\n}",
12422                Style);
12423   verifyFormat("SomeClass::Constructor()\n"
12424                "    : a(a) {\n}",
12425                Style);
12426 
12427   Style.ColumnLimit = 80;
12428   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12429   Style.ConstructorInitializerIndentWidth = 2;
12430   verifyFormat("SomeClass::Constructor()\n"
12431                "  : a(a)\n"
12432                "  , b(b)\n"
12433                "  , c(c) {}",
12434                Style);
12435 
12436   Style.ConstructorInitializerIndentWidth = 0;
12437   verifyFormat("SomeClass::Constructor()\n"
12438                ": a(a)\n"
12439                ", b(b)\n"
12440                ", c(c) {}",
12441                Style);
12442 
12443   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
12444   Style.ConstructorInitializerIndentWidth = 4;
12445   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
12446   verifyFormat(
12447       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
12448       Style);
12449   verifyFormat(
12450       "SomeClass::Constructor()\n"
12451       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
12452       Style);
12453   Style.ConstructorInitializerIndentWidth = 4;
12454   Style.ColumnLimit = 60;
12455   verifyFormat("SomeClass::Constructor()\n"
12456                "    : aaaaaaaa(aaaaaaaa)\n"
12457                "    , aaaaaaaa(aaaaaaaa)\n"
12458                "    , aaaaaaaa(aaaaaaaa) {}",
12459                Style);
12460 }
12461 
12462 TEST_F(FormatTest, Destructors) {
12463   verifyFormat("void F(int &i) { i.~int(); }");
12464   verifyFormat("void F(int &i) { i->~int(); }");
12465 }
12466 
12467 TEST_F(FormatTest, FormatsWithWebKitStyle) {
12468   FormatStyle Style = getWebKitStyle();
12469 
12470   // Don't indent in outer namespaces.
12471   verifyFormat("namespace outer {\n"
12472                "int i;\n"
12473                "namespace inner {\n"
12474                "    int i;\n"
12475                "} // namespace inner\n"
12476                "} // namespace outer\n"
12477                "namespace other_outer {\n"
12478                "int i;\n"
12479                "}",
12480                Style);
12481 
12482   // Don't indent case labels.
12483   verifyFormat("switch (variable) {\n"
12484                "case 1:\n"
12485                "case 2:\n"
12486                "    doSomething();\n"
12487                "    break;\n"
12488                "default:\n"
12489                "    ++variable;\n"
12490                "}",
12491                Style);
12492 
12493   // Wrap before binary operators.
12494   EXPECT_EQ("void f()\n"
12495             "{\n"
12496             "    if (aaaaaaaaaaaaaaaa\n"
12497             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
12498             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
12499             "        return;\n"
12500             "}",
12501             format("void f() {\n"
12502                    "if (aaaaaaaaaaaaaaaa\n"
12503                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
12504                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
12505                    "return;\n"
12506                    "}",
12507                    Style));
12508 
12509   // Allow functions on a single line.
12510   verifyFormat("void f() { return; }", Style);
12511 
12512   // Constructor initializers are formatted one per line with the "," on the
12513   // new line.
12514   verifyFormat("Constructor()\n"
12515                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
12516                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
12517                "          aaaaaaaaaaaaaa)\n"
12518                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
12519                "{\n"
12520                "}",
12521                Style);
12522   verifyFormat("SomeClass::Constructor()\n"
12523                "    : a(a)\n"
12524                "{\n"
12525                "}",
12526                Style);
12527   EXPECT_EQ("SomeClass::Constructor()\n"
12528             "    : a(a)\n"
12529             "{\n"
12530             "}",
12531             format("SomeClass::Constructor():a(a){}", Style));
12532   verifyFormat("SomeClass::Constructor()\n"
12533                "    : a(a)\n"
12534                "    , b(b)\n"
12535                "    , c(c)\n"
12536                "{\n"
12537                "}",
12538                Style);
12539   verifyFormat("SomeClass::Constructor()\n"
12540                "    : a(a)\n"
12541                "{\n"
12542                "    foo();\n"
12543                "    bar();\n"
12544                "}",
12545                Style);
12546 
12547   // Access specifiers should be aligned left.
12548   verifyFormat("class C {\n"
12549                "public:\n"
12550                "    int i;\n"
12551                "};",
12552                Style);
12553 
12554   // Do not align comments.
12555   verifyFormat("int a; // Do not\n"
12556                "double b; // align comments.",
12557                Style);
12558 
12559   // Do not align operands.
12560   EXPECT_EQ("ASSERT(aaaa\n"
12561             "    || bbbb);",
12562             format("ASSERT ( aaaa\n||bbbb);", Style));
12563 
12564   // Accept input's line breaks.
12565   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
12566             "    || bbbbbbbbbbbbbbb) {\n"
12567             "    i++;\n"
12568             "}",
12569             format("if (aaaaaaaaaaaaaaa\n"
12570                    "|| bbbbbbbbbbbbbbb) { i++; }",
12571                    Style));
12572   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
12573             "    i++;\n"
12574             "}",
12575             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
12576 
12577   // Don't automatically break all macro definitions (llvm.org/PR17842).
12578   verifyFormat("#define aNumber 10", Style);
12579   // However, generally keep the line breaks that the user authored.
12580   EXPECT_EQ("#define aNumber \\\n"
12581             "    10",
12582             format("#define aNumber \\\n"
12583                    " 10",
12584                    Style));
12585 
12586   // Keep empty and one-element array literals on a single line.
12587   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
12588             "                                  copyItems:YES];",
12589             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
12590                    "copyItems:YES];",
12591                    Style));
12592   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
12593             "                                  copyItems:YES];",
12594             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
12595                    "             copyItems:YES];",
12596                    Style));
12597   // FIXME: This does not seem right, there should be more indentation before
12598   // the array literal's entries. Nested blocks have the same problem.
12599   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
12600             "    @\"a\",\n"
12601             "    @\"a\"\n"
12602             "]\n"
12603             "                                  copyItems:YES];",
12604             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
12605                    "     @\"a\",\n"
12606                    "     @\"a\"\n"
12607                    "     ]\n"
12608                    "       copyItems:YES];",
12609                    Style));
12610   EXPECT_EQ(
12611       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
12612       "                                  copyItems:YES];",
12613       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
12614              "   copyItems:YES];",
12615              Style));
12616 
12617   verifyFormat("[self.a b:c c:d];", Style);
12618   EXPECT_EQ("[self.a b:c\n"
12619             "        c:d];",
12620             format("[self.a b:c\n"
12621                    "c:d];",
12622                    Style));
12623 }
12624 
12625 TEST_F(FormatTest, FormatsLambdas) {
12626   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
12627   verifyFormat(
12628       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
12629   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
12630   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
12631   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
12632   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
12633   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
12634   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
12635   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
12636   verifyFormat("int x = f(*+[] {});");
12637   verifyFormat("void f() {\n"
12638                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
12639                "}\n");
12640   verifyFormat("void f() {\n"
12641                "  other(x.begin(), //\n"
12642                "        x.end(),   //\n"
12643                "        [&](int, int) { return 1; });\n"
12644                "}\n");
12645   verifyFormat("void f() {\n"
12646                "  other.other.other.other.other(\n"
12647                "      x.begin(), x.end(),\n"
12648                "      [something, rather](int, int, int, int, int, int, int) { return 1; });\n"
12649                "}\n");
12650   verifyFormat("void f() {\n"
12651                "  other.other.other.other.other(\n"
12652                "      x.begin(), x.end(),\n"
12653                "      [something, rather](int, int, int, int, int, int, int) {\n"
12654                "        //\n"
12655                "      });\n"
12656                "}\n");
12657   verifyFormat("SomeFunction([]() { // A cool function...\n"
12658                "  return 43;\n"
12659                "});");
12660   EXPECT_EQ("SomeFunction([]() {\n"
12661             "#define A a\n"
12662             "  return 43;\n"
12663             "});",
12664             format("SomeFunction([](){\n"
12665                    "#define A a\n"
12666                    "return 43;\n"
12667                    "});"));
12668   verifyFormat("void f() {\n"
12669                "  SomeFunction([](decltype(x), A *a) {});\n"
12670                "}");
12671   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12672                "    [](const aaaaaaaaaa &a) { return a; });");
12673   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
12674                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
12675                "});");
12676   verifyFormat("Constructor()\n"
12677                "    : Field([] { // comment\n"
12678                "        int i;\n"
12679                "      }) {}");
12680   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
12681                "  return some_parameter.size();\n"
12682                "};");
12683   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
12684                "    [](const string &s) { return s; };");
12685   verifyFormat("int i = aaaaaa ? 1 //\n"
12686                "               : [] {\n"
12687                "                   return 2; //\n"
12688                "                 }();");
12689   verifyFormat("llvm::errs() << \"number of twos is \"\n"
12690                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
12691                "                  return x == 2; // force break\n"
12692                "                });");
12693   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12694                "    [=](int iiiiiiiiiiii) {\n"
12695                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
12696                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
12697                "    });",
12698                getLLVMStyleWithColumns(60));
12699   verifyFormat("SomeFunction({[&] {\n"
12700                "                // comment\n"
12701                "              },\n"
12702                "              [&] {\n"
12703                "                // comment\n"
12704                "              }});");
12705   verifyFormat("SomeFunction({[&] {\n"
12706                "  // comment\n"
12707                "}});");
12708   verifyFormat("virtual aaaaaaaaaaaaaaaa(\n"
12709                "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
12710                "    aaaaa aaaaaaaaa);");
12711 
12712   // Lambdas with return types.
12713   verifyFormat("int c = []() -> int { return 2; }();\n");
12714   verifyFormat("int c = []() -> int * { return 2; }();\n");
12715   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
12716   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
12717   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
12718   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
12719   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
12720   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
12721   verifyFormat("[a, a]() -> a<1> {};");
12722   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
12723   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
12724   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
12725   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
12726   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
12727   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
12728   verifyFormat("[]() -> foo<!5> { return {}; };");
12729   verifyFormat("[]() -> foo<~5> { return {}; };");
12730   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
12731   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
12732   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
12733   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
12734   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
12735   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
12736   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
12737   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
12738   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
12739   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
12740   verifyFormat("namespace bar {\n"
12741               "// broken:\n"
12742               "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
12743               "} // namespace bar");
12744   verifyFormat("namespace bar {\n"
12745               "// broken:\n"
12746               "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
12747               "} // namespace bar");
12748   verifyFormat("namespace bar {\n"
12749               "// broken:\n"
12750               "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
12751               "} // namespace bar");
12752   verifyFormat("namespace bar {\n"
12753               "// broken:\n"
12754               "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
12755               "} // namespace bar");
12756   verifyFormat("namespace bar {\n"
12757               "// broken:\n"
12758               "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
12759               "} // namespace bar");
12760   verifyFormat("namespace bar {\n"
12761               "// broken:\n"
12762               "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
12763               "} // namespace bar");
12764   verifyFormat("namespace bar {\n"
12765               "// broken:\n"
12766               "auto foo{[]() -> foo<!5> { return {}; }};\n"
12767               "} // namespace bar");
12768   verifyFormat("namespace bar {\n"
12769               "// broken:\n"
12770               "auto foo{[]() -> foo<~5> { return {}; }};\n"
12771               "} // namespace bar");
12772   verifyFormat("namespace bar {\n"
12773               "// broken:\n"
12774               "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
12775               "} // namespace bar");
12776   verifyFormat("namespace bar {\n"
12777               "// broken:\n"
12778               "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
12779               "} // namespace bar");
12780   verifyFormat("namespace bar {\n"
12781               "// broken:\n"
12782               "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
12783               "} // namespace bar");
12784   verifyFormat("namespace bar {\n"
12785               "// broken:\n"
12786               "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
12787               "} // namespace bar");
12788   verifyFormat("namespace bar {\n"
12789               "// broken:\n"
12790               "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
12791               "} // namespace bar");
12792   verifyFormat("namespace bar {\n"
12793               "// broken:\n"
12794               "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
12795               "} // namespace bar");
12796   verifyFormat("namespace bar {\n"
12797               "// broken:\n"
12798               "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
12799               "} // namespace bar");
12800   verifyFormat("namespace bar {\n"
12801               "// broken:\n"
12802               "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
12803               "} // namespace bar");
12804   verifyFormat("namespace bar {\n"
12805               "// broken:\n"
12806               "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
12807               "} // namespace bar");
12808   verifyFormat("namespace bar {\n"
12809               "// broken:\n"
12810               "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
12811               "} // namespace bar");
12812   verifyFormat("[]() -> a<1> {};");
12813   verifyFormat("[]() -> a<1> { ; };");
12814   verifyFormat("[]() -> a<1> { ; }();");
12815   verifyFormat("[a, a]() -> a<true> {};");
12816   verifyFormat("[]() -> a<true> {};");
12817   verifyFormat("[]() -> a<true> { ; };");
12818   verifyFormat("[]() -> a<true> { ; }();");
12819   verifyFormat("[a, a]() -> a<false> {};");
12820   verifyFormat("[]() -> a<false> {};");
12821   verifyFormat("[]() -> a<false> { ; };");
12822   verifyFormat("[]() -> a<false> { ; }();");
12823   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
12824   verifyFormat("namespace bar {\n"
12825                "auto foo{[]() -> foo<false> { ; }};\n"
12826                "} // namespace bar");
12827   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
12828                "                   int j) -> int {\n"
12829                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
12830                "};");
12831   verifyFormat(
12832       "aaaaaaaaaaaaaaaaaaaaaa(\n"
12833       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
12834       "      return aaaaaaaaaaaaaaaaa;\n"
12835       "    });",
12836       getLLVMStyleWithColumns(70));
12837   verifyFormat("[]() //\n"
12838                "    -> int {\n"
12839                "  return 1; //\n"
12840                "};");
12841 
12842   // Multiple lambdas in the same parentheses change indentation rules. These
12843   // lambdas are forced to start on new lines.
12844   verifyFormat("SomeFunction(\n"
12845                "    []() {\n"
12846                "      //\n"
12847                "    },\n"
12848                "    []() {\n"
12849                "      //\n"
12850                "    });");
12851 
12852   // A lambda passed as arg0 is always pushed to the next line.
12853   verifyFormat("SomeFunction(\n"
12854                "    [this] {\n"
12855                "      //\n"
12856                "    },\n"
12857                "    1);\n");
12858 
12859   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0
12860   // case above.
12861   auto Style = getGoogleStyle();
12862   Style.BinPackArguments = false;
12863   verifyFormat("SomeFunction(\n"
12864                "    a,\n"
12865                "    [this] {\n"
12866                "      //\n"
12867                "    },\n"
12868                "    b);\n",
12869                Style);
12870   verifyFormat("SomeFunction(\n"
12871                "    a,\n"
12872                "    [this] {\n"
12873                "      //\n"
12874                "    },\n"
12875                "    b);\n");
12876 
12877   // A lambda with a very long line forces arg0 to be pushed out irrespective of
12878   // the BinPackArguments value (as long as the code is wide enough).
12879   verifyFormat("something->SomeFunction(\n"
12880                "    a,\n"
12881                "    [this] {\n"
12882                "      D0000000000000000000000000000000000000000000000000000000000001();\n"
12883                "    },\n"
12884                "    b);\n");
12885 
12886   // A multi-line lambda is pulled up as long as the introducer fits on the previous
12887   // line and there are no further args.
12888   verifyFormat("function(1, [this, that] {\n"
12889                "  //\n"
12890                "});\n");
12891   verifyFormat("function([this, that] {\n"
12892                "  //\n"
12893                "});\n");
12894   // FIXME: this format is not ideal and we should consider forcing the first arg
12895   // onto its own line.
12896   verifyFormat("function(a, b, c, //\n"
12897                "         d, [this, that] {\n"
12898                "           //\n"
12899                "         });\n");
12900 
12901   // Multiple lambdas are treated correctly even when there is a short arg0.
12902   verifyFormat("SomeFunction(\n"
12903                "    1,\n"
12904                "    [this] {\n"
12905                "      //\n"
12906                "    },\n"
12907                "    [this] {\n"
12908                "      //\n"
12909                "    },\n"
12910                "    1);\n");
12911 
12912   // More complex introducers.
12913   verifyFormat("return [i, args...] {};");
12914 
12915   // Not lambdas.
12916   verifyFormat("constexpr char hello[]{\"hello\"};");
12917   verifyFormat("double &operator[](int i) { return 0; }\n"
12918                "int i;");
12919   verifyFormat("std::unique_ptr<int[]> foo() {}");
12920   verifyFormat("int i = a[a][a]->f();");
12921   verifyFormat("int i = (*b)[a]->f();");
12922 
12923   // Other corner cases.
12924   verifyFormat("void f() {\n"
12925                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
12926                "  );\n"
12927                "}");
12928 
12929   // Lambdas created through weird macros.
12930   verifyFormat("void f() {\n"
12931                "  MACRO((const AA &a) { return 1; });\n"
12932                "  MACRO((AA &a) { return 1; });\n"
12933                "}");
12934 
12935   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
12936                "      doo_dah();\n"
12937                "      doo_dah();\n"
12938                "    })) {\n"
12939                "}");
12940   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
12941                "                doo_dah();\n"
12942                "                doo_dah();\n"
12943                "              })) {\n"
12944                "}");
12945   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
12946                "                doo_dah();\n"
12947                "                doo_dah();\n"
12948                "              })) {\n"
12949                "}");
12950   verifyFormat("auto lambda = []() {\n"
12951                "  int a = 2\n"
12952                "#if A\n"
12953                "          + 2\n"
12954                "#endif\n"
12955                "      ;\n"
12956                "};");
12957 
12958   // Lambdas with complex multiline introducers.
12959   verifyFormat(
12960       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12961       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
12962       "        -> ::std::unordered_set<\n"
12963       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
12964       "      //\n"
12965       "    });");
12966 
12967   FormatStyle DoNotMerge = getLLVMStyle();
12968   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
12969   verifyFormat("auto c = []() {\n"
12970                "  return b;\n"
12971                "};",
12972                "auto c = []() { return b; };", DoNotMerge);
12973   verifyFormat("auto c = []() {\n"
12974                "};",
12975                " auto c = []() {};", DoNotMerge);
12976 
12977   FormatStyle MergeEmptyOnly = getLLVMStyle();
12978   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
12979   verifyFormat("auto c = []() {\n"
12980                "  return b;\n"
12981                "};",
12982                "auto c = []() {\n"
12983                "  return b;\n"
12984                " };",
12985                MergeEmptyOnly);
12986   verifyFormat("auto c = []() {};",
12987                "auto c = []() {\n"
12988                "};",
12989                MergeEmptyOnly);
12990 
12991   FormatStyle MergeInline = getLLVMStyle();
12992   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
12993   verifyFormat("auto c = []() {\n"
12994                "  return b;\n"
12995                "};",
12996                "auto c = []() { return b; };", MergeInline);
12997   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
12998                MergeInline);
12999   verifyFormat("function([]() { return b; }, a)",
13000                "function([]() { return b; }, a)", MergeInline);
13001   verifyFormat("function(a, []() { return b; })",
13002                "function(a, []() { return b; })", MergeInline);
13003 }
13004 
13005 TEST_F(FormatTest, EmptyLinesInLambdas) {
13006   verifyFormat("auto lambda = []() {\n"
13007                "  x(); //\n"
13008                "};",
13009                "auto lambda = []() {\n"
13010                "\n"
13011                "  x(); //\n"
13012                "\n"
13013                "};");
13014 }
13015 
13016 TEST_F(FormatTest, FormatsBlocks) {
13017   FormatStyle ShortBlocks = getLLVMStyle();
13018   ShortBlocks.AllowShortBlocksOnASingleLine = true;
13019   verifyFormat("int (^Block)(int, int);", ShortBlocks);
13020   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
13021   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
13022   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
13023   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
13024   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
13025 
13026   verifyFormat("foo(^{ bar(); });", ShortBlocks);
13027   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
13028   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
13029 
13030   verifyFormat("[operation setCompletionBlock:^{\n"
13031                "  [self onOperationDone];\n"
13032                "}];");
13033   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
13034                "  [self onOperationDone];\n"
13035                "}]};");
13036   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
13037                "  f();\n"
13038                "}];");
13039   verifyFormat("int a = [operation block:^int(int *i) {\n"
13040                "  return 1;\n"
13041                "}];");
13042   verifyFormat("[myObject doSomethingWith:arg1\n"
13043                "                      aaa:^int(int *a) {\n"
13044                "                        return 1;\n"
13045                "                      }\n"
13046                "                      bbb:f(a * bbbbbbbb)];");
13047 
13048   verifyFormat("[operation setCompletionBlock:^{\n"
13049                "  [self.delegate newDataAvailable];\n"
13050                "}];",
13051                getLLVMStyleWithColumns(60));
13052   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
13053                "  NSString *path = [self sessionFilePath];\n"
13054                "  if (path) {\n"
13055                "    // ...\n"
13056                "  }\n"
13057                "});");
13058   verifyFormat("[[SessionService sharedService]\n"
13059                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
13060                "      if (window) {\n"
13061                "        [self windowDidLoad:window];\n"
13062                "      } else {\n"
13063                "        [self errorLoadingWindow];\n"
13064                "      }\n"
13065                "    }];");
13066   verifyFormat("void (^largeBlock)(void) = ^{\n"
13067                "  // ...\n"
13068                "};\n",
13069                getLLVMStyleWithColumns(40));
13070   verifyFormat("[[SessionService sharedService]\n"
13071                "    loadWindowWithCompletionBlock: //\n"
13072                "        ^(SessionWindow *window) {\n"
13073                "          if (window) {\n"
13074                "            [self windowDidLoad:window];\n"
13075                "          } else {\n"
13076                "            [self errorLoadingWindow];\n"
13077                "          }\n"
13078                "        }];",
13079                getLLVMStyleWithColumns(60));
13080   verifyFormat("[myObject doSomethingWith:arg1\n"
13081                "    firstBlock:^(Foo *a) {\n"
13082                "      // ...\n"
13083                "      int i;\n"
13084                "    }\n"
13085                "    secondBlock:^(Bar *b) {\n"
13086                "      // ...\n"
13087                "      int i;\n"
13088                "    }\n"
13089                "    thirdBlock:^Foo(Bar *b) {\n"
13090                "      // ...\n"
13091                "      int i;\n"
13092                "    }];");
13093   verifyFormat("[myObject doSomethingWith:arg1\n"
13094                "               firstBlock:-1\n"
13095                "              secondBlock:^(Bar *b) {\n"
13096                "                // ...\n"
13097                "                int i;\n"
13098                "              }];");
13099 
13100   verifyFormat("f(^{\n"
13101                "  @autoreleasepool {\n"
13102                "    if (a) {\n"
13103                "      g();\n"
13104                "    }\n"
13105                "  }\n"
13106                "});");
13107   verifyFormat("Block b = ^int *(A *a, B *b) {}");
13108   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
13109                "};");
13110 
13111   FormatStyle FourIndent = getLLVMStyle();
13112   FourIndent.ObjCBlockIndentWidth = 4;
13113   verifyFormat("[operation setCompletionBlock:^{\n"
13114                "    [self onOperationDone];\n"
13115                "}];",
13116                FourIndent);
13117 }
13118 
13119 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
13120   FormatStyle ZeroColumn = getLLVMStyle();
13121   ZeroColumn.ColumnLimit = 0;
13122 
13123   verifyFormat("[[SessionService sharedService] "
13124                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
13125                "  if (window) {\n"
13126                "    [self windowDidLoad:window];\n"
13127                "  } else {\n"
13128                "    [self errorLoadingWindow];\n"
13129                "  }\n"
13130                "}];",
13131                ZeroColumn);
13132   EXPECT_EQ("[[SessionService sharedService]\n"
13133             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
13134             "      if (window) {\n"
13135             "        [self windowDidLoad:window];\n"
13136             "      } else {\n"
13137             "        [self errorLoadingWindow];\n"
13138             "      }\n"
13139             "    }];",
13140             format("[[SessionService sharedService]\n"
13141                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
13142                    "                if (window) {\n"
13143                    "    [self windowDidLoad:window];\n"
13144                    "  } else {\n"
13145                    "    [self errorLoadingWindow];\n"
13146                    "  }\n"
13147                    "}];",
13148                    ZeroColumn));
13149   verifyFormat("[myObject doSomethingWith:arg1\n"
13150                "    firstBlock:^(Foo *a) {\n"
13151                "      // ...\n"
13152                "      int i;\n"
13153                "    }\n"
13154                "    secondBlock:^(Bar *b) {\n"
13155                "      // ...\n"
13156                "      int i;\n"
13157                "    }\n"
13158                "    thirdBlock:^Foo(Bar *b) {\n"
13159                "      // ...\n"
13160                "      int i;\n"
13161                "    }];",
13162                ZeroColumn);
13163   verifyFormat("f(^{\n"
13164                "  @autoreleasepool {\n"
13165                "    if (a) {\n"
13166                "      g();\n"
13167                "    }\n"
13168                "  }\n"
13169                "});",
13170                ZeroColumn);
13171   verifyFormat("void (^largeBlock)(void) = ^{\n"
13172                "  // ...\n"
13173                "};",
13174                ZeroColumn);
13175 
13176   ZeroColumn.AllowShortBlocksOnASingleLine = true;
13177   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
13178             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
13179   ZeroColumn.AllowShortBlocksOnASingleLine = false;
13180   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
13181             "  int i;\n"
13182             "};",
13183             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
13184 }
13185 
13186 TEST_F(FormatTest, SupportsCRLF) {
13187   EXPECT_EQ("int a;\r\n"
13188             "int b;\r\n"
13189             "int c;\r\n",
13190             format("int a;\r\n"
13191                    "  int b;\r\n"
13192                    "    int c;\r\n",
13193                    getLLVMStyle()));
13194   EXPECT_EQ("int a;\r\n"
13195             "int b;\r\n"
13196             "int c;\r\n",
13197             format("int a;\r\n"
13198                    "  int b;\n"
13199                    "    int c;\r\n",
13200                    getLLVMStyle()));
13201   EXPECT_EQ("int a;\n"
13202             "int b;\n"
13203             "int c;\n",
13204             format("int a;\r\n"
13205                    "  int b;\n"
13206                    "    int c;\n",
13207                    getLLVMStyle()));
13208   EXPECT_EQ("\"aaaaaaa \"\r\n"
13209             "\"bbbbbbb\";\r\n",
13210             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
13211   EXPECT_EQ("#define A \\\r\n"
13212             "  b;      \\\r\n"
13213             "  c;      \\\r\n"
13214             "  d;\r\n",
13215             format("#define A \\\r\n"
13216                    "  b; \\\r\n"
13217                    "  c; d; \r\n",
13218                    getGoogleStyle()));
13219 
13220   EXPECT_EQ("/*\r\n"
13221             "multi line block comments\r\n"
13222             "should not introduce\r\n"
13223             "an extra carriage return\r\n"
13224             "*/\r\n",
13225             format("/*\r\n"
13226                    "multi line block comments\r\n"
13227                    "should not introduce\r\n"
13228                    "an extra carriage return\r\n"
13229                    "*/\r\n"));
13230   EXPECT_EQ("/*\r\n"
13231             "\r\n"
13232             "*/",
13233             format("/*\r\n"
13234                    "    \r\r\r\n"
13235                    "*/"));
13236 }
13237 
13238 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
13239   verifyFormat("MY_CLASS(C) {\n"
13240                "  int i;\n"
13241                "  int j;\n"
13242                "};");
13243 }
13244 
13245 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
13246   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
13247   TwoIndent.ContinuationIndentWidth = 2;
13248 
13249   EXPECT_EQ("int i =\n"
13250             "  longFunction(\n"
13251             "    arg);",
13252             format("int i = longFunction(arg);", TwoIndent));
13253 
13254   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
13255   SixIndent.ContinuationIndentWidth = 6;
13256 
13257   EXPECT_EQ("int i =\n"
13258             "      longFunction(\n"
13259             "            arg);",
13260             format("int i = longFunction(arg);", SixIndent));
13261 }
13262 
13263 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
13264   FormatStyle Style = getLLVMStyle();
13265   verifyFormat("int Foo::getter(\n"
13266                "    //\n"
13267                ") const {\n"
13268                "  return foo;\n"
13269                "}",
13270                Style);
13271   verifyFormat("void Foo::setter(\n"
13272                "    //\n"
13273                ") {\n"
13274                "  foo = 1;\n"
13275                "}",
13276                Style);
13277 }
13278 
13279 TEST_F(FormatTest, SpacesInAngles) {
13280   FormatStyle Spaces = getLLVMStyle();
13281   Spaces.SpacesInAngles = true;
13282 
13283   verifyFormat("static_cast< int >(arg);", Spaces);
13284   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
13285   verifyFormat("f< int, float >();", Spaces);
13286   verifyFormat("template <> g() {}", Spaces);
13287   verifyFormat("template < std::vector< int > > f() {}", Spaces);
13288   verifyFormat("std::function< void(int, int) > fct;", Spaces);
13289   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
13290                Spaces);
13291 
13292   Spaces.Standard = FormatStyle::LS_Cpp03;
13293   Spaces.SpacesInAngles = true;
13294   verifyFormat("A< A< int > >();", Spaces);
13295 
13296   Spaces.SpacesInAngles = false;
13297   verifyFormat("A<A<int> >();", Spaces);
13298 
13299   Spaces.Standard = FormatStyle::LS_Cpp11;
13300   Spaces.SpacesInAngles = true;
13301   verifyFormat("A< A< int > >();", Spaces);
13302 
13303   Spaces.SpacesInAngles = false;
13304   verifyFormat("A<A<int>>();", Spaces);
13305 }
13306 
13307 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
13308   FormatStyle Style = getLLVMStyle();
13309   Style.SpaceAfterTemplateKeyword = false;
13310   verifyFormat("template<int> void foo();", Style);
13311 }
13312 
13313 TEST_F(FormatTest, TripleAngleBrackets) {
13314   verifyFormat("f<<<1, 1>>>();");
13315   verifyFormat("f<<<1, 1, 1, s>>>();");
13316   verifyFormat("f<<<a, b, c, d>>>();");
13317   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
13318   verifyFormat("f<param><<<1, 1>>>();");
13319   verifyFormat("f<1><<<1, 1>>>();");
13320   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
13321   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13322                "aaaaaaaaaaa<<<\n    1, 1>>>();");
13323   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
13324                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
13325 }
13326 
13327 TEST_F(FormatTest, MergeLessLessAtEnd) {
13328   verifyFormat("<<");
13329   EXPECT_EQ("< < <", format("\\\n<<<"));
13330   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13331                "aaallvm::outs() <<");
13332   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13333                "aaaallvm::outs()\n    <<");
13334 }
13335 
13336 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
13337   std::string code = "#if A\n"
13338                      "#if B\n"
13339                      "a.\n"
13340                      "#endif\n"
13341                      "    a = 1;\n"
13342                      "#else\n"
13343                      "#endif\n"
13344                      "#if C\n"
13345                      "#else\n"
13346                      "#endif\n";
13347   EXPECT_EQ(code, format(code));
13348 }
13349 
13350 TEST_F(FormatTest, HandleConflictMarkers) {
13351   // Git/SVN conflict markers.
13352   EXPECT_EQ("int a;\n"
13353             "void f() {\n"
13354             "  callme(some(parameter1,\n"
13355             "<<<<<<< text by the vcs\n"
13356             "              parameter2),\n"
13357             "||||||| text by the vcs\n"
13358             "              parameter2),\n"
13359             "         parameter3,\n"
13360             "======= text by the vcs\n"
13361             "              parameter2, parameter3),\n"
13362             ">>>>>>> text by the vcs\n"
13363             "         otherparameter);\n",
13364             format("int a;\n"
13365                    "void f() {\n"
13366                    "  callme(some(parameter1,\n"
13367                    "<<<<<<< text by the vcs\n"
13368                    "  parameter2),\n"
13369                    "||||||| text by the vcs\n"
13370                    "  parameter2),\n"
13371                    "  parameter3,\n"
13372                    "======= text by the vcs\n"
13373                    "  parameter2,\n"
13374                    "  parameter3),\n"
13375                    ">>>>>>> text by the vcs\n"
13376                    "  otherparameter);\n"));
13377 
13378   // Perforce markers.
13379   EXPECT_EQ("void f() {\n"
13380             "  function(\n"
13381             ">>>> text by the vcs\n"
13382             "      parameter,\n"
13383             "==== text by the vcs\n"
13384             "      parameter,\n"
13385             "==== text by the vcs\n"
13386             "      parameter,\n"
13387             "<<<< text by the vcs\n"
13388             "      parameter);\n",
13389             format("void f() {\n"
13390                    "  function(\n"
13391                    ">>>> text by the vcs\n"
13392                    "  parameter,\n"
13393                    "==== text by the vcs\n"
13394                    "  parameter,\n"
13395                    "==== text by the vcs\n"
13396                    "  parameter,\n"
13397                    "<<<< text by the vcs\n"
13398                    "  parameter);\n"));
13399 
13400   EXPECT_EQ("<<<<<<<\n"
13401             "|||||||\n"
13402             "=======\n"
13403             ">>>>>>>",
13404             format("<<<<<<<\n"
13405                    "|||||||\n"
13406                    "=======\n"
13407                    ">>>>>>>"));
13408 
13409   EXPECT_EQ("<<<<<<<\n"
13410             "|||||||\n"
13411             "int i;\n"
13412             "=======\n"
13413             ">>>>>>>",
13414             format("<<<<<<<\n"
13415                    "|||||||\n"
13416                    "int i;\n"
13417                    "=======\n"
13418                    ">>>>>>>"));
13419 
13420   // FIXME: Handle parsing of macros around conflict markers correctly:
13421   EXPECT_EQ("#define Macro \\\n"
13422             "<<<<<<<\n"
13423             "Something \\\n"
13424             "|||||||\n"
13425             "Else \\\n"
13426             "=======\n"
13427             "Other \\\n"
13428             ">>>>>>>\n"
13429             "    End int i;\n",
13430             format("#define Macro \\\n"
13431                    "<<<<<<<\n"
13432                    "  Something \\\n"
13433                    "|||||||\n"
13434                    "  Else \\\n"
13435                    "=======\n"
13436                    "  Other \\\n"
13437                    ">>>>>>>\n"
13438                    "  End\n"
13439                    "int i;\n"));
13440 }
13441 
13442 TEST_F(FormatTest, DisableRegions) {
13443   EXPECT_EQ("int i;\n"
13444             "// clang-format off\n"
13445             "  int j;\n"
13446             "// clang-format on\n"
13447             "int k;",
13448             format(" int  i;\n"
13449                    "   // clang-format off\n"
13450                    "  int j;\n"
13451                    " // clang-format on\n"
13452                    "   int   k;"));
13453   EXPECT_EQ("int i;\n"
13454             "/* clang-format off */\n"
13455             "  int j;\n"
13456             "/* clang-format on */\n"
13457             "int k;",
13458             format(" int  i;\n"
13459                    "   /* clang-format off */\n"
13460                    "  int j;\n"
13461                    " /* clang-format on */\n"
13462                    "   int   k;"));
13463 
13464   // Don't reflow comments within disabled regions.
13465   EXPECT_EQ(
13466       "// clang-format off\n"
13467       "// long long long long long long line\n"
13468       "/* clang-format on */\n"
13469       "/* long long long\n"
13470       " * long long long\n"
13471       " * line */\n"
13472       "int i;\n"
13473       "/* clang-format off */\n"
13474       "/* long long long long long long line */\n",
13475       format("// clang-format off\n"
13476              "// long long long long long long line\n"
13477              "/* clang-format on */\n"
13478              "/* long long long long long long line */\n"
13479              "int i;\n"
13480              "/* clang-format off */\n"
13481              "/* long long long long long long line */\n",
13482              getLLVMStyleWithColumns(20)));
13483 }
13484 
13485 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
13486   format("? ) =");
13487   verifyNoCrash("#define a\\\n /**/}");
13488 }
13489 
13490 TEST_F(FormatTest, FormatsTableGenCode) {
13491   FormatStyle Style = getLLVMStyle();
13492   Style.Language = FormatStyle::LK_TableGen;
13493   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
13494 }
13495 
13496 TEST_F(FormatTest, ArrayOfTemplates) {
13497   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
13498             format("auto a = new unique_ptr<int > [ 10];"));
13499 
13500   FormatStyle Spaces = getLLVMStyle();
13501   Spaces.SpacesInSquareBrackets = true;
13502   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
13503             format("auto a = new unique_ptr<int > [10];", Spaces));
13504 }
13505 
13506 TEST_F(FormatTest, ArrayAsTemplateType) {
13507   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
13508             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
13509 
13510   FormatStyle Spaces = getLLVMStyle();
13511   Spaces.SpacesInSquareBrackets = true;
13512   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
13513             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
13514 }
13515 
13516 TEST_F(FormatTest, NoSpaceAfterSuper) {
13517     verifyFormat("__super::FooBar();");
13518 }
13519 
13520 TEST(FormatStyle, GetStyleWithEmptyFileName) {
13521   llvm::vfs::InMemoryFileSystem FS;
13522   auto Style1 = getStyle("file", "", "Google", "", &FS);
13523   ASSERT_TRUE((bool)Style1);
13524   ASSERT_EQ(*Style1, getGoogleStyle());
13525 }
13526 
13527 TEST(FormatStyle, GetStyleOfFile) {
13528   llvm::vfs::InMemoryFileSystem FS;
13529   // Test 1: format file in the same directory.
13530   ASSERT_TRUE(
13531       FS.addFile("/a/.clang-format", 0,
13532                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
13533   ASSERT_TRUE(
13534       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
13535   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
13536   ASSERT_TRUE((bool)Style1);
13537   ASSERT_EQ(*Style1, getLLVMStyle());
13538 
13539   // Test 2.1: fallback to default.
13540   ASSERT_TRUE(
13541       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
13542   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
13543   ASSERT_TRUE((bool)Style2);
13544   ASSERT_EQ(*Style2, getMozillaStyle());
13545 
13546   // Test 2.2: no format on 'none' fallback style.
13547   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
13548   ASSERT_TRUE((bool)Style2);
13549   ASSERT_EQ(*Style2, getNoStyle());
13550 
13551   // Test 2.3: format if config is found with no based style while fallback is
13552   // 'none'.
13553   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
13554                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
13555   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
13556   ASSERT_TRUE((bool)Style2);
13557   ASSERT_EQ(*Style2, getLLVMStyle());
13558 
13559   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
13560   Style2 = getStyle("{}", "a.h", "none", "", &FS);
13561   ASSERT_TRUE((bool)Style2);
13562   ASSERT_EQ(*Style2, getLLVMStyle());
13563 
13564   // Test 3: format file in parent directory.
13565   ASSERT_TRUE(
13566       FS.addFile("/c/.clang-format", 0,
13567                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
13568   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
13569                          llvm::MemoryBuffer::getMemBuffer("int i;")));
13570   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
13571   ASSERT_TRUE((bool)Style3);
13572   ASSERT_EQ(*Style3, getGoogleStyle());
13573 
13574   // Test 4: error on invalid fallback style
13575   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
13576   ASSERT_FALSE((bool)Style4);
13577   llvm::consumeError(Style4.takeError());
13578 
13579   // Test 5: error on invalid yaml on command line
13580   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
13581   ASSERT_FALSE((bool)Style5);
13582   llvm::consumeError(Style5.takeError());
13583 
13584   // Test 6: error on invalid style
13585   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
13586   ASSERT_FALSE((bool)Style6);
13587   llvm::consumeError(Style6.takeError());
13588 
13589   // Test 7: found config file, error on parsing it
13590   ASSERT_TRUE(
13591       FS.addFile("/d/.clang-format", 0,
13592                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
13593                                                   "InvalidKey: InvalidValue")));
13594   ASSERT_TRUE(
13595       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
13596   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
13597   ASSERT_FALSE((bool)Style7);
13598   llvm::consumeError(Style7.takeError());
13599 
13600   // Test 8: inferred per-language defaults apply.
13601   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
13602   ASSERT_TRUE((bool)StyleTd);
13603   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
13604 }
13605 
13606 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
13607   // Column limit is 20.
13608   std::string Code = "Type *a =\n"
13609                      "    new Type();\n"
13610                      "g(iiiii, 0, jjjjj,\n"
13611                      "  0, kkkkk, 0, mm);\n"
13612                      "int  bad     = format   ;";
13613   std::string Expected = "auto a = new Type();\n"
13614                          "g(iiiii, nullptr,\n"
13615                          "  jjjjj, nullptr,\n"
13616                          "  kkkkk, nullptr,\n"
13617                          "  mm);\n"
13618                          "int  bad     = format   ;";
13619   FileID ID = Context.createInMemoryFile("format.cpp", Code);
13620   tooling::Replacements Replaces = toReplacements(
13621       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
13622                             "auto "),
13623        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
13624                             "nullptr"),
13625        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
13626                             "nullptr"),
13627        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
13628                             "nullptr")});
13629 
13630   format::FormatStyle Style = format::getLLVMStyle();
13631   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
13632   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
13633   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
13634       << llvm::toString(FormattedReplaces.takeError()) << "\n";
13635   auto Result = applyAllReplacements(Code, *FormattedReplaces);
13636   EXPECT_TRUE(static_cast<bool>(Result));
13637   EXPECT_EQ(Expected, *Result);
13638 }
13639 
13640 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
13641   std::string Code = "#include \"a.h\"\n"
13642                      "#include \"c.h\"\n"
13643                      "\n"
13644                      "int main() {\n"
13645                      "  return 0;\n"
13646                      "}";
13647   std::string Expected = "#include \"a.h\"\n"
13648                          "#include \"b.h\"\n"
13649                          "#include \"c.h\"\n"
13650                          "\n"
13651                          "int main() {\n"
13652                          "  return 0;\n"
13653                          "}";
13654   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
13655   tooling::Replacements Replaces = toReplacements(
13656       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
13657                             "#include \"b.h\"\n")});
13658 
13659   format::FormatStyle Style = format::getLLVMStyle();
13660   Style.SortIncludes = true;
13661   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
13662   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
13663       << llvm::toString(FormattedReplaces.takeError()) << "\n";
13664   auto Result = applyAllReplacements(Code, *FormattedReplaces);
13665   EXPECT_TRUE(static_cast<bool>(Result));
13666   EXPECT_EQ(Expected, *Result);
13667 }
13668 
13669 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
13670   EXPECT_EQ("using std::cin;\n"
13671             "using std::cout;",
13672             format("using std::cout;\n"
13673                    "using std::cin;", getGoogleStyle()));
13674 }
13675 
13676 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
13677   format::FormatStyle Style = format::getLLVMStyle();
13678   Style.Standard = FormatStyle::LS_Cpp03;
13679   // cpp03 recognize this string as identifier u8 and literal character 'a'
13680   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
13681 }
13682 
13683 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
13684   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
13685   // all modes, including C++11, C++14 and C++17
13686   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
13687 }
13688 
13689 TEST_F(FormatTest, DoNotFormatLikelyXml) {
13690   EXPECT_EQ("<!-- ;> -->",
13691             format("<!-- ;> -->", getGoogleStyle()));
13692   EXPECT_EQ(" <!-- >; -->",
13693             format(" <!-- >; -->", getGoogleStyle()));
13694 }
13695 
13696 TEST_F(FormatTest, StructuredBindings) {
13697   // Structured bindings is a C++17 feature.
13698   // all modes, including C++11, C++14 and C++17
13699   verifyFormat("auto [a, b] = f();");
13700   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
13701   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
13702   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
13703   EXPECT_EQ("auto const volatile [a, b] = f();",
13704             format("auto  const   volatile[a, b] = f();"));
13705   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
13706   EXPECT_EQ("auto &[a, b, c] = f();",
13707             format("auto   &[  a  ,  b,c   ] = f();"));
13708   EXPECT_EQ("auto &&[a, b, c] = f();",
13709             format("auto   &&[  a  ,  b,c   ] = f();"));
13710   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
13711   EXPECT_EQ("auto const volatile &&[a, b] = f();",
13712             format("auto  const  volatile  &&[a, b] = f();"));
13713   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
13714   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
13715   EXPECT_EQ("const auto volatile &&[a, b] = f();",
13716             format("const  auto   volatile  &&[a, b] = f();"));
13717   EXPECT_EQ("volatile const auto &&[a, b] = f();",
13718             format("volatile  const  auto   &&[a, b] = f();"));
13719   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
13720 
13721   // Make sure we don't mistake structured bindings for lambdas.
13722   FormatStyle PointerMiddle = getLLVMStyle();
13723   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
13724   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
13725   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
13726   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
13727   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
13728   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
13729   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
13730   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
13731   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
13732   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
13733   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
13734   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
13735   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
13736 
13737   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
13738             format("for (const auto   &&   [a, b] : some_range) {\n}"));
13739   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
13740             format("for (const auto   &   [a, b] : some_range) {\n}"));
13741   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
13742             format("for (const auto[a, b] : some_range) {\n}"));
13743   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
13744   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
13745   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
13746   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
13747   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
13748   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
13749   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
13750   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
13751 
13752   format::FormatStyle Spaces = format::getLLVMStyle();
13753   Spaces.SpacesInSquareBrackets = true;
13754   verifyFormat("auto [ a, b ] = f();", Spaces);
13755   verifyFormat("auto &&[ a, b ] = f();", Spaces);
13756   verifyFormat("auto &[ a, b ] = f();", Spaces);
13757   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
13758   verifyFormat("auto const &[ a, b ] = f();", Spaces);
13759 }
13760 
13761 TEST_F(FormatTest, FileAndCode) {
13762   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
13763   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
13764   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
13765   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
13766   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
13767   EXPECT_EQ(
13768       FormatStyle::LK_ObjC,
13769       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
13770   EXPECT_EQ(FormatStyle::LK_ObjC,
13771             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
13772   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
13773   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
13774   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
13775   EXPECT_EQ(FormatStyle::LK_ObjC,
13776             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
13777   EXPECT_EQ(
13778       FormatStyle::LK_ObjC,
13779       guessLanguage("foo.h",
13780                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
13781   EXPECT_EQ(
13782       FormatStyle::LK_Cpp,
13783       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
13784 }
13785 
13786 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
13787   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
13788   EXPECT_EQ(FormatStyle::LK_ObjC,
13789             guessLanguage("foo.h", "array[[calculator getIndex]];"));
13790   EXPECT_EQ(FormatStyle::LK_Cpp,
13791             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
13792   EXPECT_EQ(
13793       FormatStyle::LK_Cpp,
13794       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
13795   EXPECT_EQ(FormatStyle::LK_ObjC,
13796             guessLanguage("foo.h", "[[noreturn foo] bar];"));
13797   EXPECT_EQ(FormatStyle::LK_Cpp,
13798             guessLanguage("foo.h", "[[clang::fallthrough]];"));
13799   EXPECT_EQ(FormatStyle::LK_ObjC,
13800             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
13801   EXPECT_EQ(FormatStyle::LK_Cpp,
13802             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
13803   EXPECT_EQ(FormatStyle::LK_Cpp,
13804             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
13805   EXPECT_EQ(FormatStyle::LK_ObjC,
13806             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
13807   EXPECT_EQ(FormatStyle::LK_Cpp,
13808             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
13809   EXPECT_EQ(
13810       FormatStyle::LK_Cpp,
13811       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
13812   EXPECT_EQ(
13813       FormatStyle::LK_Cpp,
13814       guessLanguage("foo.h",
13815                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
13816   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
13817 }
13818 
13819 TEST_F(FormatTest, GuessLanguageWithCaret) {
13820   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
13821   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
13822   EXPECT_EQ(FormatStyle::LK_ObjC,
13823             guessLanguage("foo.h", "int(^)(char, float);"));
13824   EXPECT_EQ(FormatStyle::LK_ObjC,
13825             guessLanguage("foo.h", "int(^foo)(char, float);"));
13826   EXPECT_EQ(FormatStyle::LK_ObjC,
13827             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
13828   EXPECT_EQ(FormatStyle::LK_ObjC,
13829             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
13830   EXPECT_EQ(
13831       FormatStyle::LK_ObjC,
13832       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
13833 }
13834 
13835 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
13836   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13837                                                "void f() {\n"
13838                                                "  asm (\"mov %[e], %[d]\"\n"
13839                                                "     : [d] \"=rm\" (d)\n"
13840                                                "       [e] \"rm\" (*e));\n"
13841                                                "}"));
13842   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13843                                                "void f() {\n"
13844                                                "  _asm (\"mov %[e], %[d]\"\n"
13845                                                "     : [d] \"=rm\" (d)\n"
13846                                                "       [e] \"rm\" (*e));\n"
13847                                                "}"));
13848   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13849                                                "void f() {\n"
13850                                                "  __asm (\"mov %[e], %[d]\"\n"
13851                                                "     : [d] \"=rm\" (d)\n"
13852                                                "       [e] \"rm\" (*e));\n"
13853                                                "}"));
13854   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13855                                                "void f() {\n"
13856                                                "  __asm__ (\"mov %[e], %[d]\"\n"
13857                                                "     : [d] \"=rm\" (d)\n"
13858                                                "       [e] \"rm\" (*e));\n"
13859                                                "}"));
13860   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13861                                                "void f() {\n"
13862                                                "  asm (\"mov %[e], %[d]\"\n"
13863                                                "     : [d] \"=rm\" (d),\n"
13864                                                "       [e] \"rm\" (*e));\n"
13865                                                "}"));
13866   EXPECT_EQ(FormatStyle::LK_Cpp,
13867             guessLanguage("foo.h", "void f() {\n"
13868                                    "  asm volatile (\"mov %[e], %[d]\"\n"
13869                                    "     : [d] \"=rm\" (d)\n"
13870                                    "       [e] \"rm\" (*e));\n"
13871                                    "}"));
13872 }
13873 
13874 TEST_F(FormatTest, GuessLanguageWithChildLines) {
13875   EXPECT_EQ(FormatStyle::LK_Cpp,
13876             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
13877   EXPECT_EQ(FormatStyle::LK_ObjC,
13878             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
13879   EXPECT_EQ(
13880       FormatStyle::LK_Cpp,
13881       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
13882   EXPECT_EQ(
13883       FormatStyle::LK_ObjC,
13884       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
13885 }
13886 
13887 TEST_F(FormatTest, TypenameMacros) {
13888   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
13889 
13890   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
13891   FormatStyle Google = getGoogleStyleWithColumns(0);
13892   Google.TypenameMacros = TypenameMacros;
13893   verifyFormat("struct foo {\n"
13894                "  int bar;\n"
13895                "  TAILQ_ENTRY(a) bleh;\n"
13896                "};", Google);
13897 
13898   FormatStyle Macros = getLLVMStyle();
13899   Macros.TypenameMacros = TypenameMacros;
13900 
13901   verifyFormat("STACK_OF(int) a;", Macros);
13902   verifyFormat("STACK_OF(int) *a;", Macros);
13903   verifyFormat("STACK_OF(int const *) *a;", Macros);
13904   verifyFormat("STACK_OF(int *const) *a;", Macros);
13905   verifyFormat("STACK_OF(int, string) a;", Macros);
13906   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
13907   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
13908   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
13909   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
13910 
13911   Macros.PointerAlignment = FormatStyle::PAS_Left;
13912   verifyFormat("STACK_OF(int)* a;", Macros);
13913   verifyFormat("STACK_OF(int*)* a;", Macros);
13914 }
13915 
13916 } // end namespace
13917 } // end namespace format
13918 } // end namespace clang
13919