xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision fd16eeea9d16150e35cadae1d77cee831b8cf510)
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 "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
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 { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1468   verifyFormat("while (true)\n"
1469                "  ;",
1470                AllowsMergedLoops);
1471   verifyFormat("for (;;)\n"
1472                "  ;",
1473                AllowsMergedLoops);
1474   verifyFormat("for (;;)\n"
1475                "  for (;;) continue;",
1476                AllowsMergedLoops);
1477   verifyFormat("for (;;)\n"
1478                "  while (true) continue;",
1479                AllowsMergedLoops);
1480   verifyFormat("while (true)\n"
1481                "  for (;;) continue;",
1482                AllowsMergedLoops);
1483   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1484                "  for (;;) continue;",
1485                AllowsMergedLoops);
1486   verifyFormat("for (;;)\n"
1487                "  BOOST_FOREACH (int &v, vec) continue;",
1488                AllowsMergedLoops);
1489   verifyFormat("for (;;) // Can't merge this\n"
1490                "  continue;",
1491                AllowsMergedLoops);
1492   verifyFormat("for (;;) /* still don't merge */\n"
1493                "  continue;",
1494                AllowsMergedLoops);
1495   verifyFormat("do a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   verifyFormat("do /* Don't merge */\n"
1499                "  a++;\n"
1500                "while (true);",
1501                AllowsMergedLoops);
1502   verifyFormat("do // Don't merge\n"
1503                "  a++;\n"
1504                "while (true);",
1505                AllowsMergedLoops);
1506   verifyFormat("do\n"
1507                "  // Don't merge\n"
1508                "  a++;\n"
1509                "while (true);",
1510                AllowsMergedLoops);
1511   // Without braces labels are interpreted differently.
1512   verifyFormat("{\n"
1513                "  do\n"
1514                "  label:\n"
1515                "    a++;\n"
1516                "  while (true);\n"
1517                "}",
1518                AllowsMergedLoops);
1519 }
1520 
1521 TEST_F(FormatTest, FormatShortBracedStatements) {
1522   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1523   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1524   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1525   // Not IF to avoid any confusion that IF is somehow special.
1526   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1527   AllowSimpleBracedStatements.ColumnLimit = 40;
1528   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1529       FormatStyle::SBS_Always;
1530 
1531   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1532       FormatStyle::SIS_WithoutElse;
1533   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1534 
1535   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1536   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1537   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1538 
1539   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1540   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1541   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1542   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1543   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1544   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1545   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1546   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1547   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1548   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1549   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1550   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1551   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1552   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1553   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1554   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1555   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1556                AllowSimpleBracedStatements);
1557   verifyFormat("if (true) {\n"
1558                "  ffffffffffffffffffffffff();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1563                "}",
1564                AllowSimpleBracedStatements);
1565   verifyFormat("if (true) { //\n"
1566                "  f();\n"
1567                "}",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("if (true) {\n"
1570                "  f();\n"
1571                "  f();\n"
1572                "}",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("if (true) {\n"
1575                "  f();\n"
1576                "} else {\n"
1577                "  f();\n"
1578                "}",
1579                AllowSimpleBracedStatements);
1580   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1581                AllowSimpleBracedStatements);
1582   verifyFormat("MYIF (true) {\n"
1583                "  ffffffffffffffffffffffff();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1588                "}",
1589                AllowSimpleBracedStatements);
1590   verifyFormat("MYIF (true) { //\n"
1591                "  f();\n"
1592                "}",
1593                AllowSimpleBracedStatements);
1594   verifyFormat("MYIF (true) {\n"
1595                "  f();\n"
1596                "  f();\n"
1597                "}",
1598                AllowSimpleBracedStatements);
1599   verifyFormat("MYIF (true) {\n"
1600                "  f();\n"
1601                "} else {\n"
1602                "  f();\n"
1603                "}",
1604                AllowSimpleBracedStatements);
1605 
1606   verifyFormat("struct A2 {\n"
1607                "  int X;\n"
1608                "};",
1609                AllowSimpleBracedStatements);
1610   verifyFormat("typedef struct A2 {\n"
1611                "  int X;\n"
1612                "} A2_t;",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("template <int> struct A2 {\n"
1615                "  struct B {};\n"
1616                "};",
1617                AllowSimpleBracedStatements);
1618 
1619   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1620       FormatStyle::SIS_Never;
1621   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1622   verifyFormat("if (true) {\n"
1623                "  f();\n"
1624                "}",
1625                AllowSimpleBracedStatements);
1626   verifyFormat("if (true) {\n"
1627                "  f();\n"
1628                "} else {\n"
1629                "  f();\n"
1630                "}",
1631                AllowSimpleBracedStatements);
1632   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("MYIF (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("MYIF (true) {\n"
1638                "  f();\n"
1639                "} else {\n"
1640                "  f();\n"
1641                "}",
1642                AllowSimpleBracedStatements);
1643 
1644   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1645   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1646   verifyFormat("while (true) {\n"
1647                "  f();\n"
1648                "}",
1649                AllowSimpleBracedStatements);
1650   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1651   verifyFormat("for (;;) {\n"
1652                "  f();\n"
1653                "}",
1654                AllowSimpleBracedStatements);
1655   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1656   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1657                "  f();\n"
1658                "}",
1659                AllowSimpleBracedStatements);
1660 
1661   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1662       FormatStyle::SIS_WithoutElse;
1663   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1664   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1665       FormatStyle::BWACS_Always;
1666 
1667   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1668   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1669   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1670   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1671   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1672   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1673   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1674   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1675   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1676   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1677   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1678   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1679   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1680   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1681   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1682   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1683   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("if (true)\n"
1686                "{\n"
1687                "  ffffffffffffffffffffffff();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690   verifyFormat("if (true)\n"
1691                "{\n"
1692                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1693                "}",
1694                AllowSimpleBracedStatements);
1695   verifyFormat("if (true)\n"
1696                "{ //\n"
1697                "  f();\n"
1698                "}",
1699                AllowSimpleBracedStatements);
1700   verifyFormat("if (true)\n"
1701                "{\n"
1702                "  f();\n"
1703                "  f();\n"
1704                "}",
1705                AllowSimpleBracedStatements);
1706   verifyFormat("if (true)\n"
1707                "{\n"
1708                "  f();\n"
1709                "} else\n"
1710                "{\n"
1711                "  f();\n"
1712                "}",
1713                AllowSimpleBracedStatements);
1714   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1715                AllowSimpleBracedStatements);
1716   verifyFormat("MYIF (true)\n"
1717                "{\n"
1718                "  ffffffffffffffffffffffff();\n"
1719                "}",
1720                AllowSimpleBracedStatements);
1721   verifyFormat("MYIF (true)\n"
1722                "{\n"
1723                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1724                "}",
1725                AllowSimpleBracedStatements);
1726   verifyFormat("MYIF (true)\n"
1727                "{ //\n"
1728                "  f();\n"
1729                "}",
1730                AllowSimpleBracedStatements);
1731   verifyFormat("MYIF (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "  f();\n"
1735                "}",
1736                AllowSimpleBracedStatements);
1737   verifyFormat("MYIF (true)\n"
1738                "{\n"
1739                "  f();\n"
1740                "} else\n"
1741                "{\n"
1742                "  f();\n"
1743                "}",
1744                AllowSimpleBracedStatements);
1745 
1746   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1747       FormatStyle::SIS_Never;
1748   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1749   verifyFormat("if (true)\n"
1750                "{\n"
1751                "  f();\n"
1752                "}",
1753                AllowSimpleBracedStatements);
1754   verifyFormat("if (true)\n"
1755                "{\n"
1756                "  f();\n"
1757                "} else\n"
1758                "{\n"
1759                "  f();\n"
1760                "}",
1761                AllowSimpleBracedStatements);
1762   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1763   verifyFormat("MYIF (true)\n"
1764                "{\n"
1765                "  f();\n"
1766                "}",
1767                AllowSimpleBracedStatements);
1768   verifyFormat("MYIF (true)\n"
1769                "{\n"
1770                "  f();\n"
1771                "} else\n"
1772                "{\n"
1773                "  f();\n"
1774                "}",
1775                AllowSimpleBracedStatements);
1776 
1777   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1778   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1779   verifyFormat("while (true)\n"
1780                "{\n"
1781                "  f();\n"
1782                "}",
1783                AllowSimpleBracedStatements);
1784   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1785   verifyFormat("for (;;)\n"
1786                "{\n"
1787                "  f();\n"
1788                "}",
1789                AllowSimpleBracedStatements);
1790   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1791   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1792                "{\n"
1793                "  f();\n"
1794                "}",
1795                AllowSimpleBracedStatements);
1796 }
1797 
1798 TEST_F(FormatTest, UnderstandsMacros) {
1799   verifyFormat("#define A (parentheses)");
1800   verifyFormat("/* comment */ #define A (parentheses)");
1801   verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1802   // Even the partial code should never be merged.
1803   EXPECT_EQ("/* comment */ #define A (parentheses)\n"
1804             "#",
1805             format("/* comment */ #define A (parentheses)\n"
1806                    "#"));
1807   verifyFormat("/* comment */ #define A (parentheses)\n"
1808                "#\n");
1809   verifyFormat("/* comment */ #define A (parentheses)\n"
1810                "#define B (parentheses)");
1811   verifyFormat("#define true ((int)1)");
1812   verifyFormat("#define and(x)");
1813   verifyFormat("#define if(x) x");
1814   verifyFormat("#define return(x) (x)");
1815   verifyFormat("#define while(x) for (; x;)");
1816   verifyFormat("#define xor(x) (^(x))");
1817   verifyFormat("#define __except(x)");
1818   verifyFormat("#define __try(x)");
1819 
1820   FormatStyle Style = getLLVMStyle();
1821   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1822   Style.BraceWrapping.AfterFunction = true;
1823   // Test that a macro definition never gets merged with the following
1824   // definition.
1825   // FIXME: The AAA macro definition probably should not be split into 3 lines.
1826   verifyFormat("#define AAA                                                    "
1827                "                \\\n"
1828                "  N                                                            "
1829                "                \\\n"
1830                "  {\n"
1831                "#define BBB }\n",
1832                Style);
1833   // verifyFormat("#define AAA N { //\n", Style);
1834 }
1835 
1836 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1837   FormatStyle Style = getLLVMStyleWithColumns(60);
1838   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1839   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1840   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1841   EXPECT_EQ("#define A                                                  \\\n"
1842             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1843             "  {                                                        \\\n"
1844             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1845             "  }\n"
1846             "X;",
1847             format("#define A \\\n"
1848                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1849                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1850                    "   }\n"
1851                    "X;",
1852                    Style));
1853 }
1854 
1855 TEST_F(FormatTest, ParseIfElse) {
1856   verifyFormat("if (true)\n"
1857                "  if (true)\n"
1858                "    if (true)\n"
1859                "      f();\n"
1860                "    else\n"
1861                "      g();\n"
1862                "  else\n"
1863                "    h();\n"
1864                "else\n"
1865                "  i();");
1866   verifyFormat("if (true)\n"
1867                "  if (true)\n"
1868                "    if (true) {\n"
1869                "      if (true)\n"
1870                "        f();\n"
1871                "    } else {\n"
1872                "      g();\n"
1873                "    }\n"
1874                "  else\n"
1875                "    h();\n"
1876                "else {\n"
1877                "  i();\n"
1878                "}");
1879   verifyFormat("if (true)\n"
1880                "  if constexpr (true)\n"
1881                "    if (true) {\n"
1882                "      if constexpr (true)\n"
1883                "        f();\n"
1884                "    } else {\n"
1885                "      g();\n"
1886                "    }\n"
1887                "  else\n"
1888                "    h();\n"
1889                "else {\n"
1890                "  i();\n"
1891                "}");
1892   verifyFormat("if (true)\n"
1893                "  if CONSTEXPR (true)\n"
1894                "    if (true) {\n"
1895                "      if CONSTEXPR (true)\n"
1896                "        f();\n"
1897                "    } else {\n"
1898                "      g();\n"
1899                "    }\n"
1900                "  else\n"
1901                "    h();\n"
1902                "else {\n"
1903                "  i();\n"
1904                "}");
1905   verifyFormat("void f() {\n"
1906                "  if (a) {\n"
1907                "  } else {\n"
1908                "  }\n"
1909                "}");
1910 }
1911 
1912 TEST_F(FormatTest, ElseIf) {
1913   verifyFormat("if (a) {\n} else if (b) {\n}");
1914   verifyFormat("if (a)\n"
1915                "  f();\n"
1916                "else if (b)\n"
1917                "  g();\n"
1918                "else\n"
1919                "  h();");
1920   verifyFormat("if (a)\n"
1921                "  f();\n"
1922                "else // comment\n"
1923                "  if (b) {\n"
1924                "    g();\n"
1925                "    h();\n"
1926                "  }");
1927   verifyFormat("if constexpr (a)\n"
1928                "  f();\n"
1929                "else if constexpr (b)\n"
1930                "  g();\n"
1931                "else\n"
1932                "  h();");
1933   verifyFormat("if CONSTEXPR (a)\n"
1934                "  f();\n"
1935                "else if CONSTEXPR (b)\n"
1936                "  g();\n"
1937                "else\n"
1938                "  h();");
1939   verifyFormat("if (a) {\n"
1940                "  f();\n"
1941                "}\n"
1942                "// or else ..\n"
1943                "else {\n"
1944                "  g()\n"
1945                "}");
1946 
1947   verifyFormat("if (a) {\n"
1948                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1949                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1950                "}");
1951   verifyFormat("if (a) {\n"
1952                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1953                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1954                "}");
1955   verifyFormat("if (a) {\n"
1956                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1957                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1958                "}");
1959   verifyFormat("if (a) {\n"
1960                "} else if (\n"
1961                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1962                "}",
1963                getLLVMStyleWithColumns(62));
1964   verifyFormat("if (a) {\n"
1965                "} else if constexpr (\n"
1966                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1967                "}",
1968                getLLVMStyleWithColumns(62));
1969   verifyFormat("if (a) {\n"
1970                "} else if CONSTEXPR (\n"
1971                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1972                "}",
1973                getLLVMStyleWithColumns(62));
1974 }
1975 
1976 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1977   FormatStyle Style = getLLVMStyle();
1978   EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
1979   EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
1980   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1981   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1982   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1983   verifyFormat("int *f1(int &a) const &;", Style);
1984   verifyFormat("int *f1(int &a) const & = 0;", Style);
1985   verifyFormat("int *a = f1();", Style);
1986   verifyFormat("int &b = f2();", Style);
1987   verifyFormat("int &&c = f3();", Style);
1988   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1989   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1990   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1991   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
1992   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1993   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1994   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1995   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
1996   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
1997   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
1998   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
1999   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2000   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2001   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2002   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2003   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2004 
2005   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2006   verifyFormat("Const unsigned int *c;\n"
2007                "const unsigned int *d;\n"
2008                "Const unsigned int &e;\n"
2009                "const unsigned int &f;\n"
2010                "const unsigned    &&g;\n"
2011                "Const unsigned      h;",
2012                Style);
2013 
2014   Style.PointerAlignment = FormatStyle::PAS_Left;
2015   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2016   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2017   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2018   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2019   verifyFormat("int* f1(int& a) const& = 0;", Style);
2020   verifyFormat("int* a = f1();", Style);
2021   verifyFormat("int& b = f2();", Style);
2022   verifyFormat("int&& c = f3();", Style);
2023   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2024   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2025   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2026   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2027   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2028   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2029   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2030   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2031   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2032   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2033   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2034   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2035   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2036   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2037   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2038   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2039   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2040   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2041 
2042   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2043   verifyFormat("Const unsigned int* c;\n"
2044                "const unsigned int* d;\n"
2045                "Const unsigned int& e;\n"
2046                "const unsigned int& f;\n"
2047                "const unsigned&&    g;\n"
2048                "Const unsigned      h;",
2049                Style);
2050 
2051   Style.PointerAlignment = FormatStyle::PAS_Right;
2052   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2053   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2054   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2055   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2056   verifyFormat("int *a = f1();", Style);
2057   verifyFormat("int& b = f2();", Style);
2058   verifyFormat("int&& c = f3();", Style);
2059   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2060   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2061   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2062 
2063   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2064   verifyFormat("Const unsigned int *c;\n"
2065                "const unsigned int *d;\n"
2066                "Const unsigned int& e;\n"
2067                "const unsigned int& f;\n"
2068                "const unsigned      g;\n"
2069                "Const unsigned      h;",
2070                Style);
2071 
2072   Style.PointerAlignment = FormatStyle::PAS_Left;
2073   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2074   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2075   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2076   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2077   verifyFormat("int* a = f1();", Style);
2078   verifyFormat("int & b = f2();", Style);
2079   verifyFormat("int && c = f3();", Style);
2080   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2081   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2082   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2083   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2084   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2085   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2086   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2087   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2088   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2089   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2090   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2091   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2092   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2093   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2094 
2095   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2096   verifyFormat("Const unsigned int*  c;\n"
2097                "const unsigned int*  d;\n"
2098                "Const unsigned int & e;\n"
2099                "const unsigned int & f;\n"
2100                "const unsigned &&    g;\n"
2101                "Const unsigned       h;",
2102                Style);
2103 
2104   Style.PointerAlignment = FormatStyle::PAS_Middle;
2105   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2106   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2107   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2108   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2109   verifyFormat("int * a = f1();", Style);
2110   verifyFormat("int &b = f2();", Style);
2111   verifyFormat("int &&c = f3();", Style);
2112   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2113   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2114   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2115 
2116   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2117   // specifically handled
2118   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2119 }
2120 
2121 TEST_F(FormatTest, FormatsForLoop) {
2122   verifyFormat(
2123       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2124       "     ++VeryVeryLongLoopVariable)\n"
2125       "  ;");
2126   verifyFormat("for (;;)\n"
2127                "  f();");
2128   verifyFormat("for (;;) {\n}");
2129   verifyFormat("for (;;) {\n"
2130                "  f();\n"
2131                "}");
2132   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2133 
2134   verifyFormat(
2135       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2136       "                                          E = UnwrappedLines.end();\n"
2137       "     I != E; ++I) {\n}");
2138 
2139   verifyFormat(
2140       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2141       "     ++IIIII) {\n}");
2142   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2143                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2144                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2145   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2146                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2147                "         E = FD->getDeclsInPrototypeScope().end();\n"
2148                "     I != E; ++I) {\n}");
2149   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2150                "         I = Container.begin(),\n"
2151                "         E = Container.end();\n"
2152                "     I != E; ++I) {\n}",
2153                getLLVMStyleWithColumns(76));
2154 
2155   verifyFormat(
2156       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2157       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2158       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2159       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2160       "     ++aaaaaaaaaaa) {\n}");
2161   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2162                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2163                "     ++i) {\n}");
2164   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2165                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2166                "}");
2167   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2168                "         aaaaaaaaaa);\n"
2169                "     iter; ++iter) {\n"
2170                "}");
2171   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2172                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2173                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2174                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2175 
2176   // These should not be formatted as Objective-C for-in loops.
2177   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2178   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2179   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2180   verifyFormat(
2181       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2182 
2183   FormatStyle NoBinPacking = getLLVMStyle();
2184   NoBinPacking.BinPackParameters = false;
2185   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2186                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2187                "                                           aaaaaaaaaaaaaaaa,\n"
2188                "                                           aaaaaaaaaaaaaaaa,\n"
2189                "                                           aaaaaaaaaaaaaaaa);\n"
2190                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2191                "}",
2192                NoBinPacking);
2193   verifyFormat(
2194       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2195       "                                          E = UnwrappedLines.end();\n"
2196       "     I != E;\n"
2197       "     ++I) {\n}",
2198       NoBinPacking);
2199 
2200   FormatStyle AlignLeft = getLLVMStyle();
2201   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2202   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2203 }
2204 
2205 TEST_F(FormatTest, RangeBasedForLoops) {
2206   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2207                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2208   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2209                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2210   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2211                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2212   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2213                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2214 }
2215 
2216 TEST_F(FormatTest, ForEachLoops) {
2217   FormatStyle Style = getLLVMStyle();
2218   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2219   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2220   verifyFormat("void f() {\n"
2221                "  for (;;) {\n"
2222                "  }\n"
2223                "  foreach (Item *item, itemlist) {\n"
2224                "  }\n"
2225                "  Q_FOREACH (Item *item, itemlist) {\n"
2226                "  }\n"
2227                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2228                "  }\n"
2229                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2230                "}",
2231                Style);
2232   verifyFormat("void f() {\n"
2233                "  for (;;)\n"
2234                "    int j = 1;\n"
2235                "  Q_FOREACH (int v, vec)\n"
2236                "    v *= 2;\n"
2237                "  for (;;) {\n"
2238                "    int j = 1;\n"
2239                "  }\n"
2240                "  Q_FOREACH (int v, vec) {\n"
2241                "    v *= 2;\n"
2242                "  }\n"
2243                "}",
2244                Style);
2245 
2246   FormatStyle ShortBlocks = getLLVMStyle();
2247   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2248   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2249   verifyFormat("void f() {\n"
2250                "  for (;;)\n"
2251                "    int j = 1;\n"
2252                "  Q_FOREACH (int &v, vec)\n"
2253                "    v *= 2;\n"
2254                "  for (;;) {\n"
2255                "    int j = 1;\n"
2256                "  }\n"
2257                "  Q_FOREACH (int &v, vec) {\n"
2258                "    int j = 1;\n"
2259                "  }\n"
2260                "}",
2261                ShortBlocks);
2262 
2263   FormatStyle ShortLoops = getLLVMStyle();
2264   ShortLoops.AllowShortLoopsOnASingleLine = true;
2265   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2266   verifyFormat("void f() {\n"
2267                "  for (;;) int j = 1;\n"
2268                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2269                "  for (;;) {\n"
2270                "    int j = 1;\n"
2271                "  }\n"
2272                "  Q_FOREACH (int &v, vec) {\n"
2273                "    int j = 1;\n"
2274                "  }\n"
2275                "}",
2276                ShortLoops);
2277 
2278   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2279   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2280   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2281   verifyFormat("void f() {\n"
2282                "  for (;;) int j = 1;\n"
2283                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2284                "  for (;;) { int j = 1; }\n"
2285                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2286                "}",
2287                ShortBlocksAndLoops);
2288 
2289   Style.SpaceBeforeParens =
2290       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2291   verifyFormat("void f() {\n"
2292                "  for (;;) {\n"
2293                "  }\n"
2294                "  foreach(Item *item, itemlist) {\n"
2295                "  }\n"
2296                "  Q_FOREACH(Item *item, itemlist) {\n"
2297                "  }\n"
2298                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2299                "  }\n"
2300                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2301                "}",
2302                Style);
2303 
2304   // As function-like macros.
2305   verifyFormat("#define foreach(x, y)\n"
2306                "#define Q_FOREACH(x, y)\n"
2307                "#define BOOST_FOREACH(x, y)\n"
2308                "#define UNKNOWN_FOREACH(x, y)\n");
2309 
2310   // Not as function-like macros.
2311   verifyFormat("#define foreach (x, y)\n"
2312                "#define Q_FOREACH (x, y)\n"
2313                "#define BOOST_FOREACH (x, y)\n"
2314                "#define UNKNOWN_FOREACH (x, y)\n");
2315 
2316   // handle microsoft non standard extension
2317   verifyFormat("for each (char c in x->MyStringProperty)");
2318 }
2319 
2320 TEST_F(FormatTest, FormatsWhileLoop) {
2321   verifyFormat("while (true) {\n}");
2322   verifyFormat("while (true)\n"
2323                "  f();");
2324   verifyFormat("while () {\n}");
2325   verifyFormat("while () {\n"
2326                "  f();\n"
2327                "}");
2328 }
2329 
2330 TEST_F(FormatTest, FormatsDoWhile) {
2331   verifyFormat("do {\n"
2332                "  do_something();\n"
2333                "} while (something());");
2334   verifyFormat("do\n"
2335                "  do_something();\n"
2336                "while (something());");
2337 }
2338 
2339 TEST_F(FormatTest, FormatsSwitchStatement) {
2340   verifyFormat("switch (x) {\n"
2341                "case 1:\n"
2342                "  f();\n"
2343                "  break;\n"
2344                "case kFoo:\n"
2345                "case ns::kBar:\n"
2346                "case kBaz:\n"
2347                "  break;\n"
2348                "default:\n"
2349                "  g();\n"
2350                "  break;\n"
2351                "}");
2352   verifyFormat("switch (x) {\n"
2353                "case 1: {\n"
2354                "  f();\n"
2355                "  break;\n"
2356                "}\n"
2357                "case 2: {\n"
2358                "  break;\n"
2359                "}\n"
2360                "}");
2361   verifyFormat("switch (x) {\n"
2362                "case 1: {\n"
2363                "  f();\n"
2364                "  {\n"
2365                "    g();\n"
2366                "    h();\n"
2367                "  }\n"
2368                "  break;\n"
2369                "}\n"
2370                "}");
2371   verifyFormat("switch (x) {\n"
2372                "case 1: {\n"
2373                "  f();\n"
2374                "  if (foo) {\n"
2375                "    g();\n"
2376                "    h();\n"
2377                "  }\n"
2378                "  break;\n"
2379                "}\n"
2380                "}");
2381   verifyFormat("switch (x) {\n"
2382                "case 1: {\n"
2383                "  f();\n"
2384                "  g();\n"
2385                "} break;\n"
2386                "}");
2387   verifyFormat("switch (test)\n"
2388                "  ;");
2389   verifyFormat("switch (x) {\n"
2390                "default: {\n"
2391                "  // Do nothing.\n"
2392                "}\n"
2393                "}");
2394   verifyFormat("switch (x) {\n"
2395                "// comment\n"
2396                "// if 1, do f()\n"
2397                "case 1:\n"
2398                "  f();\n"
2399                "}");
2400   verifyFormat("switch (x) {\n"
2401                "case 1:\n"
2402                "  // Do amazing stuff\n"
2403                "  {\n"
2404                "    f();\n"
2405                "    g();\n"
2406                "  }\n"
2407                "  break;\n"
2408                "}");
2409   verifyFormat("#define A          \\\n"
2410                "  switch (x) {     \\\n"
2411                "  case a:          \\\n"
2412                "    foo = b;       \\\n"
2413                "  }",
2414                getLLVMStyleWithColumns(20));
2415   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2416                "  case OP_name:                        \\\n"
2417                "    return operations::Operation##name\n",
2418                getLLVMStyleWithColumns(40));
2419   verifyFormat("switch (x) {\n"
2420                "case 1:;\n"
2421                "default:;\n"
2422                "  int i;\n"
2423                "}");
2424 
2425   verifyGoogleFormat("switch (x) {\n"
2426                      "  case 1:\n"
2427                      "    f();\n"
2428                      "    break;\n"
2429                      "  case kFoo:\n"
2430                      "  case ns::kBar:\n"
2431                      "  case kBaz:\n"
2432                      "    break;\n"
2433                      "  default:\n"
2434                      "    g();\n"
2435                      "    break;\n"
2436                      "}");
2437   verifyGoogleFormat("switch (x) {\n"
2438                      "  case 1: {\n"
2439                      "    f();\n"
2440                      "    break;\n"
2441                      "  }\n"
2442                      "}");
2443   verifyGoogleFormat("switch (test)\n"
2444                      "  ;");
2445 
2446   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2447                      "  case OP_name:              \\\n"
2448                      "    return operations::Operation##name\n");
2449   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2450                      "  // Get the correction operation class.\n"
2451                      "  switch (OpCode) {\n"
2452                      "    CASE(Add);\n"
2453                      "    CASE(Subtract);\n"
2454                      "    default:\n"
2455                      "      return operations::Unknown;\n"
2456                      "  }\n"
2457                      "#undef OPERATION_CASE\n"
2458                      "}");
2459   verifyFormat("DEBUG({\n"
2460                "  switch (x) {\n"
2461                "  case A:\n"
2462                "    f();\n"
2463                "    break;\n"
2464                "    // fallthrough\n"
2465                "  case B:\n"
2466                "    g();\n"
2467                "    break;\n"
2468                "  }\n"
2469                "});");
2470   EXPECT_EQ("DEBUG({\n"
2471             "  switch (x) {\n"
2472             "  case A:\n"
2473             "    f();\n"
2474             "    break;\n"
2475             "  // On B:\n"
2476             "  case B:\n"
2477             "    g();\n"
2478             "    break;\n"
2479             "  }\n"
2480             "});",
2481             format("DEBUG({\n"
2482                    "  switch (x) {\n"
2483                    "  case A:\n"
2484                    "    f();\n"
2485                    "    break;\n"
2486                    "  // On B:\n"
2487                    "  case B:\n"
2488                    "    g();\n"
2489                    "    break;\n"
2490                    "  }\n"
2491                    "});",
2492                    getLLVMStyle()));
2493   EXPECT_EQ("switch (n) {\n"
2494             "case 0: {\n"
2495             "  return false;\n"
2496             "}\n"
2497             "default: {\n"
2498             "  return true;\n"
2499             "}\n"
2500             "}",
2501             format("switch (n)\n"
2502                    "{\n"
2503                    "case 0: {\n"
2504                    "  return false;\n"
2505                    "}\n"
2506                    "default: {\n"
2507                    "  return true;\n"
2508                    "}\n"
2509                    "}",
2510                    getLLVMStyle()));
2511   verifyFormat("switch (a) {\n"
2512                "case (b):\n"
2513                "  return;\n"
2514                "}");
2515 
2516   verifyFormat("switch (a) {\n"
2517                "case some_namespace::\n"
2518                "    some_constant:\n"
2519                "  return;\n"
2520                "}",
2521                getLLVMStyleWithColumns(34));
2522 
2523   FormatStyle Style = getLLVMStyle();
2524   Style.IndentCaseLabels = true;
2525   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2526   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2527   Style.BraceWrapping.AfterCaseLabel = true;
2528   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2529   EXPECT_EQ("switch (n)\n"
2530             "{\n"
2531             "  case 0:\n"
2532             "  {\n"
2533             "    return false;\n"
2534             "  }\n"
2535             "  default:\n"
2536             "  {\n"
2537             "    return true;\n"
2538             "  }\n"
2539             "}",
2540             format("switch (n) {\n"
2541                    "  case 0: {\n"
2542                    "    return false;\n"
2543                    "  }\n"
2544                    "  default: {\n"
2545                    "    return true;\n"
2546                    "  }\n"
2547                    "}",
2548                    Style));
2549   Style.BraceWrapping.AfterCaseLabel = false;
2550   EXPECT_EQ("switch (n)\n"
2551             "{\n"
2552             "  case 0: {\n"
2553             "    return false;\n"
2554             "  }\n"
2555             "  default: {\n"
2556             "    return true;\n"
2557             "  }\n"
2558             "}",
2559             format("switch (n) {\n"
2560                    "  case 0:\n"
2561                    "  {\n"
2562                    "    return false;\n"
2563                    "  }\n"
2564                    "  default:\n"
2565                    "  {\n"
2566                    "    return true;\n"
2567                    "  }\n"
2568                    "}",
2569                    Style));
2570   Style.IndentCaseLabels = false;
2571   Style.IndentCaseBlocks = true;
2572   EXPECT_EQ("switch (n)\n"
2573             "{\n"
2574             "case 0:\n"
2575             "  {\n"
2576             "    return false;\n"
2577             "  }\n"
2578             "case 1:\n"
2579             "  break;\n"
2580             "default:\n"
2581             "  {\n"
2582             "    return true;\n"
2583             "  }\n"
2584             "}",
2585             format("switch (n) {\n"
2586                    "case 0: {\n"
2587                    "  return false;\n"
2588                    "}\n"
2589                    "case 1:\n"
2590                    "  break;\n"
2591                    "default: {\n"
2592                    "  return true;\n"
2593                    "}\n"
2594                    "}",
2595                    Style));
2596   Style.IndentCaseLabels = true;
2597   Style.IndentCaseBlocks = true;
2598   EXPECT_EQ("switch (n)\n"
2599             "{\n"
2600             "  case 0:\n"
2601             "    {\n"
2602             "      return false;\n"
2603             "    }\n"
2604             "  case 1:\n"
2605             "    break;\n"
2606             "  default:\n"
2607             "    {\n"
2608             "      return true;\n"
2609             "    }\n"
2610             "}",
2611             format("switch (n) {\n"
2612                    "case 0: {\n"
2613                    "  return false;\n"
2614                    "}\n"
2615                    "case 1:\n"
2616                    "  break;\n"
2617                    "default: {\n"
2618                    "  return true;\n"
2619                    "}\n"
2620                    "}",
2621                    Style));
2622 }
2623 
2624 TEST_F(FormatTest, CaseRanges) {
2625   verifyFormat("switch (x) {\n"
2626                "case 'A' ... 'Z':\n"
2627                "case 1 ... 5:\n"
2628                "case a ... b:\n"
2629                "  break;\n"
2630                "}");
2631 }
2632 
2633 TEST_F(FormatTest, ShortEnums) {
2634   FormatStyle Style = getLLVMStyle();
2635   Style.AllowShortEnumsOnASingleLine = true;
2636   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2637   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2638   Style.AllowShortEnumsOnASingleLine = false;
2639   verifyFormat("enum {\n"
2640                "  A,\n"
2641                "  B,\n"
2642                "  C\n"
2643                "} ShortEnum1, ShortEnum2;",
2644                Style);
2645   verifyFormat("typedef enum {\n"
2646                "  A,\n"
2647                "  B,\n"
2648                "  C\n"
2649                "} ShortEnum1, ShortEnum2;",
2650                Style);
2651   verifyFormat("enum {\n"
2652                "  A,\n"
2653                "} ShortEnum1, ShortEnum2;",
2654                Style);
2655   verifyFormat("typedef enum {\n"
2656                "  A,\n"
2657                "} ShortEnum1, ShortEnum2;",
2658                Style);
2659   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2660   Style.BraceWrapping.AfterEnum = true;
2661   verifyFormat("enum\n"
2662                "{\n"
2663                "  A,\n"
2664                "  B,\n"
2665                "  C\n"
2666                "} ShortEnum1, ShortEnum2;",
2667                Style);
2668   verifyFormat("typedef enum\n"
2669                "{\n"
2670                "  A,\n"
2671                "  B,\n"
2672                "  C\n"
2673                "} ShortEnum1, ShortEnum2;",
2674                Style);
2675 }
2676 
2677 TEST_F(FormatTest, ShortCaseLabels) {
2678   FormatStyle Style = getLLVMStyle();
2679   Style.AllowShortCaseLabelsOnASingleLine = true;
2680   verifyFormat("switch (a) {\n"
2681                "case 1: x = 1; break;\n"
2682                "case 2: return;\n"
2683                "case 3:\n"
2684                "case 4:\n"
2685                "case 5: return;\n"
2686                "case 6: // comment\n"
2687                "  return;\n"
2688                "case 7:\n"
2689                "  // comment\n"
2690                "  return;\n"
2691                "case 8:\n"
2692                "  x = 8; // comment\n"
2693                "  break;\n"
2694                "default: y = 1; break;\n"
2695                "}",
2696                Style);
2697   verifyFormat("switch (a) {\n"
2698                "case 0: return; // comment\n"
2699                "case 1: break;  // comment\n"
2700                "case 2: return;\n"
2701                "// comment\n"
2702                "case 3: return;\n"
2703                "// comment 1\n"
2704                "// comment 2\n"
2705                "// comment 3\n"
2706                "case 4: break; /* comment */\n"
2707                "case 5:\n"
2708                "  // comment\n"
2709                "  break;\n"
2710                "case 6: /* comment */ x = 1; break;\n"
2711                "case 7: x = /* comment */ 1; break;\n"
2712                "case 8:\n"
2713                "  x = 1; /* comment */\n"
2714                "  break;\n"
2715                "case 9:\n"
2716                "  break; // comment line 1\n"
2717                "         // comment line 2\n"
2718                "}",
2719                Style);
2720   EXPECT_EQ("switch (a) {\n"
2721             "case 1:\n"
2722             "  x = 8;\n"
2723             "  // fall through\n"
2724             "case 2: x = 8;\n"
2725             "// comment\n"
2726             "case 3:\n"
2727             "  return; /* comment line 1\n"
2728             "           * comment line 2 */\n"
2729             "case 4: i = 8;\n"
2730             "// something else\n"
2731             "#if FOO\n"
2732             "case 5: break;\n"
2733             "#endif\n"
2734             "}",
2735             format("switch (a) {\n"
2736                    "case 1: x = 8;\n"
2737                    "  // fall through\n"
2738                    "case 2:\n"
2739                    "  x = 8;\n"
2740                    "// comment\n"
2741                    "case 3:\n"
2742                    "  return; /* comment line 1\n"
2743                    "           * comment line 2 */\n"
2744                    "case 4:\n"
2745                    "  i = 8;\n"
2746                    "// something else\n"
2747                    "#if FOO\n"
2748                    "case 5: break;\n"
2749                    "#endif\n"
2750                    "}",
2751                    Style));
2752   EXPECT_EQ("switch (a) {\n"
2753             "case 0:\n"
2754             "  return; // long long long long long long long long long long "
2755             "long long comment\n"
2756             "          // line\n"
2757             "}",
2758             format("switch (a) {\n"
2759                    "case 0: return; // long long long long long long long long "
2760                    "long long long long comment line\n"
2761                    "}",
2762                    Style));
2763   EXPECT_EQ("switch (a) {\n"
2764             "case 0:\n"
2765             "  return; /* long long long long long long long long long long "
2766             "long long comment\n"
2767             "             line */\n"
2768             "}",
2769             format("switch (a) {\n"
2770                    "case 0: return; /* long long long long long long long long "
2771                    "long long long long comment line */\n"
2772                    "}",
2773                    Style));
2774   verifyFormat("switch (a) {\n"
2775                "#if FOO\n"
2776                "case 0: return 0;\n"
2777                "#endif\n"
2778                "}",
2779                Style);
2780   verifyFormat("switch (a) {\n"
2781                "case 1: {\n"
2782                "}\n"
2783                "case 2: {\n"
2784                "  return;\n"
2785                "}\n"
2786                "case 3: {\n"
2787                "  x = 1;\n"
2788                "  return;\n"
2789                "}\n"
2790                "case 4:\n"
2791                "  if (x)\n"
2792                "    return;\n"
2793                "}",
2794                Style);
2795   Style.ColumnLimit = 21;
2796   verifyFormat("switch (a) {\n"
2797                "case 1: x = 1; break;\n"
2798                "case 2: return;\n"
2799                "case 3:\n"
2800                "case 4:\n"
2801                "case 5: return;\n"
2802                "default:\n"
2803                "  y = 1;\n"
2804                "  break;\n"
2805                "}",
2806                Style);
2807   Style.ColumnLimit = 80;
2808   Style.AllowShortCaseLabelsOnASingleLine = false;
2809   Style.IndentCaseLabels = true;
2810   EXPECT_EQ("switch (n) {\n"
2811             "  default /*comments*/:\n"
2812             "    return true;\n"
2813             "  case 0:\n"
2814             "    return false;\n"
2815             "}",
2816             format("switch (n) {\n"
2817                    "default/*comments*/:\n"
2818                    "  return true;\n"
2819                    "case 0:\n"
2820                    "  return false;\n"
2821                    "}",
2822                    Style));
2823   Style.AllowShortCaseLabelsOnASingleLine = true;
2824   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2825   Style.BraceWrapping.AfterCaseLabel = true;
2826   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2827   EXPECT_EQ("switch (n)\n"
2828             "{\n"
2829             "  case 0:\n"
2830             "  {\n"
2831             "    return false;\n"
2832             "  }\n"
2833             "  default:\n"
2834             "  {\n"
2835             "    return true;\n"
2836             "  }\n"
2837             "}",
2838             format("switch (n) {\n"
2839                    "  case 0: {\n"
2840                    "    return false;\n"
2841                    "  }\n"
2842                    "  default:\n"
2843                    "  {\n"
2844                    "    return true;\n"
2845                    "  }\n"
2846                    "}",
2847                    Style));
2848 }
2849 
2850 TEST_F(FormatTest, FormatsLabels) {
2851   verifyFormat("void f() {\n"
2852                "  some_code();\n"
2853                "test_label:\n"
2854                "  some_other_code();\n"
2855                "  {\n"
2856                "    some_more_code();\n"
2857                "  another_label:\n"
2858                "    some_more_code();\n"
2859                "  }\n"
2860                "}");
2861   verifyFormat("{\n"
2862                "  some_code();\n"
2863                "test_label:\n"
2864                "  some_other_code();\n"
2865                "}");
2866   verifyFormat("{\n"
2867                "  some_code();\n"
2868                "test_label:;\n"
2869                "  int i = 0;\n"
2870                "}");
2871   FormatStyle Style = getLLVMStyle();
2872   Style.IndentGotoLabels = false;
2873   verifyFormat("void f() {\n"
2874                "  some_code();\n"
2875                "test_label:\n"
2876                "  some_other_code();\n"
2877                "  {\n"
2878                "    some_more_code();\n"
2879                "another_label:\n"
2880                "    some_more_code();\n"
2881                "  }\n"
2882                "}",
2883                Style);
2884   verifyFormat("{\n"
2885                "  some_code();\n"
2886                "test_label:\n"
2887                "  some_other_code();\n"
2888                "}",
2889                Style);
2890   verifyFormat("{\n"
2891                "  some_code();\n"
2892                "test_label:;\n"
2893                "  int i = 0;\n"
2894                "}");
2895 }
2896 
2897 TEST_F(FormatTest, MultiLineControlStatements) {
2898   FormatStyle Style = getLLVMStyleWithColumns(20);
2899   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2900   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2901   // Short lines should keep opening brace on same line.
2902   EXPECT_EQ("if (foo) {\n"
2903             "  bar();\n"
2904             "}",
2905             format("if(foo){bar();}", Style));
2906   EXPECT_EQ("if (foo) {\n"
2907             "  bar();\n"
2908             "} else {\n"
2909             "  baz();\n"
2910             "}",
2911             format("if(foo){bar();}else{baz();}", Style));
2912   EXPECT_EQ("if (foo && bar) {\n"
2913             "  baz();\n"
2914             "}",
2915             format("if(foo&&bar){baz();}", Style));
2916   EXPECT_EQ("if (foo) {\n"
2917             "  bar();\n"
2918             "} else if (baz) {\n"
2919             "  quux();\n"
2920             "}",
2921             format("if(foo){bar();}else if(baz){quux();}", Style));
2922   EXPECT_EQ(
2923       "if (foo) {\n"
2924       "  bar();\n"
2925       "} else if (baz) {\n"
2926       "  quux();\n"
2927       "} else {\n"
2928       "  foobar();\n"
2929       "}",
2930       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2931   EXPECT_EQ("for (;;) {\n"
2932             "  foo();\n"
2933             "}",
2934             format("for(;;){foo();}"));
2935   EXPECT_EQ("while (1) {\n"
2936             "  foo();\n"
2937             "}",
2938             format("while(1){foo();}", Style));
2939   EXPECT_EQ("switch (foo) {\n"
2940             "case bar:\n"
2941             "  return;\n"
2942             "}",
2943             format("switch(foo){case bar:return;}", Style));
2944   EXPECT_EQ("try {\n"
2945             "  foo();\n"
2946             "} catch (...) {\n"
2947             "  bar();\n"
2948             "}",
2949             format("try{foo();}catch(...){bar();}", Style));
2950   EXPECT_EQ("do {\n"
2951             "  foo();\n"
2952             "} while (bar &&\n"
2953             "         baz);",
2954             format("do{foo();}while(bar&&baz);", Style));
2955   // Long lines should put opening brace on new line.
2956   EXPECT_EQ("if (foo && bar &&\n"
2957             "    baz)\n"
2958             "{\n"
2959             "  quux();\n"
2960             "}",
2961             format("if(foo&&bar&&baz){quux();}", Style));
2962   EXPECT_EQ("if (foo && bar &&\n"
2963             "    baz)\n"
2964             "{\n"
2965             "  quux();\n"
2966             "}",
2967             format("if (foo && bar &&\n"
2968                    "    baz) {\n"
2969                    "  quux();\n"
2970                    "}",
2971                    Style));
2972   EXPECT_EQ("if (foo) {\n"
2973             "  bar();\n"
2974             "} else if (baz ||\n"
2975             "           quux)\n"
2976             "{\n"
2977             "  foobar();\n"
2978             "}",
2979             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2980   EXPECT_EQ(
2981       "if (foo) {\n"
2982       "  bar();\n"
2983       "} else if (baz ||\n"
2984       "           quux)\n"
2985       "{\n"
2986       "  foobar();\n"
2987       "} else {\n"
2988       "  barbaz();\n"
2989       "}",
2990       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2991              Style));
2992   EXPECT_EQ("for (int i = 0;\n"
2993             "     i < 10; ++i)\n"
2994             "{\n"
2995             "  foo();\n"
2996             "}",
2997             format("for(int i=0;i<10;++i){foo();}", Style));
2998   EXPECT_EQ("foreach (int i,\n"
2999             "         list)\n"
3000             "{\n"
3001             "  foo();\n"
3002             "}",
3003             format("foreach(int i, list){foo();}", Style));
3004   Style.ColumnLimit =
3005       40; // to concentrate at brace wrapping, not line wrap due to column limit
3006   EXPECT_EQ("foreach (int i, list) {\n"
3007             "  foo();\n"
3008             "}",
3009             format("foreach(int i, list){foo();}", Style));
3010   Style.ColumnLimit =
3011       20; // to concentrate at brace wrapping, not line wrap due to column limit
3012   EXPECT_EQ("while (foo || bar ||\n"
3013             "       baz)\n"
3014             "{\n"
3015             "  quux();\n"
3016             "}",
3017             format("while(foo||bar||baz){quux();}", Style));
3018   EXPECT_EQ("switch (\n"
3019             "    foo = barbaz)\n"
3020             "{\n"
3021             "case quux:\n"
3022             "  return;\n"
3023             "}",
3024             format("switch(foo=barbaz){case quux:return;}", Style));
3025   EXPECT_EQ("try {\n"
3026             "  foo();\n"
3027             "} catch (\n"
3028             "    Exception &bar)\n"
3029             "{\n"
3030             "  baz();\n"
3031             "}",
3032             format("try{foo();}catch(Exception&bar){baz();}", Style));
3033   Style.ColumnLimit =
3034       40; // to concentrate at brace wrapping, not line wrap due to column limit
3035   EXPECT_EQ("try {\n"
3036             "  foo();\n"
3037             "} catch (Exception &bar) {\n"
3038             "  baz();\n"
3039             "}",
3040             format("try{foo();}catch(Exception&bar){baz();}", Style));
3041   Style.ColumnLimit =
3042       20; // to concentrate at brace wrapping, not line wrap due to column limit
3043 
3044   Style.BraceWrapping.BeforeElse = true;
3045   EXPECT_EQ(
3046       "if (foo) {\n"
3047       "  bar();\n"
3048       "}\n"
3049       "else if (baz ||\n"
3050       "         quux)\n"
3051       "{\n"
3052       "  foobar();\n"
3053       "}\n"
3054       "else {\n"
3055       "  barbaz();\n"
3056       "}",
3057       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3058              Style));
3059 
3060   Style.BraceWrapping.BeforeCatch = true;
3061   EXPECT_EQ("try {\n"
3062             "  foo();\n"
3063             "}\n"
3064             "catch (...) {\n"
3065             "  baz();\n"
3066             "}",
3067             format("try{foo();}catch(...){baz();}", Style));
3068 
3069   Style.BraceWrapping.AfterFunction = true;
3070   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3071   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3072   Style.ColumnLimit = 80;
3073   verifyFormat("void shortfunction() { bar(); }", Style);
3074 
3075   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3076   verifyFormat("void shortfunction()\n"
3077                "{\n"
3078                "  bar();\n"
3079                "}",
3080                Style);
3081 }
3082 
3083 TEST_F(FormatTest, BeforeWhile) {
3084   FormatStyle Style = getLLVMStyle();
3085   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3086 
3087   verifyFormat("do {\n"
3088                "  foo();\n"
3089                "} while (1);",
3090                Style);
3091   Style.BraceWrapping.BeforeWhile = true;
3092   verifyFormat("do {\n"
3093                "  foo();\n"
3094                "}\n"
3095                "while (1);",
3096                Style);
3097 }
3098 
3099 //===----------------------------------------------------------------------===//
3100 // Tests for classes, namespaces, etc.
3101 //===----------------------------------------------------------------------===//
3102 
3103 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3104   verifyFormat("class A {};");
3105 }
3106 
3107 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3108   verifyFormat("class A {\n"
3109                "public:\n"
3110                "public: // comment\n"
3111                "protected:\n"
3112                "private:\n"
3113                "  void f() {}\n"
3114                "};");
3115   verifyFormat("export class A {\n"
3116                "public:\n"
3117                "public: // comment\n"
3118                "protected:\n"
3119                "private:\n"
3120                "  void f() {}\n"
3121                "};");
3122   verifyGoogleFormat("class A {\n"
3123                      " public:\n"
3124                      " protected:\n"
3125                      " private:\n"
3126                      "  void f() {}\n"
3127                      "};");
3128   verifyGoogleFormat("export class A {\n"
3129                      " public:\n"
3130                      " protected:\n"
3131                      " private:\n"
3132                      "  void f() {}\n"
3133                      "};");
3134   verifyFormat("class A {\n"
3135                "public slots:\n"
3136                "  void f1() {}\n"
3137                "public Q_SLOTS:\n"
3138                "  void f2() {}\n"
3139                "protected slots:\n"
3140                "  void f3() {}\n"
3141                "protected Q_SLOTS:\n"
3142                "  void f4() {}\n"
3143                "private slots:\n"
3144                "  void f5() {}\n"
3145                "private Q_SLOTS:\n"
3146                "  void f6() {}\n"
3147                "signals:\n"
3148                "  void g1();\n"
3149                "Q_SIGNALS:\n"
3150                "  void g2();\n"
3151                "};");
3152 
3153   // Don't interpret 'signals' the wrong way.
3154   verifyFormat("signals.set();");
3155   verifyFormat("for (Signals signals : f()) {\n}");
3156   verifyFormat("{\n"
3157                "  signals.set(); // This needs indentation.\n"
3158                "}");
3159   verifyFormat("void f() {\n"
3160                "label:\n"
3161                "  signals.baz();\n"
3162                "}");
3163   verifyFormat("private[1];");
3164   verifyFormat("testArray[public] = 1;");
3165   verifyFormat("public();");
3166   verifyFormat("myFunc(public);");
3167   verifyFormat("std::vector<int> testVec = {private};");
3168   verifyFormat("private.p = 1;");
3169   verifyFormat("void function(private...){};");
3170   verifyFormat("if (private && public)\n");
3171   verifyFormat("private &= true;");
3172   verifyFormat("int x = private * public;");
3173   verifyFormat("public *= private;");
3174   verifyFormat("int x = public + private;");
3175   verifyFormat("private++;");
3176   verifyFormat("++private;");
3177   verifyFormat("public += private;");
3178   verifyFormat("public = public - private;");
3179   verifyFormat("public->foo();");
3180   verifyFormat("private--;");
3181   verifyFormat("--private;");
3182   verifyFormat("public -= 1;");
3183   verifyFormat("if (!private && !public)\n");
3184   verifyFormat("public != private;");
3185   verifyFormat("int x = public / private;");
3186   verifyFormat("public /= 2;");
3187   verifyFormat("public = public % 2;");
3188   verifyFormat("public %= 2;");
3189   verifyFormat("if (public < private)\n");
3190   verifyFormat("public << private;");
3191   verifyFormat("public <<= private;");
3192   verifyFormat("if (public > private)\n");
3193   verifyFormat("public >> private;");
3194   verifyFormat("public >>= private;");
3195   verifyFormat("public ^ private;");
3196   verifyFormat("public ^= private;");
3197   verifyFormat("public | private;");
3198   verifyFormat("public |= private;");
3199   verifyFormat("auto x = private ? 1 : 2;");
3200   verifyFormat("if (public == private)\n");
3201   verifyFormat("void foo(public, private)");
3202   verifyFormat("public::foo();");
3203 }
3204 
3205 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3206   EXPECT_EQ("class A {\n"
3207             "public:\n"
3208             "  void f();\n"
3209             "\n"
3210             "private:\n"
3211             "  void g() {}\n"
3212             "  // test\n"
3213             "protected:\n"
3214             "  int h;\n"
3215             "};",
3216             format("class A {\n"
3217                    "public:\n"
3218                    "void f();\n"
3219                    "private:\n"
3220                    "void g() {}\n"
3221                    "// test\n"
3222                    "protected:\n"
3223                    "int h;\n"
3224                    "};"));
3225   EXPECT_EQ("class A {\n"
3226             "protected:\n"
3227             "public:\n"
3228             "  void f();\n"
3229             "};",
3230             format("class A {\n"
3231                    "protected:\n"
3232                    "\n"
3233                    "public:\n"
3234                    "\n"
3235                    "  void f();\n"
3236                    "};"));
3237 
3238   // Even ensure proper spacing inside macros.
3239   EXPECT_EQ("#define B     \\\n"
3240             "  class A {   \\\n"
3241             "   protected: \\\n"
3242             "   public:    \\\n"
3243             "    void f(); \\\n"
3244             "  };",
3245             format("#define B     \\\n"
3246                    "  class A {   \\\n"
3247                    "   protected: \\\n"
3248                    "              \\\n"
3249                    "   public:    \\\n"
3250                    "              \\\n"
3251                    "    void f(); \\\n"
3252                    "  };",
3253                    getGoogleStyle()));
3254   // But don't remove empty lines after macros ending in access specifiers.
3255   EXPECT_EQ("#define A private:\n"
3256             "\n"
3257             "int i;",
3258             format("#define A         private:\n"
3259                    "\n"
3260                    "int              i;"));
3261 }
3262 
3263 TEST_F(FormatTest, FormatsClasses) {
3264   verifyFormat("class A : public B {};");
3265   verifyFormat("class A : public ::B {};");
3266 
3267   verifyFormat(
3268       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3269       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3270   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3271                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3272                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3273   verifyFormat(
3274       "class A : public B, public C, public D, public E, public F {};");
3275   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3276                "                     public C,\n"
3277                "                     public D,\n"
3278                "                     public E,\n"
3279                "                     public F,\n"
3280                "                     public G {};");
3281 
3282   verifyFormat("class\n"
3283                "    ReallyReallyLongClassName {\n"
3284                "  int i;\n"
3285                "};",
3286                getLLVMStyleWithColumns(32));
3287   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3288                "                           aaaaaaaaaaaaaaaa> {};");
3289   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3290                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3291                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3292   verifyFormat("template <class R, class C>\n"
3293                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3294                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3295   verifyFormat("class ::A::B {};");
3296 }
3297 
3298 TEST_F(FormatTest, BreakInheritanceStyle) {
3299   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3300   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3301       FormatStyle::BILS_BeforeComma;
3302   verifyFormat("class MyClass : public X {};",
3303                StyleWithInheritanceBreakBeforeComma);
3304   verifyFormat("class MyClass\n"
3305                "    : public X\n"
3306                "    , public Y {};",
3307                StyleWithInheritanceBreakBeforeComma);
3308   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3309                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3310                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3311                StyleWithInheritanceBreakBeforeComma);
3312   verifyFormat("struct aaaaaaaaaaaaa\n"
3313                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3314                "          aaaaaaaaaaaaaaaa> {};",
3315                StyleWithInheritanceBreakBeforeComma);
3316 
3317   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3318   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3319       FormatStyle::BILS_AfterColon;
3320   verifyFormat("class MyClass : public X {};",
3321                StyleWithInheritanceBreakAfterColon);
3322   verifyFormat("class MyClass : public X, public Y {};",
3323                StyleWithInheritanceBreakAfterColon);
3324   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3325                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3326                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3327                StyleWithInheritanceBreakAfterColon);
3328   verifyFormat("struct aaaaaaaaaaaaa :\n"
3329                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3330                "        aaaaaaaaaaaaaaaa> {};",
3331                StyleWithInheritanceBreakAfterColon);
3332 
3333   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3334   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3335       FormatStyle::BILS_AfterComma;
3336   verifyFormat("class MyClass : public X {};",
3337                StyleWithInheritanceBreakAfterComma);
3338   verifyFormat("class MyClass : public X,\n"
3339                "                public Y {};",
3340                StyleWithInheritanceBreakAfterComma);
3341   verifyFormat(
3342       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3343       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3344       "{};",
3345       StyleWithInheritanceBreakAfterComma);
3346   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3347                "                           aaaaaaaaaaaaaaaa> {};",
3348                StyleWithInheritanceBreakAfterComma);
3349   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3350                "    : public OnceBreak,\n"
3351                "      public AlwaysBreak,\n"
3352                "      EvenBasesFitInOneLine {};",
3353                StyleWithInheritanceBreakAfterComma);
3354 }
3355 
3356 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3357   verifyFormat("class A {\n} a, b;");
3358   verifyFormat("struct A {\n} a, b;");
3359   verifyFormat("union A {\n} a;");
3360 }
3361 
3362 TEST_F(FormatTest, FormatsEnum) {
3363   verifyFormat("enum {\n"
3364                "  Zero,\n"
3365                "  One = 1,\n"
3366                "  Two = One + 1,\n"
3367                "  Three = (One + Two),\n"
3368                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3369                "  Five = (One, Two, Three, Four, 5)\n"
3370                "};");
3371   verifyGoogleFormat("enum {\n"
3372                      "  Zero,\n"
3373                      "  One = 1,\n"
3374                      "  Two = One + 1,\n"
3375                      "  Three = (One + Two),\n"
3376                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3377                      "  Five = (One, Two, Three, Four, 5)\n"
3378                      "};");
3379   verifyFormat("enum Enum {};");
3380   verifyFormat("enum {};");
3381   verifyFormat("enum X E {} d;");
3382   verifyFormat("enum __attribute__((...)) E {} d;");
3383   verifyFormat("enum __declspec__((...)) E {} d;");
3384   verifyFormat("enum {\n"
3385                "  Bar = Foo<int, int>::value\n"
3386                "};",
3387                getLLVMStyleWithColumns(30));
3388 
3389   verifyFormat("enum ShortEnum { A, B, C };");
3390   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3391 
3392   EXPECT_EQ("enum KeepEmptyLines {\n"
3393             "  ONE,\n"
3394             "\n"
3395             "  TWO,\n"
3396             "\n"
3397             "  THREE\n"
3398             "}",
3399             format("enum KeepEmptyLines {\n"
3400                    "  ONE,\n"
3401                    "\n"
3402                    "  TWO,\n"
3403                    "\n"
3404                    "\n"
3405                    "  THREE\n"
3406                    "}"));
3407   verifyFormat("enum E { // comment\n"
3408                "  ONE,\n"
3409                "  TWO\n"
3410                "};\n"
3411                "int i;");
3412 
3413   FormatStyle EightIndent = getLLVMStyle();
3414   EightIndent.IndentWidth = 8;
3415   verifyFormat("enum {\n"
3416                "        VOID,\n"
3417                "        CHAR,\n"
3418                "        SHORT,\n"
3419                "        INT,\n"
3420                "        LONG,\n"
3421                "        SIGNED,\n"
3422                "        UNSIGNED,\n"
3423                "        BOOL,\n"
3424                "        FLOAT,\n"
3425                "        DOUBLE,\n"
3426                "        COMPLEX\n"
3427                "};",
3428                EightIndent);
3429 
3430   // Not enums.
3431   verifyFormat("enum X f() {\n"
3432                "  a();\n"
3433                "  return 42;\n"
3434                "}");
3435   verifyFormat("enum X Type::f() {\n"
3436                "  a();\n"
3437                "  return 42;\n"
3438                "}");
3439   verifyFormat("enum ::X f() {\n"
3440                "  a();\n"
3441                "  return 42;\n"
3442                "}");
3443   verifyFormat("enum ns::X f() {\n"
3444                "  a();\n"
3445                "  return 42;\n"
3446                "}");
3447 }
3448 
3449 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3450   verifyFormat("enum Type {\n"
3451                "  One = 0; // These semicolons should be commas.\n"
3452                "  Two = 1;\n"
3453                "};");
3454   verifyFormat("namespace n {\n"
3455                "enum Type {\n"
3456                "  One,\n"
3457                "  Two, // missing };\n"
3458                "  int i;\n"
3459                "}\n"
3460                "void g() {}");
3461 }
3462 
3463 TEST_F(FormatTest, FormatsEnumStruct) {
3464   verifyFormat("enum struct {\n"
3465                "  Zero,\n"
3466                "  One = 1,\n"
3467                "  Two = One + 1,\n"
3468                "  Three = (One + Two),\n"
3469                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3470                "  Five = (One, Two, Three, Four, 5)\n"
3471                "};");
3472   verifyFormat("enum struct Enum {};");
3473   verifyFormat("enum struct {};");
3474   verifyFormat("enum struct X E {} d;");
3475   verifyFormat("enum struct __attribute__((...)) E {} d;");
3476   verifyFormat("enum struct __declspec__((...)) E {} d;");
3477   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3478 }
3479 
3480 TEST_F(FormatTest, FormatsEnumClass) {
3481   verifyFormat("enum class {\n"
3482                "  Zero,\n"
3483                "  One = 1,\n"
3484                "  Two = One + 1,\n"
3485                "  Three = (One + Two),\n"
3486                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3487                "  Five = (One, Two, Three, Four, 5)\n"
3488                "};");
3489   verifyFormat("enum class Enum {};");
3490   verifyFormat("enum class {};");
3491   verifyFormat("enum class X E {} d;");
3492   verifyFormat("enum class __attribute__((...)) E {} d;");
3493   verifyFormat("enum class __declspec__((...)) E {} d;");
3494   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3495 }
3496 
3497 TEST_F(FormatTest, FormatsEnumTypes) {
3498   verifyFormat("enum X : int {\n"
3499                "  A, // Force multiple lines.\n"
3500                "  B\n"
3501                "};");
3502   verifyFormat("enum X : int { A, B };");
3503   verifyFormat("enum X : std::uint32_t { A, B };");
3504 }
3505 
3506 TEST_F(FormatTest, FormatsTypedefEnum) {
3507   FormatStyle Style = getLLVMStyleWithColumns(40);
3508   verifyFormat("typedef enum {} EmptyEnum;");
3509   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3510   verifyFormat("typedef enum {\n"
3511                "  ZERO = 0,\n"
3512                "  ONE = 1,\n"
3513                "  TWO = 2,\n"
3514                "  THREE = 3\n"
3515                "} LongEnum;",
3516                Style);
3517   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3518   Style.BraceWrapping.AfterEnum = true;
3519   verifyFormat("typedef enum {} EmptyEnum;");
3520   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3521   verifyFormat("typedef enum\n"
3522                "{\n"
3523                "  ZERO = 0,\n"
3524                "  ONE = 1,\n"
3525                "  TWO = 2,\n"
3526                "  THREE = 3\n"
3527                "} LongEnum;",
3528                Style);
3529 }
3530 
3531 TEST_F(FormatTest, FormatsNSEnums) {
3532   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3533   verifyGoogleFormat(
3534       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3535   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3536                      "  // Information about someDecentlyLongValue.\n"
3537                      "  someDecentlyLongValue,\n"
3538                      "  // Information about anotherDecentlyLongValue.\n"
3539                      "  anotherDecentlyLongValue,\n"
3540                      "  // Information about aThirdDecentlyLongValue.\n"
3541                      "  aThirdDecentlyLongValue\n"
3542                      "};");
3543   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3544                      "  // Information about someDecentlyLongValue.\n"
3545                      "  someDecentlyLongValue,\n"
3546                      "  // Information about anotherDecentlyLongValue.\n"
3547                      "  anotherDecentlyLongValue,\n"
3548                      "  // Information about aThirdDecentlyLongValue.\n"
3549                      "  aThirdDecentlyLongValue\n"
3550                      "};");
3551   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3552                      "  a = 1,\n"
3553                      "  b = 2,\n"
3554                      "  c = 3,\n"
3555                      "};");
3556   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3557                      "  a = 1,\n"
3558                      "  b = 2,\n"
3559                      "  c = 3,\n"
3560                      "};");
3561   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3562                      "  a = 1,\n"
3563                      "  b = 2,\n"
3564                      "  c = 3,\n"
3565                      "};");
3566   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3567                      "  a = 1,\n"
3568                      "  b = 2,\n"
3569                      "  c = 3,\n"
3570                      "};");
3571 }
3572 
3573 TEST_F(FormatTest, FormatsBitfields) {
3574   verifyFormat("struct Bitfields {\n"
3575                "  unsigned sClass : 8;\n"
3576                "  unsigned ValueKind : 2;\n"
3577                "};");
3578   verifyFormat("struct A {\n"
3579                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3580                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3581                "};");
3582   verifyFormat("struct MyStruct {\n"
3583                "  uchar data;\n"
3584                "  uchar : 8;\n"
3585                "  uchar : 8;\n"
3586                "  uchar other;\n"
3587                "};");
3588   FormatStyle Style = getLLVMStyle();
3589   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3590   verifyFormat("struct Bitfields {\n"
3591                "  unsigned sClass:8;\n"
3592                "  unsigned ValueKind:2;\n"
3593                "  uchar other;\n"
3594                "};",
3595                Style);
3596   verifyFormat("struct A {\n"
3597                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3598                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3599                "};",
3600                Style);
3601   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3602   verifyFormat("struct Bitfields {\n"
3603                "  unsigned sClass :8;\n"
3604                "  unsigned ValueKind :2;\n"
3605                "  uchar other;\n"
3606                "};",
3607                Style);
3608   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3609   verifyFormat("struct Bitfields {\n"
3610                "  unsigned sClass: 8;\n"
3611                "  unsigned ValueKind: 2;\n"
3612                "  uchar other;\n"
3613                "};",
3614                Style);
3615 }
3616 
3617 TEST_F(FormatTest, FormatsNamespaces) {
3618   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3619   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3620 
3621   verifyFormat("namespace some_namespace {\n"
3622                "class A {};\n"
3623                "void f() { f(); }\n"
3624                "}",
3625                LLVMWithNoNamespaceFix);
3626   verifyFormat("namespace N::inline D {\n"
3627                "class A {};\n"
3628                "void f() { f(); }\n"
3629                "}",
3630                LLVMWithNoNamespaceFix);
3631   verifyFormat("namespace N::inline D::E {\n"
3632                "class A {};\n"
3633                "void f() { f(); }\n"
3634                "}",
3635                LLVMWithNoNamespaceFix);
3636   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3637                "class A {};\n"
3638                "void f() { f(); }\n"
3639                "}",
3640                LLVMWithNoNamespaceFix);
3641   verifyFormat("/* something */ namespace some_namespace {\n"
3642                "class A {};\n"
3643                "void f() { f(); }\n"
3644                "}",
3645                LLVMWithNoNamespaceFix);
3646   verifyFormat("namespace {\n"
3647                "class A {};\n"
3648                "void f() { f(); }\n"
3649                "}",
3650                LLVMWithNoNamespaceFix);
3651   verifyFormat("/* something */ namespace {\n"
3652                "class A {};\n"
3653                "void f() { f(); }\n"
3654                "}",
3655                LLVMWithNoNamespaceFix);
3656   verifyFormat("inline namespace X {\n"
3657                "class A {};\n"
3658                "void f() { f(); }\n"
3659                "}",
3660                LLVMWithNoNamespaceFix);
3661   verifyFormat("/* something */ inline namespace X {\n"
3662                "class A {};\n"
3663                "void f() { f(); }\n"
3664                "}",
3665                LLVMWithNoNamespaceFix);
3666   verifyFormat("export namespace X {\n"
3667                "class A {};\n"
3668                "void f() { f(); }\n"
3669                "}",
3670                LLVMWithNoNamespaceFix);
3671   verifyFormat("using namespace some_namespace;\n"
3672                "class A {};\n"
3673                "void f() { f(); }",
3674                LLVMWithNoNamespaceFix);
3675 
3676   // This code is more common than we thought; if we
3677   // layout this correctly the semicolon will go into
3678   // its own line, which is undesirable.
3679   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3680   verifyFormat("namespace {\n"
3681                "class A {};\n"
3682                "};",
3683                LLVMWithNoNamespaceFix);
3684 
3685   verifyFormat("namespace {\n"
3686                "int SomeVariable = 0; // comment\n"
3687                "} // namespace",
3688                LLVMWithNoNamespaceFix);
3689   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3690             "#define HEADER_GUARD\n"
3691             "namespace my_namespace {\n"
3692             "int i;\n"
3693             "} // my_namespace\n"
3694             "#endif // HEADER_GUARD",
3695             format("#ifndef HEADER_GUARD\n"
3696                    " #define HEADER_GUARD\n"
3697                    "   namespace my_namespace {\n"
3698                    "int i;\n"
3699                    "}    // my_namespace\n"
3700                    "#endif    // HEADER_GUARD",
3701                    LLVMWithNoNamespaceFix));
3702 
3703   EXPECT_EQ("namespace A::B {\n"
3704             "class C {};\n"
3705             "}",
3706             format("namespace A::B {\n"
3707                    "class C {};\n"
3708                    "}",
3709                    LLVMWithNoNamespaceFix));
3710 
3711   FormatStyle Style = getLLVMStyle();
3712   Style.NamespaceIndentation = FormatStyle::NI_All;
3713   EXPECT_EQ("namespace out {\n"
3714             "  int i;\n"
3715             "  namespace in {\n"
3716             "    int i;\n"
3717             "  } // namespace in\n"
3718             "} // namespace out",
3719             format("namespace out {\n"
3720                    "int i;\n"
3721                    "namespace in {\n"
3722                    "int i;\n"
3723                    "} // namespace in\n"
3724                    "} // namespace out",
3725                    Style));
3726 
3727   FormatStyle ShortInlineFunctions = getLLVMStyle();
3728   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3729   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3730       FormatStyle::SFS_Inline;
3731   verifyFormat("namespace {\n"
3732                "  void f() {\n"
3733                "    return;\n"
3734                "  }\n"
3735                "} // namespace\n",
3736                ShortInlineFunctions);
3737   verifyFormat("namespace {\n"
3738                "  int some_int;\n"
3739                "  void f() {\n"
3740                "    return;\n"
3741                "  }\n"
3742                "} // namespace\n",
3743                ShortInlineFunctions);
3744   verifyFormat("namespace interface {\n"
3745                "  void f() {\n"
3746                "    return;\n"
3747                "  }\n"
3748                "} // namespace interface\n",
3749                ShortInlineFunctions);
3750   verifyFormat("namespace {\n"
3751                "  class X {\n"
3752                "    void f() { return; }\n"
3753                "  };\n"
3754                "} // namespace\n",
3755                ShortInlineFunctions);
3756   verifyFormat("namespace {\n"
3757                "  struct X {\n"
3758                "    void f() { return; }\n"
3759                "  };\n"
3760                "} // namespace\n",
3761                ShortInlineFunctions);
3762   verifyFormat("namespace {\n"
3763                "  union X {\n"
3764                "    void f() { return; }\n"
3765                "  };\n"
3766                "} // namespace\n",
3767                ShortInlineFunctions);
3768   verifyFormat("extern \"C\" {\n"
3769                "void f() {\n"
3770                "  return;\n"
3771                "}\n"
3772                "} // namespace\n",
3773                ShortInlineFunctions);
3774   verifyFormat("namespace {\n"
3775                "  class X {\n"
3776                "    void f() { return; }\n"
3777                "  } x;\n"
3778                "} // namespace\n",
3779                ShortInlineFunctions);
3780   verifyFormat("namespace {\n"
3781                "  [[nodiscard]] class X {\n"
3782                "    void f() { return; }\n"
3783                "  };\n"
3784                "} // namespace\n",
3785                ShortInlineFunctions);
3786   verifyFormat("namespace {\n"
3787                "  static class X {\n"
3788                "    void f() { return; }\n"
3789                "  } x;\n"
3790                "} // namespace\n",
3791                ShortInlineFunctions);
3792   verifyFormat("namespace {\n"
3793                "  constexpr class X {\n"
3794                "    void f() { return; }\n"
3795                "  } x;\n"
3796                "} // namespace\n",
3797                ShortInlineFunctions);
3798 
3799   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3800   verifyFormat("extern \"C\" {\n"
3801                "  void f() {\n"
3802                "    return;\n"
3803                "  }\n"
3804                "} // namespace\n",
3805                ShortInlineFunctions);
3806 
3807   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3808   EXPECT_EQ("namespace out {\n"
3809             "int i;\n"
3810             "namespace in {\n"
3811             "  int i;\n"
3812             "} // namespace in\n"
3813             "} // namespace out",
3814             format("namespace out {\n"
3815                    "int i;\n"
3816                    "namespace in {\n"
3817                    "int i;\n"
3818                    "} // namespace in\n"
3819                    "} // namespace out",
3820                    Style));
3821 
3822   Style.NamespaceIndentation = FormatStyle::NI_None;
3823   verifyFormat("template <class T>\n"
3824                "concept a_concept = X<>;\n"
3825                "namespace B {\n"
3826                "struct b_struct {};\n"
3827                "} // namespace B\n",
3828                Style);
3829   verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n"
3830                "namespace ns {\n"
3831                "void foo() {}\n"
3832                "} // namespace ns\n",
3833                Style);
3834 }
3835 
3836 TEST_F(FormatTest, NamespaceMacros) {
3837   FormatStyle Style = getLLVMStyle();
3838   Style.NamespaceMacros.push_back("TESTSUITE");
3839 
3840   verifyFormat("TESTSUITE(A) {\n"
3841                "int foo();\n"
3842                "} // TESTSUITE(A)",
3843                Style);
3844 
3845   verifyFormat("TESTSUITE(A, B) {\n"
3846                "int foo();\n"
3847                "} // TESTSUITE(A)",
3848                Style);
3849 
3850   // Properly indent according to NamespaceIndentation style
3851   Style.NamespaceIndentation = FormatStyle::NI_All;
3852   verifyFormat("TESTSUITE(A) {\n"
3853                "  int foo();\n"
3854                "} // TESTSUITE(A)",
3855                Style);
3856   verifyFormat("TESTSUITE(A) {\n"
3857                "  namespace B {\n"
3858                "    int foo();\n"
3859                "  } // namespace B\n"
3860                "} // TESTSUITE(A)",
3861                Style);
3862   verifyFormat("namespace A {\n"
3863                "  TESTSUITE(B) {\n"
3864                "    int foo();\n"
3865                "  } // TESTSUITE(B)\n"
3866                "} // namespace A",
3867                Style);
3868 
3869   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3870   verifyFormat("TESTSUITE(A) {\n"
3871                "TESTSUITE(B) {\n"
3872                "  int foo();\n"
3873                "} // TESTSUITE(B)\n"
3874                "} // TESTSUITE(A)",
3875                Style);
3876   verifyFormat("TESTSUITE(A) {\n"
3877                "namespace B {\n"
3878                "  int foo();\n"
3879                "} // namespace B\n"
3880                "} // TESTSUITE(A)",
3881                Style);
3882   verifyFormat("namespace A {\n"
3883                "TESTSUITE(B) {\n"
3884                "  int foo();\n"
3885                "} // TESTSUITE(B)\n"
3886                "} // namespace A",
3887                Style);
3888 
3889   // Properly merge namespace-macros blocks in CompactNamespaces mode
3890   Style.NamespaceIndentation = FormatStyle::NI_None;
3891   Style.CompactNamespaces = true;
3892   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3893                "}} // TESTSUITE(A::B)",
3894                Style);
3895 
3896   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3897             "}} // TESTSUITE(out::in)",
3898             format("TESTSUITE(out) {\n"
3899                    "TESTSUITE(in) {\n"
3900                    "} // TESTSUITE(in)\n"
3901                    "} // TESTSUITE(out)",
3902                    Style));
3903 
3904   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3905             "}} // TESTSUITE(out::in)",
3906             format("TESTSUITE(out) {\n"
3907                    "TESTSUITE(in) {\n"
3908                    "} // TESTSUITE(in)\n"
3909                    "} // TESTSUITE(out)",
3910                    Style));
3911 
3912   // Do not merge different namespaces/macros
3913   EXPECT_EQ("namespace out {\n"
3914             "TESTSUITE(in) {\n"
3915             "} // TESTSUITE(in)\n"
3916             "} // namespace out",
3917             format("namespace out {\n"
3918                    "TESTSUITE(in) {\n"
3919                    "} // TESTSUITE(in)\n"
3920                    "} // namespace out",
3921                    Style));
3922   EXPECT_EQ("TESTSUITE(out) {\n"
3923             "namespace in {\n"
3924             "} // namespace in\n"
3925             "} // TESTSUITE(out)",
3926             format("TESTSUITE(out) {\n"
3927                    "namespace in {\n"
3928                    "} // namespace in\n"
3929                    "} // TESTSUITE(out)",
3930                    Style));
3931   Style.NamespaceMacros.push_back("FOOBAR");
3932   EXPECT_EQ("TESTSUITE(out) {\n"
3933             "FOOBAR(in) {\n"
3934             "} // FOOBAR(in)\n"
3935             "} // TESTSUITE(out)",
3936             format("TESTSUITE(out) {\n"
3937                    "FOOBAR(in) {\n"
3938                    "} // FOOBAR(in)\n"
3939                    "} // TESTSUITE(out)",
3940                    Style));
3941 }
3942 
3943 TEST_F(FormatTest, FormatsCompactNamespaces) {
3944   FormatStyle Style = getLLVMStyle();
3945   Style.CompactNamespaces = true;
3946   Style.NamespaceMacros.push_back("TESTSUITE");
3947 
3948   verifyFormat("namespace A { namespace B {\n"
3949                "}} // namespace A::B",
3950                Style);
3951 
3952   EXPECT_EQ("namespace out { namespace in {\n"
3953             "}} // namespace out::in",
3954             format("namespace out {\n"
3955                    "namespace in {\n"
3956                    "} // namespace in\n"
3957                    "} // namespace out",
3958                    Style));
3959 
3960   // Only namespaces which have both consecutive opening and end get compacted
3961   EXPECT_EQ("namespace out {\n"
3962             "namespace in1 {\n"
3963             "} // namespace in1\n"
3964             "namespace in2 {\n"
3965             "} // namespace in2\n"
3966             "} // namespace out",
3967             format("namespace out {\n"
3968                    "namespace in1 {\n"
3969                    "} // namespace in1\n"
3970                    "namespace in2 {\n"
3971                    "} // namespace in2\n"
3972                    "} // namespace out",
3973                    Style));
3974 
3975   EXPECT_EQ("namespace out {\n"
3976             "int i;\n"
3977             "namespace in {\n"
3978             "int j;\n"
3979             "} // namespace in\n"
3980             "int k;\n"
3981             "} // namespace out",
3982             format("namespace out { int i;\n"
3983                    "namespace in { int j; } // namespace in\n"
3984                    "int k; } // namespace out",
3985                    Style));
3986 
3987   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3988             "}}} // namespace A::B::C\n",
3989             format("namespace A { namespace B {\n"
3990                    "namespace C {\n"
3991                    "}} // namespace B::C\n"
3992                    "} // namespace A\n",
3993                    Style));
3994 
3995   Style.ColumnLimit = 40;
3996   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3997             "namespace bbbbbbbbbb {\n"
3998             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3999             format("namespace aaaaaaaaaa {\n"
4000                    "namespace bbbbbbbbbb {\n"
4001                    "} // namespace bbbbbbbbbb\n"
4002                    "} // namespace aaaaaaaaaa",
4003                    Style));
4004 
4005   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
4006             "namespace cccccc {\n"
4007             "}}} // namespace aaaaaa::bbbbbb::cccccc",
4008             format("namespace aaaaaa {\n"
4009                    "namespace bbbbbb {\n"
4010                    "namespace cccccc {\n"
4011                    "} // namespace cccccc\n"
4012                    "} // namespace bbbbbb\n"
4013                    "} // namespace aaaaaa",
4014                    Style));
4015   Style.ColumnLimit = 80;
4016 
4017   // Extra semicolon after 'inner' closing brace prevents merging
4018   EXPECT_EQ("namespace out { namespace in {\n"
4019             "}; } // namespace out::in",
4020             format("namespace out {\n"
4021                    "namespace in {\n"
4022                    "}; // namespace in\n"
4023                    "} // namespace out",
4024                    Style));
4025 
4026   // Extra semicolon after 'outer' closing brace is conserved
4027   EXPECT_EQ("namespace out { namespace in {\n"
4028             "}}; // namespace out::in",
4029             format("namespace out {\n"
4030                    "namespace in {\n"
4031                    "} // namespace in\n"
4032                    "}; // namespace out",
4033                    Style));
4034 
4035   Style.NamespaceIndentation = FormatStyle::NI_All;
4036   EXPECT_EQ("namespace out { namespace in {\n"
4037             "  int i;\n"
4038             "}} // namespace out::in",
4039             format("namespace out {\n"
4040                    "namespace in {\n"
4041                    "int i;\n"
4042                    "} // namespace in\n"
4043                    "} // namespace out",
4044                    Style));
4045   EXPECT_EQ("namespace out { namespace mid {\n"
4046             "  namespace in {\n"
4047             "    int j;\n"
4048             "  } // namespace in\n"
4049             "  int k;\n"
4050             "}} // namespace out::mid",
4051             format("namespace out { namespace mid {\n"
4052                    "namespace in { int j; } // namespace in\n"
4053                    "int k; }} // namespace out::mid",
4054                    Style));
4055 
4056   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4057   EXPECT_EQ("namespace out { namespace in {\n"
4058             "  int i;\n"
4059             "}} // namespace out::in",
4060             format("namespace out {\n"
4061                    "namespace in {\n"
4062                    "int i;\n"
4063                    "} // namespace in\n"
4064                    "} // namespace out",
4065                    Style));
4066   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4067             "  int i;\n"
4068             "}}} // namespace out::mid::in",
4069             format("namespace out {\n"
4070                    "namespace mid {\n"
4071                    "namespace in {\n"
4072                    "int i;\n"
4073                    "} // namespace in\n"
4074                    "} // namespace mid\n"
4075                    "} // namespace out",
4076                    Style));
4077 
4078   Style.CompactNamespaces = true;
4079   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4080   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4081   Style.BraceWrapping.BeforeLambdaBody = true;
4082   verifyFormat("namespace out { namespace in {\n"
4083                "}} // namespace out::in",
4084                Style);
4085   EXPECT_EQ("namespace out { namespace in {\n"
4086             "}} // namespace out::in",
4087             format("namespace out {\n"
4088                    "namespace in {\n"
4089                    "} // namespace in\n"
4090                    "} // namespace out",
4091                    Style));
4092 }
4093 
4094 TEST_F(FormatTest, FormatsExternC) {
4095   verifyFormat("extern \"C\" {\nint a;");
4096   verifyFormat("extern \"C\" {}");
4097   verifyFormat("extern \"C\" {\n"
4098                "int foo();\n"
4099                "}");
4100   verifyFormat("extern \"C\" int foo() {}");
4101   verifyFormat("extern \"C\" int foo();");
4102   verifyFormat("extern \"C\" int foo() {\n"
4103                "  int i = 42;\n"
4104                "  return i;\n"
4105                "}");
4106 
4107   FormatStyle Style = getLLVMStyle();
4108   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4109   Style.BraceWrapping.AfterFunction = true;
4110   verifyFormat("extern \"C\" int foo() {}", Style);
4111   verifyFormat("extern \"C\" int foo();", Style);
4112   verifyFormat("extern \"C\" int foo()\n"
4113                "{\n"
4114                "  int i = 42;\n"
4115                "  return i;\n"
4116                "}",
4117                Style);
4118 
4119   Style.BraceWrapping.AfterExternBlock = true;
4120   Style.BraceWrapping.SplitEmptyRecord = false;
4121   verifyFormat("extern \"C\"\n"
4122                "{}",
4123                Style);
4124   verifyFormat("extern \"C\"\n"
4125                "{\n"
4126                "  int foo();\n"
4127                "}",
4128                Style);
4129 }
4130 
4131 TEST_F(FormatTest, IndentExternBlockStyle) {
4132   FormatStyle Style = getLLVMStyle();
4133   Style.IndentWidth = 2;
4134 
4135   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4136   verifyFormat("extern \"C\" { /*9*/\n"
4137                "}",
4138                Style);
4139   verifyFormat("extern \"C\" {\n"
4140                "  int foo10();\n"
4141                "}",
4142                Style);
4143 
4144   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4145   verifyFormat("extern \"C\" { /*11*/\n"
4146                "}",
4147                Style);
4148   verifyFormat("extern \"C\" {\n"
4149                "int foo12();\n"
4150                "}",
4151                Style);
4152 
4153   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4154   Style.BraceWrapping.AfterExternBlock = true;
4155   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4156   verifyFormat("extern \"C\"\n"
4157                "{ /*13*/\n"
4158                "}",
4159                Style);
4160   verifyFormat("extern \"C\"\n{\n"
4161                "  int foo14();\n"
4162                "}",
4163                Style);
4164 
4165   Style.BraceWrapping.AfterExternBlock = false;
4166   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4167   verifyFormat("extern \"C\" { /*15*/\n"
4168                "}",
4169                Style);
4170   verifyFormat("extern \"C\" {\n"
4171                "int foo16();\n"
4172                "}",
4173                Style);
4174 
4175   Style.BraceWrapping.AfterExternBlock = true;
4176   verifyFormat("extern \"C\"\n"
4177                "{ /*13*/\n"
4178                "}",
4179                Style);
4180   verifyFormat("extern \"C\"\n"
4181                "{\n"
4182                "int foo14();\n"
4183                "}",
4184                Style);
4185 
4186   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4187   verifyFormat("extern \"C\"\n"
4188                "{ /*13*/\n"
4189                "}",
4190                Style);
4191   verifyFormat("extern \"C\"\n"
4192                "{\n"
4193                "  int foo14();\n"
4194                "}",
4195                Style);
4196 }
4197 
4198 TEST_F(FormatTest, FormatsInlineASM) {
4199   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4200   verifyFormat("asm(\"nop\" ::: \"memory\");");
4201   verifyFormat(
4202       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4203       "    \"cpuid\\n\\t\"\n"
4204       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4205       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4206       "    : \"a\"(value));");
4207   EXPECT_EQ(
4208       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4209       "  __asm {\n"
4210       "        mov     edx,[that] // vtable in edx\n"
4211       "        mov     eax,methodIndex\n"
4212       "        call    [edx][eax*4] // stdcall\n"
4213       "  }\n"
4214       "}",
4215       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4216              "    __asm {\n"
4217              "        mov     edx,[that] // vtable in edx\n"
4218              "        mov     eax,methodIndex\n"
4219              "        call    [edx][eax*4] // stdcall\n"
4220              "    }\n"
4221              "}"));
4222   EXPECT_EQ("_asm {\n"
4223             "  xor eax, eax;\n"
4224             "  cpuid;\n"
4225             "}",
4226             format("_asm {\n"
4227                    "  xor eax, eax;\n"
4228                    "  cpuid;\n"
4229                    "}"));
4230   verifyFormat("void function() {\n"
4231                "  // comment\n"
4232                "  asm(\"\");\n"
4233                "}");
4234   EXPECT_EQ("__asm {\n"
4235             "}\n"
4236             "int i;",
4237             format("__asm   {\n"
4238                    "}\n"
4239                    "int   i;"));
4240 }
4241 
4242 TEST_F(FormatTest, FormatTryCatch) {
4243   verifyFormat("try {\n"
4244                "  throw a * b;\n"
4245                "} catch (int a) {\n"
4246                "  // Do nothing.\n"
4247                "} catch (...) {\n"
4248                "  exit(42);\n"
4249                "}");
4250 
4251   // Function-level try statements.
4252   verifyFormat("int f() try { return 4; } catch (...) {\n"
4253                "  return 5;\n"
4254                "}");
4255   verifyFormat("class A {\n"
4256                "  int a;\n"
4257                "  A() try : a(0) {\n"
4258                "  } catch (...) {\n"
4259                "    throw;\n"
4260                "  }\n"
4261                "};\n");
4262   verifyFormat("class A {\n"
4263                "  int a;\n"
4264                "  A() try : a(0), b{1} {\n"
4265                "  } catch (...) {\n"
4266                "    throw;\n"
4267                "  }\n"
4268                "};\n");
4269   verifyFormat("class A {\n"
4270                "  int a;\n"
4271                "  A() try : a(0), b{1}, c{2} {\n"
4272                "  } catch (...) {\n"
4273                "    throw;\n"
4274                "  }\n"
4275                "};\n");
4276   verifyFormat("class A {\n"
4277                "  int a;\n"
4278                "  A() try : a(0), b{1}, c{2} {\n"
4279                "    { // New scope.\n"
4280                "    }\n"
4281                "  } catch (...) {\n"
4282                "    throw;\n"
4283                "  }\n"
4284                "};\n");
4285 
4286   // Incomplete try-catch blocks.
4287   verifyIncompleteFormat("try {} catch (");
4288 }
4289 
4290 TEST_F(FormatTest, FormatTryAsAVariable) {
4291   verifyFormat("int try;");
4292   verifyFormat("int try, size;");
4293   verifyFormat("try = foo();");
4294   verifyFormat("if (try < size) {\n  return true;\n}");
4295 
4296   verifyFormat("int catch;");
4297   verifyFormat("int catch, size;");
4298   verifyFormat("catch = foo();");
4299   verifyFormat("if (catch < size) {\n  return true;\n}");
4300 
4301   FormatStyle Style = getLLVMStyle();
4302   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4303   Style.BraceWrapping.AfterFunction = true;
4304   Style.BraceWrapping.BeforeCatch = true;
4305   verifyFormat("try {\n"
4306                "  int bar = 1;\n"
4307                "}\n"
4308                "catch (...) {\n"
4309                "  int bar = 1;\n"
4310                "}",
4311                Style);
4312   verifyFormat("#if NO_EX\n"
4313                "try\n"
4314                "#endif\n"
4315                "{\n"
4316                "}\n"
4317                "#if NO_EX\n"
4318                "catch (...) {\n"
4319                "}",
4320                Style);
4321   verifyFormat("try /* abc */ {\n"
4322                "  int bar = 1;\n"
4323                "}\n"
4324                "catch (...) {\n"
4325                "  int bar = 1;\n"
4326                "}",
4327                Style);
4328   verifyFormat("try\n"
4329                "// abc\n"
4330                "{\n"
4331                "  int bar = 1;\n"
4332                "}\n"
4333                "catch (...) {\n"
4334                "  int bar = 1;\n"
4335                "}",
4336                Style);
4337 }
4338 
4339 TEST_F(FormatTest, FormatSEHTryCatch) {
4340   verifyFormat("__try {\n"
4341                "  int a = b * c;\n"
4342                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4343                "  // Do nothing.\n"
4344                "}");
4345 
4346   verifyFormat("__try {\n"
4347                "  int a = b * c;\n"
4348                "} __finally {\n"
4349                "  // Do nothing.\n"
4350                "}");
4351 
4352   verifyFormat("DEBUG({\n"
4353                "  __try {\n"
4354                "  } __finally {\n"
4355                "  }\n"
4356                "});\n");
4357 }
4358 
4359 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4360   verifyFormat("try {\n"
4361                "  f();\n"
4362                "} catch {\n"
4363                "  g();\n"
4364                "}");
4365   verifyFormat("try {\n"
4366                "  f();\n"
4367                "} catch (A a) MACRO(x) {\n"
4368                "  g();\n"
4369                "} catch (B b) MACRO(x) {\n"
4370                "  g();\n"
4371                "}");
4372 }
4373 
4374 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4375   FormatStyle Style = getLLVMStyle();
4376   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4377                           FormatStyle::BS_WebKit}) {
4378     Style.BreakBeforeBraces = BraceStyle;
4379     verifyFormat("try {\n"
4380                  "  // something\n"
4381                  "} catch (...) {\n"
4382                  "  // something\n"
4383                  "}",
4384                  Style);
4385   }
4386   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4387   verifyFormat("try {\n"
4388                "  // something\n"
4389                "}\n"
4390                "catch (...) {\n"
4391                "  // something\n"
4392                "}",
4393                Style);
4394   verifyFormat("__try {\n"
4395                "  // something\n"
4396                "}\n"
4397                "__finally {\n"
4398                "  // something\n"
4399                "}",
4400                Style);
4401   verifyFormat("@try {\n"
4402                "  // something\n"
4403                "}\n"
4404                "@finally {\n"
4405                "  // something\n"
4406                "}",
4407                Style);
4408   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4409   verifyFormat("try\n"
4410                "{\n"
4411                "  // something\n"
4412                "}\n"
4413                "catch (...)\n"
4414                "{\n"
4415                "  // something\n"
4416                "}",
4417                Style);
4418   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4419   verifyFormat("try\n"
4420                "  {\n"
4421                "  // something white\n"
4422                "  }\n"
4423                "catch (...)\n"
4424                "  {\n"
4425                "  // something white\n"
4426                "  }",
4427                Style);
4428   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4429   verifyFormat("try\n"
4430                "  {\n"
4431                "    // something\n"
4432                "  }\n"
4433                "catch (...)\n"
4434                "  {\n"
4435                "    // something\n"
4436                "  }",
4437                Style);
4438   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4439   Style.BraceWrapping.BeforeCatch = true;
4440   verifyFormat("try {\n"
4441                "  // something\n"
4442                "}\n"
4443                "catch (...) {\n"
4444                "  // something\n"
4445                "}",
4446                Style);
4447 }
4448 
4449 TEST_F(FormatTest, StaticInitializers) {
4450   verifyFormat("static SomeClass SC = {1, 'a'};");
4451 
4452   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4453                "    100000000, "
4454                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4455 
4456   // Here, everything other than the "}" would fit on a line.
4457   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4458                "    10000000000000000000000000};");
4459   EXPECT_EQ("S s = {a,\n"
4460             "\n"
4461             "       b};",
4462             format("S s = {\n"
4463                    "  a,\n"
4464                    "\n"
4465                    "  b\n"
4466                    "};"));
4467 
4468   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4469   // line. However, the formatting looks a bit off and this probably doesn't
4470   // happen often in practice.
4471   verifyFormat("static int Variable[1] = {\n"
4472                "    {1000000000000000000000000000000000000}};",
4473                getLLVMStyleWithColumns(40));
4474 }
4475 
4476 TEST_F(FormatTest, DesignatedInitializers) {
4477   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4478   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4479                "                    .bbbbbbbbbb = 2,\n"
4480                "                    .cccccccccc = 3,\n"
4481                "                    .dddddddddd = 4,\n"
4482                "                    .eeeeeeeeee = 5};");
4483   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4484                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4485                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4486                "    .ccccccccccccccccccccccccccc = 3,\n"
4487                "    .ddddddddddddddddddddddddddd = 4,\n"
4488                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4489 
4490   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4491 
4492   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4493   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4494                "                    [2] = bbbbbbbbbb,\n"
4495                "                    [3] = cccccccccc,\n"
4496                "                    [4] = dddddddddd,\n"
4497                "                    [5] = eeeeeeeeee};");
4498   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4499                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4500                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4501                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4502                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4503                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4504 }
4505 
4506 TEST_F(FormatTest, NestedStaticInitializers) {
4507   verifyFormat("static A x = {{{}}};\n");
4508   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4509                "               {init1, init2, init3, init4}}};",
4510                getLLVMStyleWithColumns(50));
4511 
4512   verifyFormat("somes Status::global_reps[3] = {\n"
4513                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4514                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4515                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4516                getLLVMStyleWithColumns(60));
4517   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4518                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4519                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4520                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4521   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4522                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4523                "rect.fTop}};");
4524 
4525   verifyFormat(
4526       "SomeArrayOfSomeType a = {\n"
4527       "    {{1, 2, 3},\n"
4528       "     {1, 2, 3},\n"
4529       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4530       "      333333333333333333333333333333},\n"
4531       "     {1, 2, 3},\n"
4532       "     {1, 2, 3}}};");
4533   verifyFormat(
4534       "SomeArrayOfSomeType a = {\n"
4535       "    {{1, 2, 3}},\n"
4536       "    {{1, 2, 3}},\n"
4537       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4538       "      333333333333333333333333333333}},\n"
4539       "    {{1, 2, 3}},\n"
4540       "    {{1, 2, 3}}};");
4541 
4542   verifyFormat("struct {\n"
4543                "  unsigned bit;\n"
4544                "  const char *const name;\n"
4545                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4546                "                 {kOsWin, \"Windows\"},\n"
4547                "                 {kOsLinux, \"Linux\"},\n"
4548                "                 {kOsCrOS, \"Chrome OS\"}};");
4549   verifyFormat("struct {\n"
4550                "  unsigned bit;\n"
4551                "  const char *const name;\n"
4552                "} kBitsToOs[] = {\n"
4553                "    {kOsMac, \"Mac\"},\n"
4554                "    {kOsWin, \"Windows\"},\n"
4555                "    {kOsLinux, \"Linux\"},\n"
4556                "    {kOsCrOS, \"Chrome OS\"},\n"
4557                "};");
4558 }
4559 
4560 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4561   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4562                "                      \\\n"
4563                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4564 }
4565 
4566 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4567   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4568                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4569 
4570   // Do break defaulted and deleted functions.
4571   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4572                "    default;",
4573                getLLVMStyleWithColumns(40));
4574   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4575                "    delete;",
4576                getLLVMStyleWithColumns(40));
4577 }
4578 
4579 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4580   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4581                getLLVMStyleWithColumns(40));
4582   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4583                getLLVMStyleWithColumns(40));
4584   EXPECT_EQ("#define Q                              \\\n"
4585             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4586             "  \"aaaaaaaa.cpp\"",
4587             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4588                    getLLVMStyleWithColumns(40)));
4589 }
4590 
4591 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4592   EXPECT_EQ("# 123 \"A string literal\"",
4593             format("   #     123    \"A string literal\""));
4594 }
4595 
4596 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4597   EXPECT_EQ("#;", format("#;"));
4598   verifyFormat("#\n;\n;\n;");
4599 }
4600 
4601 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4602   EXPECT_EQ("#line 42 \"test\"\n",
4603             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4604   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4605                                     getLLVMStyleWithColumns(12)));
4606 }
4607 
4608 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4609   EXPECT_EQ("#line 42 \"test\"",
4610             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4611   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4612 }
4613 
4614 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4615   verifyFormat("#define A \\x20");
4616   verifyFormat("#define A \\ x20");
4617   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4618   verifyFormat("#define A ''");
4619   verifyFormat("#define A ''qqq");
4620   verifyFormat("#define A `qqq");
4621   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4622   EXPECT_EQ("const char *c = STRINGIFY(\n"
4623             "\\na : b);",
4624             format("const char * c = STRINGIFY(\n"
4625                    "\\na : b);"));
4626 
4627   verifyFormat("a\r\\");
4628   verifyFormat("a\v\\");
4629   verifyFormat("a\f\\");
4630 }
4631 
4632 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4633   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4634   style.IndentWidth = 4;
4635   style.PPIndentWidth = 1;
4636 
4637   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4638   verifyFormat("#ifdef __linux__\n"
4639                "void foo() {\n"
4640                "    int x = 0;\n"
4641                "}\n"
4642                "#define FOO\n"
4643                "#endif\n"
4644                "void bar() {\n"
4645                "    int y = 0;\n"
4646                "}\n",
4647                style);
4648 
4649   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4650   verifyFormat("#ifdef __linux__\n"
4651                "void foo() {\n"
4652                "    int x = 0;\n"
4653                "}\n"
4654                "# define FOO foo\n"
4655                "#endif\n"
4656                "void bar() {\n"
4657                "    int y = 0;\n"
4658                "}\n",
4659                style);
4660 
4661   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4662   verifyFormat("#ifdef __linux__\n"
4663                "void foo() {\n"
4664                "    int x = 0;\n"
4665                "}\n"
4666                " #define FOO foo\n"
4667                "#endif\n"
4668                "void bar() {\n"
4669                "    int y = 0;\n"
4670                "}\n",
4671                style);
4672 }
4673 
4674 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4675   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4676   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4677   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4678   // FIXME: We never break before the macro name.
4679   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4680 
4681   verifyFormat("#define A A\n#define A A");
4682   verifyFormat("#define A(X) A\n#define A A");
4683 
4684   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4685   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4686 }
4687 
4688 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4689   EXPECT_EQ("// somecomment\n"
4690             "#include \"a.h\"\n"
4691             "#define A(  \\\n"
4692             "    A, B)\n"
4693             "#include \"b.h\"\n"
4694             "// somecomment\n",
4695             format("  // somecomment\n"
4696                    "  #include \"a.h\"\n"
4697                    "#define A(A,\\\n"
4698                    "    B)\n"
4699                    "    #include \"b.h\"\n"
4700                    " // somecomment\n",
4701                    getLLVMStyleWithColumns(13)));
4702 }
4703 
4704 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4705 
4706 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4707   EXPECT_EQ("#define A    \\\n"
4708             "  c;         \\\n"
4709             "  e;\n"
4710             "f;",
4711             format("#define A c; e;\n"
4712                    "f;",
4713                    getLLVMStyleWithColumns(14)));
4714 }
4715 
4716 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4717 
4718 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4719   EXPECT_EQ("int x,\n"
4720             "#define A\n"
4721             "    y;",
4722             format("int x,\n#define A\ny;"));
4723 }
4724 
4725 TEST_F(FormatTest, HashInMacroDefinition) {
4726   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4727   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4728   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4729   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4730   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4731   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4732   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4733   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4734   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4735   verifyFormat("#define A  \\\n"
4736                "  {        \\\n"
4737                "    f(#c); \\\n"
4738                "  }",
4739                getLLVMStyleWithColumns(11));
4740 
4741   verifyFormat("#define A(X)         \\\n"
4742                "  void function##X()",
4743                getLLVMStyleWithColumns(22));
4744 
4745   verifyFormat("#define A(a, b, c)   \\\n"
4746                "  void a##b##c()",
4747                getLLVMStyleWithColumns(22));
4748 
4749   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4750 }
4751 
4752 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4753   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4754   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4755 
4756   FormatStyle Style = getLLVMStyle();
4757   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4758   verifyFormat("#define true ((foo)1)", Style);
4759   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4760   verifyFormat("#define false((foo)0)", Style);
4761 }
4762 
4763 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4764   EXPECT_EQ("#define A b;", format("#define A \\\n"
4765                                    "          \\\n"
4766                                    "  b;",
4767                                    getLLVMStyleWithColumns(25)));
4768   EXPECT_EQ("#define A \\\n"
4769             "          \\\n"
4770             "  a;      \\\n"
4771             "  b;",
4772             format("#define A \\\n"
4773                    "          \\\n"
4774                    "  a;      \\\n"
4775                    "  b;",
4776                    getLLVMStyleWithColumns(11)));
4777   EXPECT_EQ("#define A \\\n"
4778             "  a;      \\\n"
4779             "          \\\n"
4780             "  b;",
4781             format("#define A \\\n"
4782                    "  a;      \\\n"
4783                    "          \\\n"
4784                    "  b;",
4785                    getLLVMStyleWithColumns(11)));
4786 }
4787 
4788 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4789   verifyIncompleteFormat("#define A :");
4790   verifyFormat("#define SOMECASES  \\\n"
4791                "  case 1:          \\\n"
4792                "  case 2\n",
4793                getLLVMStyleWithColumns(20));
4794   verifyFormat("#define MACRO(a) \\\n"
4795                "  if (a)         \\\n"
4796                "    f();         \\\n"
4797                "  else           \\\n"
4798                "    g()",
4799                getLLVMStyleWithColumns(18));
4800   verifyFormat("#define A template <typename T>");
4801   verifyIncompleteFormat("#define STR(x) #x\n"
4802                          "f(STR(this_is_a_string_literal{));");
4803   verifyFormat("#pragma omp threadprivate( \\\n"
4804                "    y)), // expected-warning",
4805                getLLVMStyleWithColumns(28));
4806   verifyFormat("#d, = };");
4807   verifyFormat("#if \"a");
4808   verifyIncompleteFormat("({\n"
4809                          "#define b     \\\n"
4810                          "  }           \\\n"
4811                          "  a\n"
4812                          "a",
4813                          getLLVMStyleWithColumns(15));
4814   verifyFormat("#define A     \\\n"
4815                "  {           \\\n"
4816                "    {\n"
4817                "#define B     \\\n"
4818                "  }           \\\n"
4819                "  }",
4820                getLLVMStyleWithColumns(15));
4821   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4822   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4823   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4824   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4825 }
4826 
4827 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4828   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4829   EXPECT_EQ("class A : public QObject {\n"
4830             "  Q_OBJECT\n"
4831             "\n"
4832             "  A() {}\n"
4833             "};",
4834             format("class A  :  public QObject {\n"
4835                    "     Q_OBJECT\n"
4836                    "\n"
4837                    "  A() {\n}\n"
4838                    "}  ;"));
4839   EXPECT_EQ("MACRO\n"
4840             "/*static*/ int i;",
4841             format("MACRO\n"
4842                    " /*static*/ int   i;"));
4843   EXPECT_EQ("SOME_MACRO\n"
4844             "namespace {\n"
4845             "void f();\n"
4846             "} // namespace",
4847             format("SOME_MACRO\n"
4848                    "  namespace    {\n"
4849                    "void   f(  );\n"
4850                    "} // namespace"));
4851   // Only if the identifier contains at least 5 characters.
4852   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4853   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4854   // Only if everything is upper case.
4855   EXPECT_EQ("class A : public QObject {\n"
4856             "  Q_Object A() {}\n"
4857             "};",
4858             format("class A  :  public QObject {\n"
4859                    "     Q_Object\n"
4860                    "  A() {\n}\n"
4861                    "}  ;"));
4862 
4863   // Only if the next line can actually start an unwrapped line.
4864   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4865             format("SOME_WEIRD_LOG_MACRO\n"
4866                    "<< SomeThing;"));
4867 
4868   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4869                "(n, buffers))\n",
4870                getChromiumStyle(FormatStyle::LK_Cpp));
4871 
4872   // See PR41483
4873   EXPECT_EQ("/**/ FOO(a)\n"
4874             "FOO(b)",
4875             format("/**/ FOO(a)\n"
4876                    "FOO(b)"));
4877 }
4878 
4879 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4880   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4881             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4882             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4883             "class X {};\n"
4884             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4885             "int *createScopDetectionPass() { return 0; }",
4886             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4887                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4888                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4889                    "  class X {};\n"
4890                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4891                    "  int *createScopDetectionPass() { return 0; }"));
4892   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4893   // braces, so that inner block is indented one level more.
4894   EXPECT_EQ("int q() {\n"
4895             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4896             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4897             "  IPC_END_MESSAGE_MAP()\n"
4898             "}",
4899             format("int q() {\n"
4900                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4901                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4902                    "  IPC_END_MESSAGE_MAP()\n"
4903                    "}"));
4904 
4905   // Same inside macros.
4906   EXPECT_EQ("#define LIST(L) \\\n"
4907             "  L(A)          \\\n"
4908             "  L(B)          \\\n"
4909             "  L(C)",
4910             format("#define LIST(L) \\\n"
4911                    "  L(A) \\\n"
4912                    "  L(B) \\\n"
4913                    "  L(C)",
4914                    getGoogleStyle()));
4915 
4916   // These must not be recognized as macros.
4917   EXPECT_EQ("int q() {\n"
4918             "  f(x);\n"
4919             "  f(x) {}\n"
4920             "  f(x)->g();\n"
4921             "  f(x)->*g();\n"
4922             "  f(x).g();\n"
4923             "  f(x) = x;\n"
4924             "  f(x) += x;\n"
4925             "  f(x) -= x;\n"
4926             "  f(x) *= x;\n"
4927             "  f(x) /= x;\n"
4928             "  f(x) %= x;\n"
4929             "  f(x) &= x;\n"
4930             "  f(x) |= x;\n"
4931             "  f(x) ^= x;\n"
4932             "  f(x) >>= x;\n"
4933             "  f(x) <<= x;\n"
4934             "  f(x)[y].z();\n"
4935             "  LOG(INFO) << x;\n"
4936             "  ifstream(x) >> x;\n"
4937             "}\n",
4938             format("int q() {\n"
4939                    "  f(x)\n;\n"
4940                    "  f(x)\n {}\n"
4941                    "  f(x)\n->g();\n"
4942                    "  f(x)\n->*g();\n"
4943                    "  f(x)\n.g();\n"
4944                    "  f(x)\n = x;\n"
4945                    "  f(x)\n += x;\n"
4946                    "  f(x)\n -= x;\n"
4947                    "  f(x)\n *= x;\n"
4948                    "  f(x)\n /= x;\n"
4949                    "  f(x)\n %= x;\n"
4950                    "  f(x)\n &= x;\n"
4951                    "  f(x)\n |= x;\n"
4952                    "  f(x)\n ^= x;\n"
4953                    "  f(x)\n >>= x;\n"
4954                    "  f(x)\n <<= x;\n"
4955                    "  f(x)\n[y].z();\n"
4956                    "  LOG(INFO)\n << x;\n"
4957                    "  ifstream(x)\n >> x;\n"
4958                    "}\n"));
4959   EXPECT_EQ("int q() {\n"
4960             "  F(x)\n"
4961             "  if (1) {\n"
4962             "  }\n"
4963             "  F(x)\n"
4964             "  while (1) {\n"
4965             "  }\n"
4966             "  F(x)\n"
4967             "  G(x);\n"
4968             "  F(x)\n"
4969             "  try {\n"
4970             "    Q();\n"
4971             "  } catch (...) {\n"
4972             "  }\n"
4973             "}\n",
4974             format("int q() {\n"
4975                    "F(x)\n"
4976                    "if (1) {}\n"
4977                    "F(x)\n"
4978                    "while (1) {}\n"
4979                    "F(x)\n"
4980                    "G(x);\n"
4981                    "F(x)\n"
4982                    "try { Q(); } catch (...) {}\n"
4983                    "}\n"));
4984   EXPECT_EQ("class A {\n"
4985             "  A() : t(0) {}\n"
4986             "  A(int i) noexcept() : {}\n"
4987             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4988             "  try : t(0) {\n"
4989             "  } catch (...) {\n"
4990             "  }\n"
4991             "};",
4992             format("class A {\n"
4993                    "  A()\n : t(0) {}\n"
4994                    "  A(int i)\n noexcept() : {}\n"
4995                    "  A(X x)\n"
4996                    "  try : t(0) {} catch (...) {}\n"
4997                    "};"));
4998   FormatStyle Style = getLLVMStyle();
4999   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5000   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5001   Style.BraceWrapping.AfterFunction = true;
5002   EXPECT_EQ("void f()\n"
5003             "try\n"
5004             "{\n"
5005             "}",
5006             format("void f() try {\n"
5007                    "}",
5008                    Style));
5009   EXPECT_EQ("class SomeClass {\n"
5010             "public:\n"
5011             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5012             "};",
5013             format("class SomeClass {\n"
5014                    "public:\n"
5015                    "  SomeClass()\n"
5016                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5017                    "};"));
5018   EXPECT_EQ("class SomeClass {\n"
5019             "public:\n"
5020             "  SomeClass()\n"
5021             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5022             "};",
5023             format("class SomeClass {\n"
5024                    "public:\n"
5025                    "  SomeClass()\n"
5026                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5027                    "};",
5028                    getLLVMStyleWithColumns(40)));
5029 
5030   verifyFormat("MACRO(>)");
5031 
5032   // Some macros contain an implicit semicolon.
5033   Style = getLLVMStyle();
5034   Style.StatementMacros.push_back("FOO");
5035   verifyFormat("FOO(a) int b = 0;");
5036   verifyFormat("FOO(a)\n"
5037                "int b = 0;",
5038                Style);
5039   verifyFormat("FOO(a);\n"
5040                "int b = 0;",
5041                Style);
5042   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5043                "int b = 0;",
5044                Style);
5045   verifyFormat("FOO()\n"
5046                "int b = 0;",
5047                Style);
5048   verifyFormat("FOO\n"
5049                "int b = 0;",
5050                Style);
5051   verifyFormat("void f() {\n"
5052                "  FOO(a)\n"
5053                "  return a;\n"
5054                "}",
5055                Style);
5056   verifyFormat("FOO(a)\n"
5057                "FOO(b)",
5058                Style);
5059   verifyFormat("int a = 0;\n"
5060                "FOO(b)\n"
5061                "int c = 0;",
5062                Style);
5063   verifyFormat("int a = 0;\n"
5064                "int x = FOO(a)\n"
5065                "int b = 0;",
5066                Style);
5067   verifyFormat("void foo(int a) { FOO(a) }\n"
5068                "uint32_t bar() {}",
5069                Style);
5070 }
5071 
5072 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5073   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5074 
5075   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5076                ZeroColumn);
5077 }
5078 
5079 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5080   verifyFormat("#define A \\\n"
5081                "  f({     \\\n"
5082                "    g();  \\\n"
5083                "  });",
5084                getLLVMStyleWithColumns(11));
5085 }
5086 
5087 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5088   FormatStyle Style = getLLVMStyleWithColumns(40);
5089   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5090   verifyFormat("#ifdef _WIN32\n"
5091                "#define A 0\n"
5092                "#ifdef VAR2\n"
5093                "#define B 1\n"
5094                "#include <someheader.h>\n"
5095                "#define MACRO                          \\\n"
5096                "  some_very_long_func_aaaaaaaaaa();\n"
5097                "#endif\n"
5098                "#else\n"
5099                "#define A 1\n"
5100                "#endif",
5101                Style);
5102   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5103   verifyFormat("#ifdef _WIN32\n"
5104                "#  define A 0\n"
5105                "#  ifdef VAR2\n"
5106                "#    define B 1\n"
5107                "#    include <someheader.h>\n"
5108                "#    define MACRO                      \\\n"
5109                "      some_very_long_func_aaaaaaaaaa();\n"
5110                "#  endif\n"
5111                "#else\n"
5112                "#  define A 1\n"
5113                "#endif",
5114                Style);
5115   verifyFormat("#if A\n"
5116                "#  define MACRO                        \\\n"
5117                "    void a(int x) {                    \\\n"
5118                "      b();                             \\\n"
5119                "      c();                             \\\n"
5120                "      d();                             \\\n"
5121                "      e();                             \\\n"
5122                "      f();                             \\\n"
5123                "    }\n"
5124                "#endif",
5125                Style);
5126   // Comments before include guard.
5127   verifyFormat("// file comment\n"
5128                "// file comment\n"
5129                "#ifndef HEADER_H\n"
5130                "#define HEADER_H\n"
5131                "code();\n"
5132                "#endif",
5133                Style);
5134   // Test with include guards.
5135   verifyFormat("#ifndef HEADER_H\n"
5136                "#define HEADER_H\n"
5137                "code();\n"
5138                "#endif",
5139                Style);
5140   // Include guards must have a #define with the same variable immediately
5141   // after #ifndef.
5142   verifyFormat("#ifndef NOT_GUARD\n"
5143                "#  define FOO\n"
5144                "code();\n"
5145                "#endif",
5146                Style);
5147 
5148   // Include guards must cover the entire file.
5149   verifyFormat("code();\n"
5150                "code();\n"
5151                "#ifndef NOT_GUARD\n"
5152                "#  define NOT_GUARD\n"
5153                "code();\n"
5154                "#endif",
5155                Style);
5156   verifyFormat("#ifndef NOT_GUARD\n"
5157                "#  define NOT_GUARD\n"
5158                "code();\n"
5159                "#endif\n"
5160                "code();",
5161                Style);
5162   // Test with trailing blank lines.
5163   verifyFormat("#ifndef HEADER_H\n"
5164                "#define HEADER_H\n"
5165                "code();\n"
5166                "#endif\n",
5167                Style);
5168   // Include guards don't have #else.
5169   verifyFormat("#ifndef NOT_GUARD\n"
5170                "#  define NOT_GUARD\n"
5171                "code();\n"
5172                "#else\n"
5173                "#endif",
5174                Style);
5175   verifyFormat("#ifndef NOT_GUARD\n"
5176                "#  define NOT_GUARD\n"
5177                "code();\n"
5178                "#elif FOO\n"
5179                "#endif",
5180                Style);
5181   // Non-identifier #define after potential include guard.
5182   verifyFormat("#ifndef FOO\n"
5183                "#  define 1\n"
5184                "#endif\n",
5185                Style);
5186   // #if closes past last non-preprocessor line.
5187   verifyFormat("#ifndef FOO\n"
5188                "#define FOO\n"
5189                "#if 1\n"
5190                "int i;\n"
5191                "#  define A 0\n"
5192                "#endif\n"
5193                "#endif\n",
5194                Style);
5195   // Don't crash if there is an #elif directive without a condition.
5196   verifyFormat("#if 1\n"
5197                "int x;\n"
5198                "#elif\n"
5199                "int y;\n"
5200                "#else\n"
5201                "int z;\n"
5202                "#endif",
5203                Style);
5204   // FIXME: This doesn't handle the case where there's code between the
5205   // #ifndef and #define but all other conditions hold. This is because when
5206   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5207   // previous code line yet, so we can't detect it.
5208   EXPECT_EQ("#ifndef NOT_GUARD\n"
5209             "code();\n"
5210             "#define NOT_GUARD\n"
5211             "code();\n"
5212             "#endif",
5213             format("#ifndef NOT_GUARD\n"
5214                    "code();\n"
5215                    "#  define NOT_GUARD\n"
5216                    "code();\n"
5217                    "#endif",
5218                    Style));
5219   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5220   // be outside an include guard. Examples are #pragma once and
5221   // #pragma GCC diagnostic, or anything else that does not change the meaning
5222   // of the file if it's included multiple times.
5223   EXPECT_EQ("#ifdef WIN32\n"
5224             "#  pragma once\n"
5225             "#endif\n"
5226             "#ifndef HEADER_H\n"
5227             "#  define HEADER_H\n"
5228             "code();\n"
5229             "#endif",
5230             format("#ifdef WIN32\n"
5231                    "#  pragma once\n"
5232                    "#endif\n"
5233                    "#ifndef HEADER_H\n"
5234                    "#define HEADER_H\n"
5235                    "code();\n"
5236                    "#endif",
5237                    Style));
5238   // FIXME: This does not detect when there is a single non-preprocessor line
5239   // in front of an include-guard-like structure where other conditions hold
5240   // because ScopedLineState hides the line.
5241   EXPECT_EQ("code();\n"
5242             "#ifndef HEADER_H\n"
5243             "#define HEADER_H\n"
5244             "code();\n"
5245             "#endif",
5246             format("code();\n"
5247                    "#ifndef HEADER_H\n"
5248                    "#  define HEADER_H\n"
5249                    "code();\n"
5250                    "#endif",
5251                    Style));
5252   // Keep comments aligned with #, otherwise indent comments normally. These
5253   // tests cannot use verifyFormat because messUp manipulates leading
5254   // whitespace.
5255   {
5256     const char *Expected = ""
5257                            "void f() {\n"
5258                            "#if 1\n"
5259                            "// Preprocessor aligned.\n"
5260                            "#  define A 0\n"
5261                            "  // Code. Separated by blank line.\n"
5262                            "\n"
5263                            "#  define B 0\n"
5264                            "  // Code. Not aligned with #\n"
5265                            "#  define C 0\n"
5266                            "#endif";
5267     const char *ToFormat = ""
5268                            "void f() {\n"
5269                            "#if 1\n"
5270                            "// Preprocessor aligned.\n"
5271                            "#  define A 0\n"
5272                            "// Code. Separated by blank line.\n"
5273                            "\n"
5274                            "#  define B 0\n"
5275                            "   // Code. Not aligned with #\n"
5276                            "#  define C 0\n"
5277                            "#endif";
5278     EXPECT_EQ(Expected, format(ToFormat, Style));
5279     EXPECT_EQ(Expected, format(Expected, Style));
5280   }
5281   // Keep block quotes aligned.
5282   {
5283     const char *Expected = ""
5284                            "void f() {\n"
5285                            "#if 1\n"
5286                            "/* Preprocessor aligned. */\n"
5287                            "#  define A 0\n"
5288                            "  /* Code. Separated by blank line. */\n"
5289                            "\n"
5290                            "#  define B 0\n"
5291                            "  /* Code. Not aligned with # */\n"
5292                            "#  define C 0\n"
5293                            "#endif";
5294     const char *ToFormat = ""
5295                            "void f() {\n"
5296                            "#if 1\n"
5297                            "/* Preprocessor aligned. */\n"
5298                            "#  define A 0\n"
5299                            "/* Code. Separated by blank line. */\n"
5300                            "\n"
5301                            "#  define B 0\n"
5302                            "   /* Code. Not aligned with # */\n"
5303                            "#  define C 0\n"
5304                            "#endif";
5305     EXPECT_EQ(Expected, format(ToFormat, Style));
5306     EXPECT_EQ(Expected, format(Expected, Style));
5307   }
5308   // Keep comments aligned with un-indented directives.
5309   {
5310     const char *Expected = ""
5311                            "void f() {\n"
5312                            "// Preprocessor aligned.\n"
5313                            "#define A 0\n"
5314                            "  // Code. Separated by blank line.\n"
5315                            "\n"
5316                            "#define B 0\n"
5317                            "  // Code. Not aligned with #\n"
5318                            "#define C 0\n";
5319     const char *ToFormat = ""
5320                            "void f() {\n"
5321                            "// Preprocessor aligned.\n"
5322                            "#define A 0\n"
5323                            "// Code. Separated by blank line.\n"
5324                            "\n"
5325                            "#define B 0\n"
5326                            "   // Code. Not aligned with #\n"
5327                            "#define C 0\n";
5328     EXPECT_EQ(Expected, format(ToFormat, Style));
5329     EXPECT_EQ(Expected, format(Expected, Style));
5330   }
5331   // Test AfterHash with tabs.
5332   {
5333     FormatStyle Tabbed = Style;
5334     Tabbed.UseTab = FormatStyle::UT_Always;
5335     Tabbed.IndentWidth = 8;
5336     Tabbed.TabWidth = 8;
5337     verifyFormat("#ifdef _WIN32\n"
5338                  "#\tdefine A 0\n"
5339                  "#\tifdef VAR2\n"
5340                  "#\t\tdefine B 1\n"
5341                  "#\t\tinclude <someheader.h>\n"
5342                  "#\t\tdefine MACRO          \\\n"
5343                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5344                  "#\tendif\n"
5345                  "#else\n"
5346                  "#\tdefine A 1\n"
5347                  "#endif",
5348                  Tabbed);
5349   }
5350 
5351   // Regression test: Multiline-macro inside include guards.
5352   verifyFormat("#ifndef HEADER_H\n"
5353                "#define HEADER_H\n"
5354                "#define A()        \\\n"
5355                "  int i;           \\\n"
5356                "  int j;\n"
5357                "#endif // HEADER_H",
5358                getLLVMStyleWithColumns(20));
5359 
5360   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5361   // Basic before hash indent tests
5362   verifyFormat("#ifdef _WIN32\n"
5363                "  #define A 0\n"
5364                "  #ifdef VAR2\n"
5365                "    #define B 1\n"
5366                "    #include <someheader.h>\n"
5367                "    #define MACRO                      \\\n"
5368                "      some_very_long_func_aaaaaaaaaa();\n"
5369                "  #endif\n"
5370                "#else\n"
5371                "  #define A 1\n"
5372                "#endif",
5373                Style);
5374   verifyFormat("#if A\n"
5375                "  #define MACRO                        \\\n"
5376                "    void a(int x) {                    \\\n"
5377                "      b();                             \\\n"
5378                "      c();                             \\\n"
5379                "      d();                             \\\n"
5380                "      e();                             \\\n"
5381                "      f();                             \\\n"
5382                "    }\n"
5383                "#endif",
5384                Style);
5385   // Keep comments aligned with indented directives. These
5386   // tests cannot use verifyFormat because messUp manipulates leading
5387   // whitespace.
5388   {
5389     const char *Expected = "void f() {\n"
5390                            "// Aligned to preprocessor.\n"
5391                            "#if 1\n"
5392                            "  // Aligned to code.\n"
5393                            "  int a;\n"
5394                            "  #if 1\n"
5395                            "    // Aligned to preprocessor.\n"
5396                            "    #define A 0\n"
5397                            "  // Aligned to code.\n"
5398                            "  int b;\n"
5399                            "  #endif\n"
5400                            "#endif\n"
5401                            "}";
5402     const char *ToFormat = "void f() {\n"
5403                            "// Aligned to preprocessor.\n"
5404                            "#if 1\n"
5405                            "// Aligned to code.\n"
5406                            "int a;\n"
5407                            "#if 1\n"
5408                            "// Aligned to preprocessor.\n"
5409                            "#define A 0\n"
5410                            "// Aligned to code.\n"
5411                            "int b;\n"
5412                            "#endif\n"
5413                            "#endif\n"
5414                            "}";
5415     EXPECT_EQ(Expected, format(ToFormat, Style));
5416     EXPECT_EQ(Expected, format(Expected, Style));
5417   }
5418   {
5419     const char *Expected = "void f() {\n"
5420                            "/* Aligned to preprocessor. */\n"
5421                            "#if 1\n"
5422                            "  /* Aligned to code. */\n"
5423                            "  int a;\n"
5424                            "  #if 1\n"
5425                            "    /* Aligned to preprocessor. */\n"
5426                            "    #define A 0\n"
5427                            "  /* Aligned to code. */\n"
5428                            "  int b;\n"
5429                            "  #endif\n"
5430                            "#endif\n"
5431                            "}";
5432     const char *ToFormat = "void f() {\n"
5433                            "/* Aligned to preprocessor. */\n"
5434                            "#if 1\n"
5435                            "/* Aligned to code. */\n"
5436                            "int a;\n"
5437                            "#if 1\n"
5438                            "/* Aligned to preprocessor. */\n"
5439                            "#define A 0\n"
5440                            "/* Aligned to code. */\n"
5441                            "int b;\n"
5442                            "#endif\n"
5443                            "#endif\n"
5444                            "}";
5445     EXPECT_EQ(Expected, format(ToFormat, Style));
5446     EXPECT_EQ(Expected, format(Expected, Style));
5447   }
5448 
5449   // Test single comment before preprocessor
5450   verifyFormat("// Comment\n"
5451                "\n"
5452                "#if 1\n"
5453                "#endif",
5454                Style);
5455 }
5456 
5457 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5458   verifyFormat("{\n  { a #c; }\n}");
5459 }
5460 
5461 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5462   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5463             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5464   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5465             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5466 }
5467 
5468 TEST_F(FormatTest, EscapedNewlines) {
5469   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5470   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5471             format("#define A \\\nint i;\\\n  int j;", Narrow));
5472   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5473   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5474   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5475   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5476 
5477   FormatStyle AlignLeft = getLLVMStyle();
5478   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5479   EXPECT_EQ("#define MACRO(x) \\\n"
5480             "private:         \\\n"
5481             "  int x(int a);\n",
5482             format("#define MACRO(x) \\\n"
5483                    "private:         \\\n"
5484                    "  int x(int a);\n",
5485                    AlignLeft));
5486 
5487   // CRLF line endings
5488   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5489             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5490   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5491   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5492   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5493   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5494   EXPECT_EQ("#define MACRO(x) \\\r\n"
5495             "private:         \\\r\n"
5496             "  int x(int a);\r\n",
5497             format("#define MACRO(x) \\\r\n"
5498                    "private:         \\\r\n"
5499                    "  int x(int a);\r\n",
5500                    AlignLeft));
5501 
5502   FormatStyle DontAlign = getLLVMStyle();
5503   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5504   DontAlign.MaxEmptyLinesToKeep = 3;
5505   // FIXME: can't use verifyFormat here because the newline before
5506   // "public:" is not inserted the first time it's reformatted
5507   EXPECT_EQ("#define A \\\n"
5508             "  class Foo { \\\n"
5509             "    void bar(); \\\n"
5510             "\\\n"
5511             "\\\n"
5512             "\\\n"
5513             "  public: \\\n"
5514             "    void baz(); \\\n"
5515             "  };",
5516             format("#define A \\\n"
5517                    "  class Foo { \\\n"
5518                    "    void bar(); \\\n"
5519                    "\\\n"
5520                    "\\\n"
5521                    "\\\n"
5522                    "  public: \\\n"
5523                    "    void baz(); \\\n"
5524                    "  };",
5525                    DontAlign));
5526 }
5527 
5528 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5529   verifyFormat("#define A \\\n"
5530                "  int v(  \\\n"
5531                "      a); \\\n"
5532                "  int i;",
5533                getLLVMStyleWithColumns(11));
5534 }
5535 
5536 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5537   EXPECT_EQ(
5538       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5539       "                      \\\n"
5540       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5541       "\n"
5542       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5543       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5544       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5545              "\\\n"
5546              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5547              "  \n"
5548              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5549              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5550 }
5551 
5552 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5553   EXPECT_EQ("int\n"
5554             "#define A\n"
5555             "    a;",
5556             format("int\n#define A\na;"));
5557   verifyFormat("functionCallTo(\n"
5558                "    someOtherFunction(\n"
5559                "        withSomeParameters, whichInSequence,\n"
5560                "        areLongerThanALine(andAnotherCall,\n"
5561                "#define A B\n"
5562                "                           withMoreParamters,\n"
5563                "                           whichStronglyInfluenceTheLayout),\n"
5564                "        andMoreParameters),\n"
5565                "    trailing);",
5566                getLLVMStyleWithColumns(69));
5567   verifyFormat("Foo::Foo()\n"
5568                "#ifdef BAR\n"
5569                "    : baz(0)\n"
5570                "#endif\n"
5571                "{\n"
5572                "}");
5573   verifyFormat("void f() {\n"
5574                "  if (true)\n"
5575                "#ifdef A\n"
5576                "    f(42);\n"
5577                "  x();\n"
5578                "#else\n"
5579                "    g();\n"
5580                "  x();\n"
5581                "#endif\n"
5582                "}");
5583   verifyFormat("void f(param1, param2,\n"
5584                "       param3,\n"
5585                "#ifdef A\n"
5586                "       param4(param5,\n"
5587                "#ifdef A1\n"
5588                "              param6,\n"
5589                "#ifdef A2\n"
5590                "              param7),\n"
5591                "#else\n"
5592                "              param8),\n"
5593                "       param9,\n"
5594                "#endif\n"
5595                "       param10,\n"
5596                "#endif\n"
5597                "       param11)\n"
5598                "#else\n"
5599                "       param12)\n"
5600                "#endif\n"
5601                "{\n"
5602                "  x();\n"
5603                "}",
5604                getLLVMStyleWithColumns(28));
5605   verifyFormat("#if 1\n"
5606                "int i;");
5607   verifyFormat("#if 1\n"
5608                "#endif\n"
5609                "#if 1\n"
5610                "#else\n"
5611                "#endif\n");
5612   verifyFormat("DEBUG({\n"
5613                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5614                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5615                "});\n"
5616                "#if a\n"
5617                "#else\n"
5618                "#endif");
5619 
5620   verifyIncompleteFormat("void f(\n"
5621                          "#if A\n"
5622                          ");\n"
5623                          "#else\n"
5624                          "#endif");
5625 }
5626 
5627 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5628   verifyFormat("#endif\n"
5629                "#if B");
5630 }
5631 
5632 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5633   FormatStyle SingleLine = getLLVMStyle();
5634   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5635   verifyFormat("#if 0\n"
5636                "#elif 1\n"
5637                "#endif\n"
5638                "void foo() {\n"
5639                "  if (test) foo2();\n"
5640                "}",
5641                SingleLine);
5642 }
5643 
5644 TEST_F(FormatTest, LayoutBlockInsideParens) {
5645   verifyFormat("functionCall({ int i; });");
5646   verifyFormat("functionCall({\n"
5647                "  int i;\n"
5648                "  int j;\n"
5649                "});");
5650   verifyFormat("functionCall(\n"
5651                "    {\n"
5652                "      int i;\n"
5653                "      int j;\n"
5654                "    },\n"
5655                "    aaaa, bbbb, cccc);");
5656   verifyFormat("functionA(functionB({\n"
5657                "            int i;\n"
5658                "            int j;\n"
5659                "          }),\n"
5660                "          aaaa, bbbb, cccc);");
5661   verifyFormat("functionCall(\n"
5662                "    {\n"
5663                "      int i;\n"
5664                "      int j;\n"
5665                "    },\n"
5666                "    aaaa, bbbb, // comment\n"
5667                "    cccc);");
5668   verifyFormat("functionA(functionB({\n"
5669                "            int i;\n"
5670                "            int j;\n"
5671                "          }),\n"
5672                "          aaaa, bbbb, // comment\n"
5673                "          cccc);");
5674   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5675   verifyFormat("functionCall(aaaa, bbbb, {\n"
5676                "  int i;\n"
5677                "  int j;\n"
5678                "});");
5679   verifyFormat(
5680       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5681       "    {\n"
5682       "      int i; // break\n"
5683       "    },\n"
5684       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5685       "                                     ccccccccccccccccc));");
5686   verifyFormat("DEBUG({\n"
5687                "  if (a)\n"
5688                "    f();\n"
5689                "});");
5690 }
5691 
5692 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5693   EXPECT_EQ("SOME_MACRO { int i; }\n"
5694             "int i;",
5695             format("  SOME_MACRO  {int i;}  int i;"));
5696 }
5697 
5698 TEST_F(FormatTest, LayoutNestedBlocks) {
5699   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5700                "  struct s {\n"
5701                "    int i;\n"
5702                "  };\n"
5703                "  s kBitsToOs[] = {{10}};\n"
5704                "  for (int i = 0; i < 10; ++i)\n"
5705                "    return;\n"
5706                "}");
5707   verifyFormat("call(parameter, {\n"
5708                "  something();\n"
5709                "  // Comment using all columns.\n"
5710                "  somethingelse();\n"
5711                "});",
5712                getLLVMStyleWithColumns(40));
5713   verifyFormat("DEBUG( //\n"
5714                "    { f(); }, a);");
5715   verifyFormat("DEBUG( //\n"
5716                "    {\n"
5717                "      f(); //\n"
5718                "    },\n"
5719                "    a);");
5720 
5721   EXPECT_EQ("call(parameter, {\n"
5722             "  something();\n"
5723             "  // Comment too\n"
5724             "  // looooooooooong.\n"
5725             "  somethingElse();\n"
5726             "});",
5727             format("call(parameter, {\n"
5728                    "  something();\n"
5729                    "  // Comment too looooooooooong.\n"
5730                    "  somethingElse();\n"
5731                    "});",
5732                    getLLVMStyleWithColumns(29)));
5733   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5734   EXPECT_EQ("DEBUG({ // comment\n"
5735             "  int i;\n"
5736             "});",
5737             format("DEBUG({ // comment\n"
5738                    "int  i;\n"
5739                    "});"));
5740   EXPECT_EQ("DEBUG({\n"
5741             "  int i;\n"
5742             "\n"
5743             "  // comment\n"
5744             "  int j;\n"
5745             "});",
5746             format("DEBUG({\n"
5747                    "  int  i;\n"
5748                    "\n"
5749                    "  // comment\n"
5750                    "  int  j;\n"
5751                    "});"));
5752 
5753   verifyFormat("DEBUG({\n"
5754                "  if (a)\n"
5755                "    return;\n"
5756                "});");
5757   verifyGoogleFormat("DEBUG({\n"
5758                      "  if (a) return;\n"
5759                      "});");
5760   FormatStyle Style = getGoogleStyle();
5761   Style.ColumnLimit = 45;
5762   verifyFormat("Debug(\n"
5763                "    aaaaa,\n"
5764                "    {\n"
5765                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5766                "    },\n"
5767                "    a);",
5768                Style);
5769 
5770   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5771 
5772   verifyNoCrash("^{v^{a}}");
5773 }
5774 
5775 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5776   EXPECT_EQ("#define MACRO()                     \\\n"
5777             "  Debug(aaa, /* force line break */ \\\n"
5778             "        {                           \\\n"
5779             "          int i;                    \\\n"
5780             "          int j;                    \\\n"
5781             "        })",
5782             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5783                    "          {  int   i;  int  j;   })",
5784                    getGoogleStyle()));
5785 
5786   EXPECT_EQ("#define A                                       \\\n"
5787             "  [] {                                          \\\n"
5788             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5789             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5790             "  }",
5791             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5792                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5793                    getGoogleStyle()));
5794 }
5795 
5796 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5797   EXPECT_EQ("{}", format("{}"));
5798   verifyFormat("enum E {};");
5799   verifyFormat("enum E {}");
5800   FormatStyle Style = getLLVMStyle();
5801   Style.SpaceInEmptyBlock = true;
5802   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5803   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5804   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5805   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5806   Style.BraceWrapping.BeforeElse = false;
5807   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5808   verifyFormat("if (a)\n"
5809                "{\n"
5810                "} else if (b)\n"
5811                "{\n"
5812                "} else\n"
5813                "{ }",
5814                Style);
5815   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5816   verifyFormat("if (a) {\n"
5817                "} else if (b) {\n"
5818                "} else {\n"
5819                "}",
5820                Style);
5821   Style.BraceWrapping.BeforeElse = true;
5822   verifyFormat("if (a) { }\n"
5823                "else if (b) { }\n"
5824                "else { }",
5825                Style);
5826 }
5827 
5828 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5829   FormatStyle Style = getLLVMStyle();
5830   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5831   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5832   verifyFormat("FOO_BEGIN\n"
5833                "  FOO_ENTRY\n"
5834                "FOO_END",
5835                Style);
5836   verifyFormat("FOO_BEGIN\n"
5837                "  NESTED_FOO_BEGIN\n"
5838                "    NESTED_FOO_ENTRY\n"
5839                "  NESTED_FOO_END\n"
5840                "FOO_END",
5841                Style);
5842   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5843                "  int x;\n"
5844                "  x = 1;\n"
5845                "FOO_END(Baz)",
5846                Style);
5847 }
5848 
5849 //===----------------------------------------------------------------------===//
5850 // Line break tests.
5851 //===----------------------------------------------------------------------===//
5852 
5853 TEST_F(FormatTest, PreventConfusingIndents) {
5854   verifyFormat(
5855       "void f() {\n"
5856       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5857       "                         parameter, parameter, parameter)),\n"
5858       "                     SecondLongCall(parameter));\n"
5859       "}");
5860   verifyFormat(
5861       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5862       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5863       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5864       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5865   verifyFormat(
5866       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5867       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5868       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5869       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5870   verifyFormat(
5871       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5872       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5873       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5874       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5875   verifyFormat("int a = bbbb && ccc &&\n"
5876                "        fffff(\n"
5877                "#define A Just forcing a new line\n"
5878                "            ddd);");
5879 }
5880 
5881 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5882   verifyFormat(
5883       "bool aaaaaaa =\n"
5884       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5885       "    bbbbbbbb();");
5886   verifyFormat(
5887       "bool aaaaaaa =\n"
5888       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5889       "    bbbbbbbb();");
5890 
5891   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5892                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5893                "    ccccccccc == ddddddddddd;");
5894   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5895                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5896                "    ccccccccc == ddddddddddd;");
5897   verifyFormat(
5898       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5899       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5900       "    ccccccccc == ddddddddddd;");
5901 
5902   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5903                "                 aaaaaa) &&\n"
5904                "         bbbbbb && cccccc;");
5905   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5906                "                 aaaaaa) >>\n"
5907                "         bbbbbb;");
5908   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5909                "    SourceMgr.getSpellingColumnNumber(\n"
5910                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5911                "    1);");
5912 
5913   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5914                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5915                "    cccccc) {\n}");
5916   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5917                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5918                "              cccccc) {\n}");
5919   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5920                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5921                "              cccccc) {\n}");
5922   verifyFormat("b = a &&\n"
5923                "    // Comment\n"
5924                "    b.c && d;");
5925 
5926   // If the LHS of a comparison is not a binary expression itself, the
5927   // additional linebreak confuses many people.
5928   verifyFormat(
5929       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5930       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5931       "}");
5932   verifyFormat(
5933       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5934       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5935       "}");
5936   verifyFormat(
5937       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5938       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5939       "}");
5940   verifyFormat(
5941       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5942       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5943       "}");
5944   // Even explicit parentheses stress the precedence enough to make the
5945   // additional break unnecessary.
5946   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5947                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5948                "}");
5949   // This cases is borderline, but with the indentation it is still readable.
5950   verifyFormat(
5951       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5952       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5953       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5954       "}",
5955       getLLVMStyleWithColumns(75));
5956 
5957   // If the LHS is a binary expression, we should still use the additional break
5958   // as otherwise the formatting hides the operator precedence.
5959   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5960                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5961                "    5) {\n"
5962                "}");
5963   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5964                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5965                "    5) {\n"
5966                "}");
5967 
5968   FormatStyle OnePerLine = getLLVMStyle();
5969   OnePerLine.BinPackParameters = false;
5970   verifyFormat(
5971       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5972       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5973       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5974       OnePerLine);
5975 
5976   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5977                "                .aaa(aaaaaaaaaaaaa) *\n"
5978                "            aaaaaaa +\n"
5979                "        aaaaaaa;",
5980                getLLVMStyleWithColumns(40));
5981 }
5982 
5983 TEST_F(FormatTest, ExpressionIndentation) {
5984   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5985                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5986                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5987                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5988                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5989                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5990                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5991                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5992                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5993   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5994                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5995                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5996                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5997   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5998                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5999                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6000                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6001   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6002                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6003                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6004                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6005   verifyFormat("if () {\n"
6006                "} else if (aaaaa && bbbbb > // break\n"
6007                "                        ccccc) {\n"
6008                "}");
6009   verifyFormat("if () {\n"
6010                "} else if constexpr (aaaaa && bbbbb > // break\n"
6011                "                                  ccccc) {\n"
6012                "}");
6013   verifyFormat("if () {\n"
6014                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
6015                "                                  ccccc) {\n"
6016                "}");
6017   verifyFormat("if () {\n"
6018                "} else if (aaaaa &&\n"
6019                "           bbbbb > // break\n"
6020                "               ccccc &&\n"
6021                "           ddddd) {\n"
6022                "}");
6023 
6024   // Presence of a trailing comment used to change indentation of b.
6025   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6026                "       b;\n"
6027                "return aaaaaaaaaaaaaaaaaaa +\n"
6028                "       b; //",
6029                getLLVMStyleWithColumns(30));
6030 }
6031 
6032 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6033   // Not sure what the best system is here. Like this, the LHS can be found
6034   // immediately above an operator (everything with the same or a higher
6035   // indent). The RHS is aligned right of the operator and so compasses
6036   // everything until something with the same indent as the operator is found.
6037   // FIXME: Is this a good system?
6038   FormatStyle Style = getLLVMStyle();
6039   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6040   verifyFormat(
6041       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6042       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6043       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6044       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6045       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6046       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6047       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6048       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6049       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
6050       Style);
6051   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6052                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6053                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6054                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6055                Style);
6056   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6057                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6058                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6059                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6060                Style);
6061   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6062                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6063                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6064                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6065                Style);
6066   verifyFormat("if () {\n"
6067                "} else if (aaaaa\n"
6068                "           && bbbbb // break\n"
6069                "                  > ccccc) {\n"
6070                "}",
6071                Style);
6072   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6073                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6074                Style);
6075   verifyFormat("return (a)\n"
6076                "       // comment\n"
6077                "       + b;",
6078                Style);
6079   verifyFormat(
6080       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6081       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6082       "             + cc;",
6083       Style);
6084 
6085   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6086                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6087                Style);
6088 
6089   // Forced by comments.
6090   verifyFormat(
6091       "unsigned ContentSize =\n"
6092       "    sizeof(int16_t)   // DWARF ARange version number\n"
6093       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6094       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6095       "    + sizeof(int8_t); // Segment Size (in bytes)");
6096 
6097   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6098                "       == boost::fusion::at_c<1>(iiii).second;",
6099                Style);
6100 
6101   Style.ColumnLimit = 60;
6102   verifyFormat("zzzzzzzzzz\n"
6103                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6104                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6105                Style);
6106 
6107   Style.ColumnLimit = 80;
6108   Style.IndentWidth = 4;
6109   Style.TabWidth = 4;
6110   Style.UseTab = FormatStyle::UT_Always;
6111   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6112   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6113   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6114             "\t&& (someOtherLongishConditionPart1\n"
6115             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6116             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6117                    "(someOtherLongishConditionPart1 || "
6118                    "someOtherEvenLongerNestedConditionPart2);",
6119                    Style));
6120 }
6121 
6122 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6123   FormatStyle Style = getLLVMStyle();
6124   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6125   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6126 
6127   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6128                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6129                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6130                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6131                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6132                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6133                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6134                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6135                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6136                Style);
6137   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6138                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6139                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6140                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6141                Style);
6142   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6143                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6144                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6145                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6146                Style);
6147   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6148                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6149                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6150                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6151                Style);
6152   verifyFormat("if () {\n"
6153                "} else if (aaaaa\n"
6154                "           && bbbbb // break\n"
6155                "                  > ccccc) {\n"
6156                "}",
6157                Style);
6158   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6159                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6160                Style);
6161   verifyFormat("return (a)\n"
6162                "     // comment\n"
6163                "     + b;",
6164                Style);
6165   verifyFormat(
6166       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6167       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6168       "           + cc;",
6169       Style);
6170   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6171                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6172                "                        : 3333333333333333;",
6173                Style);
6174   verifyFormat(
6175       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6176       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6177       "                                             : eeeeeeeeeeeeeeeeee)\n"
6178       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6179       "                        : 3333333333333333;",
6180       Style);
6181   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6182                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6183                Style);
6184 
6185   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6186                "    == boost::fusion::at_c<1>(iiii).second;",
6187                Style);
6188 
6189   Style.ColumnLimit = 60;
6190   verifyFormat("zzzzzzzzzzzzz\n"
6191                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6192                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6193                Style);
6194 
6195   // Forced by comments.
6196   Style.ColumnLimit = 80;
6197   verifyFormat(
6198       "unsigned ContentSize\n"
6199       "    = sizeof(int16_t) // DWARF ARange version number\n"
6200       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6201       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6202       "    + sizeof(int8_t); // Segment Size (in bytes)",
6203       Style);
6204 
6205   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6206   verifyFormat(
6207       "unsigned ContentSize =\n"
6208       "    sizeof(int16_t)   // DWARF ARange version number\n"
6209       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6210       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6211       "    + sizeof(int8_t); // Segment Size (in bytes)",
6212       Style);
6213 
6214   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6215   verifyFormat(
6216       "unsigned ContentSize =\n"
6217       "    sizeof(int16_t)   // DWARF ARange version number\n"
6218       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6219       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6220       "    + sizeof(int8_t); // Segment Size (in bytes)",
6221       Style);
6222 }
6223 
6224 TEST_F(FormatTest, EnforcedOperatorWraps) {
6225   // Here we'd like to wrap after the || operators, but a comment is forcing an
6226   // earlier wrap.
6227   verifyFormat("bool x = aaaaa //\n"
6228                "         || bbbbb\n"
6229                "         //\n"
6230                "         || cccc;");
6231 }
6232 
6233 TEST_F(FormatTest, NoOperandAlignment) {
6234   FormatStyle Style = getLLVMStyle();
6235   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6236   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6237                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6238                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6239                Style);
6240   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6241   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6242                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6243                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6244                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6245                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6246                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6247                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6248                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6249                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6250                Style);
6251 
6252   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6253                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6254                "    + cc;",
6255                Style);
6256   verifyFormat("int a = aa\n"
6257                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6258                "        * cccccccccccccccccccccccccccccccccccc;\n",
6259                Style);
6260 
6261   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6262   verifyFormat("return (a > b\n"
6263                "    // comment1\n"
6264                "    // comment2\n"
6265                "    || c);",
6266                Style);
6267 }
6268 
6269 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6270   FormatStyle Style = getLLVMStyle();
6271   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6272   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6273                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6274                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6275                Style);
6276 }
6277 
6278 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6279   FormatStyle Style = getLLVMStyleWithColumns(40);
6280   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6281   Style.BinPackArguments = false;
6282   verifyFormat("void test() {\n"
6283                "  someFunction(\n"
6284                "      this + argument + is + quite\n"
6285                "      + long + so + it + gets + wrapped\n"
6286                "      + but + remains + bin - packed);\n"
6287                "}",
6288                Style);
6289   verifyFormat("void test() {\n"
6290                "  someFunction(arg1,\n"
6291                "               this + argument + is\n"
6292                "                   + quite + long + so\n"
6293                "                   + it + gets + wrapped\n"
6294                "                   + but + remains + bin\n"
6295                "                   - packed,\n"
6296                "               arg3);\n"
6297                "}",
6298                Style);
6299   verifyFormat("void test() {\n"
6300                "  someFunction(\n"
6301                "      arg1,\n"
6302                "      this + argument + has\n"
6303                "          + anotherFunc(nested,\n"
6304                "                        calls + whose\n"
6305                "                            + arguments\n"
6306                "                            + are + also\n"
6307                "                            + wrapped,\n"
6308                "                        in + addition)\n"
6309                "          + to + being + bin - packed,\n"
6310                "      arg3);\n"
6311                "}",
6312                Style);
6313 
6314   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6315   verifyFormat("void test() {\n"
6316                "  someFunction(\n"
6317                "      arg1,\n"
6318                "      this + argument + has +\n"
6319                "          anotherFunc(nested,\n"
6320                "                      calls + whose +\n"
6321                "                          arguments +\n"
6322                "                          are + also +\n"
6323                "                          wrapped,\n"
6324                "                      in + addition) +\n"
6325                "          to + being + bin - packed,\n"
6326                "      arg3);\n"
6327                "}",
6328                Style);
6329 }
6330 
6331 TEST_F(FormatTest, ConstructorInitializers) {
6332   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6333   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6334                getLLVMStyleWithColumns(45));
6335   verifyFormat("Constructor()\n"
6336                "    : Inttializer(FitsOnTheLine) {}",
6337                getLLVMStyleWithColumns(44));
6338   verifyFormat("Constructor()\n"
6339                "    : Inttializer(FitsOnTheLine) {}",
6340                getLLVMStyleWithColumns(43));
6341 
6342   verifyFormat("template <typename T>\n"
6343                "Constructor() : Initializer(FitsOnTheLine) {}",
6344                getLLVMStyleWithColumns(45));
6345 
6346   verifyFormat(
6347       "SomeClass::Constructor()\n"
6348       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6349 
6350   verifyFormat(
6351       "SomeClass::Constructor()\n"
6352       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6353       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6354   verifyFormat(
6355       "SomeClass::Constructor()\n"
6356       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6357       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6358   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6359                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6360                "    : aaaaaaaaaa(aaaaaa) {}");
6361 
6362   verifyFormat("Constructor()\n"
6363                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6364                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6365                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6366                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6367 
6368   verifyFormat("Constructor()\n"
6369                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6370                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6371 
6372   verifyFormat("Constructor(int Parameter = 0)\n"
6373                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6374                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6375   verifyFormat("Constructor()\n"
6376                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6377                "}",
6378                getLLVMStyleWithColumns(60));
6379   verifyFormat("Constructor()\n"
6380                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6381                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6382 
6383   // Here a line could be saved by splitting the second initializer onto two
6384   // lines, but that is not desirable.
6385   verifyFormat("Constructor()\n"
6386                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6387                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6388                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6389 
6390   FormatStyle OnePerLine = getLLVMStyle();
6391   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6392   verifyFormat("MyClass::MyClass()\n"
6393                "    : a(a),\n"
6394                "      b(b),\n"
6395                "      c(c) {}",
6396                OnePerLine);
6397   verifyFormat("MyClass::MyClass()\n"
6398                "    : a(a), // comment\n"
6399                "      b(b),\n"
6400                "      c(c) {}",
6401                OnePerLine);
6402   verifyFormat("MyClass::MyClass(int a)\n"
6403                "    : b(a),      // comment\n"
6404                "      c(a + 1) { // lined up\n"
6405                "}",
6406                OnePerLine);
6407   verifyFormat("Constructor()\n"
6408                "    : a(b, b, b) {}",
6409                OnePerLine);
6410   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6411   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6412   verifyFormat("SomeClass::Constructor()\n"
6413                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6414                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6415                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6416                OnePerLine);
6417   verifyFormat("SomeClass::Constructor()\n"
6418                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6419                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6420                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6421                OnePerLine);
6422   verifyFormat("MyClass::MyClass(int var)\n"
6423                "    : some_var_(var),            // 4 space indent\n"
6424                "      some_other_var_(var + 1) { // lined up\n"
6425                "}",
6426                OnePerLine);
6427   verifyFormat("Constructor()\n"
6428                "    : aaaaa(aaaaaa),\n"
6429                "      aaaaa(aaaaaa),\n"
6430                "      aaaaa(aaaaaa),\n"
6431                "      aaaaa(aaaaaa),\n"
6432                "      aaaaa(aaaaaa) {}",
6433                OnePerLine);
6434   verifyFormat("Constructor()\n"
6435                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6436                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6437                OnePerLine);
6438   OnePerLine.BinPackParameters = false;
6439   verifyFormat(
6440       "Constructor()\n"
6441       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6442       "          aaaaaaaaaaa().aaa(),\n"
6443       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6444       OnePerLine);
6445   OnePerLine.ColumnLimit = 60;
6446   verifyFormat("Constructor()\n"
6447                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6448                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6449                OnePerLine);
6450 
6451   EXPECT_EQ("Constructor()\n"
6452             "    : // Comment forcing unwanted break.\n"
6453             "      aaaa(aaaa) {}",
6454             format("Constructor() :\n"
6455                    "    // Comment forcing unwanted break.\n"
6456                    "    aaaa(aaaa) {}"));
6457 }
6458 
6459 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6460   FormatStyle Style = getLLVMStyleWithColumns(60);
6461   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6462   Style.BinPackParameters = false;
6463 
6464   for (int i = 0; i < 4; ++i) {
6465     // Test all combinations of parameters that should not have an effect.
6466     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6467     Style.AllowAllArgumentsOnNextLine = i & 2;
6468 
6469     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6470     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6471     verifyFormat("Constructor()\n"
6472                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6473                  Style);
6474     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6475 
6476     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6477     verifyFormat("Constructor()\n"
6478                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6479                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6480                  Style);
6481     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6482 
6483     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6484     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6485     verifyFormat("Constructor()\n"
6486                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6487                  Style);
6488 
6489     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6490     verifyFormat("Constructor()\n"
6491                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6492                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6493                  Style);
6494 
6495     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6496     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6497     verifyFormat("Constructor() :\n"
6498                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6499                  Style);
6500 
6501     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6502     verifyFormat("Constructor() :\n"
6503                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6504                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6505                  Style);
6506   }
6507 
6508   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6509   // AllowAllConstructorInitializersOnNextLine in all
6510   // BreakConstructorInitializers modes
6511   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6512   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6513   verifyFormat("SomeClassWithALongName::Constructor(\n"
6514                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6515                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6516                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6517                Style);
6518 
6519   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6520   verifyFormat("SomeClassWithALongName::Constructor(\n"
6521                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6522                "    int bbbbbbbbbbbbb,\n"
6523                "    int cccccccccccccccc)\n"
6524                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6525                Style);
6526 
6527   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6528   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6529   verifyFormat("SomeClassWithALongName::Constructor(\n"
6530                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6531                "    int bbbbbbbbbbbbb)\n"
6532                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6533                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6534                Style);
6535 
6536   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6537 
6538   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6539   verifyFormat("SomeClassWithALongName::Constructor(\n"
6540                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6541                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6542                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6543                Style);
6544 
6545   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6546   verifyFormat("SomeClassWithALongName::Constructor(\n"
6547                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6548                "    int bbbbbbbbbbbbb,\n"
6549                "    int cccccccccccccccc)\n"
6550                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6551                Style);
6552 
6553   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6554   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6555   verifyFormat("SomeClassWithALongName::Constructor(\n"
6556                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6557                "    int bbbbbbbbbbbbb)\n"
6558                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6559                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6560                Style);
6561 
6562   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6563   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6564   verifyFormat("SomeClassWithALongName::Constructor(\n"
6565                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6566                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6567                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6568                Style);
6569 
6570   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6571   verifyFormat("SomeClassWithALongName::Constructor(\n"
6572                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6573                "    int bbbbbbbbbbbbb,\n"
6574                "    int cccccccccccccccc) :\n"
6575                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6576                Style);
6577 
6578   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6579   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6580   verifyFormat("SomeClassWithALongName::Constructor(\n"
6581                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6582                "    int bbbbbbbbbbbbb) :\n"
6583                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6584                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6585                Style);
6586 }
6587 
6588 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6589   FormatStyle Style = getLLVMStyleWithColumns(60);
6590   Style.BinPackArguments = false;
6591   for (int i = 0; i < 4; ++i) {
6592     // Test all combinations of parameters that should not have an effect.
6593     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6594     Style.PackConstructorInitializers =
6595         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6596 
6597     Style.AllowAllArgumentsOnNextLine = true;
6598     verifyFormat("void foo() {\n"
6599                  "  FunctionCallWithReallyLongName(\n"
6600                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6601                  "}",
6602                  Style);
6603     Style.AllowAllArgumentsOnNextLine = false;
6604     verifyFormat("void foo() {\n"
6605                  "  FunctionCallWithReallyLongName(\n"
6606                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6607                  "      bbbbbbbbbbbb);\n"
6608                  "}",
6609                  Style);
6610 
6611     Style.AllowAllArgumentsOnNextLine = true;
6612     verifyFormat("void foo() {\n"
6613                  "  auto VariableWithReallyLongName = {\n"
6614                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6615                  "}",
6616                  Style);
6617     Style.AllowAllArgumentsOnNextLine = false;
6618     verifyFormat("void foo() {\n"
6619                  "  auto VariableWithReallyLongName = {\n"
6620                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6621                  "      bbbbbbbbbbbb};\n"
6622                  "}",
6623                  Style);
6624   }
6625 
6626   // This parameter should not affect declarations.
6627   Style.BinPackParameters = false;
6628   Style.AllowAllArgumentsOnNextLine = false;
6629   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6630   verifyFormat("void FunctionCallWithReallyLongName(\n"
6631                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6632                Style);
6633   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6634   verifyFormat("void FunctionCallWithReallyLongName(\n"
6635                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6636                "    int bbbbbbbbbbbb);",
6637                Style);
6638 }
6639 
6640 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6641   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6642   // and BAS_Align.
6643   FormatStyle Style = getLLVMStyleWithColumns(35);
6644   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6645                     "void functionDecl(int A, int B, int C);";
6646   Style.AllowAllArgumentsOnNextLine = false;
6647   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6648   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6649                       "    paramC);\n"
6650                       "void functionDecl(int A, int B,\n"
6651                       "    int C);"),
6652             format(Input, Style));
6653   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6654   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6655                       "             paramC);\n"
6656                       "void functionDecl(int A, int B,\n"
6657                       "                  int C);"),
6658             format(Input, Style));
6659   // However, BAS_AlwaysBreak should take precedence over
6660   // AllowAllArgumentsOnNextLine.
6661   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6662   EXPECT_EQ(StringRef("functionCall(\n"
6663                       "    paramA, paramB, paramC);\n"
6664                       "void functionDecl(\n"
6665                       "    int A, int B, int C);"),
6666             format(Input, Style));
6667 
6668   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6669   // first argument.
6670   Style.AllowAllArgumentsOnNextLine = true;
6671   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6672   EXPECT_EQ(StringRef("functionCall(\n"
6673                       "    paramA, paramB, paramC);\n"
6674                       "void functionDecl(\n"
6675                       "    int A, int B, int C);"),
6676             format(Input, Style));
6677   // It wouldn't fit on one line with aligned parameters so this setting
6678   // doesn't change anything for BAS_Align.
6679   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6680   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6681                       "             paramC);\n"
6682                       "void functionDecl(int A, int B,\n"
6683                       "                  int C);"),
6684             format(Input, Style));
6685   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6686   EXPECT_EQ(StringRef("functionCall(\n"
6687                       "    paramA, paramB, paramC);\n"
6688                       "void functionDecl(\n"
6689                       "    int A, int B, int C);"),
6690             format(Input, Style));
6691 }
6692 
6693 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6694   FormatStyle Style = getLLVMStyle();
6695   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6696 
6697   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6698   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6699                getStyleWithColumns(Style, 45));
6700   verifyFormat("Constructor() :\n"
6701                "    Initializer(FitsOnTheLine) {}",
6702                getStyleWithColumns(Style, 44));
6703   verifyFormat("Constructor() :\n"
6704                "    Initializer(FitsOnTheLine) {}",
6705                getStyleWithColumns(Style, 43));
6706 
6707   verifyFormat("template <typename T>\n"
6708                "Constructor() : Initializer(FitsOnTheLine) {}",
6709                getStyleWithColumns(Style, 50));
6710   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6711   verifyFormat(
6712       "SomeClass::Constructor() :\n"
6713       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6714       Style);
6715 
6716   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6717   verifyFormat(
6718       "SomeClass::Constructor() :\n"
6719       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6720       Style);
6721 
6722   verifyFormat(
6723       "SomeClass::Constructor() :\n"
6724       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6725       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6726       Style);
6727   verifyFormat(
6728       "SomeClass::Constructor() :\n"
6729       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6730       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6731       Style);
6732   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6733                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6734                "    aaaaaaaaaa(aaaaaa) {}",
6735                Style);
6736 
6737   verifyFormat("Constructor() :\n"
6738                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6739                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6740                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6741                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6742                Style);
6743 
6744   verifyFormat("Constructor() :\n"
6745                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6746                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6747                Style);
6748 
6749   verifyFormat("Constructor(int Parameter = 0) :\n"
6750                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6751                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6752                Style);
6753   verifyFormat("Constructor() :\n"
6754                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6755                "}",
6756                getStyleWithColumns(Style, 60));
6757   verifyFormat("Constructor() :\n"
6758                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6759                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6760                Style);
6761 
6762   // Here a line could be saved by splitting the second initializer onto two
6763   // lines, but that is not desirable.
6764   verifyFormat("Constructor() :\n"
6765                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6766                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6767                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6768                Style);
6769 
6770   FormatStyle OnePerLine = Style;
6771   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6772   verifyFormat("SomeClass::Constructor() :\n"
6773                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6774                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6775                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6776                OnePerLine);
6777   verifyFormat("SomeClass::Constructor() :\n"
6778                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6779                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6780                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6781                OnePerLine);
6782   verifyFormat("MyClass::MyClass(int var) :\n"
6783                "    some_var_(var),            // 4 space indent\n"
6784                "    some_other_var_(var + 1) { // lined up\n"
6785                "}",
6786                OnePerLine);
6787   verifyFormat("Constructor() :\n"
6788                "    aaaaa(aaaaaa),\n"
6789                "    aaaaa(aaaaaa),\n"
6790                "    aaaaa(aaaaaa),\n"
6791                "    aaaaa(aaaaaa),\n"
6792                "    aaaaa(aaaaaa) {}",
6793                OnePerLine);
6794   verifyFormat("Constructor() :\n"
6795                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6796                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6797                OnePerLine);
6798   OnePerLine.BinPackParameters = false;
6799   verifyFormat("Constructor() :\n"
6800                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6801                "        aaaaaaaaaaa().aaa(),\n"
6802                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6803                OnePerLine);
6804   OnePerLine.ColumnLimit = 60;
6805   verifyFormat("Constructor() :\n"
6806                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6807                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6808                OnePerLine);
6809 
6810   EXPECT_EQ("Constructor() :\n"
6811             "    // Comment forcing unwanted break.\n"
6812             "    aaaa(aaaa) {}",
6813             format("Constructor() :\n"
6814                    "    // Comment forcing unwanted break.\n"
6815                    "    aaaa(aaaa) {}",
6816                    Style));
6817 
6818   Style.ColumnLimit = 0;
6819   verifyFormat("SomeClass::Constructor() :\n"
6820                "    a(a) {}",
6821                Style);
6822   verifyFormat("SomeClass::Constructor() noexcept :\n"
6823                "    a(a) {}",
6824                Style);
6825   verifyFormat("SomeClass::Constructor() :\n"
6826                "    a(a), b(b), c(c) {}",
6827                Style);
6828   verifyFormat("SomeClass::Constructor() :\n"
6829                "    a(a) {\n"
6830                "  foo();\n"
6831                "  bar();\n"
6832                "}",
6833                Style);
6834 
6835   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6836   verifyFormat("SomeClass::Constructor() :\n"
6837                "    a(a), b(b), c(c) {\n"
6838                "}",
6839                Style);
6840   verifyFormat("SomeClass::Constructor() :\n"
6841                "    a(a) {\n"
6842                "}",
6843                Style);
6844 
6845   Style.ColumnLimit = 80;
6846   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6847   Style.ConstructorInitializerIndentWidth = 2;
6848   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6849   verifyFormat("SomeClass::Constructor() :\n"
6850                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6851                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6852                Style);
6853 
6854   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6855   // well
6856   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6857   verifyFormat(
6858       "class SomeClass\n"
6859       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6860       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6861       Style);
6862   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6863   verifyFormat(
6864       "class SomeClass\n"
6865       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6866       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6867       Style);
6868   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6869   verifyFormat(
6870       "class SomeClass :\n"
6871       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6872       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6873       Style);
6874   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6875   verifyFormat(
6876       "class SomeClass\n"
6877       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6878       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6879       Style);
6880 }
6881 
6882 #ifndef EXPENSIVE_CHECKS
6883 // Expensive checks enables libstdc++ checking which includes validating the
6884 // state of ranges used in std::priority_queue - this blows out the
6885 // runtime/scalability of the function and makes this test unacceptably slow.
6886 TEST_F(FormatTest, MemoizationTests) {
6887   // This breaks if the memoization lookup does not take \c Indent and
6888   // \c LastSpace into account.
6889   verifyFormat(
6890       "extern CFRunLoopTimerRef\n"
6891       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6892       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6893       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6894       "                     CFRunLoopTimerContext *context) {}");
6895 
6896   // Deep nesting somewhat works around our memoization.
6897   verifyFormat(
6898       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6899       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6900       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6901       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6902       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6903       getLLVMStyleWithColumns(65));
6904   verifyFormat(
6905       "aaaaa(\n"
6906       "    aaaaa,\n"
6907       "    aaaaa(\n"
6908       "        aaaaa,\n"
6909       "        aaaaa(\n"
6910       "            aaaaa,\n"
6911       "            aaaaa(\n"
6912       "                aaaaa,\n"
6913       "                aaaaa(\n"
6914       "                    aaaaa,\n"
6915       "                    aaaaa(\n"
6916       "                        aaaaa,\n"
6917       "                        aaaaa(\n"
6918       "                            aaaaa,\n"
6919       "                            aaaaa(\n"
6920       "                                aaaaa,\n"
6921       "                                aaaaa(\n"
6922       "                                    aaaaa,\n"
6923       "                                    aaaaa(\n"
6924       "                                        aaaaa,\n"
6925       "                                        aaaaa(\n"
6926       "                                            aaaaa,\n"
6927       "                                            aaaaa(\n"
6928       "                                                aaaaa,\n"
6929       "                                                aaaaa))))))))))));",
6930       getLLVMStyleWithColumns(65));
6931   verifyFormat(
6932       "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"
6933       "                                  a),\n"
6934       "                                a),\n"
6935       "                              a),\n"
6936       "                            a),\n"
6937       "                          a),\n"
6938       "                        a),\n"
6939       "                      a),\n"
6940       "                    a),\n"
6941       "                  a),\n"
6942       "                a),\n"
6943       "              a),\n"
6944       "            a),\n"
6945       "          a),\n"
6946       "        a),\n"
6947       "      a),\n"
6948       "    a),\n"
6949       "  a)",
6950       getLLVMStyleWithColumns(65));
6951 
6952   // This test takes VERY long when memoization is broken.
6953   FormatStyle OnePerLine = getLLVMStyle();
6954   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6955   OnePerLine.BinPackParameters = false;
6956   std::string input = "Constructor()\n"
6957                       "    : aaaa(a,\n";
6958   for (unsigned i = 0, e = 80; i != e; ++i) {
6959     input += "           a,\n";
6960   }
6961   input += "           a) {}";
6962   verifyFormat(input, OnePerLine);
6963 }
6964 #endif
6965 
6966 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6967   verifyFormat(
6968       "void f() {\n"
6969       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6970       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6971       "    f();\n"
6972       "}");
6973   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6974                "    Intervals[i - 1].getRange().getLast()) {\n}");
6975 }
6976 
6977 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6978   // Principially, we break function declarations in a certain order:
6979   // 1) break amongst arguments.
6980   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6981                "                              Cccccccccccccc cccccccccccccc);");
6982   verifyFormat("template <class TemplateIt>\n"
6983                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6984                "                            TemplateIt *stop) {}");
6985 
6986   // 2) break after return type.
6987   verifyFormat(
6988       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6989       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6990       getGoogleStyle());
6991 
6992   // 3) break after (.
6993   verifyFormat(
6994       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6995       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6996       getGoogleStyle());
6997 
6998   // 4) break before after nested name specifiers.
6999   verifyFormat(
7000       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7001       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
7002       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
7003       getGoogleStyle());
7004 
7005   // However, there are exceptions, if a sufficient amount of lines can be
7006   // saved.
7007   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
7008   // more adjusting.
7009   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7010                "                                  Cccccccccccccc cccccccccc,\n"
7011                "                                  Cccccccccccccc cccccccccc,\n"
7012                "                                  Cccccccccccccc cccccccccc,\n"
7013                "                                  Cccccccccccccc cccccccccc);");
7014   verifyFormat(
7015       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7016       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7017       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7018       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
7019       getGoogleStyle());
7020   verifyFormat(
7021       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7022       "                                          Cccccccccccccc cccccccccc,\n"
7023       "                                          Cccccccccccccc cccccccccc,\n"
7024       "                                          Cccccccccccccc cccccccccc,\n"
7025       "                                          Cccccccccccccc cccccccccc,\n"
7026       "                                          Cccccccccccccc cccccccccc,\n"
7027       "                                          Cccccccccccccc cccccccccc);");
7028   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7029                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7030                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7031                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7032                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7033 
7034   // Break after multi-line parameters.
7035   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7036                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7037                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7038                "    bbbb bbbb);");
7039   verifyFormat("void SomeLoooooooooooongFunction(\n"
7040                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7041                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7042                "    int bbbbbbbbbbbbb);");
7043 
7044   // Treat overloaded operators like other functions.
7045   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7046                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7047   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7048                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7049   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7050                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7051   verifyGoogleFormat(
7052       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7053       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7054   verifyGoogleFormat(
7055       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7056       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7057   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7058                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7059   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7060                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7061   verifyGoogleFormat(
7062       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7063       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7064       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7065   verifyGoogleFormat("template <typename T>\n"
7066                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7067                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7068                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7069 
7070   FormatStyle Style = getLLVMStyle();
7071   Style.PointerAlignment = FormatStyle::PAS_Left;
7072   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7073                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7074                Style);
7075   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7076                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7077                Style);
7078 }
7079 
7080 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7081   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7082   // Prefer keeping `::` followed by `operator` together.
7083   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7084             "ccccccccc::operator++() {\n"
7085             "  stuff();\n"
7086             "}",
7087             format("const aaaa::bbbbbbb\n"
7088                    "&ccccccccc::operator++() { stuff(); }",
7089                    getLLVMStyleWithColumns(40)));
7090 }
7091 
7092 TEST_F(FormatTest, TrailingReturnType) {
7093   verifyFormat("auto foo() -> int;\n");
7094   // correct trailing return type spacing
7095   verifyFormat("auto operator->() -> int;\n");
7096   verifyFormat("auto operator++(int) -> int;\n");
7097 
7098   verifyFormat("struct S {\n"
7099                "  auto bar() const -> int;\n"
7100                "};");
7101   verifyFormat("template <size_t Order, typename T>\n"
7102                "auto load_img(const std::string &filename)\n"
7103                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7104   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7105                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7106   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7107   verifyFormat("template <typename T>\n"
7108                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7109                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7110 
7111   // Not trailing return types.
7112   verifyFormat("void f() { auto a = b->c(); }");
7113   verifyFormat("auto a = p->foo();");
7114   verifyFormat("int a = p->foo();");
7115   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7116 }
7117 
7118 TEST_F(FormatTest, DeductionGuides) {
7119   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7120   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7121   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7122   verifyFormat(
7123       "template <class... T>\n"
7124       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7125   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7126   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7127   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7128   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7129   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7130   verifyFormat("template <class T> x() -> x<1>;");
7131   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7132 
7133   // Ensure not deduction guides.
7134   verifyFormat("c()->f<int>();");
7135   verifyFormat("x()->foo<1>;");
7136   verifyFormat("x = p->foo<3>();");
7137   verifyFormat("x()->x<1>();");
7138   verifyFormat("x()->x<1>;");
7139 }
7140 
7141 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7142   // Avoid breaking before trailing 'const' or other trailing annotations, if
7143   // they are not function-like.
7144   FormatStyle Style = getGoogleStyleWithColumns(47);
7145   verifyFormat("void someLongFunction(\n"
7146                "    int someLoooooooooooooongParameter) const {\n}",
7147                getLLVMStyleWithColumns(47));
7148   verifyFormat("LoooooongReturnType\n"
7149                "someLoooooooongFunction() const {}",
7150                getLLVMStyleWithColumns(47));
7151   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7152                "    const {}",
7153                Style);
7154   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7155                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7156   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7157                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7158   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7159                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7160   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7161                "                   aaaaaaaaaaa aaaaa) const override;");
7162   verifyGoogleFormat(
7163       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7164       "    const override;");
7165 
7166   // Even if the first parameter has to be wrapped.
7167   verifyFormat("void someLongFunction(\n"
7168                "    int someLongParameter) const {}",
7169                getLLVMStyleWithColumns(46));
7170   verifyFormat("void someLongFunction(\n"
7171                "    int someLongParameter) const {}",
7172                Style);
7173   verifyFormat("void someLongFunction(\n"
7174                "    int someLongParameter) override {}",
7175                Style);
7176   verifyFormat("void someLongFunction(\n"
7177                "    int someLongParameter) OVERRIDE {}",
7178                Style);
7179   verifyFormat("void someLongFunction(\n"
7180                "    int someLongParameter) final {}",
7181                Style);
7182   verifyFormat("void someLongFunction(\n"
7183                "    int someLongParameter) FINAL {}",
7184                Style);
7185   verifyFormat("void someLongFunction(\n"
7186                "    int parameter) const override {}",
7187                Style);
7188 
7189   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7190   verifyFormat("void someLongFunction(\n"
7191                "    int someLongParameter) const\n"
7192                "{\n"
7193                "}",
7194                Style);
7195 
7196   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7197   verifyFormat("void someLongFunction(\n"
7198                "    int someLongParameter) const\n"
7199                "  {\n"
7200                "  }",
7201                Style);
7202 
7203   // Unless these are unknown annotations.
7204   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7205                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7206                "    LONG_AND_UGLY_ANNOTATION;");
7207 
7208   // Breaking before function-like trailing annotations is fine to keep them
7209   // close to their arguments.
7210   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7211                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7212   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7213                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7214   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7215                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7216   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7217                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7218   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7219 
7220   verifyFormat(
7221       "void aaaaaaaaaaaaaaaaaa()\n"
7222       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7223       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7224   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7225                "    __attribute__((unused));");
7226   verifyGoogleFormat(
7227       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7228       "    GUARDED_BY(aaaaaaaaaaaa);");
7229   verifyGoogleFormat(
7230       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7231       "    GUARDED_BY(aaaaaaaaaaaa);");
7232   verifyGoogleFormat(
7233       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7234       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7235   verifyGoogleFormat(
7236       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7237       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7238 }
7239 
7240 TEST_F(FormatTest, FunctionAnnotations) {
7241   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7242                "int OldFunction(const string &parameter) {}");
7243   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7244                "string OldFunction(const string &parameter) {}");
7245   verifyFormat("template <typename T>\n"
7246                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7247                "string OldFunction(const string &parameter) {}");
7248 
7249   // Not function annotations.
7250   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7251                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7252   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7253                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7254   verifyFormat("MACRO(abc).function() // wrap\n"
7255                "    << abc;");
7256   verifyFormat("MACRO(abc)->function() // wrap\n"
7257                "    << abc;");
7258   verifyFormat("MACRO(abc)::function() // wrap\n"
7259                "    << abc;");
7260 }
7261 
7262 TEST_F(FormatTest, BreaksDesireably) {
7263   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7264                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7265                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7266   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7267                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7268                "}");
7269 
7270   verifyFormat(
7271       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7272       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7273 
7274   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7275                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7276                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7277 
7278   verifyFormat(
7279       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7280       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7281       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7282       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7283       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7284 
7285   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7286                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7287 
7288   verifyFormat(
7289       "void f() {\n"
7290       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7291       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7292       "}");
7293   verifyFormat(
7294       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7295       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7296   verifyFormat(
7297       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7298       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7299   verifyFormat(
7300       "aaaaaa(aaa,\n"
7301       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7302       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7303       "       aaaa);");
7304   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7305                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7306                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7307 
7308   // Indent consistently independent of call expression and unary operator.
7309   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7310                "    dddddddddddddddddddddddddddddd));");
7311   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7312                "    dddddddddddddddddddddddddddddd));");
7313   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7314                "    dddddddddddddddddddddddddddddd));");
7315 
7316   // This test case breaks on an incorrect memoization, i.e. an optimization not
7317   // taking into account the StopAt value.
7318   verifyFormat(
7319       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7320       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7321       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7322       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7323 
7324   verifyFormat("{\n  {\n    {\n"
7325                "      Annotation.SpaceRequiredBefore =\n"
7326                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7327                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7328                "    }\n  }\n}");
7329 
7330   // Break on an outer level if there was a break on an inner level.
7331   EXPECT_EQ("f(g(h(a, // comment\n"
7332             "      b, c),\n"
7333             "    d, e),\n"
7334             "  x, y);",
7335             format("f(g(h(a, // comment\n"
7336                    "    b, c), d, e), x, y);"));
7337 
7338   // Prefer breaking similar line breaks.
7339   verifyFormat(
7340       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7341       "                             NSTrackingMouseEnteredAndExited |\n"
7342       "                             NSTrackingActiveAlways;");
7343 }
7344 
7345 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7346   FormatStyle NoBinPacking = getGoogleStyle();
7347   NoBinPacking.BinPackParameters = false;
7348   NoBinPacking.BinPackArguments = true;
7349   verifyFormat("void f() {\n"
7350                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7351                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7352                "}",
7353                NoBinPacking);
7354   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7355                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7356                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7357                NoBinPacking);
7358 
7359   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7360   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7361                "                        vector<int> bbbbbbbbbbbbbbb);",
7362                NoBinPacking);
7363   // FIXME: This behavior difference is probably not wanted. However, currently
7364   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7365   // template arguments from BreakBeforeParameter being set because of the
7366   // one-per-line formatting.
7367   verifyFormat(
7368       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7369       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7370       NoBinPacking);
7371   verifyFormat(
7372       "void fffffffffff(\n"
7373       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7374       "        aaaaaaaaaa);");
7375 }
7376 
7377 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7378   FormatStyle NoBinPacking = getGoogleStyle();
7379   NoBinPacking.BinPackParameters = false;
7380   NoBinPacking.BinPackArguments = false;
7381   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7382                "  aaaaaaaaaaaaaaaaaaaa,\n"
7383                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7384                NoBinPacking);
7385   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7386                "        aaaaaaaaaaaaa,\n"
7387                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7388                NoBinPacking);
7389   verifyFormat(
7390       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7391       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7392       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7393       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7394       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7395       NoBinPacking);
7396   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7397                "    .aaaaaaaaaaaaaaaaaa();",
7398                NoBinPacking);
7399   verifyFormat("void f() {\n"
7400                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7401                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7402                "}",
7403                NoBinPacking);
7404 
7405   verifyFormat(
7406       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7407       "             aaaaaaaaaaaa,\n"
7408       "             aaaaaaaaaaaa);",
7409       NoBinPacking);
7410   verifyFormat(
7411       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7412       "                               ddddddddddddddddddddddddddddd),\n"
7413       "             test);",
7414       NoBinPacking);
7415 
7416   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7417                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7418                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7419                "    aaaaaaaaaaaaaaaaaa;",
7420                NoBinPacking);
7421   verifyFormat("a(\"a\"\n"
7422                "  \"a\",\n"
7423                "  a);");
7424 
7425   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7426   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7427                "                aaaaaaaaa,\n"
7428                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7429                NoBinPacking);
7430   verifyFormat(
7431       "void f() {\n"
7432       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7433       "      .aaaaaaa();\n"
7434       "}",
7435       NoBinPacking);
7436   verifyFormat(
7437       "template <class SomeType, class SomeOtherType>\n"
7438       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7439       NoBinPacking);
7440 }
7441 
7442 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7443   FormatStyle Style = getLLVMStyleWithColumns(15);
7444   Style.ExperimentalAutoDetectBinPacking = true;
7445   EXPECT_EQ("aaa(aaaa,\n"
7446             "    aaaa,\n"
7447             "    aaaa);\n"
7448             "aaa(aaaa,\n"
7449             "    aaaa,\n"
7450             "    aaaa);",
7451             format("aaa(aaaa,\n" // one-per-line
7452                    "  aaaa,\n"
7453                    "    aaaa  );\n"
7454                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7455                    Style));
7456   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7457             "    aaaa);\n"
7458             "aaa(aaaa, aaaa,\n"
7459             "    aaaa);",
7460             format("aaa(aaaa,  aaaa,\n" // bin-packed
7461                    "    aaaa  );\n"
7462                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7463                    Style));
7464 }
7465 
7466 TEST_F(FormatTest, FormatsBuilderPattern) {
7467   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7468                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7469                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7470                "    .StartsWith(\".init\", ORDER_INIT)\n"
7471                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7472                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7473                "    .Default(ORDER_TEXT);\n");
7474 
7475   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7476                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7477   verifyFormat("aaaaaaa->aaaaaaa\n"
7478                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7479                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7480                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7481   verifyFormat(
7482       "aaaaaaa->aaaaaaa\n"
7483       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7484       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7485   verifyFormat(
7486       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7487       "    aaaaaaaaaaaaaa);");
7488   verifyFormat(
7489       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7490       "    aaaaaa->aaaaaaaaaaaa()\n"
7491       "        ->aaaaaaaaaaaaaaaa(\n"
7492       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7493       "        ->aaaaaaaaaaaaaaaaa();");
7494   verifyGoogleFormat(
7495       "void f() {\n"
7496       "  someo->Add((new util::filetools::Handler(dir))\n"
7497       "                 ->OnEvent1(NewPermanentCallback(\n"
7498       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7499       "                 ->OnEvent2(NewPermanentCallback(\n"
7500       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7501       "                 ->OnEvent3(NewPermanentCallback(\n"
7502       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7503       "                 ->OnEvent5(NewPermanentCallback(\n"
7504       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7505       "                 ->OnEvent6(NewPermanentCallback(\n"
7506       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7507       "}");
7508 
7509   verifyFormat(
7510       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7511   verifyFormat("aaaaaaaaaaaaaaa()\n"
7512                "    .aaaaaaaaaaaaaaa()\n"
7513                "    .aaaaaaaaaaaaaaa()\n"
7514                "    .aaaaaaaaaaaaaaa()\n"
7515                "    .aaaaaaaaaaaaaaa();");
7516   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7517                "    .aaaaaaaaaaaaaaa()\n"
7518                "    .aaaaaaaaaaaaaaa()\n"
7519                "    .aaaaaaaaaaaaaaa();");
7520   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7521                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7522                "    .aaaaaaaaaaaaaaa();");
7523   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7524                "    ->aaaaaaaaaaaaaae(0)\n"
7525                "    ->aaaaaaaaaaaaaaa();");
7526 
7527   // Don't linewrap after very short segments.
7528   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7529                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7530                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7531   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7532                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7533                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7534   verifyFormat("aaa()\n"
7535                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7536                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7537                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7538 
7539   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7540                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7541                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7542   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7543                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7544                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7545 
7546   // Prefer not to break after empty parentheses.
7547   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7548                "    First->LastNewlineOffset);");
7549 
7550   // Prefer not to create "hanging" indents.
7551   verifyFormat(
7552       "return !soooooooooooooome_map\n"
7553       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7554       "            .second;");
7555   verifyFormat(
7556       "return aaaaaaaaaaaaaaaa\n"
7557       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7558       "    .aaaa(aaaaaaaaaaaaaa);");
7559   // No hanging indent here.
7560   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7561                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7562   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7563                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7564   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7565                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7566                getLLVMStyleWithColumns(60));
7567   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7568                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7569                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7570                getLLVMStyleWithColumns(59));
7571   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7572                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7573                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7574 
7575   // Dont break if only closing statements before member call
7576   verifyFormat("test() {\n"
7577                "  ([]() -> {\n"
7578                "    int b = 32;\n"
7579                "    return 3;\n"
7580                "  }).foo();\n"
7581                "}");
7582   verifyFormat("test() {\n"
7583                "  (\n"
7584                "      []() -> {\n"
7585                "        int b = 32;\n"
7586                "        return 3;\n"
7587                "      },\n"
7588                "      foo, bar)\n"
7589                "      .foo();\n"
7590                "}");
7591   verifyFormat("test() {\n"
7592                "  ([]() -> {\n"
7593                "    int b = 32;\n"
7594                "    return 3;\n"
7595                "  })\n"
7596                "      .foo()\n"
7597                "      .bar();\n"
7598                "}");
7599   verifyFormat("test() {\n"
7600                "  ([]() -> {\n"
7601                "    int b = 32;\n"
7602                "    return 3;\n"
7603                "  })\n"
7604                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7605                "           \"bbbb\");\n"
7606                "}",
7607                getLLVMStyleWithColumns(30));
7608 }
7609 
7610 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7611   verifyFormat(
7612       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7613       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7614   verifyFormat(
7615       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7616       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7617 
7618   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7619                "    ccccccccccccccccccccccccc) {\n}");
7620   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7621                "    ccccccccccccccccccccccccc) {\n}");
7622 
7623   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7624                "    ccccccccccccccccccccccccc) {\n}");
7625   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7626                "    ccccccccccccccccccccccccc) {\n}");
7627 
7628   verifyFormat(
7629       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7630       "    ccccccccccccccccccccccccc) {\n}");
7631   verifyFormat(
7632       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7633       "    ccccccccccccccccccccccccc) {\n}");
7634 
7635   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7636                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7637                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7638                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7639   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7640                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7641                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7642                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7643 
7644   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7645                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7646                "    aaaaaaaaaaaaaaa != aa) {\n}");
7647   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7648                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7649                "    aaaaaaaaaaaaaaa != aa) {\n}");
7650 }
7651 
7652 TEST_F(FormatTest, BreaksAfterAssignments) {
7653   verifyFormat(
7654       "unsigned Cost =\n"
7655       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7656       "                        SI->getPointerAddressSpaceee());\n");
7657   verifyFormat(
7658       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7659       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7660 
7661   verifyFormat(
7662       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7663       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7664   verifyFormat("unsigned OriginalStartColumn =\n"
7665                "    SourceMgr.getSpellingColumnNumber(\n"
7666                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7667                "    1;");
7668 }
7669 
7670 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7671   FormatStyle Style = getLLVMStyle();
7672   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7673                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7674                Style);
7675 
7676   Style.PenaltyBreakAssignment = 20;
7677   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7678                "                                 cccccccccccccccccccccccccc;",
7679                Style);
7680 }
7681 
7682 TEST_F(FormatTest, AlignsAfterAssignments) {
7683   verifyFormat(
7684       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7685       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7686   verifyFormat(
7687       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7688       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7689   verifyFormat(
7690       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7691       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7692   verifyFormat(
7693       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7694       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7695   verifyFormat(
7696       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7697       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7698       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7699 }
7700 
7701 TEST_F(FormatTest, AlignsAfterReturn) {
7702   verifyFormat(
7703       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7704       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7705   verifyFormat(
7706       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7707       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7708   verifyFormat(
7709       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7710       "       aaaaaaaaaaaaaaaaaaaaaa();");
7711   verifyFormat(
7712       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7713       "        aaaaaaaaaaaaaaaaaaaaaa());");
7714   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7715                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7716   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7717                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7718                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7719   verifyFormat("return\n"
7720                "    // true if code is one of a or b.\n"
7721                "    code == a || code == b;");
7722 }
7723 
7724 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7725   verifyFormat(
7726       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7727       "                                                aaaaaaaaa aaaaaaa) {}");
7728   verifyFormat(
7729       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7730       "                                               aaaaaaaaaaa aaaaaaaaa);");
7731   verifyFormat(
7732       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7733       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7734   FormatStyle Style = getLLVMStyle();
7735   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7736   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7737                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7738                Style);
7739   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7740                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7741                Style);
7742   verifyFormat("SomeLongVariableName->someFunction(\n"
7743                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7744                Style);
7745   verifyFormat(
7746       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7747       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7748       Style);
7749   verifyFormat(
7750       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7751       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7752       Style);
7753   verifyFormat(
7754       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7755       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7756       Style);
7757 
7758   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7759                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7760                "        b));",
7761                Style);
7762 
7763   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7764   Style.BinPackArguments = false;
7765   Style.BinPackParameters = false;
7766   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7767                "    aaaaaaaaaaa aaaaaaaa,\n"
7768                "    aaaaaaaaa aaaaaaa,\n"
7769                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7770                Style);
7771   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7772                "    aaaaaaaaaaa aaaaaaaaa,\n"
7773                "    aaaaaaaaaaa aaaaaaaaa,\n"
7774                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7775                Style);
7776   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7777                "    aaaaaaaaaaaaaaa,\n"
7778                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7779                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7780                Style);
7781   verifyFormat(
7782       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7783       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7784       Style);
7785   verifyFormat(
7786       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7787       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7788       Style);
7789   verifyFormat(
7790       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7791       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7792       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7793       "    aaaaaaaaaaaaaaaa);",
7794       Style);
7795   verifyFormat(
7796       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7797       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7798       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7799       "    aaaaaaaaaaaaaaaa);",
7800       Style);
7801 }
7802 
7803 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7804   FormatStyle Style = getLLVMStyleWithColumns(40);
7805   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7806                "          bbbbbbbbbbbbbbbbbbbbbb);",
7807                Style);
7808   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7809   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7810   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7811                "          bbbbbbbbbbbbbbbbbbbbbb);",
7812                Style);
7813   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7814   Style.AlignOperands = FormatStyle::OAS_Align;
7815   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7816                "          bbbbbbbbbbbbbbbbbbbbbb);",
7817                Style);
7818   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7819   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7820   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7821                "    bbbbbbbbbbbbbbbbbbbbbb);",
7822                Style);
7823 }
7824 
7825 TEST_F(FormatTest, BreaksConditionalExpressions) {
7826   verifyFormat(
7827       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7828       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7829       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7830   verifyFormat(
7831       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7832       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7833       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7834   verifyFormat(
7835       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7836       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7837   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7838                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7839                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7840   verifyFormat(
7841       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7842       "                                                    : aaaaaaaaaaaaa);");
7843   verifyFormat(
7844       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7845       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7846       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7847       "                   aaaaaaaaaaaaa);");
7848   verifyFormat(
7849       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7850       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7851       "                   aaaaaaaaaaaaa);");
7852   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7853                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7854                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7855                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7856                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7857   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7858                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7859                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7860                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7861                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7862                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7863                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7864   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7865                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7866                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7867                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7868                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7869   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7870                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7871                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7872   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7873                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7874                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7875                "        : aaaaaaaaaaaaaaaa;");
7876   verifyFormat(
7877       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7878       "    ? aaaaaaaaaaaaaaa\n"
7879       "    : aaaaaaaaaaaaaaa;");
7880   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7881                "          aaaaaaaaa\n"
7882                "      ? b\n"
7883                "      : c);");
7884   verifyFormat("return aaaa == bbbb\n"
7885                "           // comment\n"
7886                "           ? aaaa\n"
7887                "           : bbbb;");
7888   verifyFormat("unsigned Indent =\n"
7889                "    format(TheLine.First,\n"
7890                "           IndentForLevel[TheLine.Level] >= 0\n"
7891                "               ? IndentForLevel[TheLine.Level]\n"
7892                "               : TheLine * 2,\n"
7893                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7894                getLLVMStyleWithColumns(60));
7895   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7896                "                  ? aaaaaaaaaaaaaaa\n"
7897                "                  : bbbbbbbbbbbbbbb //\n"
7898                "                        ? ccccccccccccccc\n"
7899                "                        : ddddddddddddddd;");
7900   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7901                "                  ? aaaaaaaaaaaaaaa\n"
7902                "                  : (bbbbbbbbbbbbbbb //\n"
7903                "                         ? ccccccccccccccc\n"
7904                "                         : ddddddddddddddd);");
7905   verifyFormat(
7906       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7907       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7908       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7909       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7910       "                                      : aaaaaaaaaa;");
7911   verifyFormat(
7912       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7913       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7914       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7915 
7916   FormatStyle NoBinPacking = getLLVMStyle();
7917   NoBinPacking.BinPackArguments = false;
7918   verifyFormat(
7919       "void f() {\n"
7920       "  g(aaa,\n"
7921       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7922       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7923       "        ? aaaaaaaaaaaaaaa\n"
7924       "        : aaaaaaaaaaaaaaa);\n"
7925       "}",
7926       NoBinPacking);
7927   verifyFormat(
7928       "void f() {\n"
7929       "  g(aaa,\n"
7930       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7931       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7932       "        ?: aaaaaaaaaaaaaaa);\n"
7933       "}",
7934       NoBinPacking);
7935 
7936   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7937                "             // comment.\n"
7938                "             ccccccccccccccccccccccccccccccccccccccc\n"
7939                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7940                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7941 
7942   // Assignments in conditional expressions. Apparently not uncommon :-(.
7943   verifyFormat("return a != b\n"
7944                "           // comment\n"
7945                "           ? a = b\n"
7946                "           : a = b;");
7947   verifyFormat("return a != b\n"
7948                "           // comment\n"
7949                "           ? a = a != b\n"
7950                "                     // comment\n"
7951                "                     ? a = b\n"
7952                "                     : a\n"
7953                "           : a;\n");
7954   verifyFormat("return a != b\n"
7955                "           // comment\n"
7956                "           ? a\n"
7957                "           : a = a != b\n"
7958                "                     // comment\n"
7959                "                     ? a = b\n"
7960                "                     : a;");
7961 
7962   // Chained conditionals
7963   FormatStyle Style = getLLVMStyleWithColumns(70);
7964   Style.AlignOperands = FormatStyle::OAS_Align;
7965   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7966                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7967                "                        : 3333333333333333;",
7968                Style);
7969   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7970                "       : bbbbbbbbbb     ? 2222222222222222\n"
7971                "                        : 3333333333333333;",
7972                Style);
7973   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7974                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7975                "                          : 3333333333333333;",
7976                Style);
7977   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7978                "       : bbbbbbbbbbbbbb ? 222222\n"
7979                "                        : 333333;",
7980                Style);
7981   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7982                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7983                "       : cccccccccccccc ? 3333333333333333\n"
7984                "                        : 4444444444444444;",
7985                Style);
7986   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7987                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7988                "                        : 3333333333333333;",
7989                Style);
7990   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7991                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7992                "                        : (aaa ? bbb : ccc);",
7993                Style);
7994   verifyFormat(
7995       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7996       "                                             : cccccccccccccccccc)\n"
7997       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7998       "                        : 3333333333333333;",
7999       Style);
8000   verifyFormat(
8001       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8002       "                                             : cccccccccccccccccc)\n"
8003       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8004       "                        : 3333333333333333;",
8005       Style);
8006   verifyFormat(
8007       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8008       "                                             : dddddddddddddddddd)\n"
8009       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8010       "                        : 3333333333333333;",
8011       Style);
8012   verifyFormat(
8013       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8014       "                                             : dddddddddddddddddd)\n"
8015       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8016       "                        : 3333333333333333;",
8017       Style);
8018   verifyFormat(
8019       "return aaaaaaaaa        ? 1111111111111111\n"
8020       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8021       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8022       "                                             : dddddddddddddddddd)\n",
8023       Style);
8024   verifyFormat(
8025       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8026       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8027       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8028       "                                             : cccccccccccccccccc);",
8029       Style);
8030   verifyFormat(
8031       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8032       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8033       "                                             : eeeeeeeeeeeeeeeeee)\n"
8034       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8035       "                        : 3333333333333333;",
8036       Style);
8037   verifyFormat(
8038       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8039       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8040       "                                             : eeeeeeeeeeeeeeeeee)\n"
8041       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8042       "                        : 3333333333333333;",
8043       Style);
8044   verifyFormat(
8045       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8046       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8047       "                                             : eeeeeeeeeeeeeeeeee)\n"
8048       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8049       "                        : 3333333333333333;",
8050       Style);
8051   verifyFormat(
8052       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8053       "                                             : cccccccccccccccccc\n"
8054       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8055       "                        : 3333333333333333;",
8056       Style);
8057   verifyFormat(
8058       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8059       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8060       "                                             : eeeeeeeeeeeeeeeeee\n"
8061       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8062       "                        : 3333333333333333;",
8063       Style);
8064   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8065                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8066                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8067                "                                   : eeeeeeeeeeeeeeeeee)\n"
8068                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8069                "                             : 3333333333333333;",
8070                Style);
8071   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8072                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8073                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8074                "                                : eeeeeeeeeeeeeeeeee\n"
8075                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8076                "                                 : 3333333333333333;",
8077                Style);
8078 
8079   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8080   Style.BreakBeforeTernaryOperators = false;
8081   // FIXME: Aligning the question marks is weird given DontAlign.
8082   // Consider disabling this alignment in this case. Also check whether this
8083   // will render the adjustment from https://reviews.llvm.org/D82199
8084   // unnecessary.
8085   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8086                "    bbbb                ? cccccccccccccccccc :\n"
8087                "                          ddddd;\n",
8088                Style);
8089 
8090   EXPECT_EQ(
8091       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8092       "    /*\n"
8093       "     */\n"
8094       "    function() {\n"
8095       "      try {\n"
8096       "        return JJJJJJJJJJJJJJ(\n"
8097       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8098       "      }\n"
8099       "    } :\n"
8100       "    function() {};",
8101       format(
8102           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8103           "     /*\n"
8104           "      */\n"
8105           "     function() {\n"
8106           "      try {\n"
8107           "        return JJJJJJJJJJJJJJ(\n"
8108           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8109           "      }\n"
8110           "    } :\n"
8111           "    function() {};",
8112           getGoogleStyle(FormatStyle::LK_JavaScript)));
8113 }
8114 
8115 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8116   FormatStyle Style = getLLVMStyleWithColumns(70);
8117   Style.BreakBeforeTernaryOperators = false;
8118   verifyFormat(
8119       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8120       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8121       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8122       Style);
8123   verifyFormat(
8124       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8125       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8126       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8127       Style);
8128   verifyFormat(
8129       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8130       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8131       Style);
8132   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8133                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8134                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8135                Style);
8136   verifyFormat(
8137       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8138       "                                                      aaaaaaaaaaaaa);",
8139       Style);
8140   verifyFormat(
8141       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8142       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8143       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8144       "                   aaaaaaaaaaaaa);",
8145       Style);
8146   verifyFormat(
8147       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8148       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8149       "                   aaaaaaaaaaaaa);",
8150       Style);
8151   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8152                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8153                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8154                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8155                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8156                Style);
8157   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8158                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8159                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8160                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8161                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8162                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8163                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8164                Style);
8165   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8166                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8167                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8168                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8169                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8170                Style);
8171   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8172                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8173                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8174                Style);
8175   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8176                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8177                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8178                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8179                Style);
8180   verifyFormat(
8181       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8182       "    aaaaaaaaaaaaaaa :\n"
8183       "    aaaaaaaaaaaaaaa;",
8184       Style);
8185   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8186                "          aaaaaaaaa ?\n"
8187                "      b :\n"
8188                "      c);",
8189                Style);
8190   verifyFormat("unsigned Indent =\n"
8191                "    format(TheLine.First,\n"
8192                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8193                "               IndentForLevel[TheLine.Level] :\n"
8194                "               TheLine * 2,\n"
8195                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8196                Style);
8197   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8198                "                  aaaaaaaaaaaaaaa :\n"
8199                "                  bbbbbbbbbbbbbbb ? //\n"
8200                "                      ccccccccccccccc :\n"
8201                "                      ddddddddddddddd;",
8202                Style);
8203   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8204                "                  aaaaaaaaaaaaaaa :\n"
8205                "                  (bbbbbbbbbbbbbbb ? //\n"
8206                "                       ccccccccccccccc :\n"
8207                "                       ddddddddddddddd);",
8208                Style);
8209   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8210                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8211                "            ccccccccccccccccccccccccccc;",
8212                Style);
8213   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8214                "           aaaaa :\n"
8215                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8216                Style);
8217 
8218   // Chained conditionals
8219   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8220                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8221                "                          3333333333333333;",
8222                Style);
8223   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8224                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8225                "                          3333333333333333;",
8226                Style);
8227   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8228                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8229                "                          3333333333333333;",
8230                Style);
8231   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8232                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8233                "                          333333;",
8234                Style);
8235   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8236                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8237                "       cccccccccccccccc ? 3333333333333333 :\n"
8238                "                          4444444444444444;",
8239                Style);
8240   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8241                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8242                "                          3333333333333333;",
8243                Style);
8244   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8245                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8246                "                          (aaa ? bbb : ccc);",
8247                Style);
8248   verifyFormat(
8249       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8250       "                                               cccccccccccccccccc) :\n"
8251       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8252       "                          3333333333333333;",
8253       Style);
8254   verifyFormat(
8255       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8256       "                                               cccccccccccccccccc) :\n"
8257       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8258       "                          3333333333333333;",
8259       Style);
8260   verifyFormat(
8261       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8262       "                                               dddddddddddddddddd) :\n"
8263       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8264       "                          3333333333333333;",
8265       Style);
8266   verifyFormat(
8267       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8268       "                                               dddddddddddddddddd) :\n"
8269       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8270       "                          3333333333333333;",
8271       Style);
8272   verifyFormat(
8273       "return aaaaaaaaa        ? 1111111111111111 :\n"
8274       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8275       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8276       "                                               dddddddddddddddddd)\n",
8277       Style);
8278   verifyFormat(
8279       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8280       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8281       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8282       "                                               cccccccccccccccccc);",
8283       Style);
8284   verifyFormat(
8285       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8286       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8287       "                                               eeeeeeeeeeeeeeeeee) :\n"
8288       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8289       "                          3333333333333333;",
8290       Style);
8291   verifyFormat(
8292       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8293       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8294       "                                               eeeeeeeeeeeeeeeeee) :\n"
8295       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8296       "                          3333333333333333;",
8297       Style);
8298   verifyFormat(
8299       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8300       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8301       "                                               eeeeeeeeeeeeeeeeee) :\n"
8302       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8303       "                          3333333333333333;",
8304       Style);
8305   verifyFormat(
8306       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8307       "                                               cccccccccccccccccc :\n"
8308       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8309       "                          3333333333333333;",
8310       Style);
8311   verifyFormat(
8312       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8313       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8314       "                                               eeeeeeeeeeeeeeeeee :\n"
8315       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8316       "                          3333333333333333;",
8317       Style);
8318   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8319                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8320                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8321                "                                 eeeeeeeeeeeeeeeeee) :\n"
8322                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8323                "                               3333333333333333;",
8324                Style);
8325   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8326                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8327                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8328                "                                  eeeeeeeeeeeeeeeeee :\n"
8329                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8330                "                               3333333333333333;",
8331                Style);
8332 }
8333 
8334 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8335   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8336                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8337   verifyFormat("bool a = true, b = false;");
8338 
8339   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8340                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8341                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8342                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8343   verifyFormat(
8344       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8345       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8346       "     d = e && f;");
8347   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8348                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8349   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8350                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8351   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8352                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8353 
8354   FormatStyle Style = getGoogleStyle();
8355   Style.PointerAlignment = FormatStyle::PAS_Left;
8356   Style.DerivePointerAlignment = false;
8357   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8358                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8359                "    *b = bbbbbbbbbbbbbbbbbbb;",
8360                Style);
8361   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8362                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8363                Style);
8364   verifyFormat("vector<int*> a, b;", Style);
8365   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8366 }
8367 
8368 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8369   verifyFormat("arr[foo ? bar : baz];");
8370   verifyFormat("f()[foo ? bar : baz];");
8371   verifyFormat("(a + b)[foo ? bar : baz];");
8372   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8373 }
8374 
8375 TEST_F(FormatTest, AlignsStringLiterals) {
8376   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8377                "                                      \"short literal\");");
8378   verifyFormat(
8379       "looooooooooooooooooooooooongFunction(\n"
8380       "    \"short literal\"\n"
8381       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8382   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8383                "             \" string literals\",\n"
8384                "             and, other, parameters);");
8385   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8386             "      \"5678\";",
8387             format("fun + \"1243\" /* comment */\n"
8388                    "    \"5678\";",
8389                    getLLVMStyleWithColumns(28)));
8390   EXPECT_EQ(
8391       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8392       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8393       "         \"aaaaaaaaaaaaaaaa\";",
8394       format("aaaaaa ="
8395              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8396              "aaaaaaaaaaaaaaaaaaaaa\" "
8397              "\"aaaaaaaaaaaaaaaa\";"));
8398   verifyFormat("a = a + \"a\"\n"
8399                "        \"a\"\n"
8400                "        \"a\";");
8401   verifyFormat("f(\"a\", \"b\"\n"
8402                "       \"c\");");
8403 
8404   verifyFormat(
8405       "#define LL_FORMAT \"ll\"\n"
8406       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8407       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8408 
8409   verifyFormat("#define A(X)          \\\n"
8410                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8411                "  \"ccccc\"",
8412                getLLVMStyleWithColumns(23));
8413   verifyFormat("#define A \"def\"\n"
8414                "f(\"abc\" A \"ghi\"\n"
8415                "  \"jkl\");");
8416 
8417   verifyFormat("f(L\"a\"\n"
8418                "  L\"b\");");
8419   verifyFormat("#define A(X)            \\\n"
8420                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8421                "  L\"ccccc\"",
8422                getLLVMStyleWithColumns(25));
8423 
8424   verifyFormat("f(@\"a\"\n"
8425                "  @\"b\");");
8426   verifyFormat("NSString s = @\"a\"\n"
8427                "             @\"b\"\n"
8428                "             @\"c\";");
8429   verifyFormat("NSString s = @\"a\"\n"
8430                "              \"b\"\n"
8431                "              \"c\";");
8432 }
8433 
8434 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8435   FormatStyle Style = getLLVMStyle();
8436   // No declarations or definitions should be moved to own line.
8437   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8438   verifyFormat("class A {\n"
8439                "  int f() { return 1; }\n"
8440                "  int g();\n"
8441                "};\n"
8442                "int f() { return 1; }\n"
8443                "int g();\n",
8444                Style);
8445 
8446   // All declarations and definitions should have the return type moved to its
8447   // own line.
8448   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8449   Style.TypenameMacros = {"LIST"};
8450   verifyFormat("SomeType\n"
8451                "funcdecl(LIST(uint64_t));",
8452                Style);
8453   verifyFormat("class E {\n"
8454                "  int\n"
8455                "  f() {\n"
8456                "    return 1;\n"
8457                "  }\n"
8458                "  int\n"
8459                "  g();\n"
8460                "};\n"
8461                "int\n"
8462                "f() {\n"
8463                "  return 1;\n"
8464                "}\n"
8465                "int\n"
8466                "g();\n",
8467                Style);
8468 
8469   // Top-level definitions, and no kinds of declarations should have the
8470   // return type moved to its own line.
8471   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8472   verifyFormat("class B {\n"
8473                "  int f() { return 1; }\n"
8474                "  int g();\n"
8475                "};\n"
8476                "int\n"
8477                "f() {\n"
8478                "  return 1;\n"
8479                "}\n"
8480                "int g();\n",
8481                Style);
8482 
8483   // Top-level definitions and declarations should have the return type moved
8484   // to its own line.
8485   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8486   verifyFormat("class C {\n"
8487                "  int f() { return 1; }\n"
8488                "  int g();\n"
8489                "};\n"
8490                "int\n"
8491                "f() {\n"
8492                "  return 1;\n"
8493                "}\n"
8494                "int\n"
8495                "g();\n",
8496                Style);
8497 
8498   // All definitions should have the return type moved to its own line, but no
8499   // kinds of declarations.
8500   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8501   verifyFormat("class D {\n"
8502                "  int\n"
8503                "  f() {\n"
8504                "    return 1;\n"
8505                "  }\n"
8506                "  int g();\n"
8507                "};\n"
8508                "int\n"
8509                "f() {\n"
8510                "  return 1;\n"
8511                "}\n"
8512                "int g();\n",
8513                Style);
8514   verifyFormat("const char *\n"
8515                "f(void) {\n" // Break here.
8516                "  return \"\";\n"
8517                "}\n"
8518                "const char *bar(void);\n", // No break here.
8519                Style);
8520   verifyFormat("template <class T>\n"
8521                "T *\n"
8522                "f(T &c) {\n" // Break here.
8523                "  return NULL;\n"
8524                "}\n"
8525                "template <class T> T *f(T &c);\n", // No break here.
8526                Style);
8527   verifyFormat("class C {\n"
8528                "  int\n"
8529                "  operator+() {\n"
8530                "    return 1;\n"
8531                "  }\n"
8532                "  int\n"
8533                "  operator()() {\n"
8534                "    return 1;\n"
8535                "  }\n"
8536                "};\n",
8537                Style);
8538   verifyFormat("void\n"
8539                "A::operator()() {}\n"
8540                "void\n"
8541                "A::operator>>() {}\n"
8542                "void\n"
8543                "A::operator+() {}\n"
8544                "void\n"
8545                "A::operator*() {}\n"
8546                "void\n"
8547                "A::operator->() {}\n"
8548                "void\n"
8549                "A::operator void *() {}\n"
8550                "void\n"
8551                "A::operator void &() {}\n"
8552                "void\n"
8553                "A::operator void &&() {}\n"
8554                "void\n"
8555                "A::operator char *() {}\n"
8556                "void\n"
8557                "A::operator[]() {}\n"
8558                "void\n"
8559                "A::operator!() {}\n"
8560                "void\n"
8561                "A::operator**() {}\n"
8562                "void\n"
8563                "A::operator<Foo> *() {}\n"
8564                "void\n"
8565                "A::operator<Foo> **() {}\n"
8566                "void\n"
8567                "A::operator<Foo> &() {}\n"
8568                "void\n"
8569                "A::operator void **() {}\n",
8570                Style);
8571   verifyFormat("constexpr auto\n"
8572                "operator()() const -> reference {}\n"
8573                "constexpr auto\n"
8574                "operator>>() const -> reference {}\n"
8575                "constexpr auto\n"
8576                "operator+() const -> reference {}\n"
8577                "constexpr auto\n"
8578                "operator*() const -> reference {}\n"
8579                "constexpr auto\n"
8580                "operator->() const -> reference {}\n"
8581                "constexpr auto\n"
8582                "operator++() const -> reference {}\n"
8583                "constexpr auto\n"
8584                "operator void *() const -> reference {}\n"
8585                "constexpr auto\n"
8586                "operator void **() const -> reference {}\n"
8587                "constexpr auto\n"
8588                "operator void *() const -> reference {}\n"
8589                "constexpr auto\n"
8590                "operator void &() const -> reference {}\n"
8591                "constexpr auto\n"
8592                "operator void &&() const -> reference {}\n"
8593                "constexpr auto\n"
8594                "operator char *() const -> reference {}\n"
8595                "constexpr auto\n"
8596                "operator!() const -> reference {}\n"
8597                "constexpr auto\n"
8598                "operator[]() const -> reference {}\n",
8599                Style);
8600   verifyFormat("void *operator new(std::size_t s);", // No break here.
8601                Style);
8602   verifyFormat("void *\n"
8603                "operator new(std::size_t s) {}",
8604                Style);
8605   verifyFormat("void *\n"
8606                "operator delete[](void *ptr) {}",
8607                Style);
8608   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8609   verifyFormat("const char *\n"
8610                "f(void)\n" // Break here.
8611                "{\n"
8612                "  return \"\";\n"
8613                "}\n"
8614                "const char *bar(void);\n", // No break here.
8615                Style);
8616   verifyFormat("template <class T>\n"
8617                "T *\n"     // Problem here: no line break
8618                "f(T &c)\n" // Break here.
8619                "{\n"
8620                "  return NULL;\n"
8621                "}\n"
8622                "template <class T> T *f(T &c);\n", // No break here.
8623                Style);
8624   verifyFormat("int\n"
8625                "foo(A<bool> a)\n"
8626                "{\n"
8627                "  return a;\n"
8628                "}\n",
8629                Style);
8630   verifyFormat("int\n"
8631                "foo(A<8> a)\n"
8632                "{\n"
8633                "  return a;\n"
8634                "}\n",
8635                Style);
8636   verifyFormat("int\n"
8637                "foo(A<B<bool>, 8> a)\n"
8638                "{\n"
8639                "  return a;\n"
8640                "}\n",
8641                Style);
8642   verifyFormat("int\n"
8643                "foo(A<B<8>, bool> a)\n"
8644                "{\n"
8645                "  return a;\n"
8646                "}\n",
8647                Style);
8648   verifyFormat("int\n"
8649                "foo(A<B<bool>, bool> a)\n"
8650                "{\n"
8651                "  return a;\n"
8652                "}\n",
8653                Style);
8654   verifyFormat("int\n"
8655                "foo(A<B<8>, 8> a)\n"
8656                "{\n"
8657                "  return a;\n"
8658                "}\n",
8659                Style);
8660 
8661   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8662   Style.BraceWrapping.AfterFunction = true;
8663   verifyFormat("int f(i);\n" // No break here.
8664                "int\n"       // Break here.
8665                "f(i)\n"
8666                "{\n"
8667                "  return i + 1;\n"
8668                "}\n"
8669                "int\n" // Break here.
8670                "f(i)\n"
8671                "{\n"
8672                "  return i + 1;\n"
8673                "};",
8674                Style);
8675   verifyFormat("int f(a, b, c);\n" // No break here.
8676                "int\n"             // Break here.
8677                "f(a, b, c)\n"      // Break here.
8678                "short a, b;\n"
8679                "float c;\n"
8680                "{\n"
8681                "  return a + b < c;\n"
8682                "}\n"
8683                "int\n"        // Break here.
8684                "f(a, b, c)\n" // Break here.
8685                "short a, b;\n"
8686                "float c;\n"
8687                "{\n"
8688                "  return a + b < c;\n"
8689                "};",
8690                Style);
8691   verifyFormat("byte *\n" // Break here.
8692                "f(a)\n"   // Break here.
8693                "byte a[];\n"
8694                "{\n"
8695                "  return a;\n"
8696                "}",
8697                Style);
8698   verifyFormat("bool f(int a, int) override;\n"
8699                "Bar g(int a, Bar) final;\n"
8700                "Bar h(a, Bar) final;",
8701                Style);
8702   verifyFormat("int\n"
8703                "f(a)",
8704                Style);
8705   verifyFormat("bool\n"
8706                "f(size_t = 0, bool b = false)\n"
8707                "{\n"
8708                "  return !b;\n"
8709                "}",
8710                Style);
8711 
8712   // The return breaking style doesn't affect:
8713   // * function and object definitions with attribute-like macros
8714   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8715                "    ABSL_GUARDED_BY(mutex) = {};",
8716                getGoogleStyleWithColumns(40));
8717   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8718                "    ABSL_GUARDED_BY(mutex);  // comment",
8719                getGoogleStyleWithColumns(40));
8720   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8721                "    ABSL_GUARDED_BY(mutex1)\n"
8722                "        ABSL_GUARDED_BY(mutex2);",
8723                getGoogleStyleWithColumns(40));
8724   verifyFormat("Tttttt f(int a, int b)\n"
8725                "    ABSL_GUARDED_BY(mutex1)\n"
8726                "        ABSL_GUARDED_BY(mutex2);",
8727                getGoogleStyleWithColumns(40));
8728   // * typedefs
8729   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8730 
8731   Style = getGNUStyle();
8732 
8733   // Test for comments at the end of function declarations.
8734   verifyFormat("void\n"
8735                "foo (int a, /*abc*/ int b) // def\n"
8736                "{\n"
8737                "}\n",
8738                Style);
8739 
8740   verifyFormat("void\n"
8741                "foo (int a, /* abc */ int b) /* def */\n"
8742                "{\n"
8743                "}\n",
8744                Style);
8745 
8746   // Definitions that should not break after return type
8747   verifyFormat("void foo (int a, int b); // def\n", Style);
8748   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8749   verifyFormat("void foo (int a, int b);\n", Style);
8750 }
8751 
8752 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8753   FormatStyle NoBreak = getLLVMStyle();
8754   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8755   FormatStyle Break = getLLVMStyle();
8756   Break.AlwaysBreakBeforeMultilineStrings = true;
8757   verifyFormat("aaaa = \"bbbb\"\n"
8758                "       \"cccc\";",
8759                NoBreak);
8760   verifyFormat("aaaa =\n"
8761                "    \"bbbb\"\n"
8762                "    \"cccc\";",
8763                Break);
8764   verifyFormat("aaaa(\"bbbb\"\n"
8765                "     \"cccc\");",
8766                NoBreak);
8767   verifyFormat("aaaa(\n"
8768                "    \"bbbb\"\n"
8769                "    \"cccc\");",
8770                Break);
8771   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8772                "          \"cccc\");",
8773                NoBreak);
8774   verifyFormat("aaaa(qqq,\n"
8775                "     \"bbbb\"\n"
8776                "     \"cccc\");",
8777                Break);
8778   verifyFormat("aaaa(qqq,\n"
8779                "     L\"bbbb\"\n"
8780                "     L\"cccc\");",
8781                Break);
8782   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8783                "                      \"bbbb\"));",
8784                Break);
8785   verifyFormat("string s = someFunction(\n"
8786                "    \"abc\"\n"
8787                "    \"abc\");",
8788                Break);
8789 
8790   // As we break before unary operators, breaking right after them is bad.
8791   verifyFormat("string foo = abc ? \"x\"\n"
8792                "                   \"blah blah blah blah blah blah\"\n"
8793                "                 : \"y\";",
8794                Break);
8795 
8796   // Don't break if there is no column gain.
8797   verifyFormat("f(\"aaaa\"\n"
8798                "  \"bbbb\");",
8799                Break);
8800 
8801   // Treat literals with escaped newlines like multi-line string literals.
8802   EXPECT_EQ("x = \"a\\\n"
8803             "b\\\n"
8804             "c\";",
8805             format("x = \"a\\\n"
8806                    "b\\\n"
8807                    "c\";",
8808                    NoBreak));
8809   EXPECT_EQ("xxxx =\n"
8810             "    \"a\\\n"
8811             "b\\\n"
8812             "c\";",
8813             format("xxxx = \"a\\\n"
8814                    "b\\\n"
8815                    "c\";",
8816                    Break));
8817 
8818   EXPECT_EQ("NSString *const kString =\n"
8819             "    @\"aaaa\"\n"
8820             "    @\"bbbb\";",
8821             format("NSString *const kString = @\"aaaa\"\n"
8822                    "@\"bbbb\";",
8823                    Break));
8824 
8825   Break.ColumnLimit = 0;
8826   verifyFormat("const char *hello = \"hello llvm\";", Break);
8827 }
8828 
8829 TEST_F(FormatTest, AlignsPipes) {
8830   verifyFormat(
8831       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8832       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8833       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8834   verifyFormat(
8835       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8836       "                     << aaaaaaaaaaaaaaaaaaaa;");
8837   verifyFormat(
8838       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8839       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8840   verifyFormat(
8841       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8842       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8843   verifyFormat(
8844       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8845       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8846       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8847   verifyFormat(
8848       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8849       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8850       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8851   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8852                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8853                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8854                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8855   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8856                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8857   verifyFormat(
8858       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8859       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8860   verifyFormat(
8861       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8862       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8863 
8864   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8865                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8866   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8867                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8868                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8869                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8870   verifyFormat("LOG_IF(aaa == //\n"
8871                "       bbb)\n"
8872                "    << a << b;");
8873 
8874   // But sometimes, breaking before the first "<<" is desirable.
8875   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8876                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8877   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8878                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8879                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8880   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8881                "    << BEF << IsTemplate << Description << E->getType();");
8882   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8883                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8884                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8885   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8886                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8887                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8888                "    << aaa;");
8889 
8890   verifyFormat(
8891       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8892       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8893 
8894   // Incomplete string literal.
8895   EXPECT_EQ("llvm::errs() << \"\n"
8896             "             << a;",
8897             format("llvm::errs() << \"\n<<a;"));
8898 
8899   verifyFormat("void f() {\n"
8900                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8901                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8902                "}");
8903 
8904   // Handle 'endl'.
8905   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8906                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8907   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8908 
8909   // Handle '\n'.
8910   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8911                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8912   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8913                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8914   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8915                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8916   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8917 }
8918 
8919 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8920   verifyFormat("return out << \"somepacket = {\\n\"\n"
8921                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8922                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8923                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8924                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8925                "           << \"}\";");
8926 
8927   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8928                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8929                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8930   verifyFormat(
8931       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8932       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8933       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8934       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8935       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8936   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8937                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8938   verifyFormat(
8939       "void f() {\n"
8940       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8941       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8942       "}");
8943 
8944   // Breaking before the first "<<" is generally not desirable.
8945   verifyFormat(
8946       "llvm::errs()\n"
8947       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8948       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8949       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8950       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8951       getLLVMStyleWithColumns(70));
8952   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8953                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8954                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8955                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8956                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8957                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8958                getLLVMStyleWithColumns(70));
8959 
8960   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8961                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8962                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8963   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8964                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8965                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8966   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8967                "           (aaaa + aaaa);",
8968                getLLVMStyleWithColumns(40));
8969   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8970                "                  (aaaaaaa + aaaaa));",
8971                getLLVMStyleWithColumns(40));
8972   verifyFormat(
8973       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8974       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8975       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8976 }
8977 
8978 TEST_F(FormatTest, UnderstandsEquals) {
8979   verifyFormat(
8980       "aaaaaaaaaaaaaaaaa =\n"
8981       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8982   verifyFormat(
8983       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8984       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8985   verifyFormat(
8986       "if (a) {\n"
8987       "  f();\n"
8988       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8989       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8990       "}");
8991 
8992   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8993                "        100000000 + 10000000) {\n}");
8994 }
8995 
8996 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8997   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8998                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8999 
9000   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9001                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
9002 
9003   verifyFormat(
9004       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
9005       "                                                          Parameter2);");
9006 
9007   verifyFormat(
9008       "ShortObject->shortFunction(\n"
9009       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
9010       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
9011 
9012   verifyFormat("loooooooooooooongFunction(\n"
9013                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
9014 
9015   verifyFormat(
9016       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
9017       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
9018 
9019   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9020                "    .WillRepeatedly(Return(SomeValue));");
9021   verifyFormat("void f() {\n"
9022                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9023                "      .Times(2)\n"
9024                "      .WillRepeatedly(Return(SomeValue));\n"
9025                "}");
9026   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9027                "    ccccccccccccccccccccccc);");
9028   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9029                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9030                "          .aaaaa(aaaaa),\n"
9031                "      aaaaaaaaaaaaaaaaaaaaa);");
9032   verifyFormat("void f() {\n"
9033                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9034                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9035                "}");
9036   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9037                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9038                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9039                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9040                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9041   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9042                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9043                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9044                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9045                "}");
9046 
9047   // Here, it is not necessary to wrap at "." or "->".
9048   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9049                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9050   verifyFormat(
9051       "aaaaaaaaaaa->aaaaaaaaa(\n"
9052       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9053       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9054 
9055   verifyFormat(
9056       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9057       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9058   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9059                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9060   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9061                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9062 
9063   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9064                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9065                "    .a();");
9066 
9067   FormatStyle NoBinPacking = getLLVMStyle();
9068   NoBinPacking.BinPackParameters = false;
9069   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9070                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9071                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9072                "                         aaaaaaaaaaaaaaaaaaa,\n"
9073                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9074                NoBinPacking);
9075 
9076   // If there is a subsequent call, change to hanging indentation.
9077   verifyFormat(
9078       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9079       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9080       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9081   verifyFormat(
9082       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9083       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9084   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9085                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9086                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9087   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9088                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9089                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9090 }
9091 
9092 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9093   verifyFormat("template <typename T>\n"
9094                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9095   verifyFormat("template <typename T>\n"
9096                "// T should be one of {A, B}.\n"
9097                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9098   verifyFormat(
9099       "template <typename T>\n"
9100       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9101   verifyFormat("template <typename T>\n"
9102                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9103                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9104   verifyFormat(
9105       "template <typename T>\n"
9106       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9107       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9108   verifyFormat(
9109       "template <typename T>\n"
9110       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9111       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9112       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9113   verifyFormat("template <typename T>\n"
9114                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9115                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9116   verifyFormat(
9117       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9118       "          typename T4 = char>\n"
9119       "void f();");
9120   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9121                "          template <typename> class cccccccccccccccccccccc,\n"
9122                "          typename ddddddddddddd>\n"
9123                "class C {};");
9124   verifyFormat(
9125       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9126       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9127 
9128   verifyFormat("void f() {\n"
9129                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9130                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9131                "}");
9132 
9133   verifyFormat("template <typename T> class C {};");
9134   verifyFormat("template <typename T> void f();");
9135   verifyFormat("template <typename T> void f() {}");
9136   verifyFormat(
9137       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9138       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9139       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9140       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9141       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9142       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9143       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9144       getLLVMStyleWithColumns(72));
9145   EXPECT_EQ("static_cast<A< //\n"
9146             "    B> *>(\n"
9147             "\n"
9148             ");",
9149             format("static_cast<A<//\n"
9150                    "    B>*>(\n"
9151                    "\n"
9152                    "    );"));
9153   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9154                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9155 
9156   FormatStyle AlwaysBreak = getLLVMStyle();
9157   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9158   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9159   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9160   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9161   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9162                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9163                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9164   verifyFormat("template <template <typename> class Fooooooo,\n"
9165                "          template <typename> class Baaaaaaar>\n"
9166                "struct C {};",
9167                AlwaysBreak);
9168   verifyFormat("template <typename T> // T can be A, B or C.\n"
9169                "struct C {};",
9170                AlwaysBreak);
9171   verifyFormat("template <enum E> class A {\n"
9172                "public:\n"
9173                "  E *f();\n"
9174                "};");
9175 
9176   FormatStyle NeverBreak = getLLVMStyle();
9177   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9178   verifyFormat("template <typename T> class C {};", NeverBreak);
9179   verifyFormat("template <typename T> void f();", NeverBreak);
9180   verifyFormat("template <typename T> void f() {}", NeverBreak);
9181   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9182                "bbbbbbbbbbbbbbbbbbbb) {}",
9183                NeverBreak);
9184   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9185                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9186                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9187                NeverBreak);
9188   verifyFormat("template <template <typename> class Fooooooo,\n"
9189                "          template <typename> class Baaaaaaar>\n"
9190                "struct C {};",
9191                NeverBreak);
9192   verifyFormat("template <typename T> // T can be A, B or C.\n"
9193                "struct C {};",
9194                NeverBreak);
9195   verifyFormat("template <enum E> class A {\n"
9196                "public:\n"
9197                "  E *f();\n"
9198                "};",
9199                NeverBreak);
9200   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9201   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9202                "bbbbbbbbbbbbbbbbbbbb) {}",
9203                NeverBreak);
9204 }
9205 
9206 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9207   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9208   Style.ColumnLimit = 60;
9209   EXPECT_EQ("// Baseline - no comments.\n"
9210             "template <\n"
9211             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9212             "void f() {}",
9213             format("// Baseline - no comments.\n"
9214                    "template <\n"
9215                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9216                    "void f() {}",
9217                    Style));
9218 
9219   EXPECT_EQ("template <\n"
9220             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9221             "void f() {}",
9222             format("template <\n"
9223                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9224                    "void f() {}",
9225                    Style));
9226 
9227   EXPECT_EQ(
9228       "template <\n"
9229       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9230       "void f() {}",
9231       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9232              "void f() {}",
9233              Style));
9234 
9235   EXPECT_EQ(
9236       "template <\n"
9237       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9238       "                                               // multiline\n"
9239       "void f() {}",
9240       format("template <\n"
9241              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9242              "                                              // multiline\n"
9243              "void f() {}",
9244              Style));
9245 
9246   EXPECT_EQ(
9247       "template <typename aaaaaaaaaa<\n"
9248       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9249       "void f() {}",
9250       format(
9251           "template <\n"
9252           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9253           "void f() {}",
9254           Style));
9255 }
9256 
9257 TEST_F(FormatTest, WrapsTemplateParameters) {
9258   FormatStyle Style = getLLVMStyle();
9259   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9260   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9261   verifyFormat(
9262       "template <typename... a> struct q {};\n"
9263       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9264       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9265       "    y;",
9266       Style);
9267   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9268   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9269   verifyFormat(
9270       "template <typename... a> struct r {};\n"
9271       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9272       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9273       "    y;",
9274       Style);
9275   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9276   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9277   verifyFormat("template <typename... a> struct s {};\n"
9278                "extern s<\n"
9279                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9280                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9281                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9282                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9283                "    y;",
9284                Style);
9285   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9286   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9287   verifyFormat("template <typename... a> struct t {};\n"
9288                "extern t<\n"
9289                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9290                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9291                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9292                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9293                "    y;",
9294                Style);
9295 }
9296 
9297 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9298   verifyFormat(
9299       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9300       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9301   verifyFormat(
9302       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9303       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9304       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9305 
9306   // FIXME: Should we have the extra indent after the second break?
9307   verifyFormat(
9308       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9309       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9310       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9311 
9312   verifyFormat(
9313       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9314       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9315 
9316   // Breaking at nested name specifiers is generally not desirable.
9317   verifyFormat(
9318       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9319       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9320 
9321   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9322                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9323                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9324                "                   aaaaaaaaaaaaaaaaaaaaa);",
9325                getLLVMStyleWithColumns(74));
9326 
9327   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9329                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9330 }
9331 
9332 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9333   verifyFormat("A<int> a;");
9334   verifyFormat("A<A<A<int>>> a;");
9335   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9336   verifyFormat("bool x = a < 1 || 2 > a;");
9337   verifyFormat("bool x = 5 < f<int>();");
9338   verifyFormat("bool x = f<int>() > 5;");
9339   verifyFormat("bool x = 5 < a<int>::x;");
9340   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9341   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9342 
9343   verifyGoogleFormat("A<A<int>> a;");
9344   verifyGoogleFormat("A<A<A<int>>> a;");
9345   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9346   verifyGoogleFormat("A<A<int> > a;");
9347   verifyGoogleFormat("A<A<A<int> > > a;");
9348   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9349   verifyGoogleFormat("A<::A<int>> a;");
9350   verifyGoogleFormat("A<::A> a;");
9351   verifyGoogleFormat("A< ::A> a;");
9352   verifyGoogleFormat("A< ::A<int> > a;");
9353   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9354   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9355   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9356   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9357   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9358             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9359 
9360   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9361 
9362   // template closer followed by a token that starts with > or =
9363   verifyFormat("bool b = a<1> > 1;");
9364   verifyFormat("bool b = a<1> >= 1;");
9365   verifyFormat("int i = a<1> >> 1;");
9366   FormatStyle Style = getLLVMStyle();
9367   Style.SpaceBeforeAssignmentOperators = false;
9368   verifyFormat("bool b= a<1> == 1;", Style);
9369   verifyFormat("a<int> = 1;", Style);
9370   verifyFormat("a<int> >>= 1;", Style);
9371 
9372   verifyFormat("test < a | b >> c;");
9373   verifyFormat("test<test<a | b>> c;");
9374   verifyFormat("test >> a >> b;");
9375   verifyFormat("test << a >> b;");
9376 
9377   verifyFormat("f<int>();");
9378   verifyFormat("template <typename T> void f() {}");
9379   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9380   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9381                "sizeof(char)>::type>;");
9382   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9383   verifyFormat("f(a.operator()<A>());");
9384   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9385                "      .template operator()<A>());",
9386                getLLVMStyleWithColumns(35));
9387 
9388   // Not template parameters.
9389   verifyFormat("return a < b && c > d;");
9390   verifyFormat("void f() {\n"
9391                "  while (a < b && c > d) {\n"
9392                "  }\n"
9393                "}");
9394   verifyFormat("template <typename... Types>\n"
9395                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9396 
9397   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9398                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9399                getLLVMStyleWithColumns(60));
9400   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9401   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9402   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9403   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9404 }
9405 
9406 TEST_F(FormatTest, UnderstandsShiftOperators) {
9407   verifyFormat("if (i < x >> 1)");
9408   verifyFormat("while (i < x >> 1)");
9409   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9410   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9411   verifyFormat(
9412       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9413   verifyFormat("Foo.call<Bar<Function>>()");
9414   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9415   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9416                "++i, v = v >> 1)");
9417   verifyFormat("if (w<u<v<x>>, 1>::t)");
9418 }
9419 
9420 TEST_F(FormatTest, BitshiftOperatorWidth) {
9421   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9422             "                   bar */",
9423             format("int    a=1<<2;  /* foo\n"
9424                    "                   bar */"));
9425 
9426   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9427             "                     bar */",
9428             format("int  b  =256>>1 ;  /* foo\n"
9429                    "                      bar */"));
9430 }
9431 
9432 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9433   verifyFormat("COMPARE(a, ==, b);");
9434   verifyFormat("auto s = sizeof...(Ts) - 1;");
9435 }
9436 
9437 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9438   verifyFormat("int A::*x;");
9439   verifyFormat("int (S::*func)(void *);");
9440   verifyFormat("void f() { int (S::*func)(void *); }");
9441   verifyFormat("typedef bool *(Class::*Member)() const;");
9442   verifyFormat("void f() {\n"
9443                "  (a->*f)();\n"
9444                "  a->*x;\n"
9445                "  (a.*f)();\n"
9446                "  ((*a).*f)();\n"
9447                "  a.*x;\n"
9448                "}");
9449   verifyFormat("void f() {\n"
9450                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9451                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9452                "}");
9453   verifyFormat(
9454       "(aaaaaaaaaa->*bbbbbbb)(\n"
9455       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9456   FormatStyle Style = getLLVMStyle();
9457   Style.PointerAlignment = FormatStyle::PAS_Left;
9458   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9459 }
9460 
9461 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9462   verifyFormat("int a = -2;");
9463   verifyFormat("f(-1, -2, -3);");
9464   verifyFormat("a[-1] = 5;");
9465   verifyFormat("int a = 5 + -2;");
9466   verifyFormat("if (i == -1) {\n}");
9467   verifyFormat("if (i != -1) {\n}");
9468   verifyFormat("if (i > -1) {\n}");
9469   verifyFormat("if (i < -1) {\n}");
9470   verifyFormat("++(a->f());");
9471   verifyFormat("--(a->f());");
9472   verifyFormat("(a->f())++;");
9473   verifyFormat("a[42]++;");
9474   verifyFormat("if (!(a->f())) {\n}");
9475   verifyFormat("if (!+i) {\n}");
9476   verifyFormat("~&a;");
9477 
9478   verifyFormat("a-- > b;");
9479   verifyFormat("b ? -a : c;");
9480   verifyFormat("n * sizeof char16;");
9481   verifyFormat("n * alignof char16;", getGoogleStyle());
9482   verifyFormat("sizeof(char);");
9483   verifyFormat("alignof(char);", getGoogleStyle());
9484 
9485   verifyFormat("return -1;");
9486   verifyFormat("throw -1;");
9487   verifyFormat("switch (a) {\n"
9488                "case -1:\n"
9489                "  break;\n"
9490                "}");
9491   verifyFormat("#define X -1");
9492   verifyFormat("#define X -kConstant");
9493 
9494   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9495   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9496 
9497   verifyFormat("int a = /* confusing comment */ -1;");
9498   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9499   verifyFormat("int a = i /* confusing comment */++;");
9500 
9501   verifyFormat("co_yield -1;");
9502   verifyFormat("co_return -1;");
9503 
9504   // Check that * is not treated as a binary operator when we set
9505   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9506   FormatStyle PASLeftStyle = getLLVMStyle();
9507   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9508   verifyFormat("co_return *a;", PASLeftStyle);
9509   verifyFormat("co_await *a;", PASLeftStyle);
9510   verifyFormat("co_yield *a", PASLeftStyle);
9511   verifyFormat("return *a;", PASLeftStyle);
9512 }
9513 
9514 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9515   verifyFormat("if (!aaaaaaaaaa( // break\n"
9516                "        aaaaa)) {\n"
9517                "}");
9518   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9519                "    aaaaa));");
9520   verifyFormat("*aaa = aaaaaaa( // break\n"
9521                "    bbbbbb);");
9522 }
9523 
9524 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9525   verifyFormat("bool operator<();");
9526   verifyFormat("bool operator>();");
9527   verifyFormat("bool operator=();");
9528   verifyFormat("bool operator==();");
9529   verifyFormat("bool operator!=();");
9530   verifyFormat("int operator+();");
9531   verifyFormat("int operator++();");
9532   verifyFormat("int operator++(int) volatile noexcept;");
9533   verifyFormat("bool operator,();");
9534   verifyFormat("bool operator();");
9535   verifyFormat("bool operator()();");
9536   verifyFormat("bool operator[]();");
9537   verifyFormat("operator bool();");
9538   verifyFormat("operator int();");
9539   verifyFormat("operator void *();");
9540   verifyFormat("operator SomeType<int>();");
9541   verifyFormat("operator SomeType<int, int>();");
9542   verifyFormat("operator SomeType<SomeType<int>>();");
9543   verifyFormat("operator< <>();");
9544   verifyFormat("operator<< <>();");
9545   verifyFormat("< <>");
9546 
9547   verifyFormat("void *operator new(std::size_t size);");
9548   verifyFormat("void *operator new[](std::size_t size);");
9549   verifyFormat("void operator delete(void *ptr);");
9550   verifyFormat("void operator delete[](void *ptr);");
9551   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9552                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9553   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9554                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9555 
9556   verifyFormat(
9557       "ostream &operator<<(ostream &OutputStream,\n"
9558       "                    SomeReallyLongType WithSomeReallyLongValue);");
9559   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9560                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9561                "  return left.group < right.group;\n"
9562                "}");
9563   verifyFormat("SomeType &operator=(const SomeType &S);");
9564   verifyFormat("f.template operator()<int>();");
9565 
9566   verifyGoogleFormat("operator void*();");
9567   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9568   verifyGoogleFormat("operator ::A();");
9569 
9570   verifyFormat("using A::operator+;");
9571   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9572                "int i;");
9573 
9574   // Calling an operator as a member function.
9575   verifyFormat("void f() { a.operator*(); }");
9576   verifyFormat("void f() { a.operator*(b & b); }");
9577   verifyFormat("void f() { a->operator&(a * b); }");
9578   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9579   // TODO: Calling an operator as a non-member function is hard to distinguish.
9580   // https://llvm.org/PR50629
9581   // verifyFormat("void f() { operator*(a & a); }");
9582   // verifyFormat("void f() { operator&(a, b * b); }");
9583 
9584   verifyFormat("::operator delete(foo);");
9585   verifyFormat("::operator new(n * sizeof(foo));");
9586   verifyFormat("foo() { ::operator delete(foo); }");
9587   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9588 }
9589 
9590 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9591   verifyFormat("void A::b() && {}");
9592   verifyFormat("void A::b() &&noexcept {}");
9593   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9594   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9595   verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;");
9596   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9597   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9598   verifyFormat("Deleted &operator=(const Deleted &) &;");
9599   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9600   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9601   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9602   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9603   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9604   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9605   verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}");
9606   verifyFormat("void Fn(T const &) const &;");
9607   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9608   verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;");
9609   verifyFormat("template <typename T>\n"
9610                "void F(T) && = delete;",
9611                getGoogleStyle());
9612 
9613   FormatStyle AlignLeft = getLLVMStyle();
9614   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9615   verifyFormat("void A::b() && {}", AlignLeft);
9616   verifyFormat("void A::b() && noexcept {}", AlignLeft);
9617   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9618   verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
9619                AlignLeft);
9620   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9621                AlignLeft);
9622   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9623   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9624   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9625   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9626   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9627   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9628   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9629   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9630   verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
9631                AlignLeft);
9632 
9633   FormatStyle AlignMiddle = getLLVMStyle();
9634   AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9635   verifyFormat("void A::b() && {}", AlignMiddle);
9636   verifyFormat("void A::b() && noexcept {}", AlignMiddle);
9637   verifyFormat("Deleted & operator=(const Deleted &) & = default;",
9638                AlignMiddle);
9639   verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
9640                AlignMiddle);
9641   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
9642                AlignMiddle);
9643   verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
9644   verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
9645   verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
9646   verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
9647   verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
9648   verifyFormat("auto Function(T) & -> void;", AlignMiddle);
9649   verifyFormat("void Fn(T const &) const &;", AlignMiddle);
9650   verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
9651   verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
9652                AlignMiddle);
9653 
9654   FormatStyle Spaces = getLLVMStyle();
9655   Spaces.SpacesInCStyleCastParentheses = true;
9656   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9657   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9658   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9659   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9660 
9661   Spaces.SpacesInCStyleCastParentheses = false;
9662   Spaces.SpacesInParentheses = true;
9663   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9664   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9665                Spaces);
9666   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9667   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9668 
9669   FormatStyle BreakTemplate = getLLVMStyle();
9670   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9671 
9672   verifyFormat("struct f {\n"
9673                "  template <class T>\n"
9674                "  int &foo(const std::string &str) &noexcept {}\n"
9675                "};",
9676                BreakTemplate);
9677 
9678   verifyFormat("struct f {\n"
9679                "  template <class T>\n"
9680                "  int &foo(const std::string &str) &&noexcept {}\n"
9681                "};",
9682                BreakTemplate);
9683 
9684   verifyFormat("struct f {\n"
9685                "  template <class T>\n"
9686                "  int &foo(const std::string &str) const &noexcept {}\n"
9687                "};",
9688                BreakTemplate);
9689 
9690   verifyFormat("struct f {\n"
9691                "  template <class T>\n"
9692                "  int &foo(const std::string &str) const &noexcept {}\n"
9693                "};",
9694                BreakTemplate);
9695 
9696   verifyFormat("struct f {\n"
9697                "  template <class T>\n"
9698                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9699                "};",
9700                BreakTemplate);
9701 
9702   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9703   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9704       FormatStyle::BTDS_Yes;
9705   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9706 
9707   verifyFormat("struct f {\n"
9708                "  template <class T>\n"
9709                "  int& foo(const std::string& str) & noexcept {}\n"
9710                "};",
9711                AlignLeftBreakTemplate);
9712 
9713   verifyFormat("struct f {\n"
9714                "  template <class T>\n"
9715                "  int& foo(const std::string& str) && noexcept {}\n"
9716                "};",
9717                AlignLeftBreakTemplate);
9718 
9719   verifyFormat("struct f {\n"
9720                "  template <class T>\n"
9721                "  int& foo(const std::string& str) const& noexcept {}\n"
9722                "};",
9723                AlignLeftBreakTemplate);
9724 
9725   verifyFormat("struct f {\n"
9726                "  template <class T>\n"
9727                "  int& foo(const std::string& str) const&& noexcept {}\n"
9728                "};",
9729                AlignLeftBreakTemplate);
9730 
9731   verifyFormat("struct f {\n"
9732                "  template <class T>\n"
9733                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9734                "};",
9735                AlignLeftBreakTemplate);
9736 
9737   // The `&` in `Type&` should not be confused with a trailing `&` of
9738   // DEPRECATED(reason) member function.
9739   verifyFormat("struct f {\n"
9740                "  template <class T>\n"
9741                "  DEPRECATED(reason)\n"
9742                "  Type &foo(arguments) {}\n"
9743                "};",
9744                BreakTemplate);
9745 
9746   verifyFormat("struct f {\n"
9747                "  template <class T>\n"
9748                "  DEPRECATED(reason)\n"
9749                "  Type& foo(arguments) {}\n"
9750                "};",
9751                AlignLeftBreakTemplate);
9752 
9753   verifyFormat("void (*foopt)(int) = &func;");
9754 
9755   FormatStyle DerivePointerAlignment = getLLVMStyle();
9756   DerivePointerAlignment.DerivePointerAlignment = true;
9757   // There's always a space between the function and its trailing qualifiers.
9758   // This isn't evidence for PAS_Right (or for PAS_Left).
9759   std::string Prefix = "void a() &;\n"
9760                        "void b() &;\n";
9761   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9762   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9763   // Same if the function is an overloaded operator, and with &&.
9764   Prefix = "void operator()() &&;\n"
9765            "void operator()() &&;\n";
9766   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9767   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9768   // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
9769   Prefix = "void a() const &;\n"
9770            "void b() const &;\n";
9771   EXPECT_EQ(Prefix + "int *x;",
9772             format(Prefix + "int* x;", DerivePointerAlignment));
9773 }
9774 
9775 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9776   verifyFormat("void f() {\n"
9777                "  A *a = new A;\n"
9778                "  A *a = new (placement) A;\n"
9779                "  delete a;\n"
9780                "  delete (A *)a;\n"
9781                "}");
9782   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9783                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9784   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9785                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9786                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9787   verifyFormat("delete[] h->p;");
9788   verifyFormat("delete[] (void *)p;");
9789 
9790   verifyFormat("void operator delete(void *foo) ATTRIB;");
9791   verifyFormat("void operator new(void *foo) ATTRIB;");
9792   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9793   verifyFormat("void operator delete(void *ptr) noexcept;");
9794 }
9795 
9796 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9797   verifyFormat("int *f(int *a) {}");
9798   verifyFormat("int main(int argc, char **argv) {}");
9799   verifyFormat("Test::Test(int b) : a(b * b) {}");
9800   verifyIndependentOfContext("f(a, *a);");
9801   verifyFormat("void g() { f(*a); }");
9802   verifyIndependentOfContext("int a = b * 10;");
9803   verifyIndependentOfContext("int a = 10 * b;");
9804   verifyIndependentOfContext("int a = b * c;");
9805   verifyIndependentOfContext("int a += b * c;");
9806   verifyIndependentOfContext("int a -= b * c;");
9807   verifyIndependentOfContext("int a *= b * c;");
9808   verifyIndependentOfContext("int a /= b * c;");
9809   verifyIndependentOfContext("int a = *b;");
9810   verifyIndependentOfContext("int a = *b * c;");
9811   verifyIndependentOfContext("int a = b * *c;");
9812   verifyIndependentOfContext("int a = b * (10);");
9813   verifyIndependentOfContext("S << b * (10);");
9814   verifyIndependentOfContext("return 10 * b;");
9815   verifyIndependentOfContext("return *b * *c;");
9816   verifyIndependentOfContext("return a & ~b;");
9817   verifyIndependentOfContext("f(b ? *c : *d);");
9818   verifyIndependentOfContext("int a = b ? *c : *d;");
9819   verifyIndependentOfContext("*b = a;");
9820   verifyIndependentOfContext("a * ~b;");
9821   verifyIndependentOfContext("a * !b;");
9822   verifyIndependentOfContext("a * +b;");
9823   verifyIndependentOfContext("a * -b;");
9824   verifyIndependentOfContext("a * ++b;");
9825   verifyIndependentOfContext("a * --b;");
9826   verifyIndependentOfContext("a[4] * b;");
9827   verifyIndependentOfContext("a[a * a] = 1;");
9828   verifyIndependentOfContext("f() * b;");
9829   verifyIndependentOfContext("a * [self dostuff];");
9830   verifyIndependentOfContext("int x = a * (a + b);");
9831   verifyIndependentOfContext("(a *)(a + b);");
9832   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9833   verifyIndependentOfContext("int *pa = (int *)&a;");
9834   verifyIndependentOfContext("return sizeof(int **);");
9835   verifyIndependentOfContext("return sizeof(int ******);");
9836   verifyIndependentOfContext("return (int **&)a;");
9837   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9838   verifyFormat("void f(Type (*parameter)[10]) {}");
9839   verifyFormat("void f(Type (&parameter)[10]) {}");
9840   verifyGoogleFormat("return sizeof(int**);");
9841   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9842   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9843   verifyFormat("auto a = [](int **&, int ***) {};");
9844   verifyFormat("auto PointerBinding = [](const char *S) {};");
9845   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9846   verifyFormat("[](const decltype(*a) &value) {}");
9847   verifyFormat("[](const typeof(*a) &value) {}");
9848   verifyFormat("[](const _Atomic(a *) &value) {}");
9849   verifyFormat("[](const __underlying_type(a) &value) {}");
9850   verifyFormat("decltype(a * b) F();");
9851   verifyFormat("typeof(a * b) F();");
9852   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9853   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9854   verifyIndependentOfContext("typedef void (*f)(int *a);");
9855   verifyIndependentOfContext("int i{a * b};");
9856   verifyIndependentOfContext("aaa && aaa->f();");
9857   verifyIndependentOfContext("int x = ~*p;");
9858   verifyFormat("Constructor() : a(a), area(width * height) {}");
9859   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9860   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9861   verifyFormat("void f() { f(a, c * d); }");
9862   verifyFormat("void f() { f(new a(), c * d); }");
9863   verifyFormat("void f(const MyOverride &override);");
9864   verifyFormat("void f(const MyFinal &final);");
9865   verifyIndependentOfContext("bool a = f() && override.f();");
9866   verifyIndependentOfContext("bool a = f() && final.f();");
9867 
9868   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9869 
9870   verifyIndependentOfContext("A<int *> a;");
9871   verifyIndependentOfContext("A<int **> a;");
9872   verifyIndependentOfContext("A<int *, int *> a;");
9873   verifyIndependentOfContext("A<int *[]> a;");
9874   verifyIndependentOfContext(
9875       "const char *const p = reinterpret_cast<const char *const>(q);");
9876   verifyIndependentOfContext("A<int **, int **> a;");
9877   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9878   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9879   verifyFormat("for (; a && b;) {\n}");
9880   verifyFormat("bool foo = true && [] { return false; }();");
9881 
9882   verifyFormat(
9883       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9884       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9885 
9886   verifyGoogleFormat("int const* a = &b;");
9887   verifyGoogleFormat("**outparam = 1;");
9888   verifyGoogleFormat("*outparam = a * b;");
9889   verifyGoogleFormat("int main(int argc, char** argv) {}");
9890   verifyGoogleFormat("A<int*> a;");
9891   verifyGoogleFormat("A<int**> a;");
9892   verifyGoogleFormat("A<int*, int*> a;");
9893   verifyGoogleFormat("A<int**, int**> a;");
9894   verifyGoogleFormat("f(b ? *c : *d);");
9895   verifyGoogleFormat("int a = b ? *c : *d;");
9896   verifyGoogleFormat("Type* t = **x;");
9897   verifyGoogleFormat("Type* t = *++*x;");
9898   verifyGoogleFormat("*++*x;");
9899   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9900   verifyGoogleFormat("Type* t = x++ * y;");
9901   verifyGoogleFormat(
9902       "const char* const p = reinterpret_cast<const char* const>(q);");
9903   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9904   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9905   verifyGoogleFormat("template <typename T>\n"
9906                      "void f(int i = 0, SomeType** temps = NULL);");
9907 
9908   FormatStyle Left = getLLVMStyle();
9909   Left.PointerAlignment = FormatStyle::PAS_Left;
9910   verifyFormat("x = *a(x) = *a(y);", Left);
9911   verifyFormat("for (;; *a = b) {\n}", Left);
9912   verifyFormat("return *this += 1;", Left);
9913   verifyFormat("throw *x;", Left);
9914   verifyFormat("delete *x;", Left);
9915   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9916   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9917   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9918   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9919   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9920   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9921   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9922   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9923   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9924 
9925   verifyIndependentOfContext("a = *(x + y);");
9926   verifyIndependentOfContext("a = &(x + y);");
9927   verifyIndependentOfContext("*(x + y).call();");
9928   verifyIndependentOfContext("&(x + y)->call();");
9929   verifyFormat("void f() { &(*I).first; }");
9930 
9931   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9932   verifyFormat("f(* /* confusing comment */ foo);");
9933   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9934   verifyFormat("void foo(int * // this is the first paramters\n"
9935                "         ,\n"
9936                "         int second);");
9937   verifyFormat("double term = a * // first\n"
9938                "              b;");
9939   verifyFormat(
9940       "int *MyValues = {\n"
9941       "    *A, // Operator detection might be confused by the '{'\n"
9942       "    *BB // Operator detection might be confused by previous comment\n"
9943       "};");
9944 
9945   verifyIndependentOfContext("if (int *a = &b)");
9946   verifyIndependentOfContext("if (int &a = *b)");
9947   verifyIndependentOfContext("if (a & b[i])");
9948   verifyIndependentOfContext("if constexpr (a & b[i])");
9949   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9950   verifyIndependentOfContext("if (a * (b * c))");
9951   verifyIndependentOfContext("if constexpr (a * (b * c))");
9952   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9953   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9954   verifyIndependentOfContext("if (*b[i])");
9955   verifyIndependentOfContext("if (int *a = (&b))");
9956   verifyIndependentOfContext("while (int *a = &b)");
9957   verifyIndependentOfContext("while (a * (b * c))");
9958   verifyIndependentOfContext("size = sizeof *a;");
9959   verifyIndependentOfContext("if (a && (b = c))");
9960   verifyFormat("void f() {\n"
9961                "  for (const int &v : Values) {\n"
9962                "  }\n"
9963                "}");
9964   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9965   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9966   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9967 
9968   verifyFormat("#define A (!a * b)");
9969   verifyFormat("#define MACRO     \\\n"
9970                "  int *i = a * b; \\\n"
9971                "  void f(a *b);",
9972                getLLVMStyleWithColumns(19));
9973 
9974   verifyIndependentOfContext("A = new SomeType *[Length];");
9975   verifyIndependentOfContext("A = new SomeType *[Length]();");
9976   verifyIndependentOfContext("T **t = new T *;");
9977   verifyIndependentOfContext("T **t = new T *();");
9978   verifyGoogleFormat("A = new SomeType*[Length]();");
9979   verifyGoogleFormat("A = new SomeType*[Length];");
9980   verifyGoogleFormat("T** t = new T*;");
9981   verifyGoogleFormat("T** t = new T*();");
9982 
9983   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9984   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9985   verifyFormat("template <bool a, bool b> "
9986                "typename t::if<x && y>::type f() {}");
9987   verifyFormat("template <int *y> f() {}");
9988   verifyFormat("vector<int *> v;");
9989   verifyFormat("vector<int *const> v;");
9990   verifyFormat("vector<int *const **const *> v;");
9991   verifyFormat("vector<int *volatile> v;");
9992   verifyFormat("vector<a *_Nonnull> v;");
9993   verifyFormat("vector<a *_Nullable> v;");
9994   verifyFormat("vector<a *_Null_unspecified> v;");
9995   verifyFormat("vector<a *__ptr32> v;");
9996   verifyFormat("vector<a *__ptr64> v;");
9997   verifyFormat("vector<a *__capability> v;");
9998   FormatStyle TypeMacros = getLLVMStyle();
9999   TypeMacros.TypenameMacros = {"LIST"};
10000   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
10001   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
10002   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
10003   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
10004   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
10005 
10006   FormatStyle CustomQualifier = getLLVMStyle();
10007   // Add identifiers that should not be parsed as a qualifier by default.
10008   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10009   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
10010   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
10011   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
10012   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
10013   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
10014   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
10015   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
10016   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
10017   verifyFormat("vector<a * _NotAQualifier> v;");
10018   verifyFormat("vector<a * __not_a_qualifier> v;");
10019   verifyFormat("vector<a * b> v;");
10020   verifyFormat("foo<b && false>();");
10021   verifyFormat("foo<b & 1>();");
10022   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
10023   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
10024   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
10025   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
10026   verifyFormat(
10027       "template <class T, class = typename std::enable_if<\n"
10028       "                       std::is_integral<T>::value &&\n"
10029       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
10030       "void F();",
10031       getLLVMStyleWithColumns(70));
10032   verifyFormat("template <class T,\n"
10033                "          class = typename std::enable_if<\n"
10034                "              std::is_integral<T>::value &&\n"
10035                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
10036                "          class U>\n"
10037                "void F();",
10038                getLLVMStyleWithColumns(70));
10039   verifyFormat(
10040       "template <class T,\n"
10041       "          class = typename ::std::enable_if<\n"
10042       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
10043       "void F();",
10044       getGoogleStyleWithColumns(68));
10045 
10046   verifyIndependentOfContext("MACRO(int *i);");
10047   verifyIndependentOfContext("MACRO(auto *a);");
10048   verifyIndependentOfContext("MACRO(const A *a);");
10049   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
10050   verifyIndependentOfContext("MACRO(decltype(A) *a);");
10051   verifyIndependentOfContext("MACRO(typeof(A) *a);");
10052   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
10053   verifyIndependentOfContext("MACRO(A *const a);");
10054   verifyIndependentOfContext("MACRO(A *restrict a);");
10055   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
10056   verifyIndependentOfContext("MACRO(A *__restrict a);");
10057   verifyIndependentOfContext("MACRO(A *volatile a);");
10058   verifyIndependentOfContext("MACRO(A *__volatile a);");
10059   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
10060   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
10061   verifyIndependentOfContext("MACRO(A *_Nullable a);");
10062   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
10063   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
10064   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
10065   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
10066   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
10067   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
10068   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
10069   verifyIndependentOfContext("MACRO(A *__capability);");
10070   verifyIndependentOfContext("MACRO(A &__capability);");
10071   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
10072   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
10073   // If we add __my_qualifier to AttributeMacros it should always be parsed as
10074   // a type declaration:
10075   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
10076   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
10077   // Also check that TypenameMacros prevents parsing it as multiplication:
10078   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
10079   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
10080 
10081   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
10082   verifyFormat("void f() { f(float{1}, a * a); }");
10083   verifyFormat("void f() { f(float(1), a * a); }");
10084 
10085   verifyFormat("f((void (*)(int))g);");
10086   verifyFormat("f((void (&)(int))g);");
10087   verifyFormat("f((void (^)(int))g);");
10088 
10089   // FIXME: Is there a way to make this work?
10090   // verifyIndependentOfContext("MACRO(A *a);");
10091   verifyFormat("MACRO(A &B);");
10092   verifyFormat("MACRO(A *B);");
10093   verifyFormat("void f() { MACRO(A * B); }");
10094   verifyFormat("void f() { MACRO(A & B); }");
10095 
10096   // This lambda was mis-formatted after D88956 (treating it as a binop):
10097   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10098   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10099   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10100   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10101 
10102   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10103   verifyFormat("return options != nullptr && operator==(*options);");
10104 
10105   EXPECT_EQ("#define OP(x)                                    \\\n"
10106             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10107             "    return s << a.DebugString();                 \\\n"
10108             "  }",
10109             format("#define OP(x) \\\n"
10110                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10111                    "    return s << a.DebugString(); \\\n"
10112                    "  }",
10113                    getLLVMStyleWithColumns(50)));
10114 
10115   // FIXME: We cannot handle this case yet; we might be able to figure out that
10116   // foo<x> d > v; doesn't make sense.
10117   verifyFormat("foo<a<b && c> d> v;");
10118 
10119   FormatStyle PointerMiddle = getLLVMStyle();
10120   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10121   verifyFormat("delete *x;", PointerMiddle);
10122   verifyFormat("int * x;", PointerMiddle);
10123   verifyFormat("int *[] x;", PointerMiddle);
10124   verifyFormat("template <int * y> f() {}", PointerMiddle);
10125   verifyFormat("int * f(int * a) {}", PointerMiddle);
10126   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10127   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10128   verifyFormat("A<int *> a;", PointerMiddle);
10129   verifyFormat("A<int **> a;", PointerMiddle);
10130   verifyFormat("A<int *, int *> a;", PointerMiddle);
10131   verifyFormat("A<int *[]> a;", PointerMiddle);
10132   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10133   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10134   verifyFormat("T ** t = new T *;", PointerMiddle);
10135 
10136   // Member function reference qualifiers aren't binary operators.
10137   verifyFormat("string // break\n"
10138                "operator()() & {}");
10139   verifyFormat("string // break\n"
10140                "operator()() && {}");
10141   verifyGoogleFormat("template <typename T>\n"
10142                      "auto x() & -> int {}");
10143 
10144   // Should be binary operators when used as an argument expression (overloaded
10145   // operator invoked as a member function).
10146   verifyFormat("void f() { a.operator()(a * a); }");
10147   verifyFormat("void f() { a->operator()(a & a); }");
10148   verifyFormat("void f() { a.operator()(*a & *a); }");
10149   verifyFormat("void f() { a->operator()(*a * *a); }");
10150 
10151   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10152   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10153 }
10154 
10155 TEST_F(FormatTest, UnderstandsAttributes) {
10156   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10157   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10158                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10159   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10160   FormatStyle AfterType = getLLVMStyle();
10161   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10162   verifyFormat("__attribute__((nodebug)) void\n"
10163                "foo() {}\n",
10164                AfterType);
10165   verifyFormat("__unused void\n"
10166                "foo() {}",
10167                AfterType);
10168 
10169   FormatStyle CustomAttrs = getLLVMStyle();
10170   CustomAttrs.AttributeMacros.push_back("__unused");
10171   CustomAttrs.AttributeMacros.push_back("__attr1");
10172   CustomAttrs.AttributeMacros.push_back("__attr2");
10173   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10174   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10175   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10176   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10177   // Check that it is parsed as a multiplication without AttributeMacros and
10178   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10179   verifyFormat("vector<SomeType * __attr1> v;");
10180   verifyFormat("vector<SomeType __attr1 *> v;");
10181   verifyFormat("vector<SomeType __attr1 *const> v;");
10182   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10183   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10184   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10185   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10186   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10187   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10188   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10189   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10190 
10191   // Check that these are not parsed as function declarations:
10192   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10193   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10194   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10195   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10196   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10197   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10198   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10199   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10200   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10201   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10202 }
10203 
10204 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10205   // Check that qualifiers on pointers don't break parsing of casts.
10206   verifyFormat("x = (foo *const)*v;");
10207   verifyFormat("x = (foo *volatile)*v;");
10208   verifyFormat("x = (foo *restrict)*v;");
10209   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10210   verifyFormat("x = (foo *_Nonnull)*v;");
10211   verifyFormat("x = (foo *_Nullable)*v;");
10212   verifyFormat("x = (foo *_Null_unspecified)*v;");
10213   verifyFormat("x = (foo *_Nonnull)*v;");
10214   verifyFormat("x = (foo *[[clang::attr]])*v;");
10215   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10216   verifyFormat("x = (foo *__ptr32)*v;");
10217   verifyFormat("x = (foo *__ptr64)*v;");
10218   verifyFormat("x = (foo *__capability)*v;");
10219 
10220   // Check that we handle multiple trailing qualifiers and skip them all to
10221   // determine that the expression is a cast to a pointer type.
10222   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10223   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10224   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10225   StringRef AllQualifiers =
10226       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10227       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10228   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10229   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10230 
10231   // Also check that address-of is not parsed as a binary bitwise-and:
10232   verifyFormat("x = (foo *const)&v;");
10233   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10234   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10235 
10236   // Check custom qualifiers:
10237   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10238   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10239   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10240   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10241   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10242                CustomQualifier);
10243   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10244                CustomQualifier);
10245 
10246   // Check that unknown identifiers result in binary operator parsing:
10247   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10248   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10249 }
10250 
10251 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10252   verifyFormat("SomeType s [[unused]] (InitValue);");
10253   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10254   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10255   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10256   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10257   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10258                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10259   verifyFormat("[[nodiscard]] bool f() { return false; }");
10260   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10261   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10262   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10263   verifyFormat("[[nodiscard]] ::qualified_type f();");
10264 
10265   // Make sure we do not mistake attributes for array subscripts.
10266   verifyFormat("int a() {}\n"
10267                "[[unused]] int b() {}\n");
10268   verifyFormat("NSArray *arr;\n"
10269                "arr[[Foo() bar]];");
10270 
10271   // On the other hand, we still need to correctly find array subscripts.
10272   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10273 
10274   // Make sure that we do not mistake Objective-C method inside array literals
10275   // as attributes, even if those method names are also keywords.
10276   verifyFormat("@[ [foo bar] ];");
10277   verifyFormat("@[ [NSArray class] ];");
10278   verifyFormat("@[ [foo enum] ];");
10279 
10280   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10281 
10282   // Make sure we do not parse attributes as lambda introducers.
10283   FormatStyle MultiLineFunctions = getLLVMStyle();
10284   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10285   verifyFormat("[[unused]] int b() {\n"
10286                "  return 42;\n"
10287                "}\n",
10288                MultiLineFunctions);
10289 }
10290 
10291 TEST_F(FormatTest, AttributeClass) {
10292   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10293   verifyFormat("class S {\n"
10294                "  S(S&&) = default;\n"
10295                "};",
10296                Style);
10297   verifyFormat("class [[nodiscard]] S {\n"
10298                "  S(S&&) = default;\n"
10299                "};",
10300                Style);
10301   verifyFormat("class __attribute((maybeunused)) S {\n"
10302                "  S(S&&) = default;\n"
10303                "};",
10304                Style);
10305   verifyFormat("struct S {\n"
10306                "  S(S&&) = default;\n"
10307                "};",
10308                Style);
10309   verifyFormat("struct [[nodiscard]] S {\n"
10310                "  S(S&&) = default;\n"
10311                "};",
10312                Style);
10313 }
10314 
10315 TEST_F(FormatTest, AttributesAfterMacro) {
10316   FormatStyle Style = getLLVMStyle();
10317   verifyFormat("MACRO;\n"
10318                "__attribute__((maybe_unused)) int foo() {\n"
10319                "  //...\n"
10320                "}");
10321 
10322   verifyFormat("MACRO;\n"
10323                "[[nodiscard]] int foo() {\n"
10324                "  //...\n"
10325                "}");
10326 
10327   EXPECT_EQ("MACRO\n\n"
10328             "__attribute__((maybe_unused)) int foo() {\n"
10329             "  //...\n"
10330             "}",
10331             format("MACRO\n\n"
10332                    "__attribute__((maybe_unused)) int foo() {\n"
10333                    "  //...\n"
10334                    "}"));
10335 
10336   EXPECT_EQ("MACRO\n\n"
10337             "[[nodiscard]] int foo() {\n"
10338             "  //...\n"
10339             "}",
10340             format("MACRO\n\n"
10341                    "[[nodiscard]] int foo() {\n"
10342                    "  //...\n"
10343                    "}"));
10344 }
10345 
10346 TEST_F(FormatTest, AttributePenaltyBreaking) {
10347   FormatStyle Style = getLLVMStyle();
10348   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10349                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10350                Style);
10351   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10352                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10353                Style);
10354   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10355                "shared_ptr<ALongTypeName> &C d) {\n}",
10356                Style);
10357 }
10358 
10359 TEST_F(FormatTest, UnderstandsEllipsis) {
10360   FormatStyle Style = getLLVMStyle();
10361   verifyFormat("int printf(const char *fmt, ...);");
10362   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10363   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10364 
10365   verifyFormat("template <int *...PP> a;", Style);
10366 
10367   Style.PointerAlignment = FormatStyle::PAS_Left;
10368   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10369 
10370   verifyFormat("template <int*... PP> a;", Style);
10371 
10372   Style.PointerAlignment = FormatStyle::PAS_Middle;
10373   verifyFormat("template <int *... PP> a;", Style);
10374 }
10375 
10376 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10377   EXPECT_EQ("int *a;\n"
10378             "int *a;\n"
10379             "int *a;",
10380             format("int *a;\n"
10381                    "int* a;\n"
10382                    "int *a;",
10383                    getGoogleStyle()));
10384   EXPECT_EQ("int* a;\n"
10385             "int* a;\n"
10386             "int* a;",
10387             format("int* a;\n"
10388                    "int* a;\n"
10389                    "int *a;",
10390                    getGoogleStyle()));
10391   EXPECT_EQ("int *a;\n"
10392             "int *a;\n"
10393             "int *a;",
10394             format("int *a;\n"
10395                    "int * a;\n"
10396                    "int *  a;",
10397                    getGoogleStyle()));
10398   EXPECT_EQ("auto x = [] {\n"
10399             "  int *a;\n"
10400             "  int *a;\n"
10401             "  int *a;\n"
10402             "};",
10403             format("auto x=[]{int *a;\n"
10404                    "int * a;\n"
10405                    "int *  a;};",
10406                    getGoogleStyle()));
10407 }
10408 
10409 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10410   verifyFormat("int f(int &&a) {}");
10411   verifyFormat("int f(int a, char &&b) {}");
10412   verifyFormat("void f() { int &&a = b; }");
10413   verifyGoogleFormat("int f(int a, char&& b) {}");
10414   verifyGoogleFormat("void f() { int&& a = b; }");
10415 
10416   verifyIndependentOfContext("A<int &&> a;");
10417   verifyIndependentOfContext("A<int &&, int &&> a;");
10418   verifyGoogleFormat("A<int&&> a;");
10419   verifyGoogleFormat("A<int&&, int&&> a;");
10420 
10421   // Not rvalue references:
10422   verifyFormat("template <bool B, bool C> class A {\n"
10423                "  static_assert(B && C, \"Something is wrong\");\n"
10424                "};");
10425   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10426   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10427   verifyFormat("#define A(a, b) (a && b)");
10428 }
10429 
10430 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10431   verifyFormat("void f() {\n"
10432                "  x[aaaaaaaaa -\n"
10433                "    b] = 23;\n"
10434                "}",
10435                getLLVMStyleWithColumns(15));
10436 }
10437 
10438 TEST_F(FormatTest, FormatsCasts) {
10439   verifyFormat("Type *A = static_cast<Type *>(P);");
10440   verifyFormat("Type *A = (Type *)P;");
10441   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10442   verifyFormat("int a = (int)(2.0f);");
10443   verifyFormat("int a = (int)2.0f;");
10444   verifyFormat("x[(int32)y];");
10445   verifyFormat("x = (int32)y;");
10446   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10447   verifyFormat("int a = (int)*b;");
10448   verifyFormat("int a = (int)2.0f;");
10449   verifyFormat("int a = (int)~0;");
10450   verifyFormat("int a = (int)++a;");
10451   verifyFormat("int a = (int)sizeof(int);");
10452   verifyFormat("int a = (int)+2;");
10453   verifyFormat("my_int a = (my_int)2.0f;");
10454   verifyFormat("my_int a = (my_int)sizeof(int);");
10455   verifyFormat("return (my_int)aaa;");
10456   verifyFormat("#define x ((int)-1)");
10457   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10458   verifyFormat("#define p(q) ((int *)&q)");
10459   verifyFormat("fn(a)(b) + 1;");
10460 
10461   verifyFormat("void f() { my_int a = (my_int)*b; }");
10462   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10463   verifyFormat("my_int a = (my_int)~0;");
10464   verifyFormat("my_int a = (my_int)++a;");
10465   verifyFormat("my_int a = (my_int)-2;");
10466   verifyFormat("my_int a = (my_int)1;");
10467   verifyFormat("my_int a = (my_int *)1;");
10468   verifyFormat("my_int a = (const my_int)-1;");
10469   verifyFormat("my_int a = (const my_int *)-1;");
10470   verifyFormat("my_int a = (my_int)(my_int)-1;");
10471   verifyFormat("my_int a = (ns::my_int)-2;");
10472   verifyFormat("case (my_int)ONE:");
10473   verifyFormat("auto x = (X)this;");
10474   // Casts in Obj-C style calls used to not be recognized as such.
10475   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10476 
10477   // FIXME: single value wrapped with paren will be treated as cast.
10478   verifyFormat("void f(int i = (kValue)*kMask) {}");
10479 
10480   verifyFormat("{ (void)F; }");
10481 
10482   // Don't break after a cast's
10483   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10484                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10485                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10486 
10487   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10488   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10489   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10490   verifyFormat("bool *y = (bool *)(void *)(x);");
10491   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10492   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10493   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10494   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10495 
10496   // These are not casts.
10497   verifyFormat("void f(int *) {}");
10498   verifyFormat("f(foo)->b;");
10499   verifyFormat("f(foo).b;");
10500   verifyFormat("f(foo)(b);");
10501   verifyFormat("f(foo)[b];");
10502   verifyFormat("[](foo) { return 4; }(bar);");
10503   verifyFormat("(*funptr)(foo)[4];");
10504   verifyFormat("funptrs[4](foo)[4];");
10505   verifyFormat("void f(int *);");
10506   verifyFormat("void f(int *) = 0;");
10507   verifyFormat("void f(SmallVector<int>) {}");
10508   verifyFormat("void f(SmallVector<int>);");
10509   verifyFormat("void f(SmallVector<int>) = 0;");
10510   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10511   verifyFormat("int a = sizeof(int) * b;");
10512   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10513   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10514   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10515   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10516 
10517   // These are not casts, but at some point were confused with casts.
10518   verifyFormat("virtual void foo(int *) override;");
10519   verifyFormat("virtual void foo(char &) const;");
10520   verifyFormat("virtual void foo(int *a, char *) const;");
10521   verifyFormat("int a = sizeof(int *) + b;");
10522   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10523   verifyFormat("bool b = f(g<int>) && c;");
10524   verifyFormat("typedef void (*f)(int i) func;");
10525   verifyFormat("void operator++(int) noexcept;");
10526   verifyFormat("void operator++(int &) noexcept;");
10527   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10528                "&) noexcept;");
10529   verifyFormat(
10530       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10531   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10532   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10533   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10534   verifyFormat("void operator delete(foo &) noexcept;");
10535   verifyFormat("void operator delete(foo) noexcept;");
10536   verifyFormat("void operator delete(int) noexcept;");
10537   verifyFormat("void operator delete(int &) noexcept;");
10538   verifyFormat("void operator delete(int &) volatile noexcept;");
10539   verifyFormat("void operator delete(int &) const");
10540   verifyFormat("void operator delete(int &) = default");
10541   verifyFormat("void operator delete(int &) = delete");
10542   verifyFormat("void operator delete(int &) [[noreturn]]");
10543   verifyFormat("void operator delete(int &) throw();");
10544   verifyFormat("void operator delete(int &) throw(int);");
10545   verifyFormat("auto operator delete(int &) -> int;");
10546   verifyFormat("auto operator delete(int &) override");
10547   verifyFormat("auto operator delete(int &) final");
10548 
10549   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10550                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10551   // FIXME: The indentation here is not ideal.
10552   verifyFormat(
10553       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10554       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10555       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10556 }
10557 
10558 TEST_F(FormatTest, FormatsFunctionTypes) {
10559   verifyFormat("A<bool()> a;");
10560   verifyFormat("A<SomeType()> a;");
10561   verifyFormat("A<void (*)(int, std::string)> a;");
10562   verifyFormat("A<void *(int)>;");
10563   verifyFormat("void *(*a)(int *, SomeType *);");
10564   verifyFormat("int (*func)(void *);");
10565   verifyFormat("void f() { int (*func)(void *); }");
10566   verifyFormat("template <class CallbackClass>\n"
10567                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10568 
10569   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10570   verifyGoogleFormat("void* (*a)(int);");
10571   verifyGoogleFormat(
10572       "template <class CallbackClass>\n"
10573       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10574 
10575   // Other constructs can look somewhat like function types:
10576   verifyFormat("A<sizeof(*x)> a;");
10577   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10578   verifyFormat("some_var = function(*some_pointer_var)[0];");
10579   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10580   verifyFormat("int x = f(&h)();");
10581   verifyFormat("returnsFunction(&param1, &param2)(param);");
10582   verifyFormat("std::function<\n"
10583                "    LooooooooooongTemplatedType<\n"
10584                "        SomeType>*(\n"
10585                "        LooooooooooooooooongType type)>\n"
10586                "    function;",
10587                getGoogleStyleWithColumns(40));
10588 }
10589 
10590 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10591   verifyFormat("A (*foo_)[6];");
10592   verifyFormat("vector<int> (*foo_)[6];");
10593 }
10594 
10595 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10596   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10597                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10598   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10599                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10600   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10601                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10602 
10603   // Different ways of ()-initializiation.
10604   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10605                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10606   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10607                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10608   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10609                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10610   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10611                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10612 
10613   // Lambdas should not confuse the variable declaration heuristic.
10614   verifyFormat("LooooooooooooooooongType\n"
10615                "    variable(nullptr, [](A *a) {});",
10616                getLLVMStyleWithColumns(40));
10617 }
10618 
10619 TEST_F(FormatTest, BreaksLongDeclarations) {
10620   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10621                "    AnotherNameForTheLongType;");
10622   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10623                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10624   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10625                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10626   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10627                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10628   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10629                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10630   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10631                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10632   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10633                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10634   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10635                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10636   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10637                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10638   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10639                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10640   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10641                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10642   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10643                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10644   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10645                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10646   FormatStyle Indented = getLLVMStyle();
10647   Indented.IndentWrappedFunctionNames = true;
10648   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10649                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10650                Indented);
10651   verifyFormat(
10652       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10653       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10654       Indented);
10655   verifyFormat(
10656       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10657       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10658       Indented);
10659   verifyFormat(
10660       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10661       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10662       Indented);
10663 
10664   // FIXME: Without the comment, this breaks after "(".
10665   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10666                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10667                getGoogleStyle());
10668 
10669   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10670                "                  int LoooooooooooooooooooongParam2) {}");
10671   verifyFormat(
10672       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10673       "                                   SourceLocation L, IdentifierIn *II,\n"
10674       "                                   Type *T) {}");
10675   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10676                "ReallyReaaallyLongFunctionName(\n"
10677                "    const std::string &SomeParameter,\n"
10678                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10679                "        &ReallyReallyLongParameterName,\n"
10680                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10681                "        &AnotherLongParameterName) {}");
10682   verifyFormat("template <typename A>\n"
10683                "SomeLoooooooooooooooooooooongType<\n"
10684                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10685                "Function() {}");
10686 
10687   verifyGoogleFormat(
10688       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10689       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10690   verifyGoogleFormat(
10691       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10692       "                                   SourceLocation L) {}");
10693   verifyGoogleFormat(
10694       "some_namespace::LongReturnType\n"
10695       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10696       "    int first_long_parameter, int second_parameter) {}");
10697 
10698   verifyGoogleFormat("template <typename T>\n"
10699                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10700                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10701   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10702                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10703 
10704   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10705                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10706                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10707   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10708                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10709                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10710   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10711                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10712                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10713                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10714 
10715   verifyFormat("template <typename T> // Templates on own line.\n"
10716                "static int            // Some comment.\n"
10717                "MyFunction(int a);",
10718                getLLVMStyle());
10719 }
10720 
10721 TEST_F(FormatTest, FormatsAccessModifiers) {
10722   FormatStyle Style = getLLVMStyle();
10723   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10724             FormatStyle::ELBAMS_LogicalBlock);
10725   verifyFormat("struct foo {\n"
10726                "private:\n"
10727                "  void f() {}\n"
10728                "\n"
10729                "private:\n"
10730                "  int i;\n"
10731                "\n"
10732                "protected:\n"
10733                "  int j;\n"
10734                "};\n",
10735                Style);
10736   verifyFormat("struct foo {\n"
10737                "private:\n"
10738                "  void f() {}\n"
10739                "\n"
10740                "private:\n"
10741                "  int i;\n"
10742                "\n"
10743                "protected:\n"
10744                "  int j;\n"
10745                "};\n",
10746                "struct foo {\n"
10747                "private:\n"
10748                "  void f() {}\n"
10749                "private:\n"
10750                "  int i;\n"
10751                "protected:\n"
10752                "  int j;\n"
10753                "};\n",
10754                Style);
10755   verifyFormat("struct foo { /* comment */\n"
10756                "private:\n"
10757                "  int i;\n"
10758                "  // comment\n"
10759                "private:\n"
10760                "  int j;\n"
10761                "};\n",
10762                Style);
10763   verifyFormat("struct foo {\n"
10764                "#ifdef FOO\n"
10765                "#endif\n"
10766                "private:\n"
10767                "  int i;\n"
10768                "#ifdef FOO\n"
10769                "private:\n"
10770                "#endif\n"
10771                "  int j;\n"
10772                "};\n",
10773                Style);
10774   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10775   verifyFormat("struct foo {\n"
10776                "private:\n"
10777                "  void f() {}\n"
10778                "private:\n"
10779                "  int i;\n"
10780                "protected:\n"
10781                "  int j;\n"
10782                "};\n",
10783                Style);
10784   verifyFormat("struct foo {\n"
10785                "private:\n"
10786                "  void f() {}\n"
10787                "private:\n"
10788                "  int i;\n"
10789                "protected:\n"
10790                "  int j;\n"
10791                "};\n",
10792                "struct foo {\n"
10793                "\n"
10794                "private:\n"
10795                "  void f() {}\n"
10796                "\n"
10797                "private:\n"
10798                "  int i;\n"
10799                "\n"
10800                "protected:\n"
10801                "  int j;\n"
10802                "};\n",
10803                Style);
10804   verifyFormat("struct foo { /* comment */\n"
10805                "private:\n"
10806                "  int i;\n"
10807                "  // comment\n"
10808                "private:\n"
10809                "  int j;\n"
10810                "};\n",
10811                "struct foo { /* comment */\n"
10812                "\n"
10813                "private:\n"
10814                "  int i;\n"
10815                "  // comment\n"
10816                "\n"
10817                "private:\n"
10818                "  int j;\n"
10819                "};\n",
10820                Style);
10821   verifyFormat("struct foo {\n"
10822                "#ifdef FOO\n"
10823                "#endif\n"
10824                "private:\n"
10825                "  int i;\n"
10826                "#ifdef FOO\n"
10827                "private:\n"
10828                "#endif\n"
10829                "  int j;\n"
10830                "};\n",
10831                "struct foo {\n"
10832                "#ifdef FOO\n"
10833                "#endif\n"
10834                "\n"
10835                "private:\n"
10836                "  int i;\n"
10837                "#ifdef FOO\n"
10838                "\n"
10839                "private:\n"
10840                "#endif\n"
10841                "  int j;\n"
10842                "};\n",
10843                Style);
10844   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10845   verifyFormat("struct foo {\n"
10846                "private:\n"
10847                "  void f() {}\n"
10848                "\n"
10849                "private:\n"
10850                "  int i;\n"
10851                "\n"
10852                "protected:\n"
10853                "  int j;\n"
10854                "};\n",
10855                Style);
10856   verifyFormat("struct foo {\n"
10857                "private:\n"
10858                "  void f() {}\n"
10859                "\n"
10860                "private:\n"
10861                "  int i;\n"
10862                "\n"
10863                "protected:\n"
10864                "  int j;\n"
10865                "};\n",
10866                "struct foo {\n"
10867                "private:\n"
10868                "  void f() {}\n"
10869                "private:\n"
10870                "  int i;\n"
10871                "protected:\n"
10872                "  int j;\n"
10873                "};\n",
10874                Style);
10875   verifyFormat("struct foo { /* comment */\n"
10876                "private:\n"
10877                "  int i;\n"
10878                "  // comment\n"
10879                "\n"
10880                "private:\n"
10881                "  int j;\n"
10882                "};\n",
10883                "struct foo { /* comment */\n"
10884                "private:\n"
10885                "  int i;\n"
10886                "  // comment\n"
10887                "\n"
10888                "private:\n"
10889                "  int j;\n"
10890                "};\n",
10891                Style);
10892   verifyFormat("struct foo {\n"
10893                "#ifdef FOO\n"
10894                "#endif\n"
10895                "\n"
10896                "private:\n"
10897                "  int i;\n"
10898                "#ifdef FOO\n"
10899                "\n"
10900                "private:\n"
10901                "#endif\n"
10902                "  int j;\n"
10903                "};\n",
10904                "struct foo {\n"
10905                "#ifdef FOO\n"
10906                "#endif\n"
10907                "private:\n"
10908                "  int i;\n"
10909                "#ifdef FOO\n"
10910                "private:\n"
10911                "#endif\n"
10912                "  int j;\n"
10913                "};\n",
10914                Style);
10915   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10916   EXPECT_EQ("struct foo {\n"
10917             "\n"
10918             "private:\n"
10919             "  void f() {}\n"
10920             "\n"
10921             "private:\n"
10922             "  int i;\n"
10923             "\n"
10924             "protected:\n"
10925             "  int j;\n"
10926             "};\n",
10927             format("struct foo {\n"
10928                    "\n"
10929                    "private:\n"
10930                    "  void f() {}\n"
10931                    "\n"
10932                    "private:\n"
10933                    "  int i;\n"
10934                    "\n"
10935                    "protected:\n"
10936                    "  int j;\n"
10937                    "};\n",
10938                    Style));
10939   verifyFormat("struct foo {\n"
10940                "private:\n"
10941                "  void f() {}\n"
10942                "private:\n"
10943                "  int i;\n"
10944                "protected:\n"
10945                "  int j;\n"
10946                "};\n",
10947                Style);
10948   EXPECT_EQ("struct foo { /* comment */\n"
10949             "\n"
10950             "private:\n"
10951             "  int i;\n"
10952             "  // comment\n"
10953             "\n"
10954             "private:\n"
10955             "  int j;\n"
10956             "};\n",
10957             format("struct foo { /* comment */\n"
10958                    "\n"
10959                    "private:\n"
10960                    "  int i;\n"
10961                    "  // comment\n"
10962                    "\n"
10963                    "private:\n"
10964                    "  int j;\n"
10965                    "};\n",
10966                    Style));
10967   verifyFormat("struct foo { /* comment */\n"
10968                "private:\n"
10969                "  int i;\n"
10970                "  // comment\n"
10971                "private:\n"
10972                "  int j;\n"
10973                "};\n",
10974                Style);
10975   EXPECT_EQ("struct foo {\n"
10976             "#ifdef FOO\n"
10977             "#endif\n"
10978             "\n"
10979             "private:\n"
10980             "  int i;\n"
10981             "#ifdef FOO\n"
10982             "\n"
10983             "private:\n"
10984             "#endif\n"
10985             "  int j;\n"
10986             "};\n",
10987             format("struct foo {\n"
10988                    "#ifdef FOO\n"
10989                    "#endif\n"
10990                    "\n"
10991                    "private:\n"
10992                    "  int i;\n"
10993                    "#ifdef FOO\n"
10994                    "\n"
10995                    "private:\n"
10996                    "#endif\n"
10997                    "  int j;\n"
10998                    "};\n",
10999                    Style));
11000   verifyFormat("struct foo {\n"
11001                "#ifdef FOO\n"
11002                "#endif\n"
11003                "private:\n"
11004                "  int i;\n"
11005                "#ifdef FOO\n"
11006                "private:\n"
11007                "#endif\n"
11008                "  int j;\n"
11009                "};\n",
11010                Style);
11011 
11012   FormatStyle NoEmptyLines = getLLVMStyle();
11013   NoEmptyLines.MaxEmptyLinesToKeep = 0;
11014   verifyFormat("struct foo {\n"
11015                "private:\n"
11016                "  void f() {}\n"
11017                "\n"
11018                "private:\n"
11019                "  int i;\n"
11020                "\n"
11021                "public:\n"
11022                "protected:\n"
11023                "  int j;\n"
11024                "};\n",
11025                NoEmptyLines);
11026 
11027   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11028   verifyFormat("struct foo {\n"
11029                "private:\n"
11030                "  void f() {}\n"
11031                "private:\n"
11032                "  int i;\n"
11033                "public:\n"
11034                "protected:\n"
11035                "  int j;\n"
11036                "};\n",
11037                NoEmptyLines);
11038 
11039   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11040   verifyFormat("struct foo {\n"
11041                "private:\n"
11042                "  void f() {}\n"
11043                "\n"
11044                "private:\n"
11045                "  int i;\n"
11046                "\n"
11047                "public:\n"
11048                "\n"
11049                "protected:\n"
11050                "  int j;\n"
11051                "};\n",
11052                NoEmptyLines);
11053 }
11054 
11055 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
11056 
11057   FormatStyle Style = getLLVMStyle();
11058   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
11059   verifyFormat("struct foo {\n"
11060                "private:\n"
11061                "  void f() {}\n"
11062                "\n"
11063                "private:\n"
11064                "  int i;\n"
11065                "\n"
11066                "protected:\n"
11067                "  int j;\n"
11068                "};\n",
11069                Style);
11070 
11071   // Check if lines are removed.
11072   verifyFormat("struct foo {\n"
11073                "private:\n"
11074                "  void f() {}\n"
11075                "\n"
11076                "private:\n"
11077                "  int i;\n"
11078                "\n"
11079                "protected:\n"
11080                "  int j;\n"
11081                "};\n",
11082                "struct foo {\n"
11083                "private:\n"
11084                "\n"
11085                "  void f() {}\n"
11086                "\n"
11087                "private:\n"
11088                "\n"
11089                "  int i;\n"
11090                "\n"
11091                "protected:\n"
11092                "\n"
11093                "  int j;\n"
11094                "};\n",
11095                Style);
11096 
11097   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11098   verifyFormat("struct foo {\n"
11099                "private:\n"
11100                "\n"
11101                "  void f() {}\n"
11102                "\n"
11103                "private:\n"
11104                "\n"
11105                "  int i;\n"
11106                "\n"
11107                "protected:\n"
11108                "\n"
11109                "  int j;\n"
11110                "};\n",
11111                Style);
11112 
11113   // Check if lines are added.
11114   verifyFormat("struct foo {\n"
11115                "private:\n"
11116                "\n"
11117                "  void f() {}\n"
11118                "\n"
11119                "private:\n"
11120                "\n"
11121                "  int i;\n"
11122                "\n"
11123                "protected:\n"
11124                "\n"
11125                "  int j;\n"
11126                "};\n",
11127                "struct foo {\n"
11128                "private:\n"
11129                "  void f() {}\n"
11130                "\n"
11131                "private:\n"
11132                "  int i;\n"
11133                "\n"
11134                "protected:\n"
11135                "  int j;\n"
11136                "};\n",
11137                Style);
11138 
11139   // Leave tests rely on the code layout, test::messUp can not be used.
11140   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11141   Style.MaxEmptyLinesToKeep = 0u;
11142   verifyFormat("struct foo {\n"
11143                "private:\n"
11144                "  void f() {}\n"
11145                "\n"
11146                "private:\n"
11147                "  int i;\n"
11148                "\n"
11149                "protected:\n"
11150                "  int j;\n"
11151                "};\n",
11152                Style);
11153 
11154   // Check if MaxEmptyLinesToKeep is respected.
11155   EXPECT_EQ("struct foo {\n"
11156             "private:\n"
11157             "  void f() {}\n"
11158             "\n"
11159             "private:\n"
11160             "  int i;\n"
11161             "\n"
11162             "protected:\n"
11163             "  int j;\n"
11164             "};\n",
11165             format("struct foo {\n"
11166                    "private:\n"
11167                    "\n\n\n"
11168                    "  void f() {}\n"
11169                    "\n"
11170                    "private:\n"
11171                    "\n\n\n"
11172                    "  int i;\n"
11173                    "\n"
11174                    "protected:\n"
11175                    "\n\n\n"
11176                    "  int j;\n"
11177                    "};\n",
11178                    Style));
11179 
11180   Style.MaxEmptyLinesToKeep = 1u;
11181   EXPECT_EQ("struct foo {\n"
11182             "private:\n"
11183             "\n"
11184             "  void f() {}\n"
11185             "\n"
11186             "private:\n"
11187             "\n"
11188             "  int i;\n"
11189             "\n"
11190             "protected:\n"
11191             "\n"
11192             "  int j;\n"
11193             "};\n",
11194             format("struct foo {\n"
11195                    "private:\n"
11196                    "\n"
11197                    "  void f() {}\n"
11198                    "\n"
11199                    "private:\n"
11200                    "\n"
11201                    "  int i;\n"
11202                    "\n"
11203                    "protected:\n"
11204                    "\n"
11205                    "  int j;\n"
11206                    "};\n",
11207                    Style));
11208   // Check if no lines are kept.
11209   EXPECT_EQ("struct foo {\n"
11210             "private:\n"
11211             "  void f() {}\n"
11212             "\n"
11213             "private:\n"
11214             "  int i;\n"
11215             "\n"
11216             "protected:\n"
11217             "  int j;\n"
11218             "};\n",
11219             format("struct foo {\n"
11220                    "private:\n"
11221                    "  void f() {}\n"
11222                    "\n"
11223                    "private:\n"
11224                    "  int i;\n"
11225                    "\n"
11226                    "protected:\n"
11227                    "  int j;\n"
11228                    "};\n",
11229                    Style));
11230   // Check if MaxEmptyLinesToKeep is respected.
11231   EXPECT_EQ("struct foo {\n"
11232             "private:\n"
11233             "\n"
11234             "  void f() {}\n"
11235             "\n"
11236             "private:\n"
11237             "\n"
11238             "  int i;\n"
11239             "\n"
11240             "protected:\n"
11241             "\n"
11242             "  int j;\n"
11243             "};\n",
11244             format("struct foo {\n"
11245                    "private:\n"
11246                    "\n\n\n"
11247                    "  void f() {}\n"
11248                    "\n"
11249                    "private:\n"
11250                    "\n\n\n"
11251                    "  int i;\n"
11252                    "\n"
11253                    "protected:\n"
11254                    "\n\n\n"
11255                    "  int j;\n"
11256                    "};\n",
11257                    Style));
11258 
11259   Style.MaxEmptyLinesToKeep = 10u;
11260   EXPECT_EQ("struct foo {\n"
11261             "private:\n"
11262             "\n\n\n"
11263             "  void f() {}\n"
11264             "\n"
11265             "private:\n"
11266             "\n\n\n"
11267             "  int i;\n"
11268             "\n"
11269             "protected:\n"
11270             "\n\n\n"
11271             "  int j;\n"
11272             "};\n",
11273             format("struct foo {\n"
11274                    "private:\n"
11275                    "\n\n\n"
11276                    "  void f() {}\n"
11277                    "\n"
11278                    "private:\n"
11279                    "\n\n\n"
11280                    "  int i;\n"
11281                    "\n"
11282                    "protected:\n"
11283                    "\n\n\n"
11284                    "  int j;\n"
11285                    "};\n",
11286                    Style));
11287 
11288   // Test with comments.
11289   Style = getLLVMStyle();
11290   verifyFormat("struct foo {\n"
11291                "private:\n"
11292                "  // comment\n"
11293                "  void f() {}\n"
11294                "\n"
11295                "private: /* comment */\n"
11296                "  int i;\n"
11297                "};\n",
11298                Style);
11299   verifyFormat("struct foo {\n"
11300                "private:\n"
11301                "  // comment\n"
11302                "  void f() {}\n"
11303                "\n"
11304                "private: /* comment */\n"
11305                "  int i;\n"
11306                "};\n",
11307                "struct foo {\n"
11308                "private:\n"
11309                "\n"
11310                "  // comment\n"
11311                "  void f() {}\n"
11312                "\n"
11313                "private: /* comment */\n"
11314                "\n"
11315                "  int i;\n"
11316                "};\n",
11317                Style);
11318 
11319   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11320   verifyFormat("struct foo {\n"
11321                "private:\n"
11322                "\n"
11323                "  // comment\n"
11324                "  void f() {}\n"
11325                "\n"
11326                "private: /* comment */\n"
11327                "\n"
11328                "  int i;\n"
11329                "};\n",
11330                "struct foo {\n"
11331                "private:\n"
11332                "  // comment\n"
11333                "  void f() {}\n"
11334                "\n"
11335                "private: /* comment */\n"
11336                "  int i;\n"
11337                "};\n",
11338                Style);
11339   verifyFormat("struct foo {\n"
11340                "private:\n"
11341                "\n"
11342                "  // comment\n"
11343                "  void f() {}\n"
11344                "\n"
11345                "private: /* comment */\n"
11346                "\n"
11347                "  int i;\n"
11348                "};\n",
11349                Style);
11350 
11351   // Test with preprocessor defines.
11352   Style = getLLVMStyle();
11353   verifyFormat("struct foo {\n"
11354                "private:\n"
11355                "#ifdef FOO\n"
11356                "#endif\n"
11357                "  void f() {}\n"
11358                "};\n",
11359                Style);
11360   verifyFormat("struct foo {\n"
11361                "private:\n"
11362                "#ifdef FOO\n"
11363                "#endif\n"
11364                "  void f() {}\n"
11365                "};\n",
11366                "struct foo {\n"
11367                "private:\n"
11368                "\n"
11369                "#ifdef FOO\n"
11370                "#endif\n"
11371                "  void f() {}\n"
11372                "};\n",
11373                Style);
11374 
11375   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11376   verifyFormat("struct foo {\n"
11377                "private:\n"
11378                "\n"
11379                "#ifdef FOO\n"
11380                "#endif\n"
11381                "  void f() {}\n"
11382                "};\n",
11383                "struct foo {\n"
11384                "private:\n"
11385                "#ifdef FOO\n"
11386                "#endif\n"
11387                "  void f() {}\n"
11388                "};\n",
11389                Style);
11390   verifyFormat("struct foo {\n"
11391                "private:\n"
11392                "\n"
11393                "#ifdef FOO\n"
11394                "#endif\n"
11395                "  void f() {}\n"
11396                "};\n",
11397                Style);
11398 }
11399 
11400 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11401   // Combined tests of EmptyLineAfterAccessModifier and
11402   // EmptyLineBeforeAccessModifier.
11403   FormatStyle Style = getLLVMStyle();
11404   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11405   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11406   verifyFormat("struct foo {\n"
11407                "private:\n"
11408                "\n"
11409                "protected:\n"
11410                "};\n",
11411                Style);
11412 
11413   Style.MaxEmptyLinesToKeep = 10u;
11414   // Both remove all new lines.
11415   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11416   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11417   verifyFormat("struct foo {\n"
11418                "private:\n"
11419                "protected:\n"
11420                "};\n",
11421                "struct foo {\n"
11422                "private:\n"
11423                "\n\n\n"
11424                "protected:\n"
11425                "};\n",
11426                Style);
11427 
11428   // Leave tests rely on the code layout, test::messUp can not be used.
11429   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11430   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11431   Style.MaxEmptyLinesToKeep = 10u;
11432   EXPECT_EQ("struct foo {\n"
11433             "private:\n"
11434             "\n\n\n"
11435             "protected:\n"
11436             "};\n",
11437             format("struct foo {\n"
11438                    "private:\n"
11439                    "\n\n\n"
11440                    "protected:\n"
11441                    "};\n",
11442                    Style));
11443   Style.MaxEmptyLinesToKeep = 3u;
11444   EXPECT_EQ("struct foo {\n"
11445             "private:\n"
11446             "\n\n\n"
11447             "protected:\n"
11448             "};\n",
11449             format("struct foo {\n"
11450                    "private:\n"
11451                    "\n\n\n"
11452                    "protected:\n"
11453                    "};\n",
11454                    Style));
11455   Style.MaxEmptyLinesToKeep = 1u;
11456   EXPECT_EQ("struct foo {\n"
11457             "private:\n"
11458             "\n\n\n"
11459             "protected:\n"
11460             "};\n",
11461             format("struct foo {\n"
11462                    "private:\n"
11463                    "\n\n\n"
11464                    "protected:\n"
11465                    "};\n",
11466                    Style)); // Based on new lines in original document and not
11467                             // on the setting.
11468 
11469   Style.MaxEmptyLinesToKeep = 10u;
11470   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11471   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11472   // Newlines are kept if they are greater than zero,
11473   // test::messUp removes all new lines which changes the logic
11474   EXPECT_EQ("struct foo {\n"
11475             "private:\n"
11476             "\n\n\n"
11477             "protected:\n"
11478             "};\n",
11479             format("struct foo {\n"
11480                    "private:\n"
11481                    "\n\n\n"
11482                    "protected:\n"
11483                    "};\n",
11484                    Style));
11485 
11486   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11487   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11488   // test::messUp removes all new lines which changes the logic
11489   EXPECT_EQ("struct foo {\n"
11490             "private:\n"
11491             "\n\n\n"
11492             "protected:\n"
11493             "};\n",
11494             format("struct foo {\n"
11495                    "private:\n"
11496                    "\n\n\n"
11497                    "protected:\n"
11498                    "};\n",
11499                    Style));
11500 
11501   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11502   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11503   EXPECT_EQ("struct foo {\n"
11504             "private:\n"
11505             "\n\n\n"
11506             "protected:\n"
11507             "};\n",
11508             format("struct foo {\n"
11509                    "private:\n"
11510                    "\n\n\n"
11511                    "protected:\n"
11512                    "};\n",
11513                    Style)); // test::messUp removes all new lines which changes
11514                             // the logic.
11515 
11516   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11517   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11518   verifyFormat("struct foo {\n"
11519                "private:\n"
11520                "protected:\n"
11521                "};\n",
11522                "struct foo {\n"
11523                "private:\n"
11524                "\n\n\n"
11525                "protected:\n"
11526                "};\n",
11527                Style);
11528 
11529   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11530   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11531   EXPECT_EQ("struct foo {\n"
11532             "private:\n"
11533             "\n\n\n"
11534             "protected:\n"
11535             "};\n",
11536             format("struct foo {\n"
11537                    "private:\n"
11538                    "\n\n\n"
11539                    "protected:\n"
11540                    "};\n",
11541                    Style)); // test::messUp removes all new lines which changes
11542                             // the logic.
11543 
11544   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11545   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11546   verifyFormat("struct foo {\n"
11547                "private:\n"
11548                "protected:\n"
11549                "};\n",
11550                "struct foo {\n"
11551                "private:\n"
11552                "\n\n\n"
11553                "protected:\n"
11554                "};\n",
11555                Style);
11556 
11557   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11558   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11559   verifyFormat("struct foo {\n"
11560                "private:\n"
11561                "protected:\n"
11562                "};\n",
11563                "struct foo {\n"
11564                "private:\n"
11565                "\n\n\n"
11566                "protected:\n"
11567                "};\n",
11568                Style);
11569 
11570   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11571   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11572   verifyFormat("struct foo {\n"
11573                "private:\n"
11574                "protected:\n"
11575                "};\n",
11576                "struct foo {\n"
11577                "private:\n"
11578                "\n\n\n"
11579                "protected:\n"
11580                "};\n",
11581                Style);
11582 
11583   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11584   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11585   verifyFormat("struct foo {\n"
11586                "private:\n"
11587                "protected:\n"
11588                "};\n",
11589                "struct foo {\n"
11590                "private:\n"
11591                "\n\n\n"
11592                "protected:\n"
11593                "};\n",
11594                Style);
11595 }
11596 
11597 TEST_F(FormatTest, FormatsArrays) {
11598   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11599                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11600   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11601                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11602   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11603                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11604   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11605                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11606   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11607                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11608   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11609                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11610                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11611   verifyFormat(
11612       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11613       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11614       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11615   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11616                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11617 
11618   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11619                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11620   verifyFormat(
11621       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11622       "                                  .aaaaaaa[0]\n"
11623       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11624   verifyFormat("a[::b::c];");
11625 
11626   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11627 
11628   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11629   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11630 }
11631 
11632 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11633   verifyFormat("(a)->b();");
11634   verifyFormat("--a;");
11635 }
11636 
11637 TEST_F(FormatTest, HandlesIncludeDirectives) {
11638   verifyFormat("#include <string>\n"
11639                "#include <a/b/c.h>\n"
11640                "#include \"a/b/string\"\n"
11641                "#include \"string.h\"\n"
11642                "#include \"string.h\"\n"
11643                "#include <a-a>\n"
11644                "#include < path with space >\n"
11645                "#include_next <test.h>"
11646                "#include \"abc.h\" // this is included for ABC\n"
11647                "#include \"some long include\" // with a comment\n"
11648                "#include \"some very long include path\"\n"
11649                "#include <some/very/long/include/path>\n",
11650                getLLVMStyleWithColumns(35));
11651   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11652   EXPECT_EQ("#include <a>", format("#include<a>"));
11653 
11654   verifyFormat("#import <string>");
11655   verifyFormat("#import <a/b/c.h>");
11656   verifyFormat("#import \"a/b/string\"");
11657   verifyFormat("#import \"string.h\"");
11658   verifyFormat("#import \"string.h\"");
11659   verifyFormat("#if __has_include(<strstream>)\n"
11660                "#include <strstream>\n"
11661                "#endif");
11662 
11663   verifyFormat("#define MY_IMPORT <a/b>");
11664 
11665   verifyFormat("#if __has_include(<a/b>)");
11666   verifyFormat("#if __has_include_next(<a/b>)");
11667   verifyFormat("#define F __has_include(<a/b>)");
11668   verifyFormat("#define F __has_include_next(<a/b>)");
11669 
11670   // Protocol buffer definition or missing "#".
11671   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11672                getLLVMStyleWithColumns(30));
11673 
11674   FormatStyle Style = getLLVMStyle();
11675   Style.AlwaysBreakBeforeMultilineStrings = true;
11676   Style.ColumnLimit = 0;
11677   verifyFormat("#import \"abc.h\"", Style);
11678 
11679   // But 'import' might also be a regular C++ namespace.
11680   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11681                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11682 }
11683 
11684 //===----------------------------------------------------------------------===//
11685 // Error recovery tests.
11686 //===----------------------------------------------------------------------===//
11687 
11688 TEST_F(FormatTest, IncompleteParameterLists) {
11689   FormatStyle NoBinPacking = getLLVMStyle();
11690   NoBinPacking.BinPackParameters = false;
11691   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11692                "                        double *min_x,\n"
11693                "                        double *max_x,\n"
11694                "                        double *min_y,\n"
11695                "                        double *max_y,\n"
11696                "                        double *min_z,\n"
11697                "                        double *max_z, ) {}",
11698                NoBinPacking);
11699 }
11700 
11701 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11702   verifyFormat("void f() { return; }\n42");
11703   verifyFormat("void f() {\n"
11704                "  if (0)\n"
11705                "    return;\n"
11706                "}\n"
11707                "42");
11708   verifyFormat("void f() { return }\n42");
11709   verifyFormat("void f() {\n"
11710                "  if (0)\n"
11711                "    return\n"
11712                "}\n"
11713                "42");
11714 }
11715 
11716 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11717   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11718   EXPECT_EQ("void f() {\n"
11719             "  if (a)\n"
11720             "    return\n"
11721             "}",
11722             format("void  f  (  )  {  if  ( a )  return  }"));
11723   EXPECT_EQ("namespace N {\n"
11724             "void f()\n"
11725             "}",
11726             format("namespace  N  {  void f()  }"));
11727   EXPECT_EQ("namespace N {\n"
11728             "void f() {}\n"
11729             "void g()\n"
11730             "} // namespace N",
11731             format("namespace N  { void f( ) { } void g( ) }"));
11732 }
11733 
11734 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11735   verifyFormat("int aaaaaaaa =\n"
11736                "    // Overlylongcomment\n"
11737                "    b;",
11738                getLLVMStyleWithColumns(20));
11739   verifyFormat("function(\n"
11740                "    ShortArgument,\n"
11741                "    LoooooooooooongArgument);\n",
11742                getLLVMStyleWithColumns(20));
11743 }
11744 
11745 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11746   verifyFormat("public:");
11747   verifyFormat("class A {\n"
11748                "public\n"
11749                "  void f() {}\n"
11750                "};");
11751   verifyFormat("public\n"
11752                "int qwerty;");
11753   verifyFormat("public\n"
11754                "B {}");
11755   verifyFormat("public\n"
11756                "{}");
11757   verifyFormat("public\n"
11758                "B { int x; }");
11759 }
11760 
11761 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11762   verifyFormat("{");
11763   verifyFormat("#})");
11764   verifyNoCrash("(/**/[:!] ?[).");
11765 }
11766 
11767 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11768   // Found by oss-fuzz:
11769   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11770   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11771   Style.ColumnLimit = 60;
11772   verifyNoCrash(
11773       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11774       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11775       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11776       Style);
11777 }
11778 
11779 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11780   verifyFormat("do {\n}");
11781   verifyFormat("do {\n}\n"
11782                "f();");
11783   verifyFormat("do {\n}\n"
11784                "wheeee(fun);");
11785   verifyFormat("do {\n"
11786                "  f();\n"
11787                "}");
11788 }
11789 
11790 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11791   verifyFormat("if {\n  foo;\n  foo();\n}");
11792   verifyFormat("switch {\n  foo;\n  foo();\n}");
11793   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11794   verifyFormat("while {\n  foo;\n  foo();\n}");
11795   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11796 }
11797 
11798 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11799   verifyIncompleteFormat("namespace {\n"
11800                          "class Foo { Foo (\n"
11801                          "};\n"
11802                          "} // namespace");
11803 }
11804 
11805 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11806   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11807   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11808   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11809   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11810 
11811   EXPECT_EQ("{\n"
11812             "  {\n"
11813             "    breakme(\n"
11814             "        qwe);\n"
11815             "  }\n",
11816             format("{\n"
11817                    "    {\n"
11818                    " breakme(qwe);\n"
11819                    "}\n",
11820                    getLLVMStyleWithColumns(10)));
11821 }
11822 
11823 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11824   verifyFormat("int x = {\n"
11825                "    avariable,\n"
11826                "    b(alongervariable)};",
11827                getLLVMStyleWithColumns(25));
11828 }
11829 
11830 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11831   verifyFormat("return (a)(b){1, 2, 3};");
11832 }
11833 
11834 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11835   verifyFormat("vector<int> x{1, 2, 3, 4};");
11836   verifyFormat("vector<int> x{\n"
11837                "    1,\n"
11838                "    2,\n"
11839                "    3,\n"
11840                "    4,\n"
11841                "};");
11842   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11843   verifyFormat("f({1, 2});");
11844   verifyFormat("auto v = Foo{-1};");
11845   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11846   verifyFormat("Class::Class : member{1, 2, 3} {}");
11847   verifyFormat("new vector<int>{1, 2, 3};");
11848   verifyFormat("new int[3]{1, 2, 3};");
11849   verifyFormat("new int{1};");
11850   verifyFormat("return {arg1, arg2};");
11851   verifyFormat("return {arg1, SomeType{parameter}};");
11852   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11853   verifyFormat("new T{arg1, arg2};");
11854   verifyFormat("f(MyMap[{composite, key}]);");
11855   verifyFormat("class Class {\n"
11856                "  T member = {arg1, arg2};\n"
11857                "};");
11858   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11859   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11860   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11861   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11862   verifyFormat("int a = std::is_integral<int>{} + 0;");
11863 
11864   verifyFormat("int foo(int i) { return fo1{}(i); }");
11865   verifyFormat("int foo(int i) { return fo1{}(i); }");
11866   verifyFormat("auto i = decltype(x){};");
11867   verifyFormat("auto i = typeof(x){};");
11868   verifyFormat("auto i = _Atomic(x){};");
11869   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11870   verifyFormat("Node n{1, Node{1000}, //\n"
11871                "       2};");
11872   verifyFormat("Aaaa aaaaaaa{\n"
11873                "    {\n"
11874                "        aaaa,\n"
11875                "    },\n"
11876                "};");
11877   verifyFormat("class C : public D {\n"
11878                "  SomeClass SC{2};\n"
11879                "};");
11880   verifyFormat("class C : public A {\n"
11881                "  class D : public B {\n"
11882                "    void f() { int i{2}; }\n"
11883                "  };\n"
11884                "};");
11885   verifyFormat("#define A {a, a},");
11886   // Don't confuse braced list initializers with compound statements.
11887   verifyFormat(
11888       "class A {\n"
11889       "  A() : a{} {}\n"
11890       "  A(int b) : b(b) {}\n"
11891       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11892       "  int a, b;\n"
11893       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11894       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11895       "{}\n"
11896       "};");
11897 
11898   // Avoid breaking between equal sign and opening brace
11899   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11900   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11901   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11902                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11903                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11904                "     {\"ccccccccccccccccccccc\", 2}};",
11905                AvoidBreakingFirstArgument);
11906 
11907   // Binpacking only if there is no trailing comma
11908   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11909                "                      cccccccccc, dddddddddd};",
11910                getLLVMStyleWithColumns(50));
11911   verifyFormat("const Aaaaaa aaaaa = {\n"
11912                "    aaaaaaaaaaa,\n"
11913                "    bbbbbbbbbbb,\n"
11914                "    ccccccccccc,\n"
11915                "    ddddddddddd,\n"
11916                "};",
11917                getLLVMStyleWithColumns(50));
11918 
11919   // Cases where distinguising braced lists and blocks is hard.
11920   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11921   verifyFormat("void f() {\n"
11922                "  return; // comment\n"
11923                "}\n"
11924                "SomeType t;");
11925   verifyFormat("void f() {\n"
11926                "  if (a) {\n"
11927                "    f();\n"
11928                "  }\n"
11929                "}\n"
11930                "SomeType t;");
11931 
11932   // In combination with BinPackArguments = false.
11933   FormatStyle NoBinPacking = getLLVMStyle();
11934   NoBinPacking.BinPackArguments = false;
11935   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11936                "                      bbbbb,\n"
11937                "                      ccccc,\n"
11938                "                      ddddd,\n"
11939                "                      eeeee,\n"
11940                "                      ffffff,\n"
11941                "                      ggggg,\n"
11942                "                      hhhhhh,\n"
11943                "                      iiiiii,\n"
11944                "                      jjjjjj,\n"
11945                "                      kkkkkk};",
11946                NoBinPacking);
11947   verifyFormat("const Aaaaaa aaaaa = {\n"
11948                "    aaaaa,\n"
11949                "    bbbbb,\n"
11950                "    ccccc,\n"
11951                "    ddddd,\n"
11952                "    eeeee,\n"
11953                "    ffffff,\n"
11954                "    ggggg,\n"
11955                "    hhhhhh,\n"
11956                "    iiiiii,\n"
11957                "    jjjjjj,\n"
11958                "    kkkkkk,\n"
11959                "};",
11960                NoBinPacking);
11961   verifyFormat(
11962       "const Aaaaaa aaaaa = {\n"
11963       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11964       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11965       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11966       "};",
11967       NoBinPacking);
11968 
11969   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11970   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11971             "    CDDDP83848_BMCR_REGISTER,\n"
11972             "    CDDDP83848_BMSR_REGISTER,\n"
11973             "    CDDDP83848_RBR_REGISTER};",
11974             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11975                    "                                CDDDP83848_BMSR_REGISTER,\n"
11976                    "                                CDDDP83848_RBR_REGISTER};",
11977                    NoBinPacking));
11978 
11979   // FIXME: The alignment of these trailing comments might be bad. Then again,
11980   // this might be utterly useless in real code.
11981   verifyFormat("Constructor::Constructor()\n"
11982                "    : some_value{         //\n"
11983                "                 aaaaaaa, //\n"
11984                "                 bbbbbbb} {}");
11985 
11986   // In braced lists, the first comment is always assumed to belong to the
11987   // first element. Thus, it can be moved to the next or previous line as
11988   // appropriate.
11989   EXPECT_EQ("function({// First element:\n"
11990             "          1,\n"
11991             "          // Second element:\n"
11992             "          2});",
11993             format("function({\n"
11994                    "    // First element:\n"
11995                    "    1,\n"
11996                    "    // Second element:\n"
11997                    "    2});"));
11998   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11999             "    // First element:\n"
12000             "    1,\n"
12001             "    // Second element:\n"
12002             "    2};",
12003             format("std::vector<int> MyNumbers{// First element:\n"
12004                    "                           1,\n"
12005                    "                           // Second element:\n"
12006                    "                           2};",
12007                    getLLVMStyleWithColumns(30)));
12008   // A trailing comma should still lead to an enforced line break and no
12009   // binpacking.
12010   EXPECT_EQ("vector<int> SomeVector = {\n"
12011             "    // aaa\n"
12012             "    1,\n"
12013             "    2,\n"
12014             "};",
12015             format("vector<int> SomeVector = { // aaa\n"
12016                    "    1, 2, };"));
12017 
12018   // C++11 brace initializer list l-braces should not be treated any differently
12019   // when breaking before lambda bodies is enabled
12020   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
12021   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
12022   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
12023   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
12024   verifyFormat(
12025       "std::runtime_error{\n"
12026       "    \"Long string which will force a break onto the next line...\"};",
12027       BreakBeforeLambdaBody);
12028 
12029   FormatStyle ExtraSpaces = getLLVMStyle();
12030   ExtraSpaces.Cpp11BracedListStyle = false;
12031   ExtraSpaces.ColumnLimit = 75;
12032   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
12033   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
12034   verifyFormat("f({ 1, 2 });", ExtraSpaces);
12035   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
12036   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
12037   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
12038   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
12039   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
12040   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
12041   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
12042   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
12043   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
12044   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
12045   verifyFormat("class Class {\n"
12046                "  T member = { arg1, arg2 };\n"
12047                "};",
12048                ExtraSpaces);
12049   verifyFormat(
12050       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12051       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
12052       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
12053       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
12054       ExtraSpaces);
12055   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
12056   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
12057                ExtraSpaces);
12058   verifyFormat(
12059       "someFunction(OtherParam,\n"
12060       "             BracedList{ // comment 1 (Forcing interesting break)\n"
12061       "                         param1, param2,\n"
12062       "                         // comment 2\n"
12063       "                         param3, param4 });",
12064       ExtraSpaces);
12065   verifyFormat(
12066       "std::this_thread::sleep_for(\n"
12067       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
12068       ExtraSpaces);
12069   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
12070                "    aaaaaaa,\n"
12071                "    aaaaaaaaaa,\n"
12072                "    aaaaa,\n"
12073                "    aaaaaaaaaaaaaaa,\n"
12074                "    aaa,\n"
12075                "    aaaaaaaaaa,\n"
12076                "    a,\n"
12077                "    aaaaaaaaaaaaaaaaaaaaa,\n"
12078                "    aaaaaaaaaaaa,\n"
12079                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
12080                "    aaaaaaa,\n"
12081                "    a};");
12082   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
12083   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
12084   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
12085 
12086   // Avoid breaking between initializer/equal sign and opening brace
12087   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12088   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12089                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12090                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12091                "  { \"ccccccccccccccccccccc\", 2 }\n"
12092                "};",
12093                ExtraSpaces);
12094   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12095                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12096                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12097                "  { \"ccccccccccccccccccccc\", 2 }\n"
12098                "};",
12099                ExtraSpaces);
12100 
12101   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12102   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12103   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12104   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12105 
12106   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12107   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12108   SpaceBetweenBraces.SpacesInParentheses = true;
12109   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12110   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12111   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12112   verifyFormat("vector< int > x{ // comment 1\n"
12113                "                 1, 2, 3, 4 };",
12114                SpaceBetweenBraces);
12115   SpaceBetweenBraces.ColumnLimit = 20;
12116   EXPECT_EQ("vector< int > x{\n"
12117             "    1, 2, 3, 4 };",
12118             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12119   SpaceBetweenBraces.ColumnLimit = 24;
12120   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12121             "                 3, 4 };",
12122             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12123   EXPECT_EQ("vector< int > x{\n"
12124             "    1,\n"
12125             "    2,\n"
12126             "    3,\n"
12127             "    4,\n"
12128             "};",
12129             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12130   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12131   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12132   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12133 }
12134 
12135 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12136   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12137                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12138                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12139                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12140                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12141                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12142   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12143                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12144                "                 1, 22, 333, 4444, 55555, //\n"
12145                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12146                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12147   verifyFormat(
12148       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12149       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12150       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12151       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12152       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12153       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12154       "                 7777777};");
12155   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12156                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12157                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12158   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12159                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12160                "    // Separating comment.\n"
12161                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12162   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12163                "    // Leading comment\n"
12164                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12165                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12166   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12167                "                 1, 1, 1, 1};",
12168                getLLVMStyleWithColumns(39));
12169   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12170                "                 1, 1, 1, 1};",
12171                getLLVMStyleWithColumns(38));
12172   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12173                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12174                getLLVMStyleWithColumns(43));
12175   verifyFormat(
12176       "static unsigned SomeValues[10][3] = {\n"
12177       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12178       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12179   verifyFormat("static auto fields = new vector<string>{\n"
12180                "    \"aaaaaaaaaaaaa\",\n"
12181                "    \"aaaaaaaaaaaaa\",\n"
12182                "    \"aaaaaaaaaaaa\",\n"
12183                "    \"aaaaaaaaaaaaaa\",\n"
12184                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12185                "    \"aaaaaaaaaaaa\",\n"
12186                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12187                "};");
12188   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12189   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12190                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12191                "                 3, cccccccccccccccccccccc};",
12192                getLLVMStyleWithColumns(60));
12193 
12194   // Trailing commas.
12195   verifyFormat("vector<int> x = {\n"
12196                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12197                "};",
12198                getLLVMStyleWithColumns(39));
12199   verifyFormat("vector<int> x = {\n"
12200                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12201                "};",
12202                getLLVMStyleWithColumns(39));
12203   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12204                "                 1, 1, 1, 1,\n"
12205                "                 /**/ /**/};",
12206                getLLVMStyleWithColumns(39));
12207 
12208   // Trailing comment in the first line.
12209   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12210                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12211                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12212                "    11111111,   22222222,   333333333,   44444444};");
12213   // Trailing comment in the last line.
12214   verifyFormat("int aaaaa[] = {\n"
12215                "    1, 2, 3, // comment\n"
12216                "    4, 5, 6  // comment\n"
12217                "};");
12218 
12219   // With nested lists, we should either format one item per line or all nested
12220   // lists one on line.
12221   // FIXME: For some nested lists, we can do better.
12222   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12223                "        {aaaaaaaaaaaaaaaaaaa},\n"
12224                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12225                "        {aaaaaaaaaaaaaaaaa}};",
12226                getLLVMStyleWithColumns(60));
12227   verifyFormat(
12228       "SomeStruct my_struct_array = {\n"
12229       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12230       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12231       "    {aaa, aaa},\n"
12232       "    {aaa, aaa},\n"
12233       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12234       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12235       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12236 
12237   // No column layout should be used here.
12238   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12239                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12240 
12241   verifyNoCrash("a<,");
12242 
12243   // No braced initializer here.
12244   verifyFormat("void f() {\n"
12245                "  struct Dummy {};\n"
12246                "  f(v);\n"
12247                "}");
12248 
12249   // Long lists should be formatted in columns even if they are nested.
12250   verifyFormat(
12251       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12252       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12253       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12254       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12255       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12256       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12257 
12258   // Allow "single-column" layout even if that violates the column limit. There
12259   // isn't going to be a better way.
12260   verifyFormat("std::vector<int> a = {\n"
12261                "    aaaaaaaa,\n"
12262                "    aaaaaaaa,\n"
12263                "    aaaaaaaa,\n"
12264                "    aaaaaaaa,\n"
12265                "    aaaaaaaaaa,\n"
12266                "    aaaaaaaa,\n"
12267                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12268                getLLVMStyleWithColumns(30));
12269   verifyFormat("vector<int> aaaa = {\n"
12270                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12271                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12272                "    aaaaaa.aaaaaaa,\n"
12273                "    aaaaaa.aaaaaaa,\n"
12274                "    aaaaaa.aaaaaaa,\n"
12275                "    aaaaaa.aaaaaaa,\n"
12276                "};");
12277 
12278   // Don't create hanging lists.
12279   verifyFormat("someFunction(Param, {List1, List2,\n"
12280                "                     List3});",
12281                getLLVMStyleWithColumns(35));
12282   verifyFormat("someFunction(Param, Param,\n"
12283                "             {List1, List2,\n"
12284                "              List3});",
12285                getLLVMStyleWithColumns(35));
12286   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12287                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12288 }
12289 
12290 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12291   FormatStyle DoNotMerge = getLLVMStyle();
12292   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12293 
12294   verifyFormat("void f() { return 42; }");
12295   verifyFormat("void f() {\n"
12296                "  return 42;\n"
12297                "}",
12298                DoNotMerge);
12299   verifyFormat("void f() {\n"
12300                "  // Comment\n"
12301                "}");
12302   verifyFormat("{\n"
12303                "#error {\n"
12304                "  int a;\n"
12305                "}");
12306   verifyFormat("{\n"
12307                "  int a;\n"
12308                "#error {\n"
12309                "}");
12310   verifyFormat("void f() {} // comment");
12311   verifyFormat("void f() { int a; } // comment");
12312   verifyFormat("void f() {\n"
12313                "} // comment",
12314                DoNotMerge);
12315   verifyFormat("void f() {\n"
12316                "  int a;\n"
12317                "} // comment",
12318                DoNotMerge);
12319   verifyFormat("void f() {\n"
12320                "} // comment",
12321                getLLVMStyleWithColumns(15));
12322 
12323   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12324   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12325 
12326   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12327   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12328   verifyFormat("class C {\n"
12329                "  C()\n"
12330                "      : iiiiiiii(nullptr),\n"
12331                "        kkkkkkk(nullptr),\n"
12332                "        mmmmmmm(nullptr),\n"
12333                "        nnnnnnn(nullptr) {}\n"
12334                "};",
12335                getGoogleStyle());
12336 
12337   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12338   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12339   EXPECT_EQ("class C {\n"
12340             "  A() : b(0) {}\n"
12341             "};",
12342             format("class C{A():b(0){}};", NoColumnLimit));
12343   EXPECT_EQ("A()\n"
12344             "    : b(0) {\n"
12345             "}",
12346             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12347 
12348   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12349   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12350       FormatStyle::SFS_None;
12351   EXPECT_EQ("A()\n"
12352             "    : b(0) {\n"
12353             "}",
12354             format("A():b(0){}", DoNotMergeNoColumnLimit));
12355   EXPECT_EQ("A()\n"
12356             "    : b(0) {\n"
12357             "}",
12358             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12359 
12360   verifyFormat("#define A          \\\n"
12361                "  void f() {       \\\n"
12362                "    int i;         \\\n"
12363                "  }",
12364                getLLVMStyleWithColumns(20));
12365   verifyFormat("#define A           \\\n"
12366                "  void f() { int i; }",
12367                getLLVMStyleWithColumns(21));
12368   verifyFormat("#define A            \\\n"
12369                "  void f() {         \\\n"
12370                "    int i;           \\\n"
12371                "  }                  \\\n"
12372                "  int j;",
12373                getLLVMStyleWithColumns(22));
12374   verifyFormat("#define A             \\\n"
12375                "  void f() { int i; } \\\n"
12376                "  int j;",
12377                getLLVMStyleWithColumns(23));
12378 }
12379 
12380 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12381   FormatStyle MergeEmptyOnly = getLLVMStyle();
12382   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12383   verifyFormat("class C {\n"
12384                "  int f() {}\n"
12385                "};",
12386                MergeEmptyOnly);
12387   verifyFormat("class C {\n"
12388                "  int f() {\n"
12389                "    return 42;\n"
12390                "  }\n"
12391                "};",
12392                MergeEmptyOnly);
12393   verifyFormat("int f() {}", MergeEmptyOnly);
12394   verifyFormat("int f() {\n"
12395                "  return 42;\n"
12396                "}",
12397                MergeEmptyOnly);
12398 
12399   // Also verify behavior when BraceWrapping.AfterFunction = true
12400   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12401   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12402   verifyFormat("int f() {}", MergeEmptyOnly);
12403   verifyFormat("class C {\n"
12404                "  int f() {}\n"
12405                "};",
12406                MergeEmptyOnly);
12407 }
12408 
12409 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12410   FormatStyle MergeInlineOnly = getLLVMStyle();
12411   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12412   verifyFormat("class C {\n"
12413                "  int f() { return 42; }\n"
12414                "};",
12415                MergeInlineOnly);
12416   verifyFormat("int f() {\n"
12417                "  return 42;\n"
12418                "}",
12419                MergeInlineOnly);
12420 
12421   // SFS_Inline implies SFS_Empty
12422   verifyFormat("class C {\n"
12423                "  int f() {}\n"
12424                "};",
12425                MergeInlineOnly);
12426   verifyFormat("int f() {}", MergeInlineOnly);
12427 
12428   // Also verify behavior when BraceWrapping.AfterFunction = true
12429   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12430   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12431   verifyFormat("class C {\n"
12432                "  int f() { return 42; }\n"
12433                "};",
12434                MergeInlineOnly);
12435   verifyFormat("int f()\n"
12436                "{\n"
12437                "  return 42;\n"
12438                "}",
12439                MergeInlineOnly);
12440 
12441   // SFS_Inline implies SFS_Empty
12442   verifyFormat("int f() {}", MergeInlineOnly);
12443   verifyFormat("class C {\n"
12444                "  int f() {}\n"
12445                "};",
12446                MergeInlineOnly);
12447 
12448   MergeInlineOnly.BraceWrapping.AfterClass = true;
12449   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12450   verifyFormat("class C\n"
12451                "{\n"
12452                "  int f() { return 42; }\n"
12453                "};",
12454                MergeInlineOnly);
12455   verifyFormat("struct C\n"
12456                "{\n"
12457                "  int f() { return 42; }\n"
12458                "};",
12459                MergeInlineOnly);
12460   verifyFormat("int f()\n"
12461                "{\n"
12462                "  return 42;\n"
12463                "}",
12464                MergeInlineOnly);
12465   verifyFormat("int f() {}", MergeInlineOnly);
12466   verifyFormat("class C\n"
12467                "{\n"
12468                "  int f() { return 42; }\n"
12469                "};",
12470                MergeInlineOnly);
12471   verifyFormat("struct C\n"
12472                "{\n"
12473                "  int f() { return 42; }\n"
12474                "};",
12475                MergeInlineOnly);
12476   verifyFormat("struct C\n"
12477                "// comment\n"
12478                "/* comment */\n"
12479                "// comment\n"
12480                "{\n"
12481                "  int f() { return 42; }\n"
12482                "};",
12483                MergeInlineOnly);
12484   verifyFormat("/* comment */ struct C\n"
12485                "{\n"
12486                "  int f() { return 42; }\n"
12487                "};",
12488                MergeInlineOnly);
12489 }
12490 
12491 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12492   FormatStyle MergeInlineOnly = getLLVMStyle();
12493   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12494       FormatStyle::SFS_InlineOnly;
12495   verifyFormat("class C {\n"
12496                "  int f() { return 42; }\n"
12497                "};",
12498                MergeInlineOnly);
12499   verifyFormat("int f() {\n"
12500                "  return 42;\n"
12501                "}",
12502                MergeInlineOnly);
12503 
12504   // SFS_InlineOnly does not imply SFS_Empty
12505   verifyFormat("class C {\n"
12506                "  int f() {}\n"
12507                "};",
12508                MergeInlineOnly);
12509   verifyFormat("int f() {\n"
12510                "}",
12511                MergeInlineOnly);
12512 
12513   // Also verify behavior when BraceWrapping.AfterFunction = true
12514   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12515   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12516   verifyFormat("class C {\n"
12517                "  int f() { return 42; }\n"
12518                "};",
12519                MergeInlineOnly);
12520   verifyFormat("int f()\n"
12521                "{\n"
12522                "  return 42;\n"
12523                "}",
12524                MergeInlineOnly);
12525 
12526   // SFS_InlineOnly does not imply SFS_Empty
12527   verifyFormat("int f()\n"
12528                "{\n"
12529                "}",
12530                MergeInlineOnly);
12531   verifyFormat("class C {\n"
12532                "  int f() {}\n"
12533                "};",
12534                MergeInlineOnly);
12535 }
12536 
12537 TEST_F(FormatTest, SplitEmptyFunction) {
12538   FormatStyle Style = getLLVMStyleWithColumns(40);
12539   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12540   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12541   Style.BraceWrapping.AfterFunction = true;
12542   Style.BraceWrapping.SplitEmptyFunction = false;
12543 
12544   verifyFormat("int f()\n"
12545                "{}",
12546                Style);
12547   verifyFormat("int f()\n"
12548                "{\n"
12549                "  return 42;\n"
12550                "}",
12551                Style);
12552   verifyFormat("int f()\n"
12553                "{\n"
12554                "  // some comment\n"
12555                "}",
12556                Style);
12557 
12558   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12559   verifyFormat("int f() {}", Style);
12560   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12561                "{}",
12562                Style);
12563   verifyFormat("int f()\n"
12564                "{\n"
12565                "  return 0;\n"
12566                "}",
12567                Style);
12568 
12569   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12570   verifyFormat("class Foo {\n"
12571                "  int f() {}\n"
12572                "};\n",
12573                Style);
12574   verifyFormat("class Foo {\n"
12575                "  int f() { return 0; }\n"
12576                "};\n",
12577                Style);
12578   verifyFormat("class Foo {\n"
12579                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12580                "  {}\n"
12581                "};\n",
12582                Style);
12583   verifyFormat("class Foo {\n"
12584                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12585                "  {\n"
12586                "    return 0;\n"
12587                "  }\n"
12588                "};\n",
12589                Style);
12590 
12591   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12592   verifyFormat("int f() {}", Style);
12593   verifyFormat("int f() { return 0; }", Style);
12594   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12595                "{}",
12596                Style);
12597   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12598                "{\n"
12599                "  return 0;\n"
12600                "}",
12601                Style);
12602 }
12603 
12604 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12605   FormatStyle Style = getLLVMStyleWithColumns(40);
12606   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12607   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12608   Style.BraceWrapping.AfterFunction = true;
12609   Style.BraceWrapping.SplitEmptyFunction = true;
12610   Style.BraceWrapping.SplitEmptyRecord = false;
12611 
12612   verifyFormat("class C {};", Style);
12613   verifyFormat("struct C {};", Style);
12614   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12615                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12616                "{\n"
12617                "}",
12618                Style);
12619   verifyFormat("class C {\n"
12620                "  C()\n"
12621                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12622                "        bbbbbbbbbbbbbbbbbbb()\n"
12623                "  {\n"
12624                "  }\n"
12625                "  void\n"
12626                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12627                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12628                "  {\n"
12629                "  }\n"
12630                "};",
12631                Style);
12632 }
12633 
12634 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12635   FormatStyle Style = getLLVMStyle();
12636   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12637   verifyFormat("#ifdef A\n"
12638                "int f() {}\n"
12639                "#else\n"
12640                "int g() {}\n"
12641                "#endif",
12642                Style);
12643 }
12644 
12645 TEST_F(FormatTest, SplitEmptyClass) {
12646   FormatStyle Style = getLLVMStyle();
12647   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12648   Style.BraceWrapping.AfterClass = true;
12649   Style.BraceWrapping.SplitEmptyRecord = false;
12650 
12651   verifyFormat("class Foo\n"
12652                "{};",
12653                Style);
12654   verifyFormat("/* something */ class Foo\n"
12655                "{};",
12656                Style);
12657   verifyFormat("template <typename X> class Foo\n"
12658                "{};",
12659                Style);
12660   verifyFormat("class Foo\n"
12661                "{\n"
12662                "  Foo();\n"
12663                "};",
12664                Style);
12665   verifyFormat("typedef class Foo\n"
12666                "{\n"
12667                "} Foo_t;",
12668                Style);
12669 
12670   Style.BraceWrapping.SplitEmptyRecord = true;
12671   Style.BraceWrapping.AfterStruct = true;
12672   verifyFormat("class rep\n"
12673                "{\n"
12674                "};",
12675                Style);
12676   verifyFormat("struct rep\n"
12677                "{\n"
12678                "};",
12679                Style);
12680   verifyFormat("template <typename T> class rep\n"
12681                "{\n"
12682                "};",
12683                Style);
12684   verifyFormat("template <typename T> struct rep\n"
12685                "{\n"
12686                "};",
12687                Style);
12688   verifyFormat("class rep\n"
12689                "{\n"
12690                "  int x;\n"
12691                "};",
12692                Style);
12693   verifyFormat("struct rep\n"
12694                "{\n"
12695                "  int x;\n"
12696                "};",
12697                Style);
12698   verifyFormat("template <typename T> class rep\n"
12699                "{\n"
12700                "  int x;\n"
12701                "};",
12702                Style);
12703   verifyFormat("template <typename T> struct rep\n"
12704                "{\n"
12705                "  int x;\n"
12706                "};",
12707                Style);
12708   verifyFormat("template <typename T> class rep // Foo\n"
12709                "{\n"
12710                "  int x;\n"
12711                "};",
12712                Style);
12713   verifyFormat("template <typename T> struct rep // Bar\n"
12714                "{\n"
12715                "  int x;\n"
12716                "};",
12717                Style);
12718 
12719   verifyFormat("template <typename T> class rep<T>\n"
12720                "{\n"
12721                "  int x;\n"
12722                "};",
12723                Style);
12724 
12725   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12726                "{\n"
12727                "  int x;\n"
12728                "};",
12729                Style);
12730   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12731                "{\n"
12732                "};",
12733                Style);
12734 
12735   verifyFormat("#include \"stdint.h\"\n"
12736                "namespace rep {}",
12737                Style);
12738   verifyFormat("#include <stdint.h>\n"
12739                "namespace rep {}",
12740                Style);
12741   verifyFormat("#include <stdint.h>\n"
12742                "namespace rep {}",
12743                "#include <stdint.h>\n"
12744                "namespace rep {\n"
12745                "\n"
12746                "\n"
12747                "}",
12748                Style);
12749 }
12750 
12751 TEST_F(FormatTest, SplitEmptyStruct) {
12752   FormatStyle Style = getLLVMStyle();
12753   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12754   Style.BraceWrapping.AfterStruct = true;
12755   Style.BraceWrapping.SplitEmptyRecord = false;
12756 
12757   verifyFormat("struct Foo\n"
12758                "{};",
12759                Style);
12760   verifyFormat("/* something */ struct Foo\n"
12761                "{};",
12762                Style);
12763   verifyFormat("template <typename X> struct Foo\n"
12764                "{};",
12765                Style);
12766   verifyFormat("struct Foo\n"
12767                "{\n"
12768                "  Foo();\n"
12769                "};",
12770                Style);
12771   verifyFormat("typedef struct Foo\n"
12772                "{\n"
12773                "} Foo_t;",
12774                Style);
12775   // typedef struct Bar {} Bar_t;
12776 }
12777 
12778 TEST_F(FormatTest, SplitEmptyUnion) {
12779   FormatStyle Style = getLLVMStyle();
12780   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12781   Style.BraceWrapping.AfterUnion = true;
12782   Style.BraceWrapping.SplitEmptyRecord = false;
12783 
12784   verifyFormat("union Foo\n"
12785                "{};",
12786                Style);
12787   verifyFormat("/* something */ union Foo\n"
12788                "{};",
12789                Style);
12790   verifyFormat("union Foo\n"
12791                "{\n"
12792                "  A,\n"
12793                "};",
12794                Style);
12795   verifyFormat("typedef union Foo\n"
12796                "{\n"
12797                "} Foo_t;",
12798                Style);
12799 }
12800 
12801 TEST_F(FormatTest, SplitEmptyNamespace) {
12802   FormatStyle Style = getLLVMStyle();
12803   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12804   Style.BraceWrapping.AfterNamespace = true;
12805   Style.BraceWrapping.SplitEmptyNamespace = false;
12806 
12807   verifyFormat("namespace Foo\n"
12808                "{};",
12809                Style);
12810   verifyFormat("/* something */ namespace Foo\n"
12811                "{};",
12812                Style);
12813   verifyFormat("inline namespace Foo\n"
12814                "{};",
12815                Style);
12816   verifyFormat("/* something */ inline namespace Foo\n"
12817                "{};",
12818                Style);
12819   verifyFormat("export namespace Foo\n"
12820                "{};",
12821                Style);
12822   verifyFormat("namespace Foo\n"
12823                "{\n"
12824                "void Bar();\n"
12825                "};",
12826                Style);
12827 }
12828 
12829 TEST_F(FormatTest, NeverMergeShortRecords) {
12830   FormatStyle Style = getLLVMStyle();
12831 
12832   verifyFormat("class Foo {\n"
12833                "  Foo();\n"
12834                "};",
12835                Style);
12836   verifyFormat("typedef class Foo {\n"
12837                "  Foo();\n"
12838                "} Foo_t;",
12839                Style);
12840   verifyFormat("struct Foo {\n"
12841                "  Foo();\n"
12842                "};",
12843                Style);
12844   verifyFormat("typedef struct Foo {\n"
12845                "  Foo();\n"
12846                "} Foo_t;",
12847                Style);
12848   verifyFormat("union Foo {\n"
12849                "  A,\n"
12850                "};",
12851                Style);
12852   verifyFormat("typedef union Foo {\n"
12853                "  A,\n"
12854                "} Foo_t;",
12855                Style);
12856   verifyFormat("namespace Foo {\n"
12857                "void Bar();\n"
12858                "};",
12859                Style);
12860 
12861   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12862   Style.BraceWrapping.AfterClass = true;
12863   Style.BraceWrapping.AfterStruct = true;
12864   Style.BraceWrapping.AfterUnion = true;
12865   Style.BraceWrapping.AfterNamespace = true;
12866   verifyFormat("class Foo\n"
12867                "{\n"
12868                "  Foo();\n"
12869                "};",
12870                Style);
12871   verifyFormat("typedef class Foo\n"
12872                "{\n"
12873                "  Foo();\n"
12874                "} Foo_t;",
12875                Style);
12876   verifyFormat("struct Foo\n"
12877                "{\n"
12878                "  Foo();\n"
12879                "};",
12880                Style);
12881   verifyFormat("typedef struct Foo\n"
12882                "{\n"
12883                "  Foo();\n"
12884                "} Foo_t;",
12885                Style);
12886   verifyFormat("union Foo\n"
12887                "{\n"
12888                "  A,\n"
12889                "};",
12890                Style);
12891   verifyFormat("typedef union Foo\n"
12892                "{\n"
12893                "  A,\n"
12894                "} Foo_t;",
12895                Style);
12896   verifyFormat("namespace Foo\n"
12897                "{\n"
12898                "void Bar();\n"
12899                "};",
12900                Style);
12901 }
12902 
12903 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12904   // Elaborate type variable declarations.
12905   verifyFormat("struct foo a = {bar};\nint n;");
12906   verifyFormat("class foo a = {bar};\nint n;");
12907   verifyFormat("union foo a = {bar};\nint n;");
12908 
12909   // Elaborate types inside function definitions.
12910   verifyFormat("struct foo f() {}\nint n;");
12911   verifyFormat("class foo f() {}\nint n;");
12912   verifyFormat("union foo f() {}\nint n;");
12913 
12914   // Templates.
12915   verifyFormat("template <class X> void f() {}\nint n;");
12916   verifyFormat("template <struct X> void f() {}\nint n;");
12917   verifyFormat("template <union X> void f() {}\nint n;");
12918 
12919   // Actual definitions...
12920   verifyFormat("struct {\n} n;");
12921   verifyFormat(
12922       "template <template <class T, class Y>, class Z> class X {\n} n;");
12923   verifyFormat("union Z {\n  int n;\n} x;");
12924   verifyFormat("class MACRO Z {\n} n;");
12925   verifyFormat("class MACRO(X) Z {\n} n;");
12926   verifyFormat("class __attribute__(X) Z {\n} n;");
12927   verifyFormat("class __declspec(X) Z {\n} n;");
12928   verifyFormat("class A##B##C {\n} n;");
12929   verifyFormat("class alignas(16) Z {\n} n;");
12930   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12931   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12932 
12933   // Redefinition from nested context:
12934   verifyFormat("class A::B::C {\n} n;");
12935 
12936   // Template definitions.
12937   verifyFormat(
12938       "template <typename F>\n"
12939       "Matcher(const Matcher<F> &Other,\n"
12940       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12941       "                             !is_same<F, T>::value>::type * = 0)\n"
12942       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12943 
12944   // FIXME: This is still incorrectly handled at the formatter side.
12945   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12946   verifyFormat("int i = SomeFunction(a<b, a> b);");
12947 
12948   // FIXME:
12949   // This now gets parsed incorrectly as class definition.
12950   // verifyFormat("class A<int> f() {\n}\nint n;");
12951 
12952   // Elaborate types where incorrectly parsing the structural element would
12953   // break the indent.
12954   verifyFormat("if (true)\n"
12955                "  class X x;\n"
12956                "else\n"
12957                "  f();\n");
12958 
12959   // This is simply incomplete. Formatting is not important, but must not crash.
12960   verifyFormat("class A:");
12961 }
12962 
12963 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12964   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12965             format("#error Leave     all         white!!!!! space* alone!\n"));
12966   EXPECT_EQ(
12967       "#warning Leave     all         white!!!!! space* alone!\n",
12968       format("#warning Leave     all         white!!!!! space* alone!\n"));
12969   EXPECT_EQ("#error 1", format("  #  error   1"));
12970   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12971 }
12972 
12973 TEST_F(FormatTest, FormatHashIfExpressions) {
12974   verifyFormat("#if AAAA && BBBB");
12975   verifyFormat("#if (AAAA && BBBB)");
12976   verifyFormat("#elif (AAAA && BBBB)");
12977   // FIXME: Come up with a better indentation for #elif.
12978   verifyFormat(
12979       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12980       "    defined(BBBBBBBB)\n"
12981       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12982       "    defined(BBBBBBBB)\n"
12983       "#endif",
12984       getLLVMStyleWithColumns(65));
12985 }
12986 
12987 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12988   FormatStyle AllowsMergedIf = getGoogleStyle();
12989   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12990       FormatStyle::SIS_WithoutElse;
12991   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12992   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12993   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12994   EXPECT_EQ("if (true) return 42;",
12995             format("if (true)\nreturn 42;", AllowsMergedIf));
12996   FormatStyle ShortMergedIf = AllowsMergedIf;
12997   ShortMergedIf.ColumnLimit = 25;
12998   verifyFormat("#define A \\\n"
12999                "  if (true) return 42;",
13000                ShortMergedIf);
13001   verifyFormat("#define A \\\n"
13002                "  f();    \\\n"
13003                "  if (true)\n"
13004                "#define B",
13005                ShortMergedIf);
13006   verifyFormat("#define A \\\n"
13007                "  f();    \\\n"
13008                "  if (true)\n"
13009                "g();",
13010                ShortMergedIf);
13011   verifyFormat("{\n"
13012                "#ifdef A\n"
13013                "  // Comment\n"
13014                "  if (true) continue;\n"
13015                "#endif\n"
13016                "  // Comment\n"
13017                "  if (true) continue;\n"
13018                "}",
13019                ShortMergedIf);
13020   ShortMergedIf.ColumnLimit = 33;
13021   verifyFormat("#define A \\\n"
13022                "  if constexpr (true) return 42;",
13023                ShortMergedIf);
13024   verifyFormat("#define A \\\n"
13025                "  if CONSTEXPR (true) return 42;",
13026                ShortMergedIf);
13027   ShortMergedIf.ColumnLimit = 29;
13028   verifyFormat("#define A                   \\\n"
13029                "  if (aaaaaaaaaa) return 1; \\\n"
13030                "  return 2;",
13031                ShortMergedIf);
13032   ShortMergedIf.ColumnLimit = 28;
13033   verifyFormat("#define A         \\\n"
13034                "  if (aaaaaaaaaa) \\\n"
13035                "    return 1;     \\\n"
13036                "  return 2;",
13037                ShortMergedIf);
13038   verifyFormat("#define A                \\\n"
13039                "  if constexpr (aaaaaaa) \\\n"
13040                "    return 1;            \\\n"
13041                "  return 2;",
13042                ShortMergedIf);
13043   verifyFormat("#define A                \\\n"
13044                "  if CONSTEXPR (aaaaaaa) \\\n"
13045                "    return 1;            \\\n"
13046                "  return 2;",
13047                ShortMergedIf);
13048 }
13049 
13050 TEST_F(FormatTest, FormatStarDependingOnContext) {
13051   verifyFormat("void f(int *a);");
13052   verifyFormat("void f() { f(fint * b); }");
13053   verifyFormat("class A {\n  void f(int *a);\n};");
13054   verifyFormat("class A {\n  int *a;\n};");
13055   verifyFormat("namespace a {\n"
13056                "namespace b {\n"
13057                "class A {\n"
13058                "  void f() {}\n"
13059                "  int *a;\n"
13060                "};\n"
13061                "} // namespace b\n"
13062                "} // namespace a");
13063 }
13064 
13065 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
13066   verifyFormat("while");
13067   verifyFormat("operator");
13068 }
13069 
13070 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
13071   // This code would be painfully slow to format if we didn't skip it.
13072   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
13073                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13074                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13075                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13076                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13077                    "A(1, 1)\n"
13078                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
13079                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13080                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13081                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13082                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13083                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13084                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13085                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13086                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13087                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13088   // Deeply nested part is untouched, rest is formatted.
13089   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13090             format(std::string("int    i;\n") + Code + "int    j;\n",
13091                    getLLVMStyle(), SC_ExpectIncomplete));
13092 }
13093 
13094 //===----------------------------------------------------------------------===//
13095 // Objective-C tests.
13096 //===----------------------------------------------------------------------===//
13097 
13098 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13099   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13100   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13101             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13102   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13103   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13104   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13105             format("-(NSInteger)Method3:(id)anObject;"));
13106   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13107             format("-(NSInteger)Method4:(id)anObject;"));
13108   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13109             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13110   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13111             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13112   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13113             "forAllCells:(BOOL)flag;",
13114             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13115                    "forAllCells:(BOOL)flag;"));
13116 
13117   // Very long objectiveC method declaration.
13118   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13119                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13120   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13121                "                    inRange:(NSRange)range\n"
13122                "                   outRange:(NSRange)out_range\n"
13123                "                  outRange1:(NSRange)out_range1\n"
13124                "                  outRange2:(NSRange)out_range2\n"
13125                "                  outRange3:(NSRange)out_range3\n"
13126                "                  outRange4:(NSRange)out_range4\n"
13127                "                  outRange5:(NSRange)out_range5\n"
13128                "                  outRange6:(NSRange)out_range6\n"
13129                "                  outRange7:(NSRange)out_range7\n"
13130                "                  outRange8:(NSRange)out_range8\n"
13131                "                  outRange9:(NSRange)out_range9;");
13132 
13133   // When the function name has to be wrapped.
13134   FormatStyle Style = getLLVMStyle();
13135   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13136   // and always indents instead.
13137   Style.IndentWrappedFunctionNames = false;
13138   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13139                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13140                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13141                "}",
13142                Style);
13143   Style.IndentWrappedFunctionNames = true;
13144   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13145                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13146                "               anotherName:(NSString)dddddddddddddd {\n"
13147                "}",
13148                Style);
13149 
13150   verifyFormat("- (int)sum:(vector<int>)numbers;");
13151   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13152   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13153   // protocol lists (but not for template classes):
13154   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13155 
13156   verifyFormat("- (int (*)())foo:(int (*)())f;");
13157   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13158 
13159   // If there's no return type (very rare in practice!), LLVM and Google style
13160   // agree.
13161   verifyFormat("- foo;");
13162   verifyFormat("- foo:(int)f;");
13163   verifyGoogleFormat("- foo:(int)foo;");
13164 }
13165 
13166 TEST_F(FormatTest, BreaksStringLiterals) {
13167   EXPECT_EQ("\"some text \"\n"
13168             "\"other\";",
13169             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13170   EXPECT_EQ("\"some text \"\n"
13171             "\"other\";",
13172             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13173   EXPECT_EQ(
13174       "#define A  \\\n"
13175       "  \"some \"  \\\n"
13176       "  \"text \"  \\\n"
13177       "  \"other\";",
13178       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13179   EXPECT_EQ(
13180       "#define A  \\\n"
13181       "  \"so \"    \\\n"
13182       "  \"text \"  \\\n"
13183       "  \"other\";",
13184       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13185 
13186   EXPECT_EQ("\"some text\"",
13187             format("\"some text\"", getLLVMStyleWithColumns(1)));
13188   EXPECT_EQ("\"some text\"",
13189             format("\"some text\"", getLLVMStyleWithColumns(11)));
13190   EXPECT_EQ("\"some \"\n"
13191             "\"text\"",
13192             format("\"some text\"", getLLVMStyleWithColumns(10)));
13193   EXPECT_EQ("\"some \"\n"
13194             "\"text\"",
13195             format("\"some text\"", getLLVMStyleWithColumns(7)));
13196   EXPECT_EQ("\"some\"\n"
13197             "\" tex\"\n"
13198             "\"t\"",
13199             format("\"some text\"", getLLVMStyleWithColumns(6)));
13200   EXPECT_EQ("\"some\"\n"
13201             "\" tex\"\n"
13202             "\" and\"",
13203             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13204   EXPECT_EQ("\"some\"\n"
13205             "\"/tex\"\n"
13206             "\"/and\"",
13207             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13208 
13209   EXPECT_EQ("variable =\n"
13210             "    \"long string \"\n"
13211             "    \"literal\";",
13212             format("variable = \"long string literal\";",
13213                    getLLVMStyleWithColumns(20)));
13214 
13215   EXPECT_EQ("variable = f(\n"
13216             "    \"long string \"\n"
13217             "    \"literal\",\n"
13218             "    short,\n"
13219             "    loooooooooooooooooooong);",
13220             format("variable = f(\"long string literal\", short, "
13221                    "loooooooooooooooooooong);",
13222                    getLLVMStyleWithColumns(20)));
13223 
13224   EXPECT_EQ(
13225       "f(g(\"long string \"\n"
13226       "    \"literal\"),\n"
13227       "  b);",
13228       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13229   EXPECT_EQ("f(g(\"long string \"\n"
13230             "    \"literal\",\n"
13231             "    a),\n"
13232             "  b);",
13233             format("f(g(\"long string literal\", a), b);",
13234                    getLLVMStyleWithColumns(20)));
13235   EXPECT_EQ(
13236       "f(\"one two\".split(\n"
13237       "    variable));",
13238       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13239   EXPECT_EQ("f(\"one two three four five six \"\n"
13240             "  \"seven\".split(\n"
13241             "      really_looooong_variable));",
13242             format("f(\"one two three four five six seven\"."
13243                    "split(really_looooong_variable));",
13244                    getLLVMStyleWithColumns(33)));
13245 
13246   EXPECT_EQ("f(\"some \"\n"
13247             "  \"text\",\n"
13248             "  other);",
13249             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13250 
13251   // Only break as a last resort.
13252   verifyFormat(
13253       "aaaaaaaaaaaaaaaaaaaa(\n"
13254       "    aaaaaaaaaaaaaaaaaaaa,\n"
13255       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13256 
13257   EXPECT_EQ("\"splitmea\"\n"
13258             "\"trandomp\"\n"
13259             "\"oint\"",
13260             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13261 
13262   EXPECT_EQ("\"split/\"\n"
13263             "\"pathat/\"\n"
13264             "\"slashes\"",
13265             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13266 
13267   EXPECT_EQ("\"split/\"\n"
13268             "\"pathat/\"\n"
13269             "\"slashes\"",
13270             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13271   EXPECT_EQ("\"split at \"\n"
13272             "\"spaces/at/\"\n"
13273             "\"slashes.at.any$\"\n"
13274             "\"non-alphanumeric%\"\n"
13275             "\"1111111111characte\"\n"
13276             "\"rs\"",
13277             format("\"split at "
13278                    "spaces/at/"
13279                    "slashes.at."
13280                    "any$non-"
13281                    "alphanumeric%"
13282                    "1111111111characte"
13283                    "rs\"",
13284                    getLLVMStyleWithColumns(20)));
13285 
13286   // Verify that splitting the strings understands
13287   // Style::AlwaysBreakBeforeMultilineStrings.
13288   EXPECT_EQ("aaaaaaaaaaaa(\n"
13289             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13290             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13291             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13292                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13293                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13294                    getGoogleStyle()));
13295   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13296             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13297             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13298                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13299                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13300                    getGoogleStyle()));
13301   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13302             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13303             format("llvm::outs() << "
13304                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13305                    "aaaaaaaaaaaaaaaaaaa\";"));
13306   EXPECT_EQ("ffff(\n"
13307             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13308             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13309             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13310                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13311                    getGoogleStyle()));
13312 
13313   FormatStyle Style = getLLVMStyleWithColumns(12);
13314   Style.BreakStringLiterals = false;
13315   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13316 
13317   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13318   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13319   EXPECT_EQ("#define A \\\n"
13320             "  \"some \" \\\n"
13321             "  \"text \" \\\n"
13322             "  \"other\";",
13323             format("#define A \"some text other\";", AlignLeft));
13324 }
13325 
13326 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13327   EXPECT_EQ("C a = \"some more \"\n"
13328             "      \"text\";",
13329             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13330 }
13331 
13332 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13333   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13334   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13335   EXPECT_EQ("int i = a(b());",
13336             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13337 }
13338 
13339 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13340   EXPECT_EQ(
13341       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13342       "(\n"
13343       "    \"x\t\");",
13344       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13345              "aaaaaaa("
13346              "\"x\t\");"));
13347 }
13348 
13349 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13350   EXPECT_EQ(
13351       "u8\"utf8 string \"\n"
13352       "u8\"literal\";",
13353       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13354   EXPECT_EQ(
13355       "u\"utf16 string \"\n"
13356       "u\"literal\";",
13357       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13358   EXPECT_EQ(
13359       "U\"utf32 string \"\n"
13360       "U\"literal\";",
13361       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13362   EXPECT_EQ("L\"wide string \"\n"
13363             "L\"literal\";",
13364             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13365   EXPECT_EQ("@\"NSString \"\n"
13366             "@\"literal\";",
13367             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13368   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13369 
13370   // This input makes clang-format try to split the incomplete unicode escape
13371   // sequence, which used to lead to a crasher.
13372   verifyNoCrash(
13373       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13374       getLLVMStyleWithColumns(60));
13375 }
13376 
13377 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13378   FormatStyle Style = getGoogleStyleWithColumns(15);
13379   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13380   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13381   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13382   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13383   EXPECT_EQ("u8R\"x(raw literal)x\";",
13384             format("u8R\"x(raw literal)x\";", Style));
13385 }
13386 
13387 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13388   FormatStyle Style = getLLVMStyleWithColumns(20);
13389   EXPECT_EQ(
13390       "_T(\"aaaaaaaaaaaaaa\")\n"
13391       "_T(\"aaaaaaaaaaaaaa\")\n"
13392       "_T(\"aaaaaaaaaaaa\")",
13393       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13394   EXPECT_EQ("f(x,\n"
13395             "  _T(\"aaaaaaaaaaaa\")\n"
13396             "  _T(\"aaa\"),\n"
13397             "  z);",
13398             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13399 
13400   // FIXME: Handle embedded spaces in one iteration.
13401   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13402   //            "_T(\"aaaaaaaaaaaaa\")\n"
13403   //            "_T(\"aaaaaaaaaaaaa\")\n"
13404   //            "_T(\"a\")",
13405   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13406   //                   getLLVMStyleWithColumns(20)));
13407   EXPECT_EQ(
13408       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13409       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13410   EXPECT_EQ("f(\n"
13411             "#if !TEST\n"
13412             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13413             "#endif\n"
13414             ");",
13415             format("f(\n"
13416                    "#if !TEST\n"
13417                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13418                    "#endif\n"
13419                    ");"));
13420   EXPECT_EQ("f(\n"
13421             "\n"
13422             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13423             format("f(\n"
13424                    "\n"
13425                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13426   // Regression test for accessing tokens past the end of a vector in the
13427   // TokenLexer.
13428   verifyNoCrash(R"(_T(
13429 "
13430 )
13431 )");
13432 }
13433 
13434 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13435   // In a function call with two operands, the second can be broken with no line
13436   // break before it.
13437   EXPECT_EQ(
13438       "func(a, \"long long \"\n"
13439       "        \"long long\");",
13440       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13441   // In a function call with three operands, the second must be broken with a
13442   // line break before it.
13443   EXPECT_EQ("func(a,\n"
13444             "     \"long long long \"\n"
13445             "     \"long\",\n"
13446             "     c);",
13447             format("func(a, \"long long long long\", c);",
13448                    getLLVMStyleWithColumns(24)));
13449   // In a function call with three operands, the third must be broken with a
13450   // line break before it.
13451   EXPECT_EQ("func(a, b,\n"
13452             "     \"long long long \"\n"
13453             "     \"long\");",
13454             format("func(a, b, \"long long long long\");",
13455                    getLLVMStyleWithColumns(24)));
13456   // In a function call with three operands, both the second and the third must
13457   // be broken with a line break before them.
13458   EXPECT_EQ("func(a,\n"
13459             "     \"long long long \"\n"
13460             "     \"long\",\n"
13461             "     \"long long long \"\n"
13462             "     \"long\");",
13463             format("func(a, \"long long long long\", \"long long long long\");",
13464                    getLLVMStyleWithColumns(24)));
13465   // In a chain of << with two operands, the second can be broken with no line
13466   // break before it.
13467   EXPECT_EQ("a << \"line line \"\n"
13468             "     \"line\";",
13469             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13470   // In a chain of << with three operands, the second can be broken with no line
13471   // break before it.
13472   EXPECT_EQ(
13473       "abcde << \"line \"\n"
13474       "         \"line line\"\n"
13475       "      << c;",
13476       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13477   // In a chain of << with three operands, the third must be broken with a line
13478   // break before it.
13479   EXPECT_EQ(
13480       "a << b\n"
13481       "  << \"line line \"\n"
13482       "     \"line\";",
13483       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13484   // In a chain of << with three operands, the second can be broken with no line
13485   // break before it and the third must be broken with a line break before it.
13486   EXPECT_EQ("abcd << \"line line \"\n"
13487             "        \"line\"\n"
13488             "     << \"line line \"\n"
13489             "        \"line\";",
13490             format("abcd << \"line line line\" << \"line line line\";",
13491                    getLLVMStyleWithColumns(20)));
13492   // In a chain of binary operators with two operands, the second can be broken
13493   // with no line break before it.
13494   EXPECT_EQ(
13495       "abcd + \"line line \"\n"
13496       "       \"line line\";",
13497       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13498   // In a chain of binary operators with three operands, the second must be
13499   // broken with a line break before it.
13500   EXPECT_EQ("abcd +\n"
13501             "    \"line line \"\n"
13502             "    \"line line\" +\n"
13503             "    e;",
13504             format("abcd + \"line line line line\" + e;",
13505                    getLLVMStyleWithColumns(20)));
13506   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13507   // the first must be broken with a line break before it.
13508   FormatStyle Style = getLLVMStyleWithColumns(25);
13509   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13510   EXPECT_EQ("someFunction(\n"
13511             "    \"long long long \"\n"
13512             "    \"long\",\n"
13513             "    a);",
13514             format("someFunction(\"long long long long\", a);", Style));
13515 }
13516 
13517 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13518   EXPECT_EQ(
13519       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13520       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13521       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13522       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13523              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13524              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13525 }
13526 
13527 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13528   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13529             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13530   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13531             "multiline raw string literal xxxxxxxxxxxxxx\n"
13532             ")x\",\n"
13533             "              a),\n"
13534             "            b);",
13535             format("fffffffffff(g(R\"x(\n"
13536                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13537                    ")x\", a), b);",
13538                    getGoogleStyleWithColumns(20)));
13539   EXPECT_EQ("fffffffffff(\n"
13540             "    g(R\"x(qqq\n"
13541             "multiline raw string literal xxxxxxxxxxxxxx\n"
13542             ")x\",\n"
13543             "      a),\n"
13544             "    b);",
13545             format("fffffffffff(g(R\"x(qqq\n"
13546                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13547                    ")x\", a), b);",
13548                    getGoogleStyleWithColumns(20)));
13549 
13550   EXPECT_EQ("fffffffffff(R\"x(\n"
13551             "multiline raw string literal xxxxxxxxxxxxxx\n"
13552             ")x\");",
13553             format("fffffffffff(R\"x(\n"
13554                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13555                    ")x\");",
13556                    getGoogleStyleWithColumns(20)));
13557   EXPECT_EQ("fffffffffff(R\"x(\n"
13558             "multiline raw string literal xxxxxxxxxxxxxx\n"
13559             ")x\" + bbbbbb);",
13560             format("fffffffffff(R\"x(\n"
13561                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13562                    ")x\" +   bbbbbb);",
13563                    getGoogleStyleWithColumns(20)));
13564   EXPECT_EQ("fffffffffff(\n"
13565             "    R\"x(\n"
13566             "multiline raw string literal xxxxxxxxxxxxxx\n"
13567             ")x\" +\n"
13568             "    bbbbbb);",
13569             format("fffffffffff(\n"
13570                    " R\"x(\n"
13571                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13572                    ")x\" + bbbbbb);",
13573                    getGoogleStyleWithColumns(20)));
13574   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13575             format("fffffffffff(\n"
13576                    " R\"(single line raw string)\" + bbbbbb);"));
13577 }
13578 
13579 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13580   verifyFormat("string a = \"unterminated;");
13581   EXPECT_EQ("function(\"unterminated,\n"
13582             "         OtherParameter);",
13583             format("function(  \"unterminated,\n"
13584                    "    OtherParameter);"));
13585 }
13586 
13587 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13588   FormatStyle Style = getLLVMStyle();
13589   Style.Standard = FormatStyle::LS_Cpp03;
13590   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13591             format("#define x(_a) printf(\"foo\"_a);", Style));
13592 }
13593 
13594 TEST_F(FormatTest, CppLexVersion) {
13595   FormatStyle Style = getLLVMStyle();
13596   // Formatting of x * y differs if x is a type.
13597   verifyFormat("void foo() { MACRO(a * b); }", Style);
13598   verifyFormat("void foo() { MACRO(int *b); }", Style);
13599 
13600   // LLVM style uses latest lexer.
13601   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13602   Style.Standard = FormatStyle::LS_Cpp17;
13603   // But in c++17, char8_t isn't a keyword.
13604   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13605 }
13606 
13607 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13608 
13609 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13610   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13611             "             \"ddeeefff\");",
13612             format("someFunction(\"aaabbbcccdddeeefff\");",
13613                    getLLVMStyleWithColumns(25)));
13614   EXPECT_EQ("someFunction1234567890(\n"
13615             "    \"aaabbbcccdddeeefff\");",
13616             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13617                    getLLVMStyleWithColumns(26)));
13618   EXPECT_EQ("someFunction1234567890(\n"
13619             "    \"aaabbbcccdddeeeff\"\n"
13620             "    \"f\");",
13621             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13622                    getLLVMStyleWithColumns(25)));
13623   EXPECT_EQ("someFunction1234567890(\n"
13624             "    \"aaabbbcccdddeeeff\"\n"
13625             "    \"f\");",
13626             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13627                    getLLVMStyleWithColumns(24)));
13628   EXPECT_EQ("someFunction(\n"
13629             "    \"aaabbbcc ddde \"\n"
13630             "    \"efff\");",
13631             format("someFunction(\"aaabbbcc ddde efff\");",
13632                    getLLVMStyleWithColumns(25)));
13633   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13634             "             \"ddeeefff\");",
13635             format("someFunction(\"aaabbbccc ddeeefff\");",
13636                    getLLVMStyleWithColumns(25)));
13637   EXPECT_EQ("someFunction1234567890(\n"
13638             "    \"aaabb \"\n"
13639             "    \"cccdddeeefff\");",
13640             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13641                    getLLVMStyleWithColumns(25)));
13642   EXPECT_EQ("#define A          \\\n"
13643             "  string s =       \\\n"
13644             "      \"123456789\"  \\\n"
13645             "      \"0\";         \\\n"
13646             "  int i;",
13647             format("#define A string s = \"1234567890\"; int i;",
13648                    getLLVMStyleWithColumns(20)));
13649   EXPECT_EQ("someFunction(\n"
13650             "    \"aaabbbcc \"\n"
13651             "    \"dddeeefff\");",
13652             format("someFunction(\"aaabbbcc dddeeefff\");",
13653                    getLLVMStyleWithColumns(25)));
13654 }
13655 
13656 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13657   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13658   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13659   EXPECT_EQ("\"test\"\n"
13660             "\"\\n\"",
13661             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13662   EXPECT_EQ("\"tes\\\\\"\n"
13663             "\"n\"",
13664             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13665   EXPECT_EQ("\"\\\\\\\\\"\n"
13666             "\"\\n\"",
13667             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13668   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13669   EXPECT_EQ("\"\\uff01\"\n"
13670             "\"test\"",
13671             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13672   EXPECT_EQ("\"\\Uff01ff02\"",
13673             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13674   EXPECT_EQ("\"\\x000000000001\"\n"
13675             "\"next\"",
13676             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13677   EXPECT_EQ("\"\\x000000000001next\"",
13678             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13679   EXPECT_EQ("\"\\x000000000001\"",
13680             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13681   EXPECT_EQ("\"test\"\n"
13682             "\"\\000000\"\n"
13683             "\"000001\"",
13684             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13685   EXPECT_EQ("\"test\\000\"\n"
13686             "\"00000000\"\n"
13687             "\"1\"",
13688             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13689 }
13690 
13691 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13692   verifyFormat("void f() {\n"
13693                "  return g() {}\n"
13694                "  void h() {}");
13695   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13696                "g();\n"
13697                "}");
13698 }
13699 
13700 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13701   verifyFormat(
13702       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13703 }
13704 
13705 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13706   verifyFormat("class X {\n"
13707                "  void f() {\n"
13708                "  }\n"
13709                "};",
13710                getLLVMStyleWithColumns(12));
13711 }
13712 
13713 TEST_F(FormatTest, ConfigurableIndentWidth) {
13714   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13715   EightIndent.IndentWidth = 8;
13716   EightIndent.ContinuationIndentWidth = 8;
13717   verifyFormat("void f() {\n"
13718                "        someFunction();\n"
13719                "        if (true) {\n"
13720                "                f();\n"
13721                "        }\n"
13722                "}",
13723                EightIndent);
13724   verifyFormat("class X {\n"
13725                "        void f() {\n"
13726                "        }\n"
13727                "};",
13728                EightIndent);
13729   verifyFormat("int x[] = {\n"
13730                "        call(),\n"
13731                "        call()};",
13732                EightIndent);
13733 }
13734 
13735 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13736   verifyFormat("double\n"
13737                "f();",
13738                getLLVMStyleWithColumns(8));
13739 }
13740 
13741 TEST_F(FormatTest, ConfigurableUseOfTab) {
13742   FormatStyle Tab = getLLVMStyleWithColumns(42);
13743   Tab.IndentWidth = 8;
13744   Tab.UseTab = FormatStyle::UT_Always;
13745   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13746 
13747   EXPECT_EQ("if (aaaaaaaa && // q\n"
13748             "    bb)\t\t// w\n"
13749             "\t;",
13750             format("if (aaaaaaaa &&// q\n"
13751                    "bb)// w\n"
13752                    ";",
13753                    Tab));
13754   EXPECT_EQ("if (aaa && bbb) // w\n"
13755             "\t;",
13756             format("if(aaa&&bbb)// w\n"
13757                    ";",
13758                    Tab));
13759 
13760   verifyFormat("class X {\n"
13761                "\tvoid f() {\n"
13762                "\t\tsomeFunction(parameter1,\n"
13763                "\t\t\t     parameter2);\n"
13764                "\t}\n"
13765                "};",
13766                Tab);
13767   verifyFormat("#define A                        \\\n"
13768                "\tvoid f() {               \\\n"
13769                "\t\tsomeFunction(    \\\n"
13770                "\t\t    parameter1,  \\\n"
13771                "\t\t    parameter2); \\\n"
13772                "\t}",
13773                Tab);
13774   verifyFormat("int a;\t      // x\n"
13775                "int bbbbbbbb; // x\n",
13776                Tab);
13777 
13778   Tab.TabWidth = 4;
13779   Tab.IndentWidth = 8;
13780   verifyFormat("class TabWidth4Indent8 {\n"
13781                "\t\tvoid f() {\n"
13782                "\t\t\t\tsomeFunction(parameter1,\n"
13783                "\t\t\t\t\t\t\t parameter2);\n"
13784                "\t\t}\n"
13785                "};",
13786                Tab);
13787 
13788   Tab.TabWidth = 4;
13789   Tab.IndentWidth = 4;
13790   verifyFormat("class TabWidth4Indent4 {\n"
13791                "\tvoid f() {\n"
13792                "\t\tsomeFunction(parameter1,\n"
13793                "\t\t\t\t\t parameter2);\n"
13794                "\t}\n"
13795                "};",
13796                Tab);
13797 
13798   Tab.TabWidth = 8;
13799   Tab.IndentWidth = 4;
13800   verifyFormat("class TabWidth8Indent4 {\n"
13801                "    void f() {\n"
13802                "\tsomeFunction(parameter1,\n"
13803                "\t\t     parameter2);\n"
13804                "    }\n"
13805                "};",
13806                Tab);
13807 
13808   Tab.TabWidth = 8;
13809   Tab.IndentWidth = 8;
13810   EXPECT_EQ("/*\n"
13811             "\t      a\t\tcomment\n"
13812             "\t      in multiple lines\n"
13813             "       */",
13814             format("   /*\t \t \n"
13815                    " \t \t a\t\tcomment\t \t\n"
13816                    " \t \t in multiple lines\t\n"
13817                    " \t  */",
13818                    Tab));
13819 
13820   Tab.UseTab = FormatStyle::UT_ForIndentation;
13821   verifyFormat("{\n"
13822                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13823                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13824                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13825                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13826                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13827                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13828                "};",
13829                Tab);
13830   verifyFormat("enum AA {\n"
13831                "\ta1, // Force multiple lines\n"
13832                "\ta2,\n"
13833                "\ta3\n"
13834                "};",
13835                Tab);
13836   EXPECT_EQ("if (aaaaaaaa && // q\n"
13837             "    bb)         // w\n"
13838             "\t;",
13839             format("if (aaaaaaaa &&// q\n"
13840                    "bb)// w\n"
13841                    ";",
13842                    Tab));
13843   verifyFormat("class X {\n"
13844                "\tvoid f() {\n"
13845                "\t\tsomeFunction(parameter1,\n"
13846                "\t\t             parameter2);\n"
13847                "\t}\n"
13848                "};",
13849                Tab);
13850   verifyFormat("{\n"
13851                "\tQ(\n"
13852                "\t    {\n"
13853                "\t\t    int a;\n"
13854                "\t\t    someFunction(aaaaaaaa,\n"
13855                "\t\t                 bbbbbbb);\n"
13856                "\t    },\n"
13857                "\t    p);\n"
13858                "}",
13859                Tab);
13860   EXPECT_EQ("{\n"
13861             "\t/* aaaa\n"
13862             "\t   bbbb */\n"
13863             "}",
13864             format("{\n"
13865                    "/* aaaa\n"
13866                    "   bbbb */\n"
13867                    "}",
13868                    Tab));
13869   EXPECT_EQ("{\n"
13870             "\t/*\n"
13871             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13872             "\t  bbbbbbbbbbbbb\n"
13873             "\t*/\n"
13874             "}",
13875             format("{\n"
13876                    "/*\n"
13877                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13878                    "*/\n"
13879                    "}",
13880                    Tab));
13881   EXPECT_EQ("{\n"
13882             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13883             "\t// bbbbbbbbbbbbb\n"
13884             "}",
13885             format("{\n"
13886                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13887                    "}",
13888                    Tab));
13889   EXPECT_EQ("{\n"
13890             "\t/*\n"
13891             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13892             "\t  bbbbbbbbbbbbb\n"
13893             "\t*/\n"
13894             "}",
13895             format("{\n"
13896                    "\t/*\n"
13897                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13898                    "\t*/\n"
13899                    "}",
13900                    Tab));
13901   EXPECT_EQ("{\n"
13902             "\t/*\n"
13903             "\n"
13904             "\t*/\n"
13905             "}",
13906             format("{\n"
13907                    "\t/*\n"
13908                    "\n"
13909                    "\t*/\n"
13910                    "}",
13911                    Tab));
13912   EXPECT_EQ("{\n"
13913             "\t/*\n"
13914             " asdf\n"
13915             "\t*/\n"
13916             "}",
13917             format("{\n"
13918                    "\t/*\n"
13919                    " asdf\n"
13920                    "\t*/\n"
13921                    "}",
13922                    Tab));
13923 
13924   verifyFormat("void f() {\n"
13925                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13926                "\t            : bbbbbbbbbbbbbbbbbb\n"
13927                "}",
13928                Tab);
13929   FormatStyle TabNoBreak = Tab;
13930   TabNoBreak.BreakBeforeTernaryOperators = false;
13931   verifyFormat("void f() {\n"
13932                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13933                "\t              bbbbbbbbbbbbbbbbbb\n"
13934                "}",
13935                TabNoBreak);
13936   verifyFormat("void f() {\n"
13937                "\treturn true ?\n"
13938                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
13939                "\t           bbbbbbbbbbbbbbbbbbbb\n"
13940                "}",
13941                TabNoBreak);
13942 
13943   Tab.UseTab = FormatStyle::UT_Never;
13944   EXPECT_EQ("/*\n"
13945             "              a\t\tcomment\n"
13946             "              in multiple lines\n"
13947             "       */",
13948             format("   /*\t \t \n"
13949                    " \t \t a\t\tcomment\t \t\n"
13950                    " \t \t in multiple lines\t\n"
13951                    " \t  */",
13952                    Tab));
13953   EXPECT_EQ("/* some\n"
13954             "   comment */",
13955             format(" \t \t /* some\n"
13956                    " \t \t    comment */",
13957                    Tab));
13958   EXPECT_EQ("int a; /* some\n"
13959             "   comment */",
13960             format(" \t \t int a; /* some\n"
13961                    " \t \t    comment */",
13962                    Tab));
13963 
13964   EXPECT_EQ("int a; /* some\n"
13965             "comment */",
13966             format(" \t \t int\ta; /* some\n"
13967                    " \t \t    comment */",
13968                    Tab));
13969   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13970             "    comment */",
13971             format(" \t \t f(\"\t\t\"); /* some\n"
13972                    " \t \t    comment */",
13973                    Tab));
13974   EXPECT_EQ("{\n"
13975             "        /*\n"
13976             "         * Comment\n"
13977             "         */\n"
13978             "        int i;\n"
13979             "}",
13980             format("{\n"
13981                    "\t/*\n"
13982                    "\t * Comment\n"
13983                    "\t */\n"
13984                    "\t int i;\n"
13985                    "}",
13986                    Tab));
13987 
13988   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13989   Tab.TabWidth = 8;
13990   Tab.IndentWidth = 8;
13991   EXPECT_EQ("if (aaaaaaaa && // q\n"
13992             "    bb)         // w\n"
13993             "\t;",
13994             format("if (aaaaaaaa &&// q\n"
13995                    "bb)// w\n"
13996                    ";",
13997                    Tab));
13998   EXPECT_EQ("if (aaa && bbb) // w\n"
13999             "\t;",
14000             format("if(aaa&&bbb)// w\n"
14001                    ";",
14002                    Tab));
14003   verifyFormat("class X {\n"
14004                "\tvoid f() {\n"
14005                "\t\tsomeFunction(parameter1,\n"
14006                "\t\t\t     parameter2);\n"
14007                "\t}\n"
14008                "};",
14009                Tab);
14010   verifyFormat("#define A                        \\\n"
14011                "\tvoid f() {               \\\n"
14012                "\t\tsomeFunction(    \\\n"
14013                "\t\t    parameter1,  \\\n"
14014                "\t\t    parameter2); \\\n"
14015                "\t}",
14016                Tab);
14017   Tab.TabWidth = 4;
14018   Tab.IndentWidth = 8;
14019   verifyFormat("class TabWidth4Indent8 {\n"
14020                "\t\tvoid f() {\n"
14021                "\t\t\t\tsomeFunction(parameter1,\n"
14022                "\t\t\t\t\t\t\t parameter2);\n"
14023                "\t\t}\n"
14024                "};",
14025                Tab);
14026   Tab.TabWidth = 4;
14027   Tab.IndentWidth = 4;
14028   verifyFormat("class TabWidth4Indent4 {\n"
14029                "\tvoid f() {\n"
14030                "\t\tsomeFunction(parameter1,\n"
14031                "\t\t\t\t\t parameter2);\n"
14032                "\t}\n"
14033                "};",
14034                Tab);
14035   Tab.TabWidth = 8;
14036   Tab.IndentWidth = 4;
14037   verifyFormat("class TabWidth8Indent4 {\n"
14038                "    void f() {\n"
14039                "\tsomeFunction(parameter1,\n"
14040                "\t\t     parameter2);\n"
14041                "    }\n"
14042                "};",
14043                Tab);
14044   Tab.TabWidth = 8;
14045   Tab.IndentWidth = 8;
14046   EXPECT_EQ("/*\n"
14047             "\t      a\t\tcomment\n"
14048             "\t      in multiple lines\n"
14049             "       */",
14050             format("   /*\t \t \n"
14051                    " \t \t a\t\tcomment\t \t\n"
14052                    " \t \t in multiple lines\t\n"
14053                    " \t  */",
14054                    Tab));
14055   verifyFormat("{\n"
14056                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14057                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14058                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14059                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14060                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14061                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14062                "};",
14063                Tab);
14064   verifyFormat("enum AA {\n"
14065                "\ta1, // Force multiple lines\n"
14066                "\ta2,\n"
14067                "\ta3\n"
14068                "};",
14069                Tab);
14070   EXPECT_EQ("if (aaaaaaaa && // q\n"
14071             "    bb)         // w\n"
14072             "\t;",
14073             format("if (aaaaaaaa &&// q\n"
14074                    "bb)// w\n"
14075                    ";",
14076                    Tab));
14077   verifyFormat("class X {\n"
14078                "\tvoid f() {\n"
14079                "\t\tsomeFunction(parameter1,\n"
14080                "\t\t\t     parameter2);\n"
14081                "\t}\n"
14082                "};",
14083                Tab);
14084   verifyFormat("{\n"
14085                "\tQ(\n"
14086                "\t    {\n"
14087                "\t\t    int a;\n"
14088                "\t\t    someFunction(aaaaaaaa,\n"
14089                "\t\t\t\t bbbbbbb);\n"
14090                "\t    },\n"
14091                "\t    p);\n"
14092                "}",
14093                Tab);
14094   EXPECT_EQ("{\n"
14095             "\t/* aaaa\n"
14096             "\t   bbbb */\n"
14097             "}",
14098             format("{\n"
14099                    "/* aaaa\n"
14100                    "   bbbb */\n"
14101                    "}",
14102                    Tab));
14103   EXPECT_EQ("{\n"
14104             "\t/*\n"
14105             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14106             "\t  bbbbbbbbbbbbb\n"
14107             "\t*/\n"
14108             "}",
14109             format("{\n"
14110                    "/*\n"
14111                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14112                    "*/\n"
14113                    "}",
14114                    Tab));
14115   EXPECT_EQ("{\n"
14116             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14117             "\t// bbbbbbbbbbbbb\n"
14118             "}",
14119             format("{\n"
14120                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14121                    "}",
14122                    Tab));
14123   EXPECT_EQ("{\n"
14124             "\t/*\n"
14125             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14126             "\t  bbbbbbbbbbbbb\n"
14127             "\t*/\n"
14128             "}",
14129             format("{\n"
14130                    "\t/*\n"
14131                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14132                    "\t*/\n"
14133                    "}",
14134                    Tab));
14135   EXPECT_EQ("{\n"
14136             "\t/*\n"
14137             "\n"
14138             "\t*/\n"
14139             "}",
14140             format("{\n"
14141                    "\t/*\n"
14142                    "\n"
14143                    "\t*/\n"
14144                    "}",
14145                    Tab));
14146   EXPECT_EQ("{\n"
14147             "\t/*\n"
14148             " asdf\n"
14149             "\t*/\n"
14150             "}",
14151             format("{\n"
14152                    "\t/*\n"
14153                    " asdf\n"
14154                    "\t*/\n"
14155                    "}",
14156                    Tab));
14157   EXPECT_EQ("/* some\n"
14158             "   comment */",
14159             format(" \t \t /* some\n"
14160                    " \t \t    comment */",
14161                    Tab));
14162   EXPECT_EQ("int a; /* some\n"
14163             "   comment */",
14164             format(" \t \t int a; /* some\n"
14165                    " \t \t    comment */",
14166                    Tab));
14167   EXPECT_EQ("int a; /* some\n"
14168             "comment */",
14169             format(" \t \t int\ta; /* some\n"
14170                    " \t \t    comment */",
14171                    Tab));
14172   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14173             "    comment */",
14174             format(" \t \t f(\"\t\t\"); /* some\n"
14175                    " \t \t    comment */",
14176                    Tab));
14177   EXPECT_EQ("{\n"
14178             "\t/*\n"
14179             "\t * Comment\n"
14180             "\t */\n"
14181             "\tint i;\n"
14182             "}",
14183             format("{\n"
14184                    "\t/*\n"
14185                    "\t * Comment\n"
14186                    "\t */\n"
14187                    "\t int i;\n"
14188                    "}",
14189                    Tab));
14190   Tab.TabWidth = 2;
14191   Tab.IndentWidth = 2;
14192   EXPECT_EQ("{\n"
14193             "\t/* aaaa\n"
14194             "\t\t bbbb */\n"
14195             "}",
14196             format("{\n"
14197                    "/* aaaa\n"
14198                    "\t bbbb */\n"
14199                    "}",
14200                    Tab));
14201   EXPECT_EQ("{\n"
14202             "\t/*\n"
14203             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14204             "\t\tbbbbbbbbbbbbb\n"
14205             "\t*/\n"
14206             "}",
14207             format("{\n"
14208                    "/*\n"
14209                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14210                    "*/\n"
14211                    "}",
14212                    Tab));
14213   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14214   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14215   Tab.TabWidth = 4;
14216   Tab.IndentWidth = 4;
14217   verifyFormat("class Assign {\n"
14218                "\tvoid f() {\n"
14219                "\t\tint         x      = 123;\n"
14220                "\t\tint         random = 4;\n"
14221                "\t\tstd::string alphabet =\n"
14222                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14223                "\t}\n"
14224                "};",
14225                Tab);
14226 
14227   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14228   Tab.TabWidth = 8;
14229   Tab.IndentWidth = 8;
14230   EXPECT_EQ("if (aaaaaaaa && // q\n"
14231             "    bb)         // w\n"
14232             "\t;",
14233             format("if (aaaaaaaa &&// q\n"
14234                    "bb)// w\n"
14235                    ";",
14236                    Tab));
14237   EXPECT_EQ("if (aaa && bbb) // w\n"
14238             "\t;",
14239             format("if(aaa&&bbb)// w\n"
14240                    ";",
14241                    Tab));
14242   verifyFormat("class X {\n"
14243                "\tvoid f() {\n"
14244                "\t\tsomeFunction(parameter1,\n"
14245                "\t\t             parameter2);\n"
14246                "\t}\n"
14247                "};",
14248                Tab);
14249   verifyFormat("#define A                        \\\n"
14250                "\tvoid f() {               \\\n"
14251                "\t\tsomeFunction(    \\\n"
14252                "\t\t    parameter1,  \\\n"
14253                "\t\t    parameter2); \\\n"
14254                "\t}",
14255                Tab);
14256   Tab.TabWidth = 4;
14257   Tab.IndentWidth = 8;
14258   verifyFormat("class TabWidth4Indent8 {\n"
14259                "\t\tvoid f() {\n"
14260                "\t\t\t\tsomeFunction(parameter1,\n"
14261                "\t\t\t\t             parameter2);\n"
14262                "\t\t}\n"
14263                "};",
14264                Tab);
14265   Tab.TabWidth = 4;
14266   Tab.IndentWidth = 4;
14267   verifyFormat("class TabWidth4Indent4 {\n"
14268                "\tvoid f() {\n"
14269                "\t\tsomeFunction(parameter1,\n"
14270                "\t\t             parameter2);\n"
14271                "\t}\n"
14272                "};",
14273                Tab);
14274   Tab.TabWidth = 8;
14275   Tab.IndentWidth = 4;
14276   verifyFormat("class TabWidth8Indent4 {\n"
14277                "    void f() {\n"
14278                "\tsomeFunction(parameter1,\n"
14279                "\t             parameter2);\n"
14280                "    }\n"
14281                "};",
14282                Tab);
14283   Tab.TabWidth = 8;
14284   Tab.IndentWidth = 8;
14285   EXPECT_EQ("/*\n"
14286             "              a\t\tcomment\n"
14287             "              in multiple lines\n"
14288             "       */",
14289             format("   /*\t \t \n"
14290                    " \t \t a\t\tcomment\t \t\n"
14291                    " \t \t in multiple lines\t\n"
14292                    " \t  */",
14293                    Tab));
14294   verifyFormat("{\n"
14295                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14296                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14297                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14298                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14299                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14300                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14301                "};",
14302                Tab);
14303   verifyFormat("enum AA {\n"
14304                "\ta1, // Force multiple lines\n"
14305                "\ta2,\n"
14306                "\ta3\n"
14307                "};",
14308                Tab);
14309   EXPECT_EQ("if (aaaaaaaa && // q\n"
14310             "    bb)         // w\n"
14311             "\t;",
14312             format("if (aaaaaaaa &&// q\n"
14313                    "bb)// w\n"
14314                    ";",
14315                    Tab));
14316   verifyFormat("class X {\n"
14317                "\tvoid f() {\n"
14318                "\t\tsomeFunction(parameter1,\n"
14319                "\t\t             parameter2);\n"
14320                "\t}\n"
14321                "};",
14322                Tab);
14323   verifyFormat("{\n"
14324                "\tQ(\n"
14325                "\t    {\n"
14326                "\t\t    int a;\n"
14327                "\t\t    someFunction(aaaaaaaa,\n"
14328                "\t\t                 bbbbbbb);\n"
14329                "\t    },\n"
14330                "\t    p);\n"
14331                "}",
14332                Tab);
14333   EXPECT_EQ("{\n"
14334             "\t/* aaaa\n"
14335             "\t   bbbb */\n"
14336             "}",
14337             format("{\n"
14338                    "/* aaaa\n"
14339                    "   bbbb */\n"
14340                    "}",
14341                    Tab));
14342   EXPECT_EQ("{\n"
14343             "\t/*\n"
14344             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14345             "\t  bbbbbbbbbbbbb\n"
14346             "\t*/\n"
14347             "}",
14348             format("{\n"
14349                    "/*\n"
14350                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14351                    "*/\n"
14352                    "}",
14353                    Tab));
14354   EXPECT_EQ("{\n"
14355             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14356             "\t// bbbbbbbbbbbbb\n"
14357             "}",
14358             format("{\n"
14359                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14360                    "}",
14361                    Tab));
14362   EXPECT_EQ("{\n"
14363             "\t/*\n"
14364             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14365             "\t  bbbbbbbbbbbbb\n"
14366             "\t*/\n"
14367             "}",
14368             format("{\n"
14369                    "\t/*\n"
14370                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14371                    "\t*/\n"
14372                    "}",
14373                    Tab));
14374   EXPECT_EQ("{\n"
14375             "\t/*\n"
14376             "\n"
14377             "\t*/\n"
14378             "}",
14379             format("{\n"
14380                    "\t/*\n"
14381                    "\n"
14382                    "\t*/\n"
14383                    "}",
14384                    Tab));
14385   EXPECT_EQ("{\n"
14386             "\t/*\n"
14387             " asdf\n"
14388             "\t*/\n"
14389             "}",
14390             format("{\n"
14391                    "\t/*\n"
14392                    " asdf\n"
14393                    "\t*/\n"
14394                    "}",
14395                    Tab));
14396   EXPECT_EQ("/* some\n"
14397             "   comment */",
14398             format(" \t \t /* some\n"
14399                    " \t \t    comment */",
14400                    Tab));
14401   EXPECT_EQ("int a; /* some\n"
14402             "   comment */",
14403             format(" \t \t int a; /* some\n"
14404                    " \t \t    comment */",
14405                    Tab));
14406   EXPECT_EQ("int a; /* some\n"
14407             "comment */",
14408             format(" \t \t int\ta; /* some\n"
14409                    " \t \t    comment */",
14410                    Tab));
14411   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14412             "    comment */",
14413             format(" \t \t f(\"\t\t\"); /* some\n"
14414                    " \t \t    comment */",
14415                    Tab));
14416   EXPECT_EQ("{\n"
14417             "\t/*\n"
14418             "\t * Comment\n"
14419             "\t */\n"
14420             "\tint i;\n"
14421             "}",
14422             format("{\n"
14423                    "\t/*\n"
14424                    "\t * Comment\n"
14425                    "\t */\n"
14426                    "\t int i;\n"
14427                    "}",
14428                    Tab));
14429   Tab.TabWidth = 2;
14430   Tab.IndentWidth = 2;
14431   EXPECT_EQ("{\n"
14432             "\t/* aaaa\n"
14433             "\t   bbbb */\n"
14434             "}",
14435             format("{\n"
14436                    "/* aaaa\n"
14437                    "   bbbb */\n"
14438                    "}",
14439                    Tab));
14440   EXPECT_EQ("{\n"
14441             "\t/*\n"
14442             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14443             "\t  bbbbbbbbbbbbb\n"
14444             "\t*/\n"
14445             "}",
14446             format("{\n"
14447                    "/*\n"
14448                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14449                    "*/\n"
14450                    "}",
14451                    Tab));
14452   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14453   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14454   Tab.TabWidth = 4;
14455   Tab.IndentWidth = 4;
14456   verifyFormat("class Assign {\n"
14457                "\tvoid f() {\n"
14458                "\t\tint         x      = 123;\n"
14459                "\t\tint         random = 4;\n"
14460                "\t\tstd::string alphabet =\n"
14461                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14462                "\t}\n"
14463                "};",
14464                Tab);
14465   Tab.AlignOperands = FormatStyle::OAS_Align;
14466   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14467                "                 cccccccccccccccccccc;",
14468                Tab);
14469   // no alignment
14470   verifyFormat("int aaaaaaaaaa =\n"
14471                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14472                Tab);
14473   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14474                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14475                "                        : 333333333333333;",
14476                Tab);
14477   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14478   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14479   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14480                "               + cccccccccccccccccccc;",
14481                Tab);
14482 }
14483 
14484 TEST_F(FormatTest, ZeroTabWidth) {
14485   FormatStyle Tab = getLLVMStyleWithColumns(42);
14486   Tab.IndentWidth = 8;
14487   Tab.UseTab = FormatStyle::UT_Never;
14488   Tab.TabWidth = 0;
14489   EXPECT_EQ("void a(){\n"
14490             "    // line starts with '\t'\n"
14491             "};",
14492             format("void a(){\n"
14493                    "\t// line starts with '\t'\n"
14494                    "};",
14495                    Tab));
14496 
14497   EXPECT_EQ("void a(){\n"
14498             "    // line starts with '\t'\n"
14499             "};",
14500             format("void a(){\n"
14501                    "\t\t// line starts with '\t'\n"
14502                    "};",
14503                    Tab));
14504 
14505   Tab.UseTab = FormatStyle::UT_ForIndentation;
14506   EXPECT_EQ("void a(){\n"
14507             "    // line starts with '\t'\n"
14508             "};",
14509             format("void a(){\n"
14510                    "\t// line starts with '\t'\n"
14511                    "};",
14512                    Tab));
14513 
14514   EXPECT_EQ("void a(){\n"
14515             "    // line starts with '\t'\n"
14516             "};",
14517             format("void a(){\n"
14518                    "\t\t// line starts with '\t'\n"
14519                    "};",
14520                    Tab));
14521 
14522   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14523   EXPECT_EQ("void a(){\n"
14524             "    // line starts with '\t'\n"
14525             "};",
14526             format("void a(){\n"
14527                    "\t// line starts with '\t'\n"
14528                    "};",
14529                    Tab));
14530 
14531   EXPECT_EQ("void a(){\n"
14532             "    // line starts with '\t'\n"
14533             "};",
14534             format("void a(){\n"
14535                    "\t\t// line starts with '\t'\n"
14536                    "};",
14537                    Tab));
14538 
14539   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14540   EXPECT_EQ("void a(){\n"
14541             "    // line starts with '\t'\n"
14542             "};",
14543             format("void a(){\n"
14544                    "\t// line starts with '\t'\n"
14545                    "};",
14546                    Tab));
14547 
14548   EXPECT_EQ("void a(){\n"
14549             "    // line starts with '\t'\n"
14550             "};",
14551             format("void a(){\n"
14552                    "\t\t// line starts with '\t'\n"
14553                    "};",
14554                    Tab));
14555 
14556   Tab.UseTab = FormatStyle::UT_Always;
14557   EXPECT_EQ("void a(){\n"
14558             "// line starts with '\t'\n"
14559             "};",
14560             format("void a(){\n"
14561                    "\t// line starts with '\t'\n"
14562                    "};",
14563                    Tab));
14564 
14565   EXPECT_EQ("void a(){\n"
14566             "// line starts with '\t'\n"
14567             "};",
14568             format("void a(){\n"
14569                    "\t\t// line starts with '\t'\n"
14570                    "};",
14571                    Tab));
14572 }
14573 
14574 TEST_F(FormatTest, CalculatesOriginalColumn) {
14575   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14576             "q\"; /* some\n"
14577             "       comment */",
14578             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14579                    "q\"; /* some\n"
14580                    "       comment */",
14581                    getLLVMStyle()));
14582   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14583             "/* some\n"
14584             "   comment */",
14585             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14586                    " /* some\n"
14587                    "    comment */",
14588                    getLLVMStyle()));
14589   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14590             "qqq\n"
14591             "/* some\n"
14592             "   comment */",
14593             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14594                    "qqq\n"
14595                    " /* some\n"
14596                    "    comment */",
14597                    getLLVMStyle()));
14598   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14599             "wwww; /* some\n"
14600             "         comment */",
14601             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14602                    "wwww; /* some\n"
14603                    "         comment */",
14604                    getLLVMStyle()));
14605 }
14606 
14607 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14608   FormatStyle NoSpace = getLLVMStyle();
14609   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14610 
14611   verifyFormat("while(true)\n"
14612                "  continue;",
14613                NoSpace);
14614   verifyFormat("for(;;)\n"
14615                "  continue;",
14616                NoSpace);
14617   verifyFormat("if(true)\n"
14618                "  f();\n"
14619                "else if(true)\n"
14620                "  f();",
14621                NoSpace);
14622   verifyFormat("do {\n"
14623                "  do_something();\n"
14624                "} while(something());",
14625                NoSpace);
14626   verifyFormat("switch(x) {\n"
14627                "default:\n"
14628                "  break;\n"
14629                "}",
14630                NoSpace);
14631   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14632   verifyFormat("size_t x = sizeof(x);", NoSpace);
14633   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14634   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14635   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14636   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14637   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14638   verifyFormat("alignas(128) char a[128];", NoSpace);
14639   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14640   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14641   verifyFormat("int f() throw(Deprecated);", NoSpace);
14642   verifyFormat("typedef void (*cb)(int);", NoSpace);
14643   verifyFormat("T A::operator()();", NoSpace);
14644   verifyFormat("X A::operator++(T);", NoSpace);
14645   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14646 
14647   FormatStyle Space = getLLVMStyle();
14648   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14649 
14650   verifyFormat("int f ();", Space);
14651   verifyFormat("void f (int a, T b) {\n"
14652                "  while (true)\n"
14653                "    continue;\n"
14654                "}",
14655                Space);
14656   verifyFormat("if (true)\n"
14657                "  f ();\n"
14658                "else if (true)\n"
14659                "  f ();",
14660                Space);
14661   verifyFormat("do {\n"
14662                "  do_something ();\n"
14663                "} while (something ());",
14664                Space);
14665   verifyFormat("switch (x) {\n"
14666                "default:\n"
14667                "  break;\n"
14668                "}",
14669                Space);
14670   verifyFormat("A::A () : a (1) {}", Space);
14671   verifyFormat("void f () __attribute__ ((asdf));", Space);
14672   verifyFormat("*(&a + 1);\n"
14673                "&((&a)[1]);\n"
14674                "a[(b + c) * d];\n"
14675                "(((a + 1) * 2) + 3) * 4;",
14676                Space);
14677   verifyFormat("#define A(x) x", Space);
14678   verifyFormat("#define A (x) x", Space);
14679   verifyFormat("#if defined(x)\n"
14680                "#endif",
14681                Space);
14682   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14683   verifyFormat("size_t x = sizeof (x);", Space);
14684   verifyFormat("auto f (int x) -> decltype (x);", Space);
14685   verifyFormat("auto f (int x) -> typeof (x);", Space);
14686   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14687   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14688   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14689   verifyFormat("alignas (128) char a[128];", Space);
14690   verifyFormat("size_t x = alignof (MyType);", Space);
14691   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14692   verifyFormat("int f () throw (Deprecated);", Space);
14693   verifyFormat("typedef void (*cb) (int);", Space);
14694   // FIXME these tests regressed behaviour.
14695   // verifyFormat("T A::operator() ();", Space);
14696   // verifyFormat("X A::operator++ (T);", Space);
14697   verifyFormat("auto lambda = [] () { return 0; };", Space);
14698   verifyFormat("int x = int (y);", Space);
14699 
14700   FormatStyle SomeSpace = getLLVMStyle();
14701   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14702 
14703   verifyFormat("[]() -> float {}", SomeSpace);
14704   verifyFormat("[] (auto foo) {}", SomeSpace);
14705   verifyFormat("[foo]() -> int {}", SomeSpace);
14706   verifyFormat("int f();", SomeSpace);
14707   verifyFormat("void f (int a, T b) {\n"
14708                "  while (true)\n"
14709                "    continue;\n"
14710                "}",
14711                SomeSpace);
14712   verifyFormat("if (true)\n"
14713                "  f();\n"
14714                "else if (true)\n"
14715                "  f();",
14716                SomeSpace);
14717   verifyFormat("do {\n"
14718                "  do_something();\n"
14719                "} while (something());",
14720                SomeSpace);
14721   verifyFormat("switch (x) {\n"
14722                "default:\n"
14723                "  break;\n"
14724                "}",
14725                SomeSpace);
14726   verifyFormat("A::A() : a (1) {}", SomeSpace);
14727   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14728   verifyFormat("*(&a + 1);\n"
14729                "&((&a)[1]);\n"
14730                "a[(b + c) * d];\n"
14731                "(((a + 1) * 2) + 3) * 4;",
14732                SomeSpace);
14733   verifyFormat("#define A(x) x", SomeSpace);
14734   verifyFormat("#define A (x) x", SomeSpace);
14735   verifyFormat("#if defined(x)\n"
14736                "#endif",
14737                SomeSpace);
14738   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14739   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14740   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14741   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14742   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14743   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14744   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14745   verifyFormat("alignas (128) char a[128];", SomeSpace);
14746   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14747   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14748                SomeSpace);
14749   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14750   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14751   verifyFormat("T A::operator()();", SomeSpace);
14752   // FIXME these tests regressed behaviour.
14753   // verifyFormat("X A::operator++ (T);", SomeSpace);
14754   verifyFormat("int x = int (y);", SomeSpace);
14755   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14756 
14757   FormatStyle SpaceControlStatements = getLLVMStyle();
14758   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14759   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14760 
14761   verifyFormat("while (true)\n"
14762                "  continue;",
14763                SpaceControlStatements);
14764   verifyFormat("if (true)\n"
14765                "  f();\n"
14766                "else if (true)\n"
14767                "  f();",
14768                SpaceControlStatements);
14769   verifyFormat("for (;;) {\n"
14770                "  do_something();\n"
14771                "}",
14772                SpaceControlStatements);
14773   verifyFormat("do {\n"
14774                "  do_something();\n"
14775                "} while (something());",
14776                SpaceControlStatements);
14777   verifyFormat("switch (x) {\n"
14778                "default:\n"
14779                "  break;\n"
14780                "}",
14781                SpaceControlStatements);
14782 
14783   FormatStyle SpaceFuncDecl = getLLVMStyle();
14784   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14785   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14786 
14787   verifyFormat("int f ();", SpaceFuncDecl);
14788   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14789   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14790   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14791   verifyFormat("#define A(x) x", SpaceFuncDecl);
14792   verifyFormat("#define A (x) x", SpaceFuncDecl);
14793   verifyFormat("#if defined(x)\n"
14794                "#endif",
14795                SpaceFuncDecl);
14796   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14797   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14798   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14799   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14800   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14801   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14802   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14803   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14804   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14805   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14806                SpaceFuncDecl);
14807   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14808   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14809   // FIXME these tests regressed behaviour.
14810   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14811   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14812   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14813   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14814   verifyFormat("int x = int(y);", SpaceFuncDecl);
14815   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14816                SpaceFuncDecl);
14817 
14818   FormatStyle SpaceFuncDef = getLLVMStyle();
14819   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14820   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14821 
14822   verifyFormat("int f();", SpaceFuncDef);
14823   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14824   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14825   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14826   verifyFormat("#define A(x) x", SpaceFuncDef);
14827   verifyFormat("#define A (x) x", SpaceFuncDef);
14828   verifyFormat("#if defined(x)\n"
14829                "#endif",
14830                SpaceFuncDef);
14831   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14832   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14833   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14834   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14835   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14836   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14837   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14838   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14839   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14840   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14841                SpaceFuncDef);
14842   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14843   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14844   verifyFormat("T A::operator()();", SpaceFuncDef);
14845   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14846   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14847   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14848   verifyFormat("int x = int(y);", SpaceFuncDef);
14849   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14850                SpaceFuncDef);
14851 
14852   FormatStyle SpaceIfMacros = getLLVMStyle();
14853   SpaceIfMacros.IfMacros.clear();
14854   SpaceIfMacros.IfMacros.push_back("MYIF");
14855   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14856   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14857   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14858   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14859   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14860 
14861   FormatStyle SpaceForeachMacros = getLLVMStyle();
14862   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14863             FormatStyle::SBS_Never);
14864   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14865   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14866   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14867   verifyFormat("for (;;) {\n"
14868                "}",
14869                SpaceForeachMacros);
14870   verifyFormat("foreach (Item *item, itemlist) {\n"
14871                "}",
14872                SpaceForeachMacros);
14873   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14874                "}",
14875                SpaceForeachMacros);
14876   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14877                "}",
14878                SpaceForeachMacros);
14879   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14880 
14881   FormatStyle SomeSpace2 = getLLVMStyle();
14882   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14883   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14884   verifyFormat("[]() -> float {}", SomeSpace2);
14885   verifyFormat("[] (auto foo) {}", SomeSpace2);
14886   verifyFormat("[foo]() -> int {}", SomeSpace2);
14887   verifyFormat("int f();", SomeSpace2);
14888   verifyFormat("void f (int a, T b) {\n"
14889                "  while (true)\n"
14890                "    continue;\n"
14891                "}",
14892                SomeSpace2);
14893   verifyFormat("if (true)\n"
14894                "  f();\n"
14895                "else if (true)\n"
14896                "  f();",
14897                SomeSpace2);
14898   verifyFormat("do {\n"
14899                "  do_something();\n"
14900                "} while (something());",
14901                SomeSpace2);
14902   verifyFormat("switch (x) {\n"
14903                "default:\n"
14904                "  break;\n"
14905                "}",
14906                SomeSpace2);
14907   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14908   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14909   verifyFormat("*(&a + 1);\n"
14910                "&((&a)[1]);\n"
14911                "a[(b + c) * d];\n"
14912                "(((a + 1) * 2) + 3) * 4;",
14913                SomeSpace2);
14914   verifyFormat("#define A(x) x", SomeSpace2);
14915   verifyFormat("#define A (x) x", SomeSpace2);
14916   verifyFormat("#if defined(x)\n"
14917                "#endif",
14918                SomeSpace2);
14919   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14920   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14921   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14922   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14923   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14924   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14925   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14926   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14927   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14928   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14929                SomeSpace2);
14930   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14931   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14932   verifyFormat("T A::operator()();", SomeSpace2);
14933   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14934   verifyFormat("int x = int (y);", SomeSpace2);
14935   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14936 
14937   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14938   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14939   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14940       .AfterOverloadedOperator = true;
14941 
14942   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
14943   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
14944   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
14945   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14946 
14947   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14948       .AfterOverloadedOperator = false;
14949 
14950   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
14951   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
14952   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
14953   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14954 }
14955 
14956 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14957   FormatStyle Spaces = getLLVMStyle();
14958   Spaces.SpaceAfterLogicalNot = true;
14959 
14960   verifyFormat("bool x = ! y", Spaces);
14961   verifyFormat("if (! isFailure())", Spaces);
14962   verifyFormat("if (! (a && b))", Spaces);
14963   verifyFormat("\"Error!\"", Spaces);
14964   verifyFormat("! ! x", Spaces);
14965 }
14966 
14967 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14968   FormatStyle Spaces = getLLVMStyle();
14969 
14970   Spaces.SpacesInParentheses = true;
14971   verifyFormat("do_something( ::globalVar );", Spaces);
14972   verifyFormat("call( x, y, z );", Spaces);
14973   verifyFormat("call();", Spaces);
14974   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14975   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14976                Spaces);
14977   verifyFormat("while ( (bool)1 )\n"
14978                "  continue;",
14979                Spaces);
14980   verifyFormat("for ( ;; )\n"
14981                "  continue;",
14982                Spaces);
14983   verifyFormat("if ( true )\n"
14984                "  f();\n"
14985                "else if ( true )\n"
14986                "  f();",
14987                Spaces);
14988   verifyFormat("do {\n"
14989                "  do_something( (int)i );\n"
14990                "} while ( something() );",
14991                Spaces);
14992   verifyFormat("switch ( x ) {\n"
14993                "default:\n"
14994                "  break;\n"
14995                "}",
14996                Spaces);
14997 
14998   Spaces.SpacesInParentheses = false;
14999   Spaces.SpacesInCStyleCastParentheses = true;
15000   verifyFormat("Type *A = ( Type * )P;", Spaces);
15001   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
15002   verifyFormat("x = ( int32 )y;", Spaces);
15003   verifyFormat("int a = ( int )(2.0f);", Spaces);
15004   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
15005   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
15006   verifyFormat("#define x (( int )-1)", Spaces);
15007 
15008   // Run the first set of tests again with:
15009   Spaces.SpacesInParentheses = false;
15010   Spaces.SpaceInEmptyParentheses = true;
15011   Spaces.SpacesInCStyleCastParentheses = true;
15012   verifyFormat("call(x, y, z);", Spaces);
15013   verifyFormat("call( );", Spaces);
15014   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15015   verifyFormat("while (( bool )1)\n"
15016                "  continue;",
15017                Spaces);
15018   verifyFormat("for (;;)\n"
15019                "  continue;",
15020                Spaces);
15021   verifyFormat("if (true)\n"
15022                "  f( );\n"
15023                "else if (true)\n"
15024                "  f( );",
15025                Spaces);
15026   verifyFormat("do {\n"
15027                "  do_something(( int )i);\n"
15028                "} while (something( ));",
15029                Spaces);
15030   verifyFormat("switch (x) {\n"
15031                "default:\n"
15032                "  break;\n"
15033                "}",
15034                Spaces);
15035 
15036   // Run the first set of tests again with:
15037   Spaces.SpaceAfterCStyleCast = true;
15038   verifyFormat("call(x, y, z);", Spaces);
15039   verifyFormat("call( );", Spaces);
15040   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15041   verifyFormat("while (( bool ) 1)\n"
15042                "  continue;",
15043                Spaces);
15044   verifyFormat("for (;;)\n"
15045                "  continue;",
15046                Spaces);
15047   verifyFormat("if (true)\n"
15048                "  f( );\n"
15049                "else if (true)\n"
15050                "  f( );",
15051                Spaces);
15052   verifyFormat("do {\n"
15053                "  do_something(( int ) i);\n"
15054                "} while (something( ));",
15055                Spaces);
15056   verifyFormat("switch (x) {\n"
15057                "default:\n"
15058                "  break;\n"
15059                "}",
15060                Spaces);
15061   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
15062   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
15063   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
15064   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
15065   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
15066 
15067   // Run subset of tests again with:
15068   Spaces.SpacesInCStyleCastParentheses = false;
15069   Spaces.SpaceAfterCStyleCast = true;
15070   verifyFormat("while ((bool) 1)\n"
15071                "  continue;",
15072                Spaces);
15073   verifyFormat("do {\n"
15074                "  do_something((int) i);\n"
15075                "} while (something( ));",
15076                Spaces);
15077 
15078   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
15079   verifyFormat("size_t idx = (size_t) a;", Spaces);
15080   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
15081   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15082   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15083   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15084   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15085   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
15086   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
15087   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15088   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15089   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15090   Spaces.ColumnLimit = 80;
15091   Spaces.IndentWidth = 4;
15092   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15093   verifyFormat("void foo( ) {\n"
15094                "    size_t foo = (*(function))(\n"
15095                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15096                "BarrrrrrrrrrrrLong,\n"
15097                "        FoooooooooLooooong);\n"
15098                "}",
15099                Spaces);
15100   Spaces.SpaceAfterCStyleCast = false;
15101   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15102   verifyFormat("size_t idx = (size_t)a;", Spaces);
15103   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15104   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15105   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15106   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15107   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15108 
15109   verifyFormat("void foo( ) {\n"
15110                "    size_t foo = (*(function))(\n"
15111                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15112                "BarrrrrrrrrrrrLong,\n"
15113                "        FoooooooooLooooong);\n"
15114                "}",
15115                Spaces);
15116 }
15117 
15118 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15119   verifyFormat("int a[5];");
15120   verifyFormat("a[3] += 42;");
15121 
15122   FormatStyle Spaces = getLLVMStyle();
15123   Spaces.SpacesInSquareBrackets = true;
15124   // Not lambdas.
15125   verifyFormat("int a[ 5 ];", Spaces);
15126   verifyFormat("a[ 3 ] += 42;", Spaces);
15127   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15128   verifyFormat("double &operator[](int i) { return 0; }\n"
15129                "int i;",
15130                Spaces);
15131   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15132   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15133   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15134   // Lambdas.
15135   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15136   verifyFormat("return [ i, args... ] {};", Spaces);
15137   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15138   verifyFormat("int foo = [ = ]() {};", Spaces);
15139   verifyFormat("int foo = [ & ]() {};", Spaces);
15140   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15141   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15142 }
15143 
15144 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15145   FormatStyle NoSpaceStyle = getLLVMStyle();
15146   verifyFormat("int a[5];", NoSpaceStyle);
15147   verifyFormat("a[3] += 42;", NoSpaceStyle);
15148 
15149   verifyFormat("int a[1];", NoSpaceStyle);
15150   verifyFormat("int 1 [a];", NoSpaceStyle);
15151   verifyFormat("int a[1][2];", NoSpaceStyle);
15152   verifyFormat("a[7] = 5;", NoSpaceStyle);
15153   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15154   verifyFormat("f([] {})", NoSpaceStyle);
15155 
15156   FormatStyle Space = getLLVMStyle();
15157   Space.SpaceBeforeSquareBrackets = true;
15158   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15159   verifyFormat("return [i, args...] {};", Space);
15160 
15161   verifyFormat("int a [5];", Space);
15162   verifyFormat("a [3] += 42;", Space);
15163   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15164   verifyFormat("double &operator[](int i) { return 0; }\n"
15165                "int i;",
15166                Space);
15167   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15168   verifyFormat("int i = a [a][a]->f();", Space);
15169   verifyFormat("int i = (*b) [a]->f();", Space);
15170 
15171   verifyFormat("int a [1];", Space);
15172   verifyFormat("int 1 [a];", Space);
15173   verifyFormat("int a [1][2];", Space);
15174   verifyFormat("a [7] = 5;", Space);
15175   verifyFormat("int a = (f()) [23];", Space);
15176   verifyFormat("f([] {})", Space);
15177 }
15178 
15179 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15180   verifyFormat("int a = 5;");
15181   verifyFormat("a += 42;");
15182   verifyFormat("a or_eq 8;");
15183 
15184   FormatStyle Spaces = getLLVMStyle();
15185   Spaces.SpaceBeforeAssignmentOperators = false;
15186   verifyFormat("int a= 5;", Spaces);
15187   verifyFormat("a+= 42;", Spaces);
15188   verifyFormat("a or_eq 8;", Spaces);
15189 }
15190 
15191 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15192   verifyFormat("class Foo : public Bar {};");
15193   verifyFormat("Foo::Foo() : foo(1) {}");
15194   verifyFormat("for (auto a : b) {\n}");
15195   verifyFormat("int x = a ? b : c;");
15196   verifyFormat("{\n"
15197                "label0:\n"
15198                "  int x = 0;\n"
15199                "}");
15200   verifyFormat("switch (x) {\n"
15201                "case 1:\n"
15202                "default:\n"
15203                "}");
15204   verifyFormat("switch (allBraces) {\n"
15205                "case 1: {\n"
15206                "  break;\n"
15207                "}\n"
15208                "case 2: {\n"
15209                "  [[fallthrough]];\n"
15210                "}\n"
15211                "default: {\n"
15212                "  break;\n"
15213                "}\n"
15214                "}");
15215 
15216   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15217   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15218   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15219   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15220   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15221   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15222   verifyFormat("{\n"
15223                "label1:\n"
15224                "  int x = 0;\n"
15225                "}",
15226                CtorInitializerStyle);
15227   verifyFormat("switch (x) {\n"
15228                "case 1:\n"
15229                "default:\n"
15230                "}",
15231                CtorInitializerStyle);
15232   verifyFormat("switch (allBraces) {\n"
15233                "case 1: {\n"
15234                "  break;\n"
15235                "}\n"
15236                "case 2: {\n"
15237                "  [[fallthrough]];\n"
15238                "}\n"
15239                "default: {\n"
15240                "  break;\n"
15241                "}\n"
15242                "}",
15243                CtorInitializerStyle);
15244   CtorInitializerStyle.BreakConstructorInitializers =
15245       FormatStyle::BCIS_AfterColon;
15246   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15247                "    aaaaaaaaaaaaaaaa(1),\n"
15248                "    bbbbbbbbbbbbbbbb(2) {}",
15249                CtorInitializerStyle);
15250   CtorInitializerStyle.BreakConstructorInitializers =
15251       FormatStyle::BCIS_BeforeComma;
15252   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15253                "    : aaaaaaaaaaaaaaaa(1)\n"
15254                "    , bbbbbbbbbbbbbbbb(2) {}",
15255                CtorInitializerStyle);
15256   CtorInitializerStyle.BreakConstructorInitializers =
15257       FormatStyle::BCIS_BeforeColon;
15258   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15259                "    : aaaaaaaaaaaaaaaa(1),\n"
15260                "      bbbbbbbbbbbbbbbb(2) {}",
15261                CtorInitializerStyle);
15262   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15263   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15264                ": aaaaaaaaaaaaaaaa(1),\n"
15265                "  bbbbbbbbbbbbbbbb(2) {}",
15266                CtorInitializerStyle);
15267 
15268   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15269   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15270   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15271   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15272   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15273   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15274   verifyFormat("{\n"
15275                "label2:\n"
15276                "  int x = 0;\n"
15277                "}",
15278                InheritanceStyle);
15279   verifyFormat("switch (x) {\n"
15280                "case 1:\n"
15281                "default:\n"
15282                "}",
15283                InheritanceStyle);
15284   verifyFormat("switch (allBraces) {\n"
15285                "case 1: {\n"
15286                "  break;\n"
15287                "}\n"
15288                "case 2: {\n"
15289                "  [[fallthrough]];\n"
15290                "}\n"
15291                "default: {\n"
15292                "  break;\n"
15293                "}\n"
15294                "}",
15295                InheritanceStyle);
15296   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15297   verifyFormat("class Foooooooooooooooooooooo\n"
15298                "    : public aaaaaaaaaaaaaaaaaa,\n"
15299                "      public bbbbbbbbbbbbbbbbbb {\n"
15300                "}",
15301                InheritanceStyle);
15302   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15303   verifyFormat("class Foooooooooooooooooooooo:\n"
15304                "    public aaaaaaaaaaaaaaaaaa,\n"
15305                "    public bbbbbbbbbbbbbbbbbb {\n"
15306                "}",
15307                InheritanceStyle);
15308   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15309   verifyFormat("class Foooooooooooooooooooooo\n"
15310                "    : public aaaaaaaaaaaaaaaaaa\n"
15311                "    , public bbbbbbbbbbbbbbbbbb {\n"
15312                "}",
15313                InheritanceStyle);
15314   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15315   verifyFormat("class Foooooooooooooooooooooo\n"
15316                "    : public aaaaaaaaaaaaaaaaaa,\n"
15317                "      public bbbbbbbbbbbbbbbbbb {\n"
15318                "}",
15319                InheritanceStyle);
15320   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15321   verifyFormat("class Foooooooooooooooooooooo\n"
15322                ": public aaaaaaaaaaaaaaaaaa,\n"
15323                "  public bbbbbbbbbbbbbbbbbb {}",
15324                InheritanceStyle);
15325 
15326   FormatStyle ForLoopStyle = getLLVMStyle();
15327   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15328   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15329   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15330   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15331   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15332   verifyFormat("{\n"
15333                "label2:\n"
15334                "  int x = 0;\n"
15335                "}",
15336                ForLoopStyle);
15337   verifyFormat("switch (x) {\n"
15338                "case 1:\n"
15339                "default:\n"
15340                "}",
15341                ForLoopStyle);
15342   verifyFormat("switch (allBraces) {\n"
15343                "case 1: {\n"
15344                "  break;\n"
15345                "}\n"
15346                "case 2: {\n"
15347                "  [[fallthrough]];\n"
15348                "}\n"
15349                "default: {\n"
15350                "  break;\n"
15351                "}\n"
15352                "}",
15353                ForLoopStyle);
15354 
15355   FormatStyle CaseStyle = getLLVMStyle();
15356   CaseStyle.SpaceBeforeCaseColon = true;
15357   verifyFormat("class Foo : public Bar {};", CaseStyle);
15358   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15359   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15360   verifyFormat("int x = a ? b : c;", CaseStyle);
15361   verifyFormat("switch (x) {\n"
15362                "case 1 :\n"
15363                "default :\n"
15364                "}",
15365                CaseStyle);
15366   verifyFormat("switch (allBraces) {\n"
15367                "case 1 : {\n"
15368                "  break;\n"
15369                "}\n"
15370                "case 2 : {\n"
15371                "  [[fallthrough]];\n"
15372                "}\n"
15373                "default : {\n"
15374                "  break;\n"
15375                "}\n"
15376                "}",
15377                CaseStyle);
15378 
15379   FormatStyle NoSpaceStyle = getLLVMStyle();
15380   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15381   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15382   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15383   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15384   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15385   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15386   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15387   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15388   verifyFormat("{\n"
15389                "label3:\n"
15390                "  int x = 0;\n"
15391                "}",
15392                NoSpaceStyle);
15393   verifyFormat("switch (x) {\n"
15394                "case 1:\n"
15395                "default:\n"
15396                "}",
15397                NoSpaceStyle);
15398   verifyFormat("switch (allBraces) {\n"
15399                "case 1: {\n"
15400                "  break;\n"
15401                "}\n"
15402                "case 2: {\n"
15403                "  [[fallthrough]];\n"
15404                "}\n"
15405                "default: {\n"
15406                "  break;\n"
15407                "}\n"
15408                "}",
15409                NoSpaceStyle);
15410 
15411   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15412   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15413   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15414   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15415   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15416   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15417   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15418   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15419   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15420   verifyFormat("{\n"
15421                "label3:\n"
15422                "  int x = 0;\n"
15423                "}",
15424                InvertedSpaceStyle);
15425   verifyFormat("switch (x) {\n"
15426                "case 1 :\n"
15427                "case 2 : {\n"
15428                "  break;\n"
15429                "}\n"
15430                "default :\n"
15431                "  break;\n"
15432                "}",
15433                InvertedSpaceStyle);
15434   verifyFormat("switch (allBraces) {\n"
15435                "case 1 : {\n"
15436                "  break;\n"
15437                "}\n"
15438                "case 2 : {\n"
15439                "  [[fallthrough]];\n"
15440                "}\n"
15441                "default : {\n"
15442                "  break;\n"
15443                "}\n"
15444                "}",
15445                InvertedSpaceStyle);
15446 }
15447 
15448 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15449   FormatStyle Style = getLLVMStyle();
15450 
15451   Style.PointerAlignment = FormatStyle::PAS_Left;
15452   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15453   verifyFormat("void* const* x = NULL;", Style);
15454 
15455 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15456   do {                                                                         \
15457     Style.PointerAlignment = FormatStyle::Pointers;                            \
15458     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15459     verifyFormat(Code, Style);                                                 \
15460   } while (false)
15461 
15462   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15463   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15464   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15465 
15466   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15467   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15468   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15469 
15470   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15471   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15472   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15473 
15474   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15475   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15476   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15477 
15478   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15479   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15480                         SAPQ_Default);
15481   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15482                         SAPQ_Default);
15483 
15484   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15485   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15486                         SAPQ_Before);
15487   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15488                         SAPQ_Before);
15489 
15490   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15491   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15492   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15493                         SAPQ_After);
15494 
15495   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15496   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15497   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15498 
15499 #undef verifyQualifierSpaces
15500 
15501   FormatStyle Spaces = getLLVMStyle();
15502   Spaces.AttributeMacros.push_back("qualified");
15503   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15504   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15505   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15506   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15507   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15508   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15509   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15510   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15511   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15512   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15513   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15514   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15515   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15516 
15517   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15518   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15519   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15520   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15521   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15522   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15523   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15524   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15525   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15526   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15527   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15528   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15529   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15530   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15531   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15532 
15533   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15534   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15535   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15536   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15537   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15538   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15539   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15540   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15541 }
15542 
15543 TEST_F(FormatTest, AlignConsecutiveMacros) {
15544   FormatStyle Style = getLLVMStyle();
15545   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15546   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15547   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15548 
15549   verifyFormat("#define a 3\n"
15550                "#define bbbb 4\n"
15551                "#define ccc (5)",
15552                Style);
15553 
15554   verifyFormat("#define f(x) (x * x)\n"
15555                "#define fff(x, y, z) (x * y + z)\n"
15556                "#define ffff(x, y) (x - y)",
15557                Style);
15558 
15559   verifyFormat("#define foo(x, y) (x + y)\n"
15560                "#define bar (5, 6)(2 + 2)",
15561                Style);
15562 
15563   verifyFormat("#define a 3\n"
15564                "#define bbbb 4\n"
15565                "#define ccc (5)\n"
15566                "#define f(x) (x * x)\n"
15567                "#define fff(x, y, z) (x * y + z)\n"
15568                "#define ffff(x, y) (x - y)",
15569                Style);
15570 
15571   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15572   verifyFormat("#define a    3\n"
15573                "#define bbbb 4\n"
15574                "#define ccc  (5)",
15575                Style);
15576 
15577   verifyFormat("#define f(x)         (x * x)\n"
15578                "#define fff(x, y, z) (x * y + z)\n"
15579                "#define ffff(x, y)   (x - y)",
15580                Style);
15581 
15582   verifyFormat("#define foo(x, y) (x + y)\n"
15583                "#define bar       (5, 6)(2 + 2)",
15584                Style);
15585 
15586   verifyFormat("#define a            3\n"
15587                "#define bbbb         4\n"
15588                "#define ccc          (5)\n"
15589                "#define f(x)         (x * x)\n"
15590                "#define fff(x, y, z) (x * y + z)\n"
15591                "#define ffff(x, y)   (x - y)",
15592                Style);
15593 
15594   verifyFormat("#define a         5\n"
15595                "#define foo(x, y) (x + y)\n"
15596                "#define CCC       (6)\n"
15597                "auto lambda = []() {\n"
15598                "  auto  ii = 0;\n"
15599                "  float j  = 0;\n"
15600                "  return 0;\n"
15601                "};\n"
15602                "int   i  = 0;\n"
15603                "float i2 = 0;\n"
15604                "auto  v  = type{\n"
15605                "    i = 1,   //\n"
15606                "    (i = 2), //\n"
15607                "    i = 3    //\n"
15608                "};",
15609                Style);
15610 
15611   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15612   Style.ColumnLimit = 20;
15613 
15614   verifyFormat("#define a          \\\n"
15615                "  \"aabbbbbbbbbbbb\"\n"
15616                "#define D          \\\n"
15617                "  \"aabbbbbbbbbbbb\" \\\n"
15618                "  \"ccddeeeeeeeee\"\n"
15619                "#define B          \\\n"
15620                "  \"QQQQQQQQQQQQQ\"  \\\n"
15621                "  \"FFFFFFFFFFFFF\"  \\\n"
15622                "  \"LLLLLLLL\"\n",
15623                Style);
15624 
15625   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15626   verifyFormat("#define a          \\\n"
15627                "  \"aabbbbbbbbbbbb\"\n"
15628                "#define D          \\\n"
15629                "  \"aabbbbbbbbbbbb\" \\\n"
15630                "  \"ccddeeeeeeeee\"\n"
15631                "#define B          \\\n"
15632                "  \"QQQQQQQQQQQQQ\"  \\\n"
15633                "  \"FFFFFFFFFFFFF\"  \\\n"
15634                "  \"LLLLLLLL\"\n",
15635                Style);
15636 
15637   // Test across comments
15638   Style.MaxEmptyLinesToKeep = 10;
15639   Style.ReflowComments = false;
15640   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15641   EXPECT_EQ("#define a    3\n"
15642             "// line comment\n"
15643             "#define bbbb 4\n"
15644             "#define ccc  (5)",
15645             format("#define a 3\n"
15646                    "// line comment\n"
15647                    "#define bbbb 4\n"
15648                    "#define ccc (5)",
15649                    Style));
15650 
15651   EXPECT_EQ("#define a    3\n"
15652             "/* block comment */\n"
15653             "#define bbbb 4\n"
15654             "#define ccc  (5)",
15655             format("#define a  3\n"
15656                    "/* block comment */\n"
15657                    "#define bbbb 4\n"
15658                    "#define ccc (5)",
15659                    Style));
15660 
15661   EXPECT_EQ("#define a    3\n"
15662             "/* multi-line *\n"
15663             " * block comment */\n"
15664             "#define bbbb 4\n"
15665             "#define ccc  (5)",
15666             format("#define a 3\n"
15667                    "/* multi-line *\n"
15668                    " * block comment */\n"
15669                    "#define bbbb 4\n"
15670                    "#define ccc (5)",
15671                    Style));
15672 
15673   EXPECT_EQ("#define a    3\n"
15674             "// multi-line line comment\n"
15675             "//\n"
15676             "#define bbbb 4\n"
15677             "#define ccc  (5)",
15678             format("#define a  3\n"
15679                    "// multi-line line comment\n"
15680                    "//\n"
15681                    "#define bbbb 4\n"
15682                    "#define ccc (5)",
15683                    Style));
15684 
15685   EXPECT_EQ("#define a 3\n"
15686             "// empty lines still break.\n"
15687             "\n"
15688             "#define bbbb 4\n"
15689             "#define ccc  (5)",
15690             format("#define a     3\n"
15691                    "// empty lines still break.\n"
15692                    "\n"
15693                    "#define bbbb     4\n"
15694                    "#define ccc  (5)",
15695                    Style));
15696 
15697   // Test across empty lines
15698   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15699   EXPECT_EQ("#define a    3\n"
15700             "\n"
15701             "#define bbbb 4\n"
15702             "#define ccc  (5)",
15703             format("#define a 3\n"
15704                    "\n"
15705                    "#define bbbb 4\n"
15706                    "#define ccc (5)",
15707                    Style));
15708 
15709   EXPECT_EQ("#define a    3\n"
15710             "\n"
15711             "\n"
15712             "\n"
15713             "#define bbbb 4\n"
15714             "#define ccc  (5)",
15715             format("#define a        3\n"
15716                    "\n"
15717                    "\n"
15718                    "\n"
15719                    "#define bbbb 4\n"
15720                    "#define ccc (5)",
15721                    Style));
15722 
15723   EXPECT_EQ("#define a 3\n"
15724             "// comments should break alignment\n"
15725             "//\n"
15726             "#define bbbb 4\n"
15727             "#define ccc  (5)",
15728             format("#define a        3\n"
15729                    "// comments should break alignment\n"
15730                    "//\n"
15731                    "#define bbbb 4\n"
15732                    "#define ccc (5)",
15733                    Style));
15734 
15735   // Test across empty lines and comments
15736   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15737   verifyFormat("#define a    3\n"
15738                "\n"
15739                "// line comment\n"
15740                "#define bbbb 4\n"
15741                "#define ccc  (5)",
15742                Style);
15743 
15744   EXPECT_EQ("#define a    3\n"
15745             "\n"
15746             "\n"
15747             "/* multi-line *\n"
15748             " * block comment */\n"
15749             "\n"
15750             "\n"
15751             "#define bbbb 4\n"
15752             "#define ccc  (5)",
15753             format("#define a 3\n"
15754                    "\n"
15755                    "\n"
15756                    "/* multi-line *\n"
15757                    " * block comment */\n"
15758                    "\n"
15759                    "\n"
15760                    "#define bbbb 4\n"
15761                    "#define ccc (5)",
15762                    Style));
15763 
15764   EXPECT_EQ("#define a    3\n"
15765             "\n"
15766             "\n"
15767             "/* multi-line *\n"
15768             " * block comment */\n"
15769             "\n"
15770             "\n"
15771             "#define bbbb 4\n"
15772             "#define ccc  (5)",
15773             format("#define a 3\n"
15774                    "\n"
15775                    "\n"
15776                    "/* multi-line *\n"
15777                    " * block comment */\n"
15778                    "\n"
15779                    "\n"
15780                    "#define bbbb 4\n"
15781                    "#define ccc       (5)",
15782                    Style));
15783 }
15784 
15785 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15786   FormatStyle Alignment = getLLVMStyle();
15787   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15788   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15789 
15790   Alignment.MaxEmptyLinesToKeep = 10;
15791   /* Test alignment across empty lines */
15792   EXPECT_EQ("int a           = 5;\n"
15793             "\n"
15794             "int oneTwoThree = 123;",
15795             format("int a       = 5;\n"
15796                    "\n"
15797                    "int oneTwoThree= 123;",
15798                    Alignment));
15799   EXPECT_EQ("int a           = 5;\n"
15800             "int one         = 1;\n"
15801             "\n"
15802             "int oneTwoThree = 123;",
15803             format("int a = 5;\n"
15804                    "int one = 1;\n"
15805                    "\n"
15806                    "int oneTwoThree = 123;",
15807                    Alignment));
15808   EXPECT_EQ("int a           = 5;\n"
15809             "int one         = 1;\n"
15810             "\n"
15811             "int oneTwoThree = 123;\n"
15812             "int oneTwo      = 12;",
15813             format("int a = 5;\n"
15814                    "int one = 1;\n"
15815                    "\n"
15816                    "int oneTwoThree = 123;\n"
15817                    "int oneTwo = 12;",
15818                    Alignment));
15819 
15820   /* Test across comments */
15821   EXPECT_EQ("int a = 5;\n"
15822             "/* block comment */\n"
15823             "int oneTwoThree = 123;",
15824             format("int a = 5;\n"
15825                    "/* block comment */\n"
15826                    "int oneTwoThree=123;",
15827                    Alignment));
15828 
15829   EXPECT_EQ("int a = 5;\n"
15830             "// line comment\n"
15831             "int oneTwoThree = 123;",
15832             format("int a = 5;\n"
15833                    "// line comment\n"
15834                    "int oneTwoThree=123;",
15835                    Alignment));
15836 
15837   /* Test across comments and newlines */
15838   EXPECT_EQ("int a = 5;\n"
15839             "\n"
15840             "/* block comment */\n"
15841             "int oneTwoThree = 123;",
15842             format("int a = 5;\n"
15843                    "\n"
15844                    "/* block comment */\n"
15845                    "int oneTwoThree=123;",
15846                    Alignment));
15847 
15848   EXPECT_EQ("int a = 5;\n"
15849             "\n"
15850             "// line comment\n"
15851             "int oneTwoThree = 123;",
15852             format("int a = 5;\n"
15853                    "\n"
15854                    "// line comment\n"
15855                    "int oneTwoThree=123;",
15856                    Alignment));
15857 }
15858 
15859 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15860   FormatStyle Alignment = getLLVMStyle();
15861   Alignment.AlignConsecutiveDeclarations =
15862       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15863   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15864 
15865   Alignment.MaxEmptyLinesToKeep = 10;
15866   /* Test alignment across empty lines */
15867   EXPECT_EQ("int         a = 5;\n"
15868             "\n"
15869             "float const oneTwoThree = 123;",
15870             format("int a = 5;\n"
15871                    "\n"
15872                    "float const oneTwoThree = 123;",
15873                    Alignment));
15874   EXPECT_EQ("int         a = 5;\n"
15875             "float const one = 1;\n"
15876             "\n"
15877             "int         oneTwoThree = 123;",
15878             format("int a = 5;\n"
15879                    "float const one = 1;\n"
15880                    "\n"
15881                    "int oneTwoThree = 123;",
15882                    Alignment));
15883 
15884   /* Test across comments */
15885   EXPECT_EQ("float const a = 5;\n"
15886             "/* block comment */\n"
15887             "int         oneTwoThree = 123;",
15888             format("float const a = 5;\n"
15889                    "/* block comment */\n"
15890                    "int oneTwoThree=123;",
15891                    Alignment));
15892 
15893   EXPECT_EQ("float const a = 5;\n"
15894             "// line comment\n"
15895             "int         oneTwoThree = 123;",
15896             format("float const a = 5;\n"
15897                    "// line comment\n"
15898                    "int oneTwoThree=123;",
15899                    Alignment));
15900 
15901   /* Test across comments and newlines */
15902   EXPECT_EQ("float const a = 5;\n"
15903             "\n"
15904             "/* block comment */\n"
15905             "int         oneTwoThree = 123;",
15906             format("float const a = 5;\n"
15907                    "\n"
15908                    "/* block comment */\n"
15909                    "int         oneTwoThree=123;",
15910                    Alignment));
15911 
15912   EXPECT_EQ("float const a = 5;\n"
15913             "\n"
15914             "// line comment\n"
15915             "int         oneTwoThree = 123;",
15916             format("float const a = 5;\n"
15917                    "\n"
15918                    "// line comment\n"
15919                    "int oneTwoThree=123;",
15920                    Alignment));
15921 }
15922 
15923 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15924   FormatStyle Alignment = getLLVMStyle();
15925   Alignment.AlignConsecutiveBitFields =
15926       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15927 
15928   Alignment.MaxEmptyLinesToKeep = 10;
15929   /* Test alignment across empty lines */
15930   EXPECT_EQ("int a            : 5;\n"
15931             "\n"
15932             "int longbitfield : 6;",
15933             format("int a : 5;\n"
15934                    "\n"
15935                    "int longbitfield : 6;",
15936                    Alignment));
15937   EXPECT_EQ("int a            : 5;\n"
15938             "int one          : 1;\n"
15939             "\n"
15940             "int longbitfield : 6;",
15941             format("int a : 5;\n"
15942                    "int one : 1;\n"
15943                    "\n"
15944                    "int longbitfield : 6;",
15945                    Alignment));
15946 
15947   /* Test across comments */
15948   EXPECT_EQ("int a            : 5;\n"
15949             "/* block comment */\n"
15950             "int longbitfield : 6;",
15951             format("int a : 5;\n"
15952                    "/* block comment */\n"
15953                    "int longbitfield : 6;",
15954                    Alignment));
15955   EXPECT_EQ("int a            : 5;\n"
15956             "int one          : 1;\n"
15957             "// line comment\n"
15958             "int longbitfield : 6;",
15959             format("int a : 5;\n"
15960                    "int one : 1;\n"
15961                    "// line comment\n"
15962                    "int longbitfield : 6;",
15963                    Alignment));
15964 
15965   /* Test across comments and newlines */
15966   EXPECT_EQ("int a            : 5;\n"
15967             "/* block comment */\n"
15968             "\n"
15969             "int longbitfield : 6;",
15970             format("int a : 5;\n"
15971                    "/* block comment */\n"
15972                    "\n"
15973                    "int longbitfield : 6;",
15974                    Alignment));
15975   EXPECT_EQ("int a            : 5;\n"
15976             "int one          : 1;\n"
15977             "\n"
15978             "// line comment\n"
15979             "\n"
15980             "int longbitfield : 6;",
15981             format("int a : 5;\n"
15982                    "int one : 1;\n"
15983                    "\n"
15984                    "// line comment \n"
15985                    "\n"
15986                    "int longbitfield : 6;",
15987                    Alignment));
15988 }
15989 
15990 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15991   FormatStyle Alignment = getLLVMStyle();
15992   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15993   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15994 
15995   Alignment.MaxEmptyLinesToKeep = 10;
15996   /* Test alignment across empty lines */
15997   EXPECT_EQ("int a = 5;\n"
15998             "\n"
15999             "int oneTwoThree = 123;",
16000             format("int a       = 5;\n"
16001                    "\n"
16002                    "int oneTwoThree= 123;",
16003                    Alignment));
16004   EXPECT_EQ("int a   = 5;\n"
16005             "int one = 1;\n"
16006             "\n"
16007             "int oneTwoThree = 123;",
16008             format("int a = 5;\n"
16009                    "int one = 1;\n"
16010                    "\n"
16011                    "int oneTwoThree = 123;",
16012                    Alignment));
16013 
16014   /* Test across comments */
16015   EXPECT_EQ("int a           = 5;\n"
16016             "/* block comment */\n"
16017             "int oneTwoThree = 123;",
16018             format("int a = 5;\n"
16019                    "/* block comment */\n"
16020                    "int oneTwoThree=123;",
16021                    Alignment));
16022 
16023   EXPECT_EQ("int a           = 5;\n"
16024             "// line comment\n"
16025             "int oneTwoThree = 123;",
16026             format("int a = 5;\n"
16027                    "// line comment\n"
16028                    "int oneTwoThree=123;",
16029                    Alignment));
16030 
16031   EXPECT_EQ("int a           = 5;\n"
16032             "/*\n"
16033             " * multi-line block comment\n"
16034             " */\n"
16035             "int oneTwoThree = 123;",
16036             format("int a = 5;\n"
16037                    "/*\n"
16038                    " * multi-line block comment\n"
16039                    " */\n"
16040                    "int oneTwoThree=123;",
16041                    Alignment));
16042 
16043   EXPECT_EQ("int a           = 5;\n"
16044             "//\n"
16045             "// multi-line line comment\n"
16046             "//\n"
16047             "int oneTwoThree = 123;",
16048             format("int a = 5;\n"
16049                    "//\n"
16050                    "// multi-line line comment\n"
16051                    "//\n"
16052                    "int oneTwoThree=123;",
16053                    Alignment));
16054 
16055   /* Test across comments and newlines */
16056   EXPECT_EQ("int a = 5;\n"
16057             "\n"
16058             "/* block comment */\n"
16059             "int oneTwoThree = 123;",
16060             format("int a = 5;\n"
16061                    "\n"
16062                    "/* block comment */\n"
16063                    "int oneTwoThree=123;",
16064                    Alignment));
16065 
16066   EXPECT_EQ("int a = 5;\n"
16067             "\n"
16068             "// line comment\n"
16069             "int oneTwoThree = 123;",
16070             format("int a = 5;\n"
16071                    "\n"
16072                    "// line comment\n"
16073                    "int oneTwoThree=123;",
16074                    Alignment));
16075 }
16076 
16077 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
16078   FormatStyle Alignment = getLLVMStyle();
16079   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16080   Alignment.AlignConsecutiveAssignments =
16081       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16082   verifyFormat("int a           = 5;\n"
16083                "int oneTwoThree = 123;",
16084                Alignment);
16085   verifyFormat("int a           = method();\n"
16086                "int oneTwoThree = 133;",
16087                Alignment);
16088   verifyFormat("a &= 5;\n"
16089                "bcd *= 5;\n"
16090                "ghtyf += 5;\n"
16091                "dvfvdb -= 5;\n"
16092                "a /= 5;\n"
16093                "vdsvsv %= 5;\n"
16094                "sfdbddfbdfbb ^= 5;\n"
16095                "dvsdsv |= 5;\n"
16096                "int dsvvdvsdvvv = 123;",
16097                Alignment);
16098   verifyFormat("int i = 1, j = 10;\n"
16099                "something = 2000;",
16100                Alignment);
16101   verifyFormat("something = 2000;\n"
16102                "int i = 1, j = 10;\n",
16103                Alignment);
16104   verifyFormat("something = 2000;\n"
16105                "another   = 911;\n"
16106                "int i = 1, j = 10;\n"
16107                "oneMore = 1;\n"
16108                "i       = 2;",
16109                Alignment);
16110   verifyFormat("int a   = 5;\n"
16111                "int one = 1;\n"
16112                "method();\n"
16113                "int oneTwoThree = 123;\n"
16114                "int oneTwo      = 12;",
16115                Alignment);
16116   verifyFormat("int oneTwoThree = 123;\n"
16117                "int oneTwo      = 12;\n"
16118                "method();\n",
16119                Alignment);
16120   verifyFormat("int oneTwoThree = 123; // comment\n"
16121                "int oneTwo      = 12;  // comment",
16122                Alignment);
16123 
16124   // Bug 25167
16125   /* Uncomment when fixed
16126     verifyFormat("#if A\n"
16127                  "#else\n"
16128                  "int aaaaaaaa = 12;\n"
16129                  "#endif\n"
16130                  "#if B\n"
16131                  "#else\n"
16132                  "int a = 12;\n"
16133                  "#endif\n",
16134                  Alignment);
16135     verifyFormat("enum foo {\n"
16136                  "#if A\n"
16137                  "#else\n"
16138                  "  aaaaaaaa = 12;\n"
16139                  "#endif\n"
16140                  "#if B\n"
16141                  "#else\n"
16142                  "  a = 12;\n"
16143                  "#endif\n"
16144                  "};\n",
16145                  Alignment);
16146   */
16147 
16148   Alignment.MaxEmptyLinesToKeep = 10;
16149   /* Test alignment across empty lines */
16150   EXPECT_EQ("int a           = 5;\n"
16151             "\n"
16152             "int oneTwoThree = 123;",
16153             format("int a       = 5;\n"
16154                    "\n"
16155                    "int oneTwoThree= 123;",
16156                    Alignment));
16157   EXPECT_EQ("int a           = 5;\n"
16158             "int one         = 1;\n"
16159             "\n"
16160             "int oneTwoThree = 123;",
16161             format("int a = 5;\n"
16162                    "int one = 1;\n"
16163                    "\n"
16164                    "int oneTwoThree = 123;",
16165                    Alignment));
16166   EXPECT_EQ("int a           = 5;\n"
16167             "int one         = 1;\n"
16168             "\n"
16169             "int oneTwoThree = 123;\n"
16170             "int oneTwo      = 12;",
16171             format("int a = 5;\n"
16172                    "int one = 1;\n"
16173                    "\n"
16174                    "int oneTwoThree = 123;\n"
16175                    "int oneTwo = 12;",
16176                    Alignment));
16177 
16178   /* Test across comments */
16179   EXPECT_EQ("int a           = 5;\n"
16180             "/* block comment */\n"
16181             "int oneTwoThree = 123;",
16182             format("int a = 5;\n"
16183                    "/* block comment */\n"
16184                    "int oneTwoThree=123;",
16185                    Alignment));
16186 
16187   EXPECT_EQ("int a           = 5;\n"
16188             "// line comment\n"
16189             "int oneTwoThree = 123;",
16190             format("int a = 5;\n"
16191                    "// line comment\n"
16192                    "int oneTwoThree=123;",
16193                    Alignment));
16194 
16195   /* Test across comments and newlines */
16196   EXPECT_EQ("int a           = 5;\n"
16197             "\n"
16198             "/* block comment */\n"
16199             "int oneTwoThree = 123;",
16200             format("int a = 5;\n"
16201                    "\n"
16202                    "/* block comment */\n"
16203                    "int oneTwoThree=123;",
16204                    Alignment));
16205 
16206   EXPECT_EQ("int a           = 5;\n"
16207             "\n"
16208             "// line comment\n"
16209             "int oneTwoThree = 123;",
16210             format("int a = 5;\n"
16211                    "\n"
16212                    "// line comment\n"
16213                    "int oneTwoThree=123;",
16214                    Alignment));
16215 
16216   EXPECT_EQ("int a           = 5;\n"
16217             "//\n"
16218             "// multi-line line comment\n"
16219             "//\n"
16220             "int oneTwoThree = 123;",
16221             format("int a = 5;\n"
16222                    "//\n"
16223                    "// multi-line line comment\n"
16224                    "//\n"
16225                    "int oneTwoThree=123;",
16226                    Alignment));
16227 
16228   EXPECT_EQ("int a           = 5;\n"
16229             "/*\n"
16230             " *  multi-line block comment\n"
16231             " */\n"
16232             "int oneTwoThree = 123;",
16233             format("int a = 5;\n"
16234                    "/*\n"
16235                    " *  multi-line block comment\n"
16236                    " */\n"
16237                    "int oneTwoThree=123;",
16238                    Alignment));
16239 
16240   EXPECT_EQ("int a           = 5;\n"
16241             "\n"
16242             "/* block comment */\n"
16243             "\n"
16244             "\n"
16245             "\n"
16246             "int oneTwoThree = 123;",
16247             format("int a = 5;\n"
16248                    "\n"
16249                    "/* block comment */\n"
16250                    "\n"
16251                    "\n"
16252                    "\n"
16253                    "int oneTwoThree=123;",
16254                    Alignment));
16255 
16256   EXPECT_EQ("int a           = 5;\n"
16257             "\n"
16258             "// line comment\n"
16259             "\n"
16260             "\n"
16261             "\n"
16262             "int oneTwoThree = 123;",
16263             format("int a = 5;\n"
16264                    "\n"
16265                    "// line comment\n"
16266                    "\n"
16267                    "\n"
16268                    "\n"
16269                    "int oneTwoThree=123;",
16270                    Alignment));
16271 
16272   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16273   verifyFormat("#define A \\\n"
16274                "  int aaaa       = 12; \\\n"
16275                "  int b          = 23; \\\n"
16276                "  int ccc        = 234; \\\n"
16277                "  int dddddddddd = 2345;",
16278                Alignment);
16279   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16280   verifyFormat("#define A               \\\n"
16281                "  int aaaa       = 12;  \\\n"
16282                "  int b          = 23;  \\\n"
16283                "  int ccc        = 234; \\\n"
16284                "  int dddddddddd = 2345;",
16285                Alignment);
16286   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16287   verifyFormat("#define A                                                      "
16288                "                \\\n"
16289                "  int aaaa       = 12;                                         "
16290                "                \\\n"
16291                "  int b          = 23;                                         "
16292                "                \\\n"
16293                "  int ccc        = 234;                                        "
16294                "                \\\n"
16295                "  int dddddddddd = 2345;",
16296                Alignment);
16297   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16298                "k = 4, int l = 5,\n"
16299                "                  int m = 6) {\n"
16300                "  int j      = 10;\n"
16301                "  otherThing = 1;\n"
16302                "}",
16303                Alignment);
16304   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16305                "  int i   = 1;\n"
16306                "  int j   = 2;\n"
16307                "  int big = 10000;\n"
16308                "}",
16309                Alignment);
16310   verifyFormat("class C {\n"
16311                "public:\n"
16312                "  int i            = 1;\n"
16313                "  virtual void f() = 0;\n"
16314                "};",
16315                Alignment);
16316   verifyFormat("int i = 1;\n"
16317                "if (SomeType t = getSomething()) {\n"
16318                "}\n"
16319                "int j   = 2;\n"
16320                "int big = 10000;",
16321                Alignment);
16322   verifyFormat("int j = 7;\n"
16323                "for (int k = 0; k < N; ++k) {\n"
16324                "}\n"
16325                "int j   = 2;\n"
16326                "int big = 10000;\n"
16327                "}",
16328                Alignment);
16329   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16330   verifyFormat("int i = 1;\n"
16331                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16332                "    = someLooooooooooooooooongFunction();\n"
16333                "int j = 2;",
16334                Alignment);
16335   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16336   verifyFormat("int i = 1;\n"
16337                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16338                "    someLooooooooooooooooongFunction();\n"
16339                "int j = 2;",
16340                Alignment);
16341 
16342   verifyFormat("auto lambda = []() {\n"
16343                "  auto i = 0;\n"
16344                "  return 0;\n"
16345                "};\n"
16346                "int i  = 0;\n"
16347                "auto v = type{\n"
16348                "    i = 1,   //\n"
16349                "    (i = 2), //\n"
16350                "    i = 3    //\n"
16351                "};",
16352                Alignment);
16353 
16354   verifyFormat(
16355       "int i      = 1;\n"
16356       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16357       "                          loooooooooooooooooooooongParameterB);\n"
16358       "int j      = 2;",
16359       Alignment);
16360 
16361   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16362                "          typename B   = very_long_type_name_1,\n"
16363                "          typename T_2 = very_long_type_name_2>\n"
16364                "auto foo() {}\n",
16365                Alignment);
16366   verifyFormat("int a, b = 1;\n"
16367                "int c  = 2;\n"
16368                "int dd = 3;\n",
16369                Alignment);
16370   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16371                "float b[1][] = {{3.f}};\n",
16372                Alignment);
16373   verifyFormat("for (int i = 0; i < 1; i++)\n"
16374                "  int x = 1;\n",
16375                Alignment);
16376   verifyFormat("for (i = 0; i < 1; i++)\n"
16377                "  x = 1;\n"
16378                "y = 1;\n",
16379                Alignment);
16380 
16381   Alignment.ReflowComments = true;
16382   Alignment.ColumnLimit = 50;
16383   EXPECT_EQ("int x   = 0;\n"
16384             "int yy  = 1; /// specificlennospace\n"
16385             "int zzz = 2;\n",
16386             format("int x   = 0;\n"
16387                    "int yy  = 1; ///specificlennospace\n"
16388                    "int zzz = 2;\n",
16389                    Alignment));
16390 }
16391 
16392 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16393   FormatStyle Alignment = getLLVMStyle();
16394   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16395   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16396   verifyFormat("int a = 5;\n"
16397                "int oneTwoThree = 123;",
16398                Alignment);
16399   verifyFormat("int a = 5;\n"
16400                "int oneTwoThree = 123;",
16401                Alignment);
16402 
16403   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16404   verifyFormat("int a           = 5;\n"
16405                "int oneTwoThree = 123;",
16406                Alignment);
16407   verifyFormat("int a           = method();\n"
16408                "int oneTwoThree = 133;",
16409                Alignment);
16410   verifyFormat("a &= 5;\n"
16411                "bcd *= 5;\n"
16412                "ghtyf += 5;\n"
16413                "dvfvdb -= 5;\n"
16414                "a /= 5;\n"
16415                "vdsvsv %= 5;\n"
16416                "sfdbddfbdfbb ^= 5;\n"
16417                "dvsdsv |= 5;\n"
16418                "int dsvvdvsdvvv = 123;",
16419                Alignment);
16420   verifyFormat("int i = 1, j = 10;\n"
16421                "something = 2000;",
16422                Alignment);
16423   verifyFormat("something = 2000;\n"
16424                "int i = 1, j = 10;\n",
16425                Alignment);
16426   verifyFormat("something = 2000;\n"
16427                "another   = 911;\n"
16428                "int i = 1, j = 10;\n"
16429                "oneMore = 1;\n"
16430                "i       = 2;",
16431                Alignment);
16432   verifyFormat("int a   = 5;\n"
16433                "int one = 1;\n"
16434                "method();\n"
16435                "int oneTwoThree = 123;\n"
16436                "int oneTwo      = 12;",
16437                Alignment);
16438   verifyFormat("int oneTwoThree = 123;\n"
16439                "int oneTwo      = 12;\n"
16440                "method();\n",
16441                Alignment);
16442   verifyFormat("int oneTwoThree = 123; // comment\n"
16443                "int oneTwo      = 12;  // comment",
16444                Alignment);
16445   verifyFormat("int f()         = default;\n"
16446                "int &operator() = default;\n"
16447                "int &operator=() {",
16448                Alignment);
16449   verifyFormat("int f()         = delete;\n"
16450                "int &operator() = delete;\n"
16451                "int &operator=() {",
16452                Alignment);
16453   verifyFormat("int f()         = default; // comment\n"
16454                "int &operator() = default; // comment\n"
16455                "int &operator=() {",
16456                Alignment);
16457   verifyFormat("int f()         = default;\n"
16458                "int &operator() = default;\n"
16459                "int &operator==() {",
16460                Alignment);
16461   verifyFormat("int f()         = default;\n"
16462                "int &operator() = default;\n"
16463                "int &operator<=() {",
16464                Alignment);
16465   verifyFormat("int f()         = default;\n"
16466                "int &operator() = default;\n"
16467                "int &operator!=() {",
16468                Alignment);
16469   verifyFormat("int f()         = default;\n"
16470                "int &operator() = default;\n"
16471                "int &operator=();",
16472                Alignment);
16473   verifyFormat("int f()         = delete;\n"
16474                "int &operator() = delete;\n"
16475                "int &operator=();",
16476                Alignment);
16477   verifyFormat("/* long long padding */ int f() = default;\n"
16478                "int &operator()                 = default;\n"
16479                "int &operator/**/ =();",
16480                Alignment);
16481   // https://llvm.org/PR33697
16482   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16483   AlignmentWithPenalty.AlignConsecutiveAssignments =
16484       FormatStyle::ACS_Consecutive;
16485   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16486   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16487                "  void f() = delete;\n"
16488                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16489                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16490                "};\n",
16491                AlignmentWithPenalty);
16492 
16493   // Bug 25167
16494   /* Uncomment when fixed
16495     verifyFormat("#if A\n"
16496                  "#else\n"
16497                  "int aaaaaaaa = 12;\n"
16498                  "#endif\n"
16499                  "#if B\n"
16500                  "#else\n"
16501                  "int a = 12;\n"
16502                  "#endif\n",
16503                  Alignment);
16504     verifyFormat("enum foo {\n"
16505                  "#if A\n"
16506                  "#else\n"
16507                  "  aaaaaaaa = 12;\n"
16508                  "#endif\n"
16509                  "#if B\n"
16510                  "#else\n"
16511                  "  a = 12;\n"
16512                  "#endif\n"
16513                  "};\n",
16514                  Alignment);
16515   */
16516 
16517   EXPECT_EQ("int a = 5;\n"
16518             "\n"
16519             "int oneTwoThree = 123;",
16520             format("int a       = 5;\n"
16521                    "\n"
16522                    "int oneTwoThree= 123;",
16523                    Alignment));
16524   EXPECT_EQ("int a   = 5;\n"
16525             "int one = 1;\n"
16526             "\n"
16527             "int oneTwoThree = 123;",
16528             format("int a = 5;\n"
16529                    "int one = 1;\n"
16530                    "\n"
16531                    "int oneTwoThree = 123;",
16532                    Alignment));
16533   EXPECT_EQ("int a   = 5;\n"
16534             "int one = 1;\n"
16535             "\n"
16536             "int oneTwoThree = 123;\n"
16537             "int oneTwo      = 12;",
16538             format("int a = 5;\n"
16539                    "int one = 1;\n"
16540                    "\n"
16541                    "int oneTwoThree = 123;\n"
16542                    "int oneTwo = 12;",
16543                    Alignment));
16544   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16545   verifyFormat("#define A \\\n"
16546                "  int aaaa       = 12; \\\n"
16547                "  int b          = 23; \\\n"
16548                "  int ccc        = 234; \\\n"
16549                "  int dddddddddd = 2345;",
16550                Alignment);
16551   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16552   verifyFormat("#define A               \\\n"
16553                "  int aaaa       = 12;  \\\n"
16554                "  int b          = 23;  \\\n"
16555                "  int ccc        = 234; \\\n"
16556                "  int dddddddddd = 2345;",
16557                Alignment);
16558   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16559   verifyFormat("#define A                                                      "
16560                "                \\\n"
16561                "  int aaaa       = 12;                                         "
16562                "                \\\n"
16563                "  int b          = 23;                                         "
16564                "                \\\n"
16565                "  int ccc        = 234;                                        "
16566                "                \\\n"
16567                "  int dddddddddd = 2345;",
16568                Alignment);
16569   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16570                "k = 4, int l = 5,\n"
16571                "                  int m = 6) {\n"
16572                "  int j      = 10;\n"
16573                "  otherThing = 1;\n"
16574                "}",
16575                Alignment);
16576   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16577                "  int i   = 1;\n"
16578                "  int j   = 2;\n"
16579                "  int big = 10000;\n"
16580                "}",
16581                Alignment);
16582   verifyFormat("class C {\n"
16583                "public:\n"
16584                "  int i            = 1;\n"
16585                "  virtual void f() = 0;\n"
16586                "};",
16587                Alignment);
16588   verifyFormat("int i = 1;\n"
16589                "if (SomeType t = getSomething()) {\n"
16590                "}\n"
16591                "int j   = 2;\n"
16592                "int big = 10000;",
16593                Alignment);
16594   verifyFormat("int j = 7;\n"
16595                "for (int k = 0; k < N; ++k) {\n"
16596                "}\n"
16597                "int j   = 2;\n"
16598                "int big = 10000;\n"
16599                "}",
16600                Alignment);
16601   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16602   verifyFormat("int i = 1;\n"
16603                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16604                "    = someLooooooooooooooooongFunction();\n"
16605                "int j = 2;",
16606                Alignment);
16607   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16608   verifyFormat("int i = 1;\n"
16609                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16610                "    someLooooooooooooooooongFunction();\n"
16611                "int j = 2;",
16612                Alignment);
16613 
16614   verifyFormat("auto lambda = []() {\n"
16615                "  auto i = 0;\n"
16616                "  return 0;\n"
16617                "};\n"
16618                "int i  = 0;\n"
16619                "auto v = type{\n"
16620                "    i = 1,   //\n"
16621                "    (i = 2), //\n"
16622                "    i = 3    //\n"
16623                "};",
16624                Alignment);
16625 
16626   verifyFormat(
16627       "int i      = 1;\n"
16628       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16629       "                          loooooooooooooooooooooongParameterB);\n"
16630       "int j      = 2;",
16631       Alignment);
16632 
16633   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16634                "          typename B   = very_long_type_name_1,\n"
16635                "          typename T_2 = very_long_type_name_2>\n"
16636                "auto foo() {}\n",
16637                Alignment);
16638   verifyFormat("int a, b = 1;\n"
16639                "int c  = 2;\n"
16640                "int dd = 3;\n",
16641                Alignment);
16642   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16643                "float b[1][] = {{3.f}};\n",
16644                Alignment);
16645   verifyFormat("for (int i = 0; i < 1; i++)\n"
16646                "  int x = 1;\n",
16647                Alignment);
16648   verifyFormat("for (i = 0; i < 1; i++)\n"
16649                "  x = 1;\n"
16650                "y = 1;\n",
16651                Alignment);
16652 
16653   EXPECT_EQ(Alignment.ReflowComments, true);
16654   Alignment.ColumnLimit = 50;
16655   EXPECT_EQ("int x   = 0;\n"
16656             "int yy  = 1; /// specificlennospace\n"
16657             "int zzz = 2;\n",
16658             format("int x   = 0;\n"
16659                    "int yy  = 1; ///specificlennospace\n"
16660                    "int zzz = 2;\n",
16661                    Alignment));
16662 
16663   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16664                "auto b                     = [] {\n"
16665                "  f();\n"
16666                "  return;\n"
16667                "};",
16668                Alignment);
16669   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16670                "auto b                     = g([] {\n"
16671                "  f();\n"
16672                "  return;\n"
16673                "});",
16674                Alignment);
16675   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16676                "auto b                     = g(param, [] {\n"
16677                "  f();\n"
16678                "  return;\n"
16679                "});",
16680                Alignment);
16681   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16682                "auto b                     = [] {\n"
16683                "  if (condition) {\n"
16684                "    return;\n"
16685                "  }\n"
16686                "};",
16687                Alignment);
16688 
16689   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16690                "           ccc ? aaaaa : bbbbb,\n"
16691                "           dddddddddddddddddddddddddd);",
16692                Alignment);
16693   // FIXME: https://llvm.org/PR53497
16694   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
16695   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16696   //              "    ccc ? aaaaa : bbbbb,\n"
16697   //              "    dddddddddddddddddddddddddd);",
16698   //              Alignment);
16699 }
16700 
16701 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16702   FormatStyle Alignment = getLLVMStyle();
16703   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16704   verifyFormat("int const a     : 5;\n"
16705                "int oneTwoThree : 23;",
16706                Alignment);
16707 
16708   // Initializers are allowed starting with c++2a
16709   verifyFormat("int const a     : 5 = 1;\n"
16710                "int oneTwoThree : 23 = 0;",
16711                Alignment);
16712 
16713   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16714   verifyFormat("int const a           : 5;\n"
16715                "int       oneTwoThree : 23;",
16716                Alignment);
16717 
16718   verifyFormat("int const a           : 5;  // comment\n"
16719                "int       oneTwoThree : 23; // comment",
16720                Alignment);
16721 
16722   verifyFormat("int const a           : 5 = 1;\n"
16723                "int       oneTwoThree : 23 = 0;",
16724                Alignment);
16725 
16726   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16727   verifyFormat("int const a           : 5  = 1;\n"
16728                "int       oneTwoThree : 23 = 0;",
16729                Alignment);
16730   verifyFormat("int const a           : 5  = {1};\n"
16731                "int       oneTwoThree : 23 = 0;",
16732                Alignment);
16733 
16734   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16735   verifyFormat("int const a          :5;\n"
16736                "int       oneTwoThree:23;",
16737                Alignment);
16738 
16739   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16740   verifyFormat("int const a           :5;\n"
16741                "int       oneTwoThree :23;",
16742                Alignment);
16743 
16744   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16745   verifyFormat("int const a          : 5;\n"
16746                "int       oneTwoThree: 23;",
16747                Alignment);
16748 
16749   // Known limitations: ':' is only recognized as a bitfield colon when
16750   // followed by a number.
16751   /*
16752   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16753                "int a           : 5;",
16754                Alignment);
16755   */
16756 }
16757 
16758 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16759   FormatStyle Alignment = getLLVMStyle();
16760   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16761   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16762   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16763   verifyFormat("float const a = 5;\n"
16764                "int oneTwoThree = 123;",
16765                Alignment);
16766   verifyFormat("int a = 5;\n"
16767                "float const oneTwoThree = 123;",
16768                Alignment);
16769 
16770   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16771   verifyFormat("float const a = 5;\n"
16772                "int         oneTwoThree = 123;",
16773                Alignment);
16774   verifyFormat("int         a = method();\n"
16775                "float const oneTwoThree = 133;",
16776                Alignment);
16777   verifyFormat("int i = 1, j = 10;\n"
16778                "something = 2000;",
16779                Alignment);
16780   verifyFormat("something = 2000;\n"
16781                "int i = 1, j = 10;\n",
16782                Alignment);
16783   verifyFormat("float      something = 2000;\n"
16784                "double     another = 911;\n"
16785                "int        i = 1, j = 10;\n"
16786                "const int *oneMore = 1;\n"
16787                "unsigned   i = 2;",
16788                Alignment);
16789   verifyFormat("float a = 5;\n"
16790                "int   one = 1;\n"
16791                "method();\n"
16792                "const double       oneTwoThree = 123;\n"
16793                "const unsigned int oneTwo = 12;",
16794                Alignment);
16795   verifyFormat("int      oneTwoThree{0}; // comment\n"
16796                "unsigned oneTwo;         // comment",
16797                Alignment);
16798   verifyFormat("unsigned int       *a;\n"
16799                "int                *b;\n"
16800                "unsigned int Const *c;\n"
16801                "unsigned int const *d;\n"
16802                "unsigned int Const &e;\n"
16803                "unsigned int const &f;",
16804                Alignment);
16805   verifyFormat("Const unsigned int *c;\n"
16806                "const unsigned int *d;\n"
16807                "Const unsigned int &e;\n"
16808                "const unsigned int &f;\n"
16809                "const unsigned      g;\n"
16810                "Const unsigned      h;",
16811                Alignment);
16812   EXPECT_EQ("float const a = 5;\n"
16813             "\n"
16814             "int oneTwoThree = 123;",
16815             format("float const   a = 5;\n"
16816                    "\n"
16817                    "int           oneTwoThree= 123;",
16818                    Alignment));
16819   EXPECT_EQ("float a = 5;\n"
16820             "int   one = 1;\n"
16821             "\n"
16822             "unsigned oneTwoThree = 123;",
16823             format("float    a = 5;\n"
16824                    "int      one = 1;\n"
16825                    "\n"
16826                    "unsigned oneTwoThree = 123;",
16827                    Alignment));
16828   EXPECT_EQ("float a = 5;\n"
16829             "int   one = 1;\n"
16830             "\n"
16831             "unsigned oneTwoThree = 123;\n"
16832             "int      oneTwo = 12;",
16833             format("float    a = 5;\n"
16834                    "int one = 1;\n"
16835                    "\n"
16836                    "unsigned oneTwoThree = 123;\n"
16837                    "int oneTwo = 12;",
16838                    Alignment));
16839   // Function prototype alignment
16840   verifyFormat("int    a();\n"
16841                "double b();",
16842                Alignment);
16843   verifyFormat("int    a(int x);\n"
16844                "double b();",
16845                Alignment);
16846   unsigned OldColumnLimit = Alignment.ColumnLimit;
16847   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16848   // otherwise the function parameters will be re-flowed onto a single line.
16849   Alignment.ColumnLimit = 0;
16850   EXPECT_EQ("int    a(int   x,\n"
16851             "         float y);\n"
16852             "double b(int    x,\n"
16853             "         double y);",
16854             format("int a(int x,\n"
16855                    " float y);\n"
16856                    "double b(int x,\n"
16857                    " double y);",
16858                    Alignment));
16859   // This ensures that function parameters of function declarations are
16860   // correctly indented when their owning functions are indented.
16861   // The failure case here is for 'double y' to not be indented enough.
16862   EXPECT_EQ("double a(int x);\n"
16863             "int    b(int    y,\n"
16864             "         double z);",
16865             format("double a(int x);\n"
16866                    "int b(int y,\n"
16867                    " double z);",
16868                    Alignment));
16869   // Set ColumnLimit low so that we induce wrapping immediately after
16870   // the function name and opening paren.
16871   Alignment.ColumnLimit = 13;
16872   verifyFormat("int function(\n"
16873                "    int  x,\n"
16874                "    bool y);",
16875                Alignment);
16876   Alignment.ColumnLimit = OldColumnLimit;
16877   // Ensure function pointers don't screw up recursive alignment
16878   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16879                "double b();",
16880                Alignment);
16881   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16882   // Ensure recursive alignment is broken by function braces, so that the
16883   // "a = 1" does not align with subsequent assignments inside the function
16884   // body.
16885   verifyFormat("int func(int a = 1) {\n"
16886                "  int b  = 2;\n"
16887                "  int cc = 3;\n"
16888                "}",
16889                Alignment);
16890   verifyFormat("float      something = 2000;\n"
16891                "double     another   = 911;\n"
16892                "int        i = 1, j = 10;\n"
16893                "const int *oneMore = 1;\n"
16894                "unsigned   i       = 2;",
16895                Alignment);
16896   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16897                "unsigned oneTwo      = 0;   // comment",
16898                Alignment);
16899   // Make sure that scope is correctly tracked, in the absence of braces
16900   verifyFormat("for (int i = 0; i < n; i++)\n"
16901                "  j = i;\n"
16902                "double x = 1;\n",
16903                Alignment);
16904   verifyFormat("if (int i = 0)\n"
16905                "  j = i;\n"
16906                "double x = 1;\n",
16907                Alignment);
16908   // Ensure operator[] and operator() are comprehended
16909   verifyFormat("struct test {\n"
16910                "  long long int foo();\n"
16911                "  int           operator[](int a);\n"
16912                "  double        bar();\n"
16913                "};\n",
16914                Alignment);
16915   verifyFormat("struct test {\n"
16916                "  long long int foo();\n"
16917                "  int           operator()(int a);\n"
16918                "  double        bar();\n"
16919                "};\n",
16920                Alignment);
16921   // http://llvm.org/PR52914
16922   verifyFormat("char *a[]     = {\"a\", // comment\n"
16923                "                 \"bb\"};\n"
16924                "int   bbbbbbb = 0;",
16925                Alignment);
16926 
16927   // PAS_Right
16928   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16929             "  int const i   = 1;\n"
16930             "  int      *j   = 2;\n"
16931             "  int       big = 10000;\n"
16932             "\n"
16933             "  unsigned oneTwoThree = 123;\n"
16934             "  int      oneTwo      = 12;\n"
16935             "  method();\n"
16936             "  float k  = 2;\n"
16937             "  int   ll = 10000;\n"
16938             "}",
16939             format("void SomeFunction(int parameter= 0) {\n"
16940                    " int const  i= 1;\n"
16941                    "  int *j=2;\n"
16942                    " int big  =  10000;\n"
16943                    "\n"
16944                    "unsigned oneTwoThree  =123;\n"
16945                    "int oneTwo = 12;\n"
16946                    "  method();\n"
16947                    "float k= 2;\n"
16948                    "int ll=10000;\n"
16949                    "}",
16950                    Alignment));
16951   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16952             "  int const i   = 1;\n"
16953             "  int     **j   = 2, ***k;\n"
16954             "  int      &k   = i;\n"
16955             "  int     &&l   = i + j;\n"
16956             "  int       big = 10000;\n"
16957             "\n"
16958             "  unsigned oneTwoThree = 123;\n"
16959             "  int      oneTwo      = 12;\n"
16960             "  method();\n"
16961             "  float k  = 2;\n"
16962             "  int   ll = 10000;\n"
16963             "}",
16964             format("void SomeFunction(int parameter= 0) {\n"
16965                    " int const  i= 1;\n"
16966                    "  int **j=2,***k;\n"
16967                    "int &k=i;\n"
16968                    "int &&l=i+j;\n"
16969                    " int big  =  10000;\n"
16970                    "\n"
16971                    "unsigned oneTwoThree  =123;\n"
16972                    "int oneTwo = 12;\n"
16973                    "  method();\n"
16974                    "float k= 2;\n"
16975                    "int ll=10000;\n"
16976                    "}",
16977                    Alignment));
16978   // variables are aligned at their name, pointers are at the right most
16979   // position
16980   verifyFormat("int   *a;\n"
16981                "int  **b;\n"
16982                "int ***c;\n"
16983                "int    foobar;\n",
16984                Alignment);
16985 
16986   // PAS_Left
16987   FormatStyle AlignmentLeft = Alignment;
16988   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16989   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16990             "  int const i   = 1;\n"
16991             "  int*      j   = 2;\n"
16992             "  int       big = 10000;\n"
16993             "\n"
16994             "  unsigned oneTwoThree = 123;\n"
16995             "  int      oneTwo      = 12;\n"
16996             "  method();\n"
16997             "  float k  = 2;\n"
16998             "  int   ll = 10000;\n"
16999             "}",
17000             format("void SomeFunction(int parameter= 0) {\n"
17001                    " int const  i= 1;\n"
17002                    "  int *j=2;\n"
17003                    " int big  =  10000;\n"
17004                    "\n"
17005                    "unsigned oneTwoThree  =123;\n"
17006                    "int oneTwo = 12;\n"
17007                    "  method();\n"
17008                    "float k= 2;\n"
17009                    "int ll=10000;\n"
17010                    "}",
17011                    AlignmentLeft));
17012   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17013             "  int const i   = 1;\n"
17014             "  int**     j   = 2;\n"
17015             "  int&      k   = i;\n"
17016             "  int&&     l   = i + j;\n"
17017             "  int       big = 10000;\n"
17018             "\n"
17019             "  unsigned oneTwoThree = 123;\n"
17020             "  int      oneTwo      = 12;\n"
17021             "  method();\n"
17022             "  float k  = 2;\n"
17023             "  int   ll = 10000;\n"
17024             "}",
17025             format("void SomeFunction(int parameter= 0) {\n"
17026                    " int const  i= 1;\n"
17027                    "  int **j=2;\n"
17028                    "int &k=i;\n"
17029                    "int &&l=i+j;\n"
17030                    " int big  =  10000;\n"
17031                    "\n"
17032                    "unsigned oneTwoThree  =123;\n"
17033                    "int oneTwo = 12;\n"
17034                    "  method();\n"
17035                    "float k= 2;\n"
17036                    "int ll=10000;\n"
17037                    "}",
17038                    AlignmentLeft));
17039   // variables are aligned at their name, pointers are at the left most position
17040   verifyFormat("int*   a;\n"
17041                "int**  b;\n"
17042                "int*** c;\n"
17043                "int    foobar;\n",
17044                AlignmentLeft);
17045 
17046   // PAS_Middle
17047   FormatStyle AlignmentMiddle = Alignment;
17048   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
17049   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17050             "  int const i   = 1;\n"
17051             "  int *     j   = 2;\n"
17052             "  int       big = 10000;\n"
17053             "\n"
17054             "  unsigned oneTwoThree = 123;\n"
17055             "  int      oneTwo      = 12;\n"
17056             "  method();\n"
17057             "  float k  = 2;\n"
17058             "  int   ll = 10000;\n"
17059             "}",
17060             format("void SomeFunction(int parameter= 0) {\n"
17061                    " int const  i= 1;\n"
17062                    "  int *j=2;\n"
17063                    " int big  =  10000;\n"
17064                    "\n"
17065                    "unsigned oneTwoThree  =123;\n"
17066                    "int oneTwo = 12;\n"
17067                    "  method();\n"
17068                    "float k= 2;\n"
17069                    "int ll=10000;\n"
17070                    "}",
17071                    AlignmentMiddle));
17072   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17073             "  int const i   = 1;\n"
17074             "  int **    j   = 2, ***k;\n"
17075             "  int &     k   = i;\n"
17076             "  int &&    l   = i + j;\n"
17077             "  int       big = 10000;\n"
17078             "\n"
17079             "  unsigned oneTwoThree = 123;\n"
17080             "  int      oneTwo      = 12;\n"
17081             "  method();\n"
17082             "  float k  = 2;\n"
17083             "  int   ll = 10000;\n"
17084             "}",
17085             format("void SomeFunction(int parameter= 0) {\n"
17086                    " int const  i= 1;\n"
17087                    "  int **j=2,***k;\n"
17088                    "int &k=i;\n"
17089                    "int &&l=i+j;\n"
17090                    " int big  =  10000;\n"
17091                    "\n"
17092                    "unsigned oneTwoThree  =123;\n"
17093                    "int oneTwo = 12;\n"
17094                    "  method();\n"
17095                    "float k= 2;\n"
17096                    "int ll=10000;\n"
17097                    "}",
17098                    AlignmentMiddle));
17099   // variables are aligned at their name, pointers are in the middle
17100   verifyFormat("int *   a;\n"
17101                "int *   b;\n"
17102                "int *** c;\n"
17103                "int     foobar;\n",
17104                AlignmentMiddle);
17105 
17106   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17107   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17108   verifyFormat("#define A \\\n"
17109                "  int       aaaa = 12; \\\n"
17110                "  float     b = 23; \\\n"
17111                "  const int ccc = 234; \\\n"
17112                "  unsigned  dddddddddd = 2345;",
17113                Alignment);
17114   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17115   verifyFormat("#define A              \\\n"
17116                "  int       aaaa = 12; \\\n"
17117                "  float     b = 23;    \\\n"
17118                "  const int ccc = 234; \\\n"
17119                "  unsigned  dddddddddd = 2345;",
17120                Alignment);
17121   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17122   Alignment.ColumnLimit = 30;
17123   verifyFormat("#define A                    \\\n"
17124                "  int       aaaa = 12;       \\\n"
17125                "  float     b = 23;          \\\n"
17126                "  const int ccc = 234;       \\\n"
17127                "  int       dddddddddd = 2345;",
17128                Alignment);
17129   Alignment.ColumnLimit = 80;
17130   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17131                "k = 4, int l = 5,\n"
17132                "                  int m = 6) {\n"
17133                "  const int j = 10;\n"
17134                "  otherThing = 1;\n"
17135                "}",
17136                Alignment);
17137   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17138                "  int const i = 1;\n"
17139                "  int      *j = 2;\n"
17140                "  int       big = 10000;\n"
17141                "}",
17142                Alignment);
17143   verifyFormat("class C {\n"
17144                "public:\n"
17145                "  int          i = 1;\n"
17146                "  virtual void f() = 0;\n"
17147                "};",
17148                Alignment);
17149   verifyFormat("float i = 1;\n"
17150                "if (SomeType t = getSomething()) {\n"
17151                "}\n"
17152                "const unsigned j = 2;\n"
17153                "int            big = 10000;",
17154                Alignment);
17155   verifyFormat("float j = 7;\n"
17156                "for (int k = 0; k < N; ++k) {\n"
17157                "}\n"
17158                "unsigned j = 2;\n"
17159                "int      big = 10000;\n"
17160                "}",
17161                Alignment);
17162   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17163   verifyFormat("float              i = 1;\n"
17164                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17165                "    = someLooooooooooooooooongFunction();\n"
17166                "int j = 2;",
17167                Alignment);
17168   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17169   verifyFormat("int                i = 1;\n"
17170                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17171                "    someLooooooooooooooooongFunction();\n"
17172                "int j = 2;",
17173                Alignment);
17174 
17175   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17176   verifyFormat("auto lambda = []() {\n"
17177                "  auto  ii = 0;\n"
17178                "  float j  = 0;\n"
17179                "  return 0;\n"
17180                "};\n"
17181                "int   i  = 0;\n"
17182                "float i2 = 0;\n"
17183                "auto  v  = type{\n"
17184                "    i = 1,   //\n"
17185                "    (i = 2), //\n"
17186                "    i = 3    //\n"
17187                "};",
17188                Alignment);
17189   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17190 
17191   verifyFormat(
17192       "int      i = 1;\n"
17193       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17194       "                          loooooooooooooooooooooongParameterB);\n"
17195       "int      j = 2;",
17196       Alignment);
17197 
17198   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17199   // We expect declarations and assignments to align, as long as it doesn't
17200   // exceed the column limit, starting a new alignment sequence whenever it
17201   // happens.
17202   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17203   Alignment.ColumnLimit = 30;
17204   verifyFormat("float    ii              = 1;\n"
17205                "unsigned j               = 2;\n"
17206                "int someVerylongVariable = 1;\n"
17207                "AnotherLongType  ll = 123456;\n"
17208                "VeryVeryLongType k  = 2;\n"
17209                "int              myvar = 1;",
17210                Alignment);
17211   Alignment.ColumnLimit = 80;
17212   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17213 
17214   verifyFormat(
17215       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17216       "          typename LongType, typename B>\n"
17217       "auto foo() {}\n",
17218       Alignment);
17219   verifyFormat("float a, b = 1;\n"
17220                "int   c = 2;\n"
17221                "int   dd = 3;\n",
17222                Alignment);
17223   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17224                "float b[1][] = {{3.f}};\n",
17225                Alignment);
17226   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17227   verifyFormat("float a, b = 1;\n"
17228                "int   c  = 2;\n"
17229                "int   dd = 3;\n",
17230                Alignment);
17231   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17232                "float b[1][] = {{3.f}};\n",
17233                Alignment);
17234   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17235 
17236   Alignment.ColumnLimit = 30;
17237   Alignment.BinPackParameters = false;
17238   verifyFormat("void foo(float     a,\n"
17239                "         float     b,\n"
17240                "         int       c,\n"
17241                "         uint32_t *d) {\n"
17242                "  int   *e = 0;\n"
17243                "  float  f = 0;\n"
17244                "  double g = 0;\n"
17245                "}\n"
17246                "void bar(ino_t     a,\n"
17247                "         int       b,\n"
17248                "         uint32_t *c,\n"
17249                "         bool      d) {}\n",
17250                Alignment);
17251   Alignment.BinPackParameters = true;
17252   Alignment.ColumnLimit = 80;
17253 
17254   // Bug 33507
17255   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17256   verifyFormat(
17257       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17258       "  static const Version verVs2017;\n"
17259       "  return true;\n"
17260       "});\n",
17261       Alignment);
17262   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17263 
17264   // See llvm.org/PR35641
17265   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17266   verifyFormat("int func() { //\n"
17267                "  int      b;\n"
17268                "  unsigned c;\n"
17269                "}",
17270                Alignment);
17271 
17272   // See PR37175
17273   FormatStyle Style = getMozillaStyle();
17274   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17275   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17276             "foo(int a);",
17277             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17278 
17279   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17280   verifyFormat("unsigned int*       a;\n"
17281                "int*                b;\n"
17282                "unsigned int Const* c;\n"
17283                "unsigned int const* d;\n"
17284                "unsigned int Const& e;\n"
17285                "unsigned int const& f;",
17286                Alignment);
17287   verifyFormat("Const unsigned int* c;\n"
17288                "const unsigned int* d;\n"
17289                "Const unsigned int& e;\n"
17290                "const unsigned int& f;\n"
17291                "const unsigned      g;\n"
17292                "Const unsigned      h;",
17293                Alignment);
17294 
17295   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17296   verifyFormat("unsigned int *       a;\n"
17297                "int *                b;\n"
17298                "unsigned int Const * c;\n"
17299                "unsigned int const * d;\n"
17300                "unsigned int Const & e;\n"
17301                "unsigned int const & f;",
17302                Alignment);
17303   verifyFormat("Const unsigned int * c;\n"
17304                "const unsigned int * d;\n"
17305                "Const unsigned int & e;\n"
17306                "const unsigned int & f;\n"
17307                "const unsigned       g;\n"
17308                "Const unsigned       h;",
17309                Alignment);
17310 
17311   // See PR46529
17312   FormatStyle BracedAlign = getLLVMStyle();
17313   BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17314   verifyFormat("const auto result{[]() {\n"
17315                "  const auto something = 1;\n"
17316                "  return 2;\n"
17317                "}};",
17318                BracedAlign);
17319   verifyFormat("int foo{[]() {\n"
17320                "  int bar{0};\n"
17321                "  return 0;\n"
17322                "}()};",
17323                BracedAlign);
17324   BracedAlign.Cpp11BracedListStyle = false;
17325   verifyFormat("const auto result{ []() {\n"
17326                "  const auto something = 1;\n"
17327                "  return 2;\n"
17328                "} };",
17329                BracedAlign);
17330   verifyFormat("int foo{ []() {\n"
17331                "  int bar{ 0 };\n"
17332                "  return 0;\n"
17333                "}() };",
17334                BracedAlign);
17335 }
17336 
17337 TEST_F(FormatTest, AlignWithLineBreaks) {
17338   auto Style = getLLVMStyleWithColumns(120);
17339 
17340   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17341   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17342   verifyFormat("void foo() {\n"
17343                "  int myVar = 5;\n"
17344                "  double x = 3.14;\n"
17345                "  auto str = \"Hello \"\n"
17346                "             \"World\";\n"
17347                "  auto s = \"Hello \"\n"
17348                "           \"Again\";\n"
17349                "}",
17350                Style);
17351 
17352   // clang-format off
17353   verifyFormat("void foo() {\n"
17354                "  const int capacityBefore = Entries.capacity();\n"
17355                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17356                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17357                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17358                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17359                "}",
17360                Style);
17361   // clang-format on
17362 
17363   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17364   verifyFormat("void foo() {\n"
17365                "  int myVar = 5;\n"
17366                "  double x  = 3.14;\n"
17367                "  auto str  = \"Hello \"\n"
17368                "              \"World\";\n"
17369                "  auto s    = \"Hello \"\n"
17370                "              \"Again\";\n"
17371                "}",
17372                Style);
17373 
17374   // clang-format off
17375   verifyFormat("void foo() {\n"
17376                "  const int capacityBefore = Entries.capacity();\n"
17377                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17378                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17379                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17380                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17381                "}",
17382                Style);
17383   // clang-format on
17384 
17385   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17386   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17387   verifyFormat("void foo() {\n"
17388                "  int    myVar = 5;\n"
17389                "  double x = 3.14;\n"
17390                "  auto   str = \"Hello \"\n"
17391                "               \"World\";\n"
17392                "  auto   s = \"Hello \"\n"
17393                "             \"Again\";\n"
17394                "}",
17395                Style);
17396 
17397   // clang-format off
17398   verifyFormat("void foo() {\n"
17399                "  const int  capacityBefore = Entries.capacity();\n"
17400                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17401                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17402                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17403                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17404                "}",
17405                Style);
17406   // clang-format on
17407 
17408   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17409   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17410 
17411   verifyFormat("void foo() {\n"
17412                "  int    myVar = 5;\n"
17413                "  double x     = 3.14;\n"
17414                "  auto   str   = \"Hello \"\n"
17415                "                 \"World\";\n"
17416                "  auto   s     = \"Hello \"\n"
17417                "                 \"Again\";\n"
17418                "}",
17419                Style);
17420 
17421   // clang-format off
17422   verifyFormat("void foo() {\n"
17423                "  const int  capacityBefore = Entries.capacity();\n"
17424                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17425                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17426                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17427                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17428                "}",
17429                Style);
17430   // clang-format on
17431 
17432   Style = getLLVMStyleWithColumns(120);
17433   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17434   Style.ContinuationIndentWidth = 4;
17435   Style.IndentWidth = 4;
17436 
17437   // clang-format off
17438   verifyFormat("void SomeFunc() {\n"
17439                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17440                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17441                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17442                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17443                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17444                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17445                "}",
17446                Style);
17447   // clang-format on
17448 
17449   Style.BinPackArguments = false;
17450 
17451   // clang-format off
17452   verifyFormat("void SomeFunc() {\n"
17453                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17454                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17455                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17456                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17457                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17458                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17459                "}",
17460                Style);
17461   // clang-format on
17462 }
17463 
17464 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17465   auto Style = getLLVMStyleWithColumns(60);
17466 
17467   verifyFormat("void foo1(void) {\n"
17468                "  BYTE p[1] = 1;\n"
17469                "  A B = {.one_foooooooooooooooo = 2,\n"
17470                "         .two_fooooooooooooo = 3,\n"
17471                "         .three_fooooooooooooo = 4};\n"
17472                "  BYTE payload = 2;\n"
17473                "}",
17474                Style);
17475 
17476   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17477   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17478   verifyFormat("void foo2(void) {\n"
17479                "  BYTE p[1]    = 1;\n"
17480                "  A B          = {.one_foooooooooooooooo = 2,\n"
17481                "                  .two_fooooooooooooo    = 3,\n"
17482                "                  .three_fooooooooooooo  = 4};\n"
17483                "  BYTE payload = 2;\n"
17484                "}",
17485                Style);
17486 
17487   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17488   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17489   verifyFormat("void foo3(void) {\n"
17490                "  BYTE p[1] = 1;\n"
17491                "  A    B = {.one_foooooooooooooooo = 2,\n"
17492                "            .two_fooooooooooooo = 3,\n"
17493                "            .three_fooooooooooooo = 4};\n"
17494                "  BYTE payload = 2;\n"
17495                "}",
17496                Style);
17497 
17498   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17499   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17500   verifyFormat("void foo4(void) {\n"
17501                "  BYTE p[1]    = 1;\n"
17502                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17503                "                  .two_fooooooooooooo    = 3,\n"
17504                "                  .three_fooooooooooooo  = 4};\n"
17505                "  BYTE payload = 2;\n"
17506                "}",
17507                Style);
17508 }
17509 
17510 TEST_F(FormatTest, LinuxBraceBreaking) {
17511   FormatStyle LinuxBraceStyle = getLLVMStyle();
17512   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17513   verifyFormat("namespace a\n"
17514                "{\n"
17515                "class A\n"
17516                "{\n"
17517                "  void f()\n"
17518                "  {\n"
17519                "    if (true) {\n"
17520                "      a();\n"
17521                "      b();\n"
17522                "    } else {\n"
17523                "      a();\n"
17524                "    }\n"
17525                "  }\n"
17526                "  void g() { return; }\n"
17527                "};\n"
17528                "struct B {\n"
17529                "  int x;\n"
17530                "};\n"
17531                "} // namespace a\n",
17532                LinuxBraceStyle);
17533   verifyFormat("enum X {\n"
17534                "  Y = 0,\n"
17535                "}\n",
17536                LinuxBraceStyle);
17537   verifyFormat("struct S {\n"
17538                "  int Type;\n"
17539                "  union {\n"
17540                "    int x;\n"
17541                "    double y;\n"
17542                "  } Value;\n"
17543                "  class C\n"
17544                "  {\n"
17545                "    MyFavoriteType Value;\n"
17546                "  } Class;\n"
17547                "}\n",
17548                LinuxBraceStyle);
17549 }
17550 
17551 TEST_F(FormatTest, MozillaBraceBreaking) {
17552   FormatStyle MozillaBraceStyle = getLLVMStyle();
17553   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17554   MozillaBraceStyle.FixNamespaceComments = false;
17555   verifyFormat("namespace a {\n"
17556                "class A\n"
17557                "{\n"
17558                "  void f()\n"
17559                "  {\n"
17560                "    if (true) {\n"
17561                "      a();\n"
17562                "      b();\n"
17563                "    }\n"
17564                "  }\n"
17565                "  void g() { return; }\n"
17566                "};\n"
17567                "enum E\n"
17568                "{\n"
17569                "  A,\n"
17570                "  // foo\n"
17571                "  B,\n"
17572                "  C\n"
17573                "};\n"
17574                "struct B\n"
17575                "{\n"
17576                "  int x;\n"
17577                "};\n"
17578                "}\n",
17579                MozillaBraceStyle);
17580   verifyFormat("struct S\n"
17581                "{\n"
17582                "  int Type;\n"
17583                "  union\n"
17584                "  {\n"
17585                "    int x;\n"
17586                "    double y;\n"
17587                "  } Value;\n"
17588                "  class C\n"
17589                "  {\n"
17590                "    MyFavoriteType Value;\n"
17591                "  } Class;\n"
17592                "}\n",
17593                MozillaBraceStyle);
17594 }
17595 
17596 TEST_F(FormatTest, StroustrupBraceBreaking) {
17597   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17598   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17599   verifyFormat("namespace a {\n"
17600                "class A {\n"
17601                "  void f()\n"
17602                "  {\n"
17603                "    if (true) {\n"
17604                "      a();\n"
17605                "      b();\n"
17606                "    }\n"
17607                "  }\n"
17608                "  void g() { return; }\n"
17609                "};\n"
17610                "struct B {\n"
17611                "  int x;\n"
17612                "};\n"
17613                "} // namespace a\n",
17614                StroustrupBraceStyle);
17615 
17616   verifyFormat("void foo()\n"
17617                "{\n"
17618                "  if (a) {\n"
17619                "    a();\n"
17620                "  }\n"
17621                "  else {\n"
17622                "    b();\n"
17623                "  }\n"
17624                "}\n",
17625                StroustrupBraceStyle);
17626 
17627   verifyFormat("#ifdef _DEBUG\n"
17628                "int foo(int i = 0)\n"
17629                "#else\n"
17630                "int foo(int i = 5)\n"
17631                "#endif\n"
17632                "{\n"
17633                "  return i;\n"
17634                "}",
17635                StroustrupBraceStyle);
17636 
17637   verifyFormat("void foo() {}\n"
17638                "void bar()\n"
17639                "#ifdef _DEBUG\n"
17640                "{\n"
17641                "  foo();\n"
17642                "}\n"
17643                "#else\n"
17644                "{\n"
17645                "}\n"
17646                "#endif",
17647                StroustrupBraceStyle);
17648 
17649   verifyFormat("void foobar() { int i = 5; }\n"
17650                "#ifdef _DEBUG\n"
17651                "void bar() {}\n"
17652                "#else\n"
17653                "void bar() { foobar(); }\n"
17654                "#endif",
17655                StroustrupBraceStyle);
17656 }
17657 
17658 TEST_F(FormatTest, AllmanBraceBreaking) {
17659   FormatStyle AllmanBraceStyle = getLLVMStyle();
17660   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17661 
17662   EXPECT_EQ("namespace a\n"
17663             "{\n"
17664             "void f();\n"
17665             "void g();\n"
17666             "} // namespace a\n",
17667             format("namespace a\n"
17668                    "{\n"
17669                    "void f();\n"
17670                    "void g();\n"
17671                    "}\n",
17672                    AllmanBraceStyle));
17673 
17674   verifyFormat("namespace a\n"
17675                "{\n"
17676                "class A\n"
17677                "{\n"
17678                "  void f()\n"
17679                "  {\n"
17680                "    if (true)\n"
17681                "    {\n"
17682                "      a();\n"
17683                "      b();\n"
17684                "    }\n"
17685                "  }\n"
17686                "  void g() { return; }\n"
17687                "};\n"
17688                "struct B\n"
17689                "{\n"
17690                "  int x;\n"
17691                "};\n"
17692                "union C\n"
17693                "{\n"
17694                "};\n"
17695                "} // namespace a",
17696                AllmanBraceStyle);
17697 
17698   verifyFormat("void f()\n"
17699                "{\n"
17700                "  if (true)\n"
17701                "  {\n"
17702                "    a();\n"
17703                "  }\n"
17704                "  else if (false)\n"
17705                "  {\n"
17706                "    b();\n"
17707                "  }\n"
17708                "  else\n"
17709                "  {\n"
17710                "    c();\n"
17711                "  }\n"
17712                "}\n",
17713                AllmanBraceStyle);
17714 
17715   verifyFormat("void f()\n"
17716                "{\n"
17717                "  for (int i = 0; i < 10; ++i)\n"
17718                "  {\n"
17719                "    a();\n"
17720                "  }\n"
17721                "  while (false)\n"
17722                "  {\n"
17723                "    b();\n"
17724                "  }\n"
17725                "  do\n"
17726                "  {\n"
17727                "    c();\n"
17728                "  } while (false)\n"
17729                "}\n",
17730                AllmanBraceStyle);
17731 
17732   verifyFormat("void f(int a)\n"
17733                "{\n"
17734                "  switch (a)\n"
17735                "  {\n"
17736                "  case 0:\n"
17737                "    break;\n"
17738                "  case 1:\n"
17739                "  {\n"
17740                "    break;\n"
17741                "  }\n"
17742                "  case 2:\n"
17743                "  {\n"
17744                "  }\n"
17745                "  break;\n"
17746                "  default:\n"
17747                "    break;\n"
17748                "  }\n"
17749                "}\n",
17750                AllmanBraceStyle);
17751 
17752   verifyFormat("enum X\n"
17753                "{\n"
17754                "  Y = 0,\n"
17755                "}\n",
17756                AllmanBraceStyle);
17757   verifyFormat("enum X\n"
17758                "{\n"
17759                "  Y = 0\n"
17760                "}\n",
17761                AllmanBraceStyle);
17762 
17763   verifyFormat("@interface BSApplicationController ()\n"
17764                "{\n"
17765                "@private\n"
17766                "  id _extraIvar;\n"
17767                "}\n"
17768                "@end\n",
17769                AllmanBraceStyle);
17770 
17771   verifyFormat("#ifdef _DEBUG\n"
17772                "int foo(int i = 0)\n"
17773                "#else\n"
17774                "int foo(int i = 5)\n"
17775                "#endif\n"
17776                "{\n"
17777                "  return i;\n"
17778                "}",
17779                AllmanBraceStyle);
17780 
17781   verifyFormat("void foo() {}\n"
17782                "void bar()\n"
17783                "#ifdef _DEBUG\n"
17784                "{\n"
17785                "  foo();\n"
17786                "}\n"
17787                "#else\n"
17788                "{\n"
17789                "}\n"
17790                "#endif",
17791                AllmanBraceStyle);
17792 
17793   verifyFormat("void foobar() { int i = 5; }\n"
17794                "#ifdef _DEBUG\n"
17795                "void bar() {}\n"
17796                "#else\n"
17797                "void bar() { foobar(); }\n"
17798                "#endif",
17799                AllmanBraceStyle);
17800 
17801   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17802             FormatStyle::SLS_All);
17803 
17804   verifyFormat("[](int i) { return i + 2; };\n"
17805                "[](int i, int j)\n"
17806                "{\n"
17807                "  auto x = i + j;\n"
17808                "  auto y = i * j;\n"
17809                "  return x ^ y;\n"
17810                "};\n"
17811                "void foo()\n"
17812                "{\n"
17813                "  auto shortLambda = [](int i) { return i + 2; };\n"
17814                "  auto longLambda = [](int i, int j)\n"
17815                "  {\n"
17816                "    auto x = i + j;\n"
17817                "    auto y = i * j;\n"
17818                "    return x ^ y;\n"
17819                "  };\n"
17820                "}",
17821                AllmanBraceStyle);
17822 
17823   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17824 
17825   verifyFormat("[](int i)\n"
17826                "{\n"
17827                "  return i + 2;\n"
17828                "};\n"
17829                "[](int i, int j)\n"
17830                "{\n"
17831                "  auto x = i + j;\n"
17832                "  auto y = i * j;\n"
17833                "  return x ^ y;\n"
17834                "};\n"
17835                "void foo()\n"
17836                "{\n"
17837                "  auto shortLambda = [](int i)\n"
17838                "  {\n"
17839                "    return i + 2;\n"
17840                "  };\n"
17841                "  auto longLambda = [](int i, int j)\n"
17842                "  {\n"
17843                "    auto x = i + j;\n"
17844                "    auto y = i * j;\n"
17845                "    return x ^ y;\n"
17846                "  };\n"
17847                "}",
17848                AllmanBraceStyle);
17849 
17850   // Reset
17851   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17852 
17853   // This shouldn't affect ObjC blocks..
17854   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17855                "  // ...\n"
17856                "  int i;\n"
17857                "}];",
17858                AllmanBraceStyle);
17859   verifyFormat("void (^block)(void) = ^{\n"
17860                "  // ...\n"
17861                "  int i;\n"
17862                "};",
17863                AllmanBraceStyle);
17864   // .. or dict literals.
17865   verifyFormat("void f()\n"
17866                "{\n"
17867                "  // ...\n"
17868                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17869                "}",
17870                AllmanBraceStyle);
17871   verifyFormat("void f()\n"
17872                "{\n"
17873                "  // ...\n"
17874                "  [object someMethod:@{a : @\"b\"}];\n"
17875                "}",
17876                AllmanBraceStyle);
17877   verifyFormat("int f()\n"
17878                "{ // comment\n"
17879                "  return 42;\n"
17880                "}",
17881                AllmanBraceStyle);
17882 
17883   AllmanBraceStyle.ColumnLimit = 19;
17884   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17885   AllmanBraceStyle.ColumnLimit = 18;
17886   verifyFormat("void f()\n"
17887                "{\n"
17888                "  int i;\n"
17889                "}",
17890                AllmanBraceStyle);
17891   AllmanBraceStyle.ColumnLimit = 80;
17892 
17893   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17894   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17895       FormatStyle::SIS_WithoutElse;
17896   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17897   verifyFormat("void f(bool b)\n"
17898                "{\n"
17899                "  if (b)\n"
17900                "  {\n"
17901                "    return;\n"
17902                "  }\n"
17903                "}\n",
17904                BreakBeforeBraceShortIfs);
17905   verifyFormat("void f(bool b)\n"
17906                "{\n"
17907                "  if constexpr (b)\n"
17908                "  {\n"
17909                "    return;\n"
17910                "  }\n"
17911                "}\n",
17912                BreakBeforeBraceShortIfs);
17913   verifyFormat("void f(bool b)\n"
17914                "{\n"
17915                "  if CONSTEXPR (b)\n"
17916                "  {\n"
17917                "    return;\n"
17918                "  }\n"
17919                "}\n",
17920                BreakBeforeBraceShortIfs);
17921   verifyFormat("void f(bool b)\n"
17922                "{\n"
17923                "  if (b) return;\n"
17924                "}\n",
17925                BreakBeforeBraceShortIfs);
17926   verifyFormat("void f(bool b)\n"
17927                "{\n"
17928                "  if constexpr (b) return;\n"
17929                "}\n",
17930                BreakBeforeBraceShortIfs);
17931   verifyFormat("void f(bool b)\n"
17932                "{\n"
17933                "  if CONSTEXPR (b) return;\n"
17934                "}\n",
17935                BreakBeforeBraceShortIfs);
17936   verifyFormat("void f(bool b)\n"
17937                "{\n"
17938                "  while (b)\n"
17939                "  {\n"
17940                "    return;\n"
17941                "  }\n"
17942                "}\n",
17943                BreakBeforeBraceShortIfs);
17944 }
17945 
17946 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17947   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
17948   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17949 
17950   // Make a few changes to the style for testing purposes
17951   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17952       FormatStyle::SFS_Empty;
17953   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17954 
17955   // FIXME: this test case can't decide whether there should be a blank line
17956   // after the ~D() line or not. It adds one if one doesn't exist in the test
17957   // and it removes the line if one exists.
17958   /*
17959   verifyFormat("class A;\n"
17960                "namespace B\n"
17961                "  {\n"
17962                "class C;\n"
17963                "// Comment\n"
17964                "class D\n"
17965                "  {\n"
17966                "public:\n"
17967                "  D();\n"
17968                "  ~D() {}\n"
17969                "private:\n"
17970                "  enum E\n"
17971                "    {\n"
17972                "    F\n"
17973                "    }\n"
17974                "  };\n"
17975                "  } // namespace B\n",
17976                WhitesmithsBraceStyle);
17977   */
17978 
17979   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17980   verifyFormat("namespace a\n"
17981                "  {\n"
17982                "class A\n"
17983                "  {\n"
17984                "  void f()\n"
17985                "    {\n"
17986                "    if (true)\n"
17987                "      {\n"
17988                "      a();\n"
17989                "      b();\n"
17990                "      }\n"
17991                "    }\n"
17992                "  void g()\n"
17993                "    {\n"
17994                "    return;\n"
17995                "    }\n"
17996                "  };\n"
17997                "struct B\n"
17998                "  {\n"
17999                "  int x;\n"
18000                "  };\n"
18001                "  } // namespace a",
18002                WhitesmithsBraceStyle);
18003 
18004   verifyFormat("namespace a\n"
18005                "  {\n"
18006                "namespace b\n"
18007                "  {\n"
18008                "class A\n"
18009                "  {\n"
18010                "  void f()\n"
18011                "    {\n"
18012                "    if (true)\n"
18013                "      {\n"
18014                "      a();\n"
18015                "      b();\n"
18016                "      }\n"
18017                "    }\n"
18018                "  void g()\n"
18019                "    {\n"
18020                "    return;\n"
18021                "    }\n"
18022                "  };\n"
18023                "struct B\n"
18024                "  {\n"
18025                "  int x;\n"
18026                "  };\n"
18027                "  } // namespace b\n"
18028                "  } // namespace a",
18029                WhitesmithsBraceStyle);
18030 
18031   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
18032   verifyFormat("namespace a\n"
18033                "  {\n"
18034                "namespace b\n"
18035                "  {\n"
18036                "  class A\n"
18037                "    {\n"
18038                "    void f()\n"
18039                "      {\n"
18040                "      if (true)\n"
18041                "        {\n"
18042                "        a();\n"
18043                "        b();\n"
18044                "        }\n"
18045                "      }\n"
18046                "    void g()\n"
18047                "      {\n"
18048                "      return;\n"
18049                "      }\n"
18050                "    };\n"
18051                "  struct B\n"
18052                "    {\n"
18053                "    int x;\n"
18054                "    };\n"
18055                "  } // namespace b\n"
18056                "  } // namespace a",
18057                WhitesmithsBraceStyle);
18058 
18059   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
18060   verifyFormat("namespace a\n"
18061                "  {\n"
18062                "  namespace b\n"
18063                "    {\n"
18064                "    class A\n"
18065                "      {\n"
18066                "      void f()\n"
18067                "        {\n"
18068                "        if (true)\n"
18069                "          {\n"
18070                "          a();\n"
18071                "          b();\n"
18072                "          }\n"
18073                "        }\n"
18074                "      void g()\n"
18075                "        {\n"
18076                "        return;\n"
18077                "        }\n"
18078                "      };\n"
18079                "    struct B\n"
18080                "      {\n"
18081                "      int x;\n"
18082                "      };\n"
18083                "    } // namespace b\n"
18084                "  }   // namespace a",
18085                WhitesmithsBraceStyle);
18086 
18087   verifyFormat("void f()\n"
18088                "  {\n"
18089                "  if (true)\n"
18090                "    {\n"
18091                "    a();\n"
18092                "    }\n"
18093                "  else if (false)\n"
18094                "    {\n"
18095                "    b();\n"
18096                "    }\n"
18097                "  else\n"
18098                "    {\n"
18099                "    c();\n"
18100                "    }\n"
18101                "  }\n",
18102                WhitesmithsBraceStyle);
18103 
18104   verifyFormat("void f()\n"
18105                "  {\n"
18106                "  for (int i = 0; i < 10; ++i)\n"
18107                "    {\n"
18108                "    a();\n"
18109                "    }\n"
18110                "  while (false)\n"
18111                "    {\n"
18112                "    b();\n"
18113                "    }\n"
18114                "  do\n"
18115                "    {\n"
18116                "    c();\n"
18117                "    } while (false)\n"
18118                "  }\n",
18119                WhitesmithsBraceStyle);
18120 
18121   WhitesmithsBraceStyle.IndentCaseLabels = true;
18122   verifyFormat("void switchTest1(int a)\n"
18123                "  {\n"
18124                "  switch (a)\n"
18125                "    {\n"
18126                "    case 2:\n"
18127                "      {\n"
18128                "      }\n"
18129                "      break;\n"
18130                "    }\n"
18131                "  }\n",
18132                WhitesmithsBraceStyle);
18133 
18134   verifyFormat("void switchTest2(int a)\n"
18135                "  {\n"
18136                "  switch (a)\n"
18137                "    {\n"
18138                "    case 0:\n"
18139                "      break;\n"
18140                "    case 1:\n"
18141                "      {\n"
18142                "      break;\n"
18143                "      }\n"
18144                "    case 2:\n"
18145                "      {\n"
18146                "      }\n"
18147                "      break;\n"
18148                "    default:\n"
18149                "      break;\n"
18150                "    }\n"
18151                "  }\n",
18152                WhitesmithsBraceStyle);
18153 
18154   verifyFormat("void switchTest3(int a)\n"
18155                "  {\n"
18156                "  switch (a)\n"
18157                "    {\n"
18158                "    case 0:\n"
18159                "      {\n"
18160                "      foo(x);\n"
18161                "      }\n"
18162                "      break;\n"
18163                "    default:\n"
18164                "      {\n"
18165                "      foo(1);\n"
18166                "      }\n"
18167                "      break;\n"
18168                "    }\n"
18169                "  }\n",
18170                WhitesmithsBraceStyle);
18171 
18172   WhitesmithsBraceStyle.IndentCaseLabels = false;
18173 
18174   verifyFormat("void switchTest4(int a)\n"
18175                "  {\n"
18176                "  switch (a)\n"
18177                "    {\n"
18178                "  case 2:\n"
18179                "    {\n"
18180                "    }\n"
18181                "    break;\n"
18182                "    }\n"
18183                "  }\n",
18184                WhitesmithsBraceStyle);
18185 
18186   verifyFormat("void switchTest5(int a)\n"
18187                "  {\n"
18188                "  switch (a)\n"
18189                "    {\n"
18190                "  case 0:\n"
18191                "    break;\n"
18192                "  case 1:\n"
18193                "    {\n"
18194                "    foo();\n"
18195                "    break;\n"
18196                "    }\n"
18197                "  case 2:\n"
18198                "    {\n"
18199                "    }\n"
18200                "    break;\n"
18201                "  default:\n"
18202                "    break;\n"
18203                "    }\n"
18204                "  }\n",
18205                WhitesmithsBraceStyle);
18206 
18207   verifyFormat("void switchTest6(int a)\n"
18208                "  {\n"
18209                "  switch (a)\n"
18210                "    {\n"
18211                "  case 0:\n"
18212                "    {\n"
18213                "    foo(x);\n"
18214                "    }\n"
18215                "    break;\n"
18216                "  default:\n"
18217                "    {\n"
18218                "    foo(1);\n"
18219                "    }\n"
18220                "    break;\n"
18221                "    }\n"
18222                "  }\n",
18223                WhitesmithsBraceStyle);
18224 
18225   verifyFormat("enum X\n"
18226                "  {\n"
18227                "  Y = 0, // testing\n"
18228                "  }\n",
18229                WhitesmithsBraceStyle);
18230 
18231   verifyFormat("enum X\n"
18232                "  {\n"
18233                "  Y = 0\n"
18234                "  }\n",
18235                WhitesmithsBraceStyle);
18236   verifyFormat("enum X\n"
18237                "  {\n"
18238                "  Y = 0,\n"
18239                "  Z = 1\n"
18240                "  };\n",
18241                WhitesmithsBraceStyle);
18242 
18243   verifyFormat("@interface BSApplicationController ()\n"
18244                "  {\n"
18245                "@private\n"
18246                "  id _extraIvar;\n"
18247                "  }\n"
18248                "@end\n",
18249                WhitesmithsBraceStyle);
18250 
18251   verifyFormat("#ifdef _DEBUG\n"
18252                "int foo(int i = 0)\n"
18253                "#else\n"
18254                "int foo(int i = 5)\n"
18255                "#endif\n"
18256                "  {\n"
18257                "  return i;\n"
18258                "  }",
18259                WhitesmithsBraceStyle);
18260 
18261   verifyFormat("void foo() {}\n"
18262                "void bar()\n"
18263                "#ifdef _DEBUG\n"
18264                "  {\n"
18265                "  foo();\n"
18266                "  }\n"
18267                "#else\n"
18268                "  {\n"
18269                "  }\n"
18270                "#endif",
18271                WhitesmithsBraceStyle);
18272 
18273   verifyFormat("void foobar()\n"
18274                "  {\n"
18275                "  int i = 5;\n"
18276                "  }\n"
18277                "#ifdef _DEBUG\n"
18278                "void bar()\n"
18279                "  {\n"
18280                "  }\n"
18281                "#else\n"
18282                "void bar()\n"
18283                "  {\n"
18284                "  foobar();\n"
18285                "  }\n"
18286                "#endif",
18287                WhitesmithsBraceStyle);
18288 
18289   // This shouldn't affect ObjC blocks..
18290   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18291                "  // ...\n"
18292                "  int i;\n"
18293                "}];",
18294                WhitesmithsBraceStyle);
18295   verifyFormat("void (^block)(void) = ^{\n"
18296                "  // ...\n"
18297                "  int i;\n"
18298                "};",
18299                WhitesmithsBraceStyle);
18300   // .. or dict literals.
18301   verifyFormat("void f()\n"
18302                "  {\n"
18303                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18304                "  }",
18305                WhitesmithsBraceStyle);
18306 
18307   verifyFormat("int f()\n"
18308                "  { // comment\n"
18309                "  return 42;\n"
18310                "  }",
18311                WhitesmithsBraceStyle);
18312 
18313   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18314   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18315       FormatStyle::SIS_OnlyFirstIf;
18316   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18317   verifyFormat("void f(bool b)\n"
18318                "  {\n"
18319                "  if (b)\n"
18320                "    {\n"
18321                "    return;\n"
18322                "    }\n"
18323                "  }\n",
18324                BreakBeforeBraceShortIfs);
18325   verifyFormat("void f(bool b)\n"
18326                "  {\n"
18327                "  if (b) return;\n"
18328                "  }\n",
18329                BreakBeforeBraceShortIfs);
18330   verifyFormat("void f(bool b)\n"
18331                "  {\n"
18332                "  while (b)\n"
18333                "    {\n"
18334                "    return;\n"
18335                "    }\n"
18336                "  }\n",
18337                BreakBeforeBraceShortIfs);
18338 }
18339 
18340 TEST_F(FormatTest, GNUBraceBreaking) {
18341   FormatStyle GNUBraceStyle = getLLVMStyle();
18342   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18343   verifyFormat("namespace a\n"
18344                "{\n"
18345                "class A\n"
18346                "{\n"
18347                "  void f()\n"
18348                "  {\n"
18349                "    int a;\n"
18350                "    {\n"
18351                "      int b;\n"
18352                "    }\n"
18353                "    if (true)\n"
18354                "      {\n"
18355                "        a();\n"
18356                "        b();\n"
18357                "      }\n"
18358                "  }\n"
18359                "  void g() { return; }\n"
18360                "}\n"
18361                "} // namespace a",
18362                GNUBraceStyle);
18363 
18364   verifyFormat("void f()\n"
18365                "{\n"
18366                "  if (true)\n"
18367                "    {\n"
18368                "      a();\n"
18369                "    }\n"
18370                "  else if (false)\n"
18371                "    {\n"
18372                "      b();\n"
18373                "    }\n"
18374                "  else\n"
18375                "    {\n"
18376                "      c();\n"
18377                "    }\n"
18378                "}\n",
18379                GNUBraceStyle);
18380 
18381   verifyFormat("void f()\n"
18382                "{\n"
18383                "  for (int i = 0; i < 10; ++i)\n"
18384                "    {\n"
18385                "      a();\n"
18386                "    }\n"
18387                "  while (false)\n"
18388                "    {\n"
18389                "      b();\n"
18390                "    }\n"
18391                "  do\n"
18392                "    {\n"
18393                "      c();\n"
18394                "    }\n"
18395                "  while (false);\n"
18396                "}\n",
18397                GNUBraceStyle);
18398 
18399   verifyFormat("void f(int a)\n"
18400                "{\n"
18401                "  switch (a)\n"
18402                "    {\n"
18403                "    case 0:\n"
18404                "      break;\n"
18405                "    case 1:\n"
18406                "      {\n"
18407                "        break;\n"
18408                "      }\n"
18409                "    case 2:\n"
18410                "      {\n"
18411                "      }\n"
18412                "      break;\n"
18413                "    default:\n"
18414                "      break;\n"
18415                "    }\n"
18416                "}\n",
18417                GNUBraceStyle);
18418 
18419   verifyFormat("enum X\n"
18420                "{\n"
18421                "  Y = 0,\n"
18422                "}\n",
18423                GNUBraceStyle);
18424 
18425   verifyFormat("@interface BSApplicationController ()\n"
18426                "{\n"
18427                "@private\n"
18428                "  id _extraIvar;\n"
18429                "}\n"
18430                "@end\n",
18431                GNUBraceStyle);
18432 
18433   verifyFormat("#ifdef _DEBUG\n"
18434                "int foo(int i = 0)\n"
18435                "#else\n"
18436                "int foo(int i = 5)\n"
18437                "#endif\n"
18438                "{\n"
18439                "  return i;\n"
18440                "}",
18441                GNUBraceStyle);
18442 
18443   verifyFormat("void foo() {}\n"
18444                "void bar()\n"
18445                "#ifdef _DEBUG\n"
18446                "{\n"
18447                "  foo();\n"
18448                "}\n"
18449                "#else\n"
18450                "{\n"
18451                "}\n"
18452                "#endif",
18453                GNUBraceStyle);
18454 
18455   verifyFormat("void foobar() { int i = 5; }\n"
18456                "#ifdef _DEBUG\n"
18457                "void bar() {}\n"
18458                "#else\n"
18459                "void bar() { foobar(); }\n"
18460                "#endif",
18461                GNUBraceStyle);
18462 }
18463 
18464 TEST_F(FormatTest, WebKitBraceBreaking) {
18465   FormatStyle WebKitBraceStyle = getLLVMStyle();
18466   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18467   WebKitBraceStyle.FixNamespaceComments = false;
18468   verifyFormat("namespace a {\n"
18469                "class A {\n"
18470                "  void f()\n"
18471                "  {\n"
18472                "    if (true) {\n"
18473                "      a();\n"
18474                "      b();\n"
18475                "    }\n"
18476                "  }\n"
18477                "  void g() { return; }\n"
18478                "};\n"
18479                "enum E {\n"
18480                "  A,\n"
18481                "  // foo\n"
18482                "  B,\n"
18483                "  C\n"
18484                "};\n"
18485                "struct B {\n"
18486                "  int x;\n"
18487                "};\n"
18488                "}\n",
18489                WebKitBraceStyle);
18490   verifyFormat("struct S {\n"
18491                "  int Type;\n"
18492                "  union {\n"
18493                "    int x;\n"
18494                "    double y;\n"
18495                "  } Value;\n"
18496                "  class C {\n"
18497                "    MyFavoriteType Value;\n"
18498                "  } Class;\n"
18499                "};\n",
18500                WebKitBraceStyle);
18501 }
18502 
18503 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18504   verifyFormat("void f() {\n"
18505                "  try {\n"
18506                "  } catch (const Exception &e) {\n"
18507                "  }\n"
18508                "}\n",
18509                getLLVMStyle());
18510 }
18511 
18512 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18513   auto Style = getLLVMStyle();
18514   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18515   Style.AlignConsecutiveAssignments =
18516       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18517   Style.AlignConsecutiveDeclarations =
18518       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18519   verifyFormat("struct test demo[] = {\n"
18520                "    {56,    23, \"hello\"},\n"
18521                "    {-1, 93463, \"world\"},\n"
18522                "    { 7,     5,    \"!!\"}\n"
18523                "};\n",
18524                Style);
18525 
18526   verifyFormat("struct test demo[] = {\n"
18527                "    {56,    23, \"hello\"}, // first line\n"
18528                "    {-1, 93463, \"world\"}, // second line\n"
18529                "    { 7,     5,    \"!!\"}  // third line\n"
18530                "};\n",
18531                Style);
18532 
18533   verifyFormat("struct test demo[4] = {\n"
18534                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18535                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18536                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18537                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18538                "};\n",
18539                Style);
18540 
18541   verifyFormat("struct test demo[3] = {\n"
18542                "    {56,    23, \"hello\"},\n"
18543                "    {-1, 93463, \"world\"},\n"
18544                "    { 7,     5,    \"!!\"}\n"
18545                "};\n",
18546                Style);
18547 
18548   verifyFormat("struct test demo[3] = {\n"
18549                "    {int{56},    23, \"hello\"},\n"
18550                "    {int{-1}, 93463, \"world\"},\n"
18551                "    { int{7},     5,    \"!!\"}\n"
18552                "};\n",
18553                Style);
18554 
18555   verifyFormat("struct test demo[] = {\n"
18556                "    {56,    23, \"hello\"},\n"
18557                "    {-1, 93463, \"world\"},\n"
18558                "    { 7,     5,    \"!!\"},\n"
18559                "};\n",
18560                Style);
18561 
18562   verifyFormat("test demo[] = {\n"
18563                "    {56,    23, \"hello\"},\n"
18564                "    {-1, 93463, \"world\"},\n"
18565                "    { 7,     5,    \"!!\"},\n"
18566                "};\n",
18567                Style);
18568 
18569   verifyFormat("demo = std::array<struct test, 3>{\n"
18570                "    test{56,    23, \"hello\"},\n"
18571                "    test{-1, 93463, \"world\"},\n"
18572                "    test{ 7,     5,    \"!!\"},\n"
18573                "};\n",
18574                Style);
18575 
18576   verifyFormat("test demo[] = {\n"
18577                "    {56,    23, \"hello\"},\n"
18578                "#if X\n"
18579                "    {-1, 93463, \"world\"},\n"
18580                "#endif\n"
18581                "    { 7,     5,    \"!!\"}\n"
18582                "};\n",
18583                Style);
18584 
18585   verifyFormat(
18586       "test demo[] = {\n"
18587       "    { 7,    23,\n"
18588       "     \"hello world i am a very long line that really, in any\"\n"
18589       "     \"just world, ought to be split over multiple lines\"},\n"
18590       "    {-1, 93463,                                  \"world\"},\n"
18591       "    {56,     5,                                     \"!!\"}\n"
18592       "};\n",
18593       Style);
18594 
18595   verifyFormat("return GradForUnaryCwise(g, {\n"
18596                "                                {{\"sign\"}, \"Sign\",  "
18597                "  {\"x\", \"dy\"}},\n"
18598                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18599                ", \"sign\"}},\n"
18600                "});\n",
18601                Style);
18602 
18603   Style.ColumnLimit = 0;
18604   EXPECT_EQ(
18605       "test demo[] = {\n"
18606       "    {56,    23, \"hello world i am a very long line that really, "
18607       "in any just world, ought to be split over multiple lines\"},\n"
18608       "    {-1, 93463,                                                  "
18609       "                                                 \"world\"},\n"
18610       "    { 7,     5,                                                  "
18611       "                                                    \"!!\"},\n"
18612       "};",
18613       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18614              "that really, in any just world, ought to be split over multiple "
18615              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18616              Style));
18617 
18618   Style.ColumnLimit = 80;
18619   verifyFormat("test demo[] = {\n"
18620                "    {56,    23, /* a comment */ \"hello\"},\n"
18621                "    {-1, 93463,                 \"world\"},\n"
18622                "    { 7,     5,                    \"!!\"}\n"
18623                "};\n",
18624                Style);
18625 
18626   verifyFormat("test demo[] = {\n"
18627                "    {56,    23,                    \"hello\"},\n"
18628                "    {-1, 93463, \"world\" /* comment here */},\n"
18629                "    { 7,     5,                       \"!!\"}\n"
18630                "};\n",
18631                Style);
18632 
18633   verifyFormat("test demo[] = {\n"
18634                "    {56, /* a comment */ 23, \"hello\"},\n"
18635                "    {-1,              93463, \"world\"},\n"
18636                "    { 7,                  5,    \"!!\"}\n"
18637                "};\n",
18638                Style);
18639 
18640   Style.ColumnLimit = 20;
18641   EXPECT_EQ(
18642       "demo = std::array<\n"
18643       "    struct test, 3>{\n"
18644       "    test{\n"
18645       "         56,    23,\n"
18646       "         \"hello \"\n"
18647       "         \"world i \"\n"
18648       "         \"am a very \"\n"
18649       "         \"long line \"\n"
18650       "         \"that \"\n"
18651       "         \"really, \"\n"
18652       "         \"in any \"\n"
18653       "         \"just \"\n"
18654       "         \"world, \"\n"
18655       "         \"ought to \"\n"
18656       "         \"be split \"\n"
18657       "         \"over \"\n"
18658       "         \"multiple \"\n"
18659       "         \"lines\"},\n"
18660       "    test{-1, 93463,\n"
18661       "         \"world\"},\n"
18662       "    test{ 7,     5,\n"
18663       "         \"!!\"   },\n"
18664       "};",
18665       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18666              "i am a very long line that really, in any just world, ought "
18667              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18668              "test{7, 5, \"!!\"},};",
18669              Style));
18670   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18671   Style = getLLVMStyleWithColumns(50);
18672   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18673   verifyFormat("static A x = {\n"
18674                "    {{init1, init2, init3, init4},\n"
18675                "     {init1, init2, init3, init4}}\n"
18676                "};",
18677                Style);
18678   Style.ColumnLimit = 100;
18679   EXPECT_EQ(
18680       "test demo[] = {\n"
18681       "    {56,    23,\n"
18682       "     \"hello world i am a very long line that really, in any just world"
18683       ", ought to be split over \"\n"
18684       "     \"multiple lines\"  },\n"
18685       "    {-1, 93463, \"world\"},\n"
18686       "    { 7,     5,    \"!!\"},\n"
18687       "};",
18688       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18689              "that really, in any just world, ought to be split over multiple "
18690              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18691              Style));
18692 
18693   Style = getLLVMStyleWithColumns(50);
18694   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18695   Style.AlignConsecutiveAssignments =
18696       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18697   Style.AlignConsecutiveDeclarations =
18698       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18699   verifyFormat("struct test demo[] = {\n"
18700                "    {56,    23, \"hello\"},\n"
18701                "    {-1, 93463, \"world\"},\n"
18702                "    { 7,     5,    \"!!\"}\n"
18703                "};\n"
18704                "static A x = {\n"
18705                "    {{init1, init2, init3, init4},\n"
18706                "     {init1, init2, init3, init4}}\n"
18707                "};",
18708                Style);
18709   Style.ColumnLimit = 100;
18710   Style.AlignConsecutiveAssignments =
18711       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18712   Style.AlignConsecutiveDeclarations =
18713       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18714   verifyFormat("struct test demo[] = {\n"
18715                "    {56,    23, \"hello\"},\n"
18716                "    {-1, 93463, \"world\"},\n"
18717                "    { 7,     5,    \"!!\"}\n"
18718                "};\n"
18719                "struct test demo[4] = {\n"
18720                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18721                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18722                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18723                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18724                "};\n",
18725                Style);
18726   EXPECT_EQ(
18727       "test demo[] = {\n"
18728       "    {56,\n"
18729       "     \"hello world i am a very long line that really, in any just world"
18730       ", ought to be split over \"\n"
18731       "     \"multiple lines\",    23},\n"
18732       "    {-1,      \"world\", 93463},\n"
18733       "    { 7,         \"!!\",     5},\n"
18734       "};",
18735       format("test demo[] = {{56, \"hello world i am a very long line "
18736              "that really, in any just world, ought to be split over multiple "
18737              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18738              Style));
18739 }
18740 
18741 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18742   auto Style = getLLVMStyle();
18743   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18744   /* FIXME: This case gets misformatted.
18745   verifyFormat("auto foo = Items{\n"
18746                "    Section{0, bar(), },\n"
18747                "    Section{1, boo()  }\n"
18748                "};\n",
18749                Style);
18750   */
18751   verifyFormat("auto foo = Items{\n"
18752                "    Section{\n"
18753                "            0, bar(),\n"
18754                "            }\n"
18755                "};\n",
18756                Style);
18757   verifyFormat("struct test demo[] = {\n"
18758                "    {56, 23,    \"hello\"},\n"
18759                "    {-1, 93463, \"world\"},\n"
18760                "    {7,  5,     \"!!\"   }\n"
18761                "};\n",
18762                Style);
18763   verifyFormat("struct test demo[] = {\n"
18764                "    {56, 23,    \"hello\"}, // first line\n"
18765                "    {-1, 93463, \"world\"}, // second line\n"
18766                "    {7,  5,     \"!!\"   }  // third line\n"
18767                "};\n",
18768                Style);
18769   verifyFormat("struct test demo[4] = {\n"
18770                "    {56,  23,    21, \"oh\"      }, // first line\n"
18771                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18772                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18773                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18774                "};\n",
18775                Style);
18776   verifyFormat("struct test demo[3] = {\n"
18777                "    {56, 23,    \"hello\"},\n"
18778                "    {-1, 93463, \"world\"},\n"
18779                "    {7,  5,     \"!!\"   }\n"
18780                "};\n",
18781                Style);
18782 
18783   verifyFormat("struct test demo[3] = {\n"
18784                "    {int{56}, 23,    \"hello\"},\n"
18785                "    {int{-1}, 93463, \"world\"},\n"
18786                "    {int{7},  5,     \"!!\"   }\n"
18787                "};\n",
18788                Style);
18789   verifyFormat("struct test demo[] = {\n"
18790                "    {56, 23,    \"hello\"},\n"
18791                "    {-1, 93463, \"world\"},\n"
18792                "    {7,  5,     \"!!\"   },\n"
18793                "};\n",
18794                Style);
18795   verifyFormat("test demo[] = {\n"
18796                "    {56, 23,    \"hello\"},\n"
18797                "    {-1, 93463, \"world\"},\n"
18798                "    {7,  5,     \"!!\"   },\n"
18799                "};\n",
18800                Style);
18801   verifyFormat("demo = std::array<struct test, 3>{\n"
18802                "    test{56, 23,    \"hello\"},\n"
18803                "    test{-1, 93463, \"world\"},\n"
18804                "    test{7,  5,     \"!!\"   },\n"
18805                "};\n",
18806                Style);
18807   verifyFormat("test demo[] = {\n"
18808                "    {56, 23,    \"hello\"},\n"
18809                "#if X\n"
18810                "    {-1, 93463, \"world\"},\n"
18811                "#endif\n"
18812                "    {7,  5,     \"!!\"   }\n"
18813                "};\n",
18814                Style);
18815   verifyFormat(
18816       "test demo[] = {\n"
18817       "    {7,  23,\n"
18818       "     \"hello world i am a very long line that really, in any\"\n"
18819       "     \"just world, ought to be split over multiple lines\"},\n"
18820       "    {-1, 93463, \"world\"                                 },\n"
18821       "    {56, 5,     \"!!\"                                    }\n"
18822       "};\n",
18823       Style);
18824 
18825   verifyFormat("return GradForUnaryCwise(g, {\n"
18826                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18827                "\"dy\"}   },\n"
18828                "                                {{\"dx\"},   \"Mul\",  "
18829                "{\"dy\", \"sign\"}},\n"
18830                "});\n",
18831                Style);
18832 
18833   Style.ColumnLimit = 0;
18834   EXPECT_EQ(
18835       "test demo[] = {\n"
18836       "    {56, 23,    \"hello world i am a very long line that really, in any "
18837       "just world, ought to be split over multiple lines\"},\n"
18838       "    {-1, 93463, \"world\"                                               "
18839       "                                                   },\n"
18840       "    {7,  5,     \"!!\"                                                  "
18841       "                                                   },\n"
18842       "};",
18843       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18844              "that really, in any just world, ought to be split over multiple "
18845              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18846              Style));
18847 
18848   Style.ColumnLimit = 80;
18849   verifyFormat("test demo[] = {\n"
18850                "    {56, 23,    /* a comment */ \"hello\"},\n"
18851                "    {-1, 93463, \"world\"                },\n"
18852                "    {7,  5,     \"!!\"                   }\n"
18853                "};\n",
18854                Style);
18855 
18856   verifyFormat("test demo[] = {\n"
18857                "    {56, 23,    \"hello\"                   },\n"
18858                "    {-1, 93463, \"world\" /* comment here */},\n"
18859                "    {7,  5,     \"!!\"                      }\n"
18860                "};\n",
18861                Style);
18862 
18863   verifyFormat("test demo[] = {\n"
18864                "    {56, /* a comment */ 23, \"hello\"},\n"
18865                "    {-1, 93463,              \"world\"},\n"
18866                "    {7,  5,                  \"!!\"   }\n"
18867                "};\n",
18868                Style);
18869 
18870   Style.ColumnLimit = 20;
18871   EXPECT_EQ(
18872       "demo = std::array<\n"
18873       "    struct test, 3>{\n"
18874       "    test{\n"
18875       "         56, 23,\n"
18876       "         \"hello \"\n"
18877       "         \"world i \"\n"
18878       "         \"am a very \"\n"
18879       "         \"long line \"\n"
18880       "         \"that \"\n"
18881       "         \"really, \"\n"
18882       "         \"in any \"\n"
18883       "         \"just \"\n"
18884       "         \"world, \"\n"
18885       "         \"ought to \"\n"
18886       "         \"be split \"\n"
18887       "         \"over \"\n"
18888       "         \"multiple \"\n"
18889       "         \"lines\"},\n"
18890       "    test{-1, 93463,\n"
18891       "         \"world\"},\n"
18892       "    test{7,  5,\n"
18893       "         \"!!\"   },\n"
18894       "};",
18895       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18896              "i am a very long line that really, in any just world, ought "
18897              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18898              "test{7, 5, \"!!\"},};",
18899              Style));
18900 
18901   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18902   Style = getLLVMStyleWithColumns(50);
18903   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18904   verifyFormat("static A x = {\n"
18905                "    {{init1, init2, init3, init4},\n"
18906                "     {init1, init2, init3, init4}}\n"
18907                "};",
18908                Style);
18909   Style.ColumnLimit = 100;
18910   EXPECT_EQ(
18911       "test demo[] = {\n"
18912       "    {56, 23,\n"
18913       "     \"hello world i am a very long line that really, in any just world"
18914       ", ought to be split over \"\n"
18915       "     \"multiple lines\"  },\n"
18916       "    {-1, 93463, \"world\"},\n"
18917       "    {7,  5,     \"!!\"   },\n"
18918       "};",
18919       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18920              "that really, in any just world, ought to be split over multiple "
18921              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18922              Style));
18923 }
18924 
18925 TEST_F(FormatTest, UnderstandsPragmas) {
18926   verifyFormat("#pragma omp reduction(| : var)");
18927   verifyFormat("#pragma omp reduction(+ : var)");
18928 
18929   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18930             "(including parentheses).",
18931             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18932                    "(including parentheses)."));
18933 }
18934 
18935 TEST_F(FormatTest, UnderstandPragmaOption) {
18936   verifyFormat("#pragma option -C -A");
18937 
18938   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18939 }
18940 
18941 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18942   FormatStyle Style = getLLVMStyleWithColumns(20);
18943 
18944   // See PR41213
18945   EXPECT_EQ("/*\n"
18946             " *\t9012345\n"
18947             " * /8901\n"
18948             " */",
18949             format("/*\n"
18950                    " *\t9012345 /8901\n"
18951                    " */",
18952                    Style));
18953   EXPECT_EQ("/*\n"
18954             " *345678\n"
18955             " *\t/8901\n"
18956             " */",
18957             format("/*\n"
18958                    " *345678\t/8901\n"
18959                    " */",
18960                    Style));
18961 
18962   verifyFormat("int a; // the\n"
18963                "       // comment",
18964                Style);
18965   EXPECT_EQ("int a; /* first line\n"
18966             "        * second\n"
18967             "        * line third\n"
18968             "        * line\n"
18969             "        */",
18970             format("int a; /* first line\n"
18971                    "        * second\n"
18972                    "        * line third\n"
18973                    "        * line\n"
18974                    "        */",
18975                    Style));
18976   EXPECT_EQ("int a; // first line\n"
18977             "       // second\n"
18978             "       // line third\n"
18979             "       // line",
18980             format("int a; // first line\n"
18981                    "       // second line\n"
18982                    "       // third line",
18983                    Style));
18984 
18985   Style.PenaltyExcessCharacter = 90;
18986   verifyFormat("int a; // the comment", Style);
18987   EXPECT_EQ("int a; // the comment\n"
18988             "       // aaa",
18989             format("int a; // the comment aaa", Style));
18990   EXPECT_EQ("int a; /* first line\n"
18991             "        * second line\n"
18992             "        * third line\n"
18993             "        */",
18994             format("int a; /* first line\n"
18995                    "        * second line\n"
18996                    "        * third line\n"
18997                    "        */",
18998                    Style));
18999   EXPECT_EQ("int a; // first line\n"
19000             "       // second line\n"
19001             "       // third line",
19002             format("int a; // first line\n"
19003                    "       // second line\n"
19004                    "       // third line",
19005                    Style));
19006   // FIXME: Investigate why this is not getting the same layout as the test
19007   // above.
19008   EXPECT_EQ("int a; /* first line\n"
19009             "        * second line\n"
19010             "        * third line\n"
19011             "        */",
19012             format("int a; /* first line second line third line"
19013                    "\n*/",
19014                    Style));
19015 
19016   EXPECT_EQ("// foo bar baz bazfoo\n"
19017             "// foo bar foo bar\n",
19018             format("// foo bar baz bazfoo\n"
19019                    "// foo bar foo           bar\n",
19020                    Style));
19021   EXPECT_EQ("// foo bar baz bazfoo\n"
19022             "// foo bar foo bar\n",
19023             format("// foo bar baz      bazfoo\n"
19024                    "// foo            bar foo bar\n",
19025                    Style));
19026 
19027   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
19028   // next one.
19029   EXPECT_EQ("// foo bar baz bazfoo\n"
19030             "// bar foo bar\n",
19031             format("// foo bar baz      bazfoo bar\n"
19032                    "// foo            bar\n",
19033                    Style));
19034 
19035   EXPECT_EQ("// foo bar baz bazfoo\n"
19036             "// foo bar baz bazfoo\n"
19037             "// bar foo bar\n",
19038             format("// foo bar baz      bazfoo\n"
19039                    "// foo bar baz      bazfoo bar\n"
19040                    "// foo bar\n",
19041                    Style));
19042 
19043   EXPECT_EQ("// foo bar baz bazfoo\n"
19044             "// foo bar baz bazfoo\n"
19045             "// bar foo bar\n",
19046             format("// foo bar baz      bazfoo\n"
19047                    "// foo bar baz      bazfoo bar\n"
19048                    "// foo           bar\n",
19049                    Style));
19050 
19051   // Make sure we do not keep protruding characters if strict mode reflow is
19052   // cheaper than keeping protruding characters.
19053   Style.ColumnLimit = 21;
19054   EXPECT_EQ(
19055       "// foo foo foo foo\n"
19056       "// foo foo foo foo\n"
19057       "// foo foo foo foo\n",
19058       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
19059 
19060   EXPECT_EQ("int a = /* long block\n"
19061             "           comment */\n"
19062             "    42;",
19063             format("int a = /* long block comment */ 42;", Style));
19064 }
19065 
19066 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
19067   FormatStyle Style = getLLVMStyle();
19068   Style.ColumnLimit = 8;
19069   Style.PenaltyExcessCharacter = 15;
19070   verifyFormat("int foo(\n"
19071                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19072                Style);
19073   Style.PenaltyBreakOpenParenthesis = 200;
19074   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
19075             format("int foo(\n"
19076                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19077                    Style));
19078 }
19079 
19080 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
19081   FormatStyle Style = getLLVMStyle();
19082   Style.ColumnLimit = 5;
19083   Style.PenaltyExcessCharacter = 150;
19084   verifyFormat("foo((\n"
19085                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19086 
19087                Style);
19088   Style.PenaltyBreakOpenParenthesis = 100000;
19089   EXPECT_EQ("foo((int)\n"
19090             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
19091             format("foo((\n"
19092                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19093                    Style));
19094 }
19095 
19096 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
19097   FormatStyle Style = getLLVMStyle();
19098   Style.ColumnLimit = 4;
19099   Style.PenaltyExcessCharacter = 100;
19100   verifyFormat("for (\n"
19101                "    int iiiiiiiiiiiiiiiii =\n"
19102                "        0;\n"
19103                "    iiiiiiiiiiiiiiiii <\n"
19104                "    2;\n"
19105                "    iiiiiiiiiiiiiiiii++) {\n"
19106                "}",
19107 
19108                Style);
19109   Style.PenaltyBreakOpenParenthesis = 1250;
19110   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
19111             "         0;\n"
19112             "     iiiiiiiiiiiiiiiii <\n"
19113             "     2;\n"
19114             "     iiiiiiiiiiiiiiiii++) {\n"
19115             "}",
19116             format("for (\n"
19117                    "    int iiiiiiiiiiiiiiiii =\n"
19118                    "        0;\n"
19119                    "    iiiiiiiiiiiiiiiii <\n"
19120                    "    2;\n"
19121                    "    iiiiiiiiiiiiiiiii++) {\n"
19122                    "}",
19123                    Style));
19124 }
19125 
19126 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
19127   for (size_t i = 1; i < Styles.size(); ++i)                                   \
19128   EXPECT_EQ(Styles[0], Styles[i])                                              \
19129       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
19130 
19131 TEST_F(FormatTest, GetsPredefinedStyleByName) {
19132   SmallVector<FormatStyle, 3> Styles;
19133   Styles.resize(3);
19134 
19135   Styles[0] = getLLVMStyle();
19136   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
19137   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
19138   EXPECT_ALL_STYLES_EQUAL(Styles);
19139 
19140   Styles[0] = getGoogleStyle();
19141   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
19142   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
19143   EXPECT_ALL_STYLES_EQUAL(Styles);
19144 
19145   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19146   EXPECT_TRUE(
19147       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
19148   EXPECT_TRUE(
19149       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19150   EXPECT_ALL_STYLES_EQUAL(Styles);
19151 
19152   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19153   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19154   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19155   EXPECT_ALL_STYLES_EQUAL(Styles);
19156 
19157   Styles[0] = getMozillaStyle();
19158   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19159   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19160   EXPECT_ALL_STYLES_EQUAL(Styles);
19161 
19162   Styles[0] = getWebKitStyle();
19163   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19164   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19165   EXPECT_ALL_STYLES_EQUAL(Styles);
19166 
19167   Styles[0] = getGNUStyle();
19168   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19169   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19170   EXPECT_ALL_STYLES_EQUAL(Styles);
19171 
19172   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19173 }
19174 
19175 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19176   SmallVector<FormatStyle, 8> Styles;
19177   Styles.resize(2);
19178 
19179   Styles[0] = getGoogleStyle();
19180   Styles[1] = getLLVMStyle();
19181   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19182   EXPECT_ALL_STYLES_EQUAL(Styles);
19183 
19184   Styles.resize(5);
19185   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19186   Styles[1] = getLLVMStyle();
19187   Styles[1].Language = FormatStyle::LK_JavaScript;
19188   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19189 
19190   Styles[2] = getLLVMStyle();
19191   Styles[2].Language = FormatStyle::LK_JavaScript;
19192   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19193                                   "BasedOnStyle: Google",
19194                                   &Styles[2])
19195                    .value());
19196 
19197   Styles[3] = getLLVMStyle();
19198   Styles[3].Language = FormatStyle::LK_JavaScript;
19199   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19200                                   "Language: JavaScript",
19201                                   &Styles[3])
19202                    .value());
19203 
19204   Styles[4] = getLLVMStyle();
19205   Styles[4].Language = FormatStyle::LK_JavaScript;
19206   EXPECT_EQ(0, parseConfiguration("---\n"
19207                                   "BasedOnStyle: LLVM\n"
19208                                   "IndentWidth: 123\n"
19209                                   "---\n"
19210                                   "BasedOnStyle: Google\n"
19211                                   "Language: JavaScript",
19212                                   &Styles[4])
19213                    .value());
19214   EXPECT_ALL_STYLES_EQUAL(Styles);
19215 }
19216 
19217 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19218   Style.FIELD = false;                                                         \
19219   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19220   EXPECT_TRUE(Style.FIELD);                                                    \
19221   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19222   EXPECT_FALSE(Style.FIELD);
19223 
19224 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19225 
19226 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19227   Style.STRUCT.FIELD = false;                                                  \
19228   EXPECT_EQ(0,                                                                 \
19229             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19230                 .value());                                                     \
19231   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19232   EXPECT_EQ(0,                                                                 \
19233             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19234                 .value());                                                     \
19235   EXPECT_FALSE(Style.STRUCT.FIELD);
19236 
19237 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19238   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19239 
19240 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19241   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19242   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19243   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19244 
19245 TEST_F(FormatTest, ParsesConfigurationBools) {
19246   FormatStyle Style = {};
19247   Style.Language = FormatStyle::LK_Cpp;
19248   CHECK_PARSE_BOOL(AlignTrailingComments);
19249   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19250   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19251   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19252   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19253   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19254   CHECK_PARSE_BOOL(BinPackArguments);
19255   CHECK_PARSE_BOOL(BinPackParameters);
19256   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19257   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
19258   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19259   CHECK_PARSE_BOOL(BreakStringLiterals);
19260   CHECK_PARSE_BOOL(CompactNamespaces);
19261   CHECK_PARSE_BOOL(DeriveLineEnding);
19262   CHECK_PARSE_BOOL(DerivePointerAlignment);
19263   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19264   CHECK_PARSE_BOOL(DisableFormat);
19265   CHECK_PARSE_BOOL(IndentAccessModifiers);
19266   CHECK_PARSE_BOOL(IndentCaseLabels);
19267   CHECK_PARSE_BOOL(IndentCaseBlocks);
19268   CHECK_PARSE_BOOL(IndentGotoLabels);
19269   CHECK_PARSE_BOOL(IndentRequires);
19270   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19271   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19272   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19273   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19274   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19275   CHECK_PARSE_BOOL(ReflowComments);
19276   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19277   CHECK_PARSE_BOOL(SortUsingDeclarations);
19278   CHECK_PARSE_BOOL(SpacesInParentheses);
19279   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19280   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19281   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19282   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19283   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19284   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19285   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19286   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19287   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19288   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19289   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19290   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19291   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19292   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19293   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19294   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19295   CHECK_PARSE_BOOL(UseCRLF);
19296 
19297   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19298   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19299   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19300   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19301   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19302   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19303   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19304   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19305   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19306   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19307   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19308   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19309   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19310   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19311   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19312   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19313   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19314   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19315   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19316   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19317                           AfterFunctionDeclarationName);
19318   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19319                           AfterFunctionDefinitionName);
19320   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19321   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19322   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19323 }
19324 
19325 #undef CHECK_PARSE_BOOL
19326 
19327 TEST_F(FormatTest, ParsesConfiguration) {
19328   FormatStyle Style = {};
19329   Style.Language = FormatStyle::LK_Cpp;
19330   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19331   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19332               ConstructorInitializerIndentWidth, 1234u);
19333   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19334   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19335   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19336   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19337   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19338               PenaltyBreakBeforeFirstCallParameter, 1234u);
19339   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19340               PenaltyBreakTemplateDeclaration, 1234u);
19341   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19342               1234u);
19343   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19344   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19345               PenaltyReturnTypeOnItsOwnLine, 1234u);
19346   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19347               SpacesBeforeTrailingComments, 1234u);
19348   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19349   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19350   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19351 
19352   Style.QualifierAlignment = FormatStyle::QAS_Right;
19353   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19354               FormatStyle::QAS_Leave);
19355   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19356               FormatStyle::QAS_Right);
19357   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19358               FormatStyle::QAS_Left);
19359   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19360               FormatStyle::QAS_Custom);
19361 
19362   Style.QualifierOrder.clear();
19363   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19364               std::vector<std::string>({"const", "volatile", "type"}));
19365   Style.QualifierOrder.clear();
19366   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19367               std::vector<std::string>({"const", "type"}));
19368   Style.QualifierOrder.clear();
19369   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19370               std::vector<std::string>({"volatile", "type"}));
19371 
19372   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19373   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19374               FormatStyle::ACS_None);
19375   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19376               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19377   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19378               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19379   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19380               AlignConsecutiveAssignments,
19381               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19382   // For backwards compability, false / true should still parse
19383   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19384               FormatStyle::ACS_None);
19385   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19386               FormatStyle::ACS_Consecutive);
19387 
19388   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19389   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19390               FormatStyle::ACS_None);
19391   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19392               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19393   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19394               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19395   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19396               AlignConsecutiveBitFields,
19397               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19398   // For backwards compability, false / true should still parse
19399   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19400               FormatStyle::ACS_None);
19401   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19402               FormatStyle::ACS_Consecutive);
19403 
19404   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19405   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19406               FormatStyle::ACS_None);
19407   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19408               FormatStyle::ACS_Consecutive);
19409   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19410               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19411   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19412               AlignConsecutiveMacros,
19413               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19414   // For backwards compability, false / true should still parse
19415   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19416               FormatStyle::ACS_None);
19417   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19418               FormatStyle::ACS_Consecutive);
19419 
19420   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19421   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19422               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19423   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19424               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19425   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19426               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19427   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19428               AlignConsecutiveDeclarations,
19429               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19430   // For backwards compability, false / true should still parse
19431   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19432               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19433   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19434               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19435 
19436   Style.PointerAlignment = FormatStyle::PAS_Middle;
19437   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19438               FormatStyle::PAS_Left);
19439   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19440               FormatStyle::PAS_Right);
19441   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19442               FormatStyle::PAS_Middle);
19443   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19444   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19445               FormatStyle::RAS_Pointer);
19446   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19447               FormatStyle::RAS_Left);
19448   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19449               FormatStyle::RAS_Right);
19450   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19451               FormatStyle::RAS_Middle);
19452   // For backward compatibility:
19453   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19454               FormatStyle::PAS_Left);
19455   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19456               FormatStyle::PAS_Right);
19457   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19458               FormatStyle::PAS_Middle);
19459 
19460   Style.Standard = FormatStyle::LS_Auto;
19461   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19462   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19463   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19464   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19465   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19466   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19467   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19468   // Legacy aliases:
19469   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19470   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19471   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19472   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19473 
19474   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19475   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19476               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19477   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19478               FormatStyle::BOS_None);
19479   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19480               FormatStyle::BOS_All);
19481   // For backward compatibility:
19482   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19483               FormatStyle::BOS_None);
19484   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19485               FormatStyle::BOS_All);
19486 
19487   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19488   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19489               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19490   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19491               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19492   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19493               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19494   // For backward compatibility:
19495   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19496               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19497 
19498   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19499   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19500               FormatStyle::BILS_AfterComma);
19501   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19502               FormatStyle::BILS_BeforeComma);
19503   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19504               FormatStyle::BILS_AfterColon);
19505   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19506               FormatStyle::BILS_BeforeColon);
19507   // For backward compatibility:
19508   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19509               FormatStyle::BILS_BeforeComma);
19510 
19511   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19512   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19513               FormatStyle::PCIS_Never);
19514   CHECK_PARSE("PackConstructorInitializers: BinPack",
19515               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19516   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19517               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19518   CHECK_PARSE("PackConstructorInitializers: NextLine",
19519               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19520   // For backward compatibility:
19521   CHECK_PARSE("BasedOnStyle: Google\n"
19522               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19523               "AllowAllConstructorInitializersOnNextLine: false",
19524               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19525   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19526   CHECK_PARSE("BasedOnStyle: Google\n"
19527               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19528               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19529   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19530               "AllowAllConstructorInitializersOnNextLine: true",
19531               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19532   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19533   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19534               "AllowAllConstructorInitializersOnNextLine: false",
19535               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19536 
19537   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19538   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19539               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19540   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19541               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19542   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19543               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19544   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19545               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19546 
19547   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19548   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19549               FormatStyle::BAS_Align);
19550   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19551               FormatStyle::BAS_DontAlign);
19552   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19553               FormatStyle::BAS_AlwaysBreak);
19554   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19555               FormatStyle::BAS_BlockIndent);
19556   // For backward compatibility:
19557   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19558               FormatStyle::BAS_DontAlign);
19559   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19560               FormatStyle::BAS_Align);
19561 
19562   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19563   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19564               FormatStyle::ENAS_DontAlign);
19565   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19566               FormatStyle::ENAS_Left);
19567   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19568               FormatStyle::ENAS_Right);
19569   // For backward compatibility:
19570   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19571               FormatStyle::ENAS_Left);
19572   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19573               FormatStyle::ENAS_Right);
19574 
19575   Style.AlignOperands = FormatStyle::OAS_Align;
19576   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19577               FormatStyle::OAS_DontAlign);
19578   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19579   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19580               FormatStyle::OAS_AlignAfterOperator);
19581   // For backward compatibility:
19582   CHECK_PARSE("AlignOperands: false", AlignOperands,
19583               FormatStyle::OAS_DontAlign);
19584   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19585 
19586   Style.UseTab = FormatStyle::UT_ForIndentation;
19587   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19588   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19589   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19590   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19591               FormatStyle::UT_ForContinuationAndIndentation);
19592   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19593               FormatStyle::UT_AlignWithSpaces);
19594   // For backward compatibility:
19595   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19596   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19597 
19598   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19599   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19600               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19601   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19602               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19603   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19604               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19605   // For backward compatibility:
19606   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19607               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19608   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19609               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19610 
19611   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19612   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19613               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19614   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19615               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19616   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19617               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19618   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19619               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19620   // For backward compatibility:
19621   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19622               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19623   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19624               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19625 
19626   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19627   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19628               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19629   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19630               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19631   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19632               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19633   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19634               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19635 
19636   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19637   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19638               FormatStyle::SBPO_Never);
19639   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19640               FormatStyle::SBPO_Always);
19641   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19642               FormatStyle::SBPO_ControlStatements);
19643   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19644               SpaceBeforeParens,
19645               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19646   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19647               FormatStyle::SBPO_NonEmptyParentheses);
19648   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19649               FormatStyle::SBPO_Custom);
19650   // For backward compatibility:
19651   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19652               FormatStyle::SBPO_Never);
19653   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19654               FormatStyle::SBPO_ControlStatements);
19655   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19656               SpaceBeforeParens,
19657               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19658 
19659   Style.ColumnLimit = 123;
19660   FormatStyle BaseStyle = getLLVMStyle();
19661   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19662   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19663 
19664   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19665   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19666               FormatStyle::BS_Attach);
19667   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19668               FormatStyle::BS_Linux);
19669   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19670               FormatStyle::BS_Mozilla);
19671   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19672               FormatStyle::BS_Stroustrup);
19673   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19674               FormatStyle::BS_Allman);
19675   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19676               FormatStyle::BS_Whitesmiths);
19677   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19678   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19679               FormatStyle::BS_WebKit);
19680   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19681               FormatStyle::BS_Custom);
19682 
19683   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19684   CHECK_PARSE("BraceWrapping:\n"
19685               "  AfterControlStatement: MultiLine",
19686               BraceWrapping.AfterControlStatement,
19687               FormatStyle::BWACS_MultiLine);
19688   CHECK_PARSE("BraceWrapping:\n"
19689               "  AfterControlStatement: Always",
19690               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19691   CHECK_PARSE("BraceWrapping:\n"
19692               "  AfterControlStatement: Never",
19693               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19694   // For backward compatibility:
19695   CHECK_PARSE("BraceWrapping:\n"
19696               "  AfterControlStatement: true",
19697               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19698   CHECK_PARSE("BraceWrapping:\n"
19699               "  AfterControlStatement: false",
19700               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19701 
19702   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19703   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19704               FormatStyle::RTBS_None);
19705   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19706               FormatStyle::RTBS_All);
19707   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19708               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19709   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19710               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19711   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19712               AlwaysBreakAfterReturnType,
19713               FormatStyle::RTBS_TopLevelDefinitions);
19714 
19715   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19716   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19717               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19718   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19719               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19720   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19721               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19722   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19723               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19724   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19725               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19726 
19727   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19728   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19729               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19730   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19731               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19732   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19733               AlwaysBreakAfterDefinitionReturnType,
19734               FormatStyle::DRTBS_TopLevel);
19735 
19736   Style.NamespaceIndentation = FormatStyle::NI_All;
19737   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19738               FormatStyle::NI_None);
19739   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19740               FormatStyle::NI_Inner);
19741   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19742               FormatStyle::NI_All);
19743 
19744   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19745   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19746               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19747   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19748               AllowShortIfStatementsOnASingleLine,
19749               FormatStyle::SIS_WithoutElse);
19750   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19751               AllowShortIfStatementsOnASingleLine,
19752               FormatStyle::SIS_OnlyFirstIf);
19753   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19754               AllowShortIfStatementsOnASingleLine,
19755               FormatStyle::SIS_AllIfsAndElse);
19756   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19757               AllowShortIfStatementsOnASingleLine,
19758               FormatStyle::SIS_OnlyFirstIf);
19759   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19760               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19761   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19762               AllowShortIfStatementsOnASingleLine,
19763               FormatStyle::SIS_WithoutElse);
19764 
19765   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19766   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19767               FormatStyle::IEBS_AfterExternBlock);
19768   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19769               FormatStyle::IEBS_Indent);
19770   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19771               FormatStyle::IEBS_NoIndent);
19772   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19773               FormatStyle::IEBS_Indent);
19774   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19775               FormatStyle::IEBS_NoIndent);
19776 
19777   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19778   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19779               FormatStyle::BFCS_Both);
19780   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19781               FormatStyle::BFCS_None);
19782   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19783               FormatStyle::BFCS_Before);
19784   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19785               FormatStyle::BFCS_After);
19786 
19787   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19788   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19789               FormatStyle::SJSIO_After);
19790   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19791               FormatStyle::SJSIO_Before);
19792 
19793   // FIXME: This is required because parsing a configuration simply overwrites
19794   // the first N elements of the list instead of resetting it.
19795   Style.ForEachMacros.clear();
19796   std::vector<std::string> BoostForeach;
19797   BoostForeach.push_back("BOOST_FOREACH");
19798   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19799   std::vector<std::string> BoostAndQForeach;
19800   BoostAndQForeach.push_back("BOOST_FOREACH");
19801   BoostAndQForeach.push_back("Q_FOREACH");
19802   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19803               BoostAndQForeach);
19804 
19805   Style.IfMacros.clear();
19806   std::vector<std::string> CustomIfs;
19807   CustomIfs.push_back("MYIF");
19808   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19809 
19810   Style.AttributeMacros.clear();
19811   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19812               std::vector<std::string>{"__capability"});
19813   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19814               std::vector<std::string>({"attr1", "attr2"}));
19815 
19816   Style.StatementAttributeLikeMacros.clear();
19817   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19818               StatementAttributeLikeMacros,
19819               std::vector<std::string>({"emit", "Q_EMIT"}));
19820 
19821   Style.StatementMacros.clear();
19822   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19823               std::vector<std::string>{"QUNUSED"});
19824   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19825               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19826 
19827   Style.NamespaceMacros.clear();
19828   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19829               std::vector<std::string>{"TESTSUITE"});
19830   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19831               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19832 
19833   Style.WhitespaceSensitiveMacros.clear();
19834   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19835               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19836   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19837               WhitespaceSensitiveMacros,
19838               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19839   Style.WhitespaceSensitiveMacros.clear();
19840   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19841               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19842   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19843               WhitespaceSensitiveMacros,
19844               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19845 
19846   Style.IncludeStyle.IncludeCategories.clear();
19847   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19848       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19849   CHECK_PARSE("IncludeCategories:\n"
19850               "  - Regex: abc/.*\n"
19851               "    Priority: 2\n"
19852               "  - Regex: .*\n"
19853               "    Priority: 1\n"
19854               "    CaseSensitive: true\n",
19855               IncludeStyle.IncludeCategories, ExpectedCategories);
19856   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19857               "abc$");
19858   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19859               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19860 
19861   Style.SortIncludes = FormatStyle::SI_Never;
19862   CHECK_PARSE("SortIncludes: true", SortIncludes,
19863               FormatStyle::SI_CaseSensitive);
19864   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19865   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19866               FormatStyle::SI_CaseInsensitive);
19867   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19868               FormatStyle::SI_CaseSensitive);
19869   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19870 
19871   Style.RawStringFormats.clear();
19872   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19873       {
19874           FormatStyle::LK_TextProto,
19875           {"pb", "proto"},
19876           {"PARSE_TEXT_PROTO"},
19877           /*CanonicalDelimiter=*/"",
19878           "llvm",
19879       },
19880       {
19881           FormatStyle::LK_Cpp,
19882           {"cc", "cpp"},
19883           {"C_CODEBLOCK", "CPPEVAL"},
19884           /*CanonicalDelimiter=*/"cc",
19885           /*BasedOnStyle=*/"",
19886       },
19887   };
19888 
19889   CHECK_PARSE("RawStringFormats:\n"
19890               "  - Language: TextProto\n"
19891               "    Delimiters:\n"
19892               "      - 'pb'\n"
19893               "      - 'proto'\n"
19894               "    EnclosingFunctions:\n"
19895               "      - 'PARSE_TEXT_PROTO'\n"
19896               "    BasedOnStyle: llvm\n"
19897               "  - Language: Cpp\n"
19898               "    Delimiters:\n"
19899               "      - 'cc'\n"
19900               "      - 'cpp'\n"
19901               "    EnclosingFunctions:\n"
19902               "      - 'C_CODEBLOCK'\n"
19903               "      - 'CPPEVAL'\n"
19904               "    CanonicalDelimiter: 'cc'",
19905               RawStringFormats, ExpectedRawStringFormats);
19906 
19907   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19908               "  Minimum: 0\n"
19909               "  Maximum: 0",
19910               SpacesInLineCommentPrefix.Minimum, 0u);
19911   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19912   Style.SpacesInLineCommentPrefix.Minimum = 1;
19913   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19914               "  Minimum: 2",
19915               SpacesInLineCommentPrefix.Minimum, 0u);
19916   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19917               "  Maximum: -1",
19918               SpacesInLineCommentPrefix.Maximum, -1u);
19919   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19920               "  Minimum: 2",
19921               SpacesInLineCommentPrefix.Minimum, 2u);
19922   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19923               "  Maximum: 1",
19924               SpacesInLineCommentPrefix.Maximum, 1u);
19925   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19926 
19927   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19928   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19929   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19930               FormatStyle::SIAS_Always);
19931   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19932   // For backward compatibility:
19933   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19934   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19935 }
19936 
19937 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19938   FormatStyle Style = {};
19939   Style.Language = FormatStyle::LK_Cpp;
19940   CHECK_PARSE("Language: Cpp\n"
19941               "IndentWidth: 12",
19942               IndentWidth, 12u);
19943   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19944                                "IndentWidth: 34",
19945                                &Style),
19946             ParseError::Unsuitable);
19947   FormatStyle BinPackedTCS = {};
19948   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19949   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19950                                "InsertTrailingCommas: Wrapped",
19951                                &BinPackedTCS),
19952             ParseError::BinPackTrailingCommaConflict);
19953   EXPECT_EQ(12u, Style.IndentWidth);
19954   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19955   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19956 
19957   Style.Language = FormatStyle::LK_JavaScript;
19958   CHECK_PARSE("Language: JavaScript\n"
19959               "IndentWidth: 12",
19960               IndentWidth, 12u);
19961   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19962   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19963                                "IndentWidth: 34",
19964                                &Style),
19965             ParseError::Unsuitable);
19966   EXPECT_EQ(23u, Style.IndentWidth);
19967   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19968   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19969 
19970   CHECK_PARSE("BasedOnStyle: LLVM\n"
19971               "IndentWidth: 67",
19972               IndentWidth, 67u);
19973 
19974   CHECK_PARSE("---\n"
19975               "Language: JavaScript\n"
19976               "IndentWidth: 12\n"
19977               "---\n"
19978               "Language: Cpp\n"
19979               "IndentWidth: 34\n"
19980               "...\n",
19981               IndentWidth, 12u);
19982 
19983   Style.Language = FormatStyle::LK_Cpp;
19984   CHECK_PARSE("---\n"
19985               "Language: JavaScript\n"
19986               "IndentWidth: 12\n"
19987               "---\n"
19988               "Language: Cpp\n"
19989               "IndentWidth: 34\n"
19990               "...\n",
19991               IndentWidth, 34u);
19992   CHECK_PARSE("---\n"
19993               "IndentWidth: 78\n"
19994               "---\n"
19995               "Language: JavaScript\n"
19996               "IndentWidth: 56\n"
19997               "...\n",
19998               IndentWidth, 78u);
19999 
20000   Style.ColumnLimit = 123;
20001   Style.IndentWidth = 234;
20002   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
20003   Style.TabWidth = 345;
20004   EXPECT_FALSE(parseConfiguration("---\n"
20005                                   "IndentWidth: 456\n"
20006                                   "BreakBeforeBraces: Allman\n"
20007                                   "---\n"
20008                                   "Language: JavaScript\n"
20009                                   "IndentWidth: 111\n"
20010                                   "TabWidth: 111\n"
20011                                   "---\n"
20012                                   "Language: Cpp\n"
20013                                   "BreakBeforeBraces: Stroustrup\n"
20014                                   "TabWidth: 789\n"
20015                                   "...\n",
20016                                   &Style));
20017   EXPECT_EQ(123u, Style.ColumnLimit);
20018   EXPECT_EQ(456u, Style.IndentWidth);
20019   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
20020   EXPECT_EQ(789u, Style.TabWidth);
20021 
20022   EXPECT_EQ(parseConfiguration("---\n"
20023                                "Language: JavaScript\n"
20024                                "IndentWidth: 56\n"
20025                                "---\n"
20026                                "IndentWidth: 78\n"
20027                                "...\n",
20028                                &Style),
20029             ParseError::Error);
20030   EXPECT_EQ(parseConfiguration("---\n"
20031                                "Language: JavaScript\n"
20032                                "IndentWidth: 56\n"
20033                                "---\n"
20034                                "Language: JavaScript\n"
20035                                "IndentWidth: 78\n"
20036                                "...\n",
20037                                &Style),
20038             ParseError::Error);
20039 
20040   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
20041 }
20042 
20043 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
20044   FormatStyle Style = {};
20045   Style.Language = FormatStyle::LK_JavaScript;
20046   Style.BreakBeforeTernaryOperators = true;
20047   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
20048   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20049 
20050   Style.BreakBeforeTernaryOperators = true;
20051   EXPECT_EQ(0, parseConfiguration("---\n"
20052                                   "BasedOnStyle: Google\n"
20053                                   "---\n"
20054                                   "Language: JavaScript\n"
20055                                   "IndentWidth: 76\n"
20056                                   "...\n",
20057                                   &Style)
20058                    .value());
20059   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20060   EXPECT_EQ(76u, Style.IndentWidth);
20061   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
20062 }
20063 
20064 TEST_F(FormatTest, ConfigurationRoundTripTest) {
20065   FormatStyle Style = getLLVMStyle();
20066   std::string YAML = configurationAsText(Style);
20067   FormatStyle ParsedStyle = {};
20068   ParsedStyle.Language = FormatStyle::LK_Cpp;
20069   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
20070   EXPECT_EQ(Style, ParsedStyle);
20071 }
20072 
20073 TEST_F(FormatTest, WorksFor8bitEncodings) {
20074   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
20075             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
20076             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
20077             "\"\xef\xee\xf0\xf3...\"",
20078             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
20079                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
20080                    "\xef\xee\xf0\xf3...\"",
20081                    getLLVMStyleWithColumns(12)));
20082 }
20083 
20084 TEST_F(FormatTest, HandlesUTF8BOM) {
20085   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
20086   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
20087             format("\xef\xbb\xbf#include <iostream>"));
20088   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
20089             format("\xef\xbb\xbf\n#include <iostream>"));
20090 }
20091 
20092 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
20093 #if !defined(_MSC_VER)
20094 
20095 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
20096   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
20097                getLLVMStyleWithColumns(35));
20098   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
20099                getLLVMStyleWithColumns(31));
20100   verifyFormat("// Однажды в студёную зимнюю пору...",
20101                getLLVMStyleWithColumns(36));
20102   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20103   verifyFormat("/* Однажды в студёную зимнюю пору... */",
20104                getLLVMStyleWithColumns(39));
20105   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20106                getLLVMStyleWithColumns(35));
20107 }
20108 
20109 TEST_F(FormatTest, SplitsUTF8Strings) {
20110   // Non-printable characters' width is currently considered to be the length in
20111   // bytes in UTF8. The characters can be displayed in very different manner
20112   // (zero-width, single width with a substitution glyph, expanded to their code
20113   // (e.g. "<8d>"), so there's no single correct way to handle them.
20114   EXPECT_EQ("\"aaaaÄ\"\n"
20115             "\"\xc2\x8d\";",
20116             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20117   EXPECT_EQ("\"aaaaaaaÄ\"\n"
20118             "\"\xc2\x8d\";",
20119             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20120   EXPECT_EQ("\"Однажды, в \"\n"
20121             "\"студёную \"\n"
20122             "\"зимнюю \"\n"
20123             "\"пору,\"",
20124             format("\"Однажды, в студёную зимнюю пору,\"",
20125                    getLLVMStyleWithColumns(13)));
20126   EXPECT_EQ(
20127       "\"一 二 三 \"\n"
20128       "\"四 五六 \"\n"
20129       "\"七 八 九 \"\n"
20130       "\"十\"",
20131       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20132   EXPECT_EQ("\"一\t\"\n"
20133             "\"二 \t\"\n"
20134             "\"三 四 \"\n"
20135             "\"五\t\"\n"
20136             "\"六 \t\"\n"
20137             "\"七 \"\n"
20138             "\"八九十\tqq\"",
20139             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20140                    getLLVMStyleWithColumns(11)));
20141 
20142   // UTF8 character in an escape sequence.
20143   EXPECT_EQ("\"aaaaaa\"\n"
20144             "\"\\\xC2\x8D\"",
20145             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20146 }
20147 
20148 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20149   EXPECT_EQ("const char *sssss =\n"
20150             "    \"一二三四五六七八\\\n"
20151             " 九 十\";",
20152             format("const char *sssss = \"一二三四五六七八\\\n"
20153                    " 九 十\";",
20154                    getLLVMStyleWithColumns(30)));
20155 }
20156 
20157 TEST_F(FormatTest, SplitsUTF8LineComments) {
20158   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20159             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20160   EXPECT_EQ("// Я из лесу\n"
20161             "// вышел; был\n"
20162             "// сильный\n"
20163             "// мороз.",
20164             format("// Я из лесу вышел; был сильный мороз.",
20165                    getLLVMStyleWithColumns(13)));
20166   EXPECT_EQ("// 一二三\n"
20167             "// 四五六七\n"
20168             "// 八  九\n"
20169             "// 十",
20170             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20171 }
20172 
20173 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20174   EXPECT_EQ("/* Гляжу,\n"
20175             " * поднимается\n"
20176             " * медленно в\n"
20177             " * гору\n"
20178             " * Лошадка,\n"
20179             " * везущая\n"
20180             " * хворосту\n"
20181             " * воз. */",
20182             format("/* Гляжу, поднимается медленно в гору\n"
20183                    " * Лошадка, везущая хворосту воз. */",
20184                    getLLVMStyleWithColumns(13)));
20185   EXPECT_EQ(
20186       "/* 一二三\n"
20187       " * 四五六七\n"
20188       " * 八  九\n"
20189       " * 十  */",
20190       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20191   EXPECT_EQ("/* �������� ��������\n"
20192             " * ��������\n"
20193             " * ������-�� */",
20194             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20195 }
20196 
20197 #endif // _MSC_VER
20198 
20199 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20200   FormatStyle Style = getLLVMStyle();
20201 
20202   Style.ConstructorInitializerIndentWidth = 4;
20203   verifyFormat(
20204       "SomeClass::Constructor()\n"
20205       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20206       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20207       Style);
20208 
20209   Style.ConstructorInitializerIndentWidth = 2;
20210   verifyFormat(
20211       "SomeClass::Constructor()\n"
20212       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20213       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20214       Style);
20215 
20216   Style.ConstructorInitializerIndentWidth = 0;
20217   verifyFormat(
20218       "SomeClass::Constructor()\n"
20219       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20220       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20221       Style);
20222   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20223   verifyFormat(
20224       "SomeLongTemplateVariableName<\n"
20225       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20226       Style);
20227   verifyFormat("bool smaller = 1 < "
20228                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20229                "                       "
20230                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20231                Style);
20232 
20233   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20234   verifyFormat("SomeClass::Constructor() :\n"
20235                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20236                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20237                Style);
20238 }
20239 
20240 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20241   FormatStyle Style = getLLVMStyle();
20242   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20243   Style.ConstructorInitializerIndentWidth = 4;
20244   verifyFormat("SomeClass::Constructor()\n"
20245                "    : a(a)\n"
20246                "    , b(b)\n"
20247                "    , c(c) {}",
20248                Style);
20249   verifyFormat("SomeClass::Constructor()\n"
20250                "    : a(a) {}",
20251                Style);
20252 
20253   Style.ColumnLimit = 0;
20254   verifyFormat("SomeClass::Constructor()\n"
20255                "    : a(a) {}",
20256                Style);
20257   verifyFormat("SomeClass::Constructor() noexcept\n"
20258                "    : a(a) {}",
20259                Style);
20260   verifyFormat("SomeClass::Constructor()\n"
20261                "    : a(a)\n"
20262                "    , b(b)\n"
20263                "    , c(c) {}",
20264                Style);
20265   verifyFormat("SomeClass::Constructor()\n"
20266                "    : a(a) {\n"
20267                "  foo();\n"
20268                "  bar();\n"
20269                "}",
20270                Style);
20271 
20272   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20273   verifyFormat("SomeClass::Constructor()\n"
20274                "    : a(a)\n"
20275                "    , b(b)\n"
20276                "    , c(c) {\n}",
20277                Style);
20278   verifyFormat("SomeClass::Constructor()\n"
20279                "    : a(a) {\n}",
20280                Style);
20281 
20282   Style.ColumnLimit = 80;
20283   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20284   Style.ConstructorInitializerIndentWidth = 2;
20285   verifyFormat("SomeClass::Constructor()\n"
20286                "  : a(a)\n"
20287                "  , b(b)\n"
20288                "  , c(c) {}",
20289                Style);
20290 
20291   Style.ConstructorInitializerIndentWidth = 0;
20292   verifyFormat("SomeClass::Constructor()\n"
20293                ": a(a)\n"
20294                ", b(b)\n"
20295                ", c(c) {}",
20296                Style);
20297 
20298   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20299   Style.ConstructorInitializerIndentWidth = 4;
20300   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20301   verifyFormat(
20302       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20303       Style);
20304   verifyFormat(
20305       "SomeClass::Constructor()\n"
20306       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20307       Style);
20308   Style.ConstructorInitializerIndentWidth = 4;
20309   Style.ColumnLimit = 60;
20310   verifyFormat("SomeClass::Constructor()\n"
20311                "    : aaaaaaaa(aaaaaaaa)\n"
20312                "    , aaaaaaaa(aaaaaaaa)\n"
20313                "    , aaaaaaaa(aaaaaaaa) {}",
20314                Style);
20315 }
20316 
20317 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20318   FormatStyle Style = getLLVMStyle();
20319   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20320   Style.ConstructorInitializerIndentWidth = 4;
20321   verifyFormat("SomeClass::Constructor()\n"
20322                "    : a{a}\n"
20323                "    , b{b} {}",
20324                Style);
20325   verifyFormat("SomeClass::Constructor()\n"
20326                "    : a{a}\n"
20327                "#if CONDITION\n"
20328                "    , b{b}\n"
20329                "#endif\n"
20330                "{\n}",
20331                Style);
20332   Style.ConstructorInitializerIndentWidth = 2;
20333   verifyFormat("SomeClass::Constructor()\n"
20334                "#if CONDITION\n"
20335                "  : a{a}\n"
20336                "#endif\n"
20337                "  , b{b}\n"
20338                "  , c{c} {\n}",
20339                Style);
20340   Style.ConstructorInitializerIndentWidth = 0;
20341   verifyFormat("SomeClass::Constructor()\n"
20342                ": a{a}\n"
20343                "#ifdef CONDITION\n"
20344                ", b{b}\n"
20345                "#else\n"
20346                ", c{c}\n"
20347                "#endif\n"
20348                ", d{d} {\n}",
20349                Style);
20350   Style.ConstructorInitializerIndentWidth = 4;
20351   verifyFormat("SomeClass::Constructor()\n"
20352                "    : a{a}\n"
20353                "#if WINDOWS\n"
20354                "#if DEBUG\n"
20355                "    , b{0}\n"
20356                "#else\n"
20357                "    , b{1}\n"
20358                "#endif\n"
20359                "#else\n"
20360                "#if DEBUG\n"
20361                "    , b{2}\n"
20362                "#else\n"
20363                "    , b{3}\n"
20364                "#endif\n"
20365                "#endif\n"
20366                "{\n}",
20367                Style);
20368   verifyFormat("SomeClass::Constructor()\n"
20369                "    : a{a}\n"
20370                "#if WINDOWS\n"
20371                "    , b{0}\n"
20372                "#if DEBUG\n"
20373                "    , c{0}\n"
20374                "#else\n"
20375                "    , c{1}\n"
20376                "#endif\n"
20377                "#else\n"
20378                "#if DEBUG\n"
20379                "    , c{2}\n"
20380                "#else\n"
20381                "    , c{3}\n"
20382                "#endif\n"
20383                "    , b{1}\n"
20384                "#endif\n"
20385                "{\n}",
20386                Style);
20387 }
20388 
20389 TEST_F(FormatTest, Destructors) {
20390   verifyFormat("void F(int &i) { i.~int(); }");
20391   verifyFormat("void F(int &i) { i->~int(); }");
20392 }
20393 
20394 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20395   FormatStyle Style = getWebKitStyle();
20396 
20397   // Don't indent in outer namespaces.
20398   verifyFormat("namespace outer {\n"
20399                "int i;\n"
20400                "namespace inner {\n"
20401                "    int i;\n"
20402                "} // namespace inner\n"
20403                "} // namespace outer\n"
20404                "namespace other_outer {\n"
20405                "int i;\n"
20406                "}",
20407                Style);
20408 
20409   // Don't indent case labels.
20410   verifyFormat("switch (variable) {\n"
20411                "case 1:\n"
20412                "case 2:\n"
20413                "    doSomething();\n"
20414                "    break;\n"
20415                "default:\n"
20416                "    ++variable;\n"
20417                "}",
20418                Style);
20419 
20420   // Wrap before binary operators.
20421   EXPECT_EQ("void f()\n"
20422             "{\n"
20423             "    if (aaaaaaaaaaaaaaaa\n"
20424             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20425             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20426             "        return;\n"
20427             "}",
20428             format("void f() {\n"
20429                    "if (aaaaaaaaaaaaaaaa\n"
20430                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20431                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20432                    "return;\n"
20433                    "}",
20434                    Style));
20435 
20436   // Allow functions on a single line.
20437   verifyFormat("void f() { return; }", Style);
20438 
20439   // Allow empty blocks on a single line and insert a space in empty blocks.
20440   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20441   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20442   // However, don't merge non-empty short loops.
20443   EXPECT_EQ("while (true) {\n"
20444             "    continue;\n"
20445             "}",
20446             format("while (true) { continue; }", Style));
20447 
20448   // Constructor initializers are formatted one per line with the "," on the
20449   // new line.
20450   verifyFormat("Constructor()\n"
20451                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20452                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20453                "          aaaaaaaaaaaaaa)\n"
20454                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20455                "{\n"
20456                "}",
20457                Style);
20458   verifyFormat("SomeClass::Constructor()\n"
20459                "    : a(a)\n"
20460                "{\n"
20461                "}",
20462                Style);
20463   EXPECT_EQ("SomeClass::Constructor()\n"
20464             "    : a(a)\n"
20465             "{\n"
20466             "}",
20467             format("SomeClass::Constructor():a(a){}", Style));
20468   verifyFormat("SomeClass::Constructor()\n"
20469                "    : a(a)\n"
20470                "    , b(b)\n"
20471                "    , c(c)\n"
20472                "{\n"
20473                "}",
20474                Style);
20475   verifyFormat("SomeClass::Constructor()\n"
20476                "    : a(a)\n"
20477                "{\n"
20478                "    foo();\n"
20479                "    bar();\n"
20480                "}",
20481                Style);
20482 
20483   // Access specifiers should be aligned left.
20484   verifyFormat("class C {\n"
20485                "public:\n"
20486                "    int i;\n"
20487                "};",
20488                Style);
20489 
20490   // Do not align comments.
20491   verifyFormat("int a; // Do not\n"
20492                "double b; // align comments.",
20493                Style);
20494 
20495   // Do not align operands.
20496   EXPECT_EQ("ASSERT(aaaa\n"
20497             "    || bbbb);",
20498             format("ASSERT ( aaaa\n||bbbb);", Style));
20499 
20500   // Accept input's line breaks.
20501   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20502             "    || bbbbbbbbbbbbbbb) {\n"
20503             "    i++;\n"
20504             "}",
20505             format("if (aaaaaaaaaaaaaaa\n"
20506                    "|| bbbbbbbbbbbbbbb) { i++; }",
20507                    Style));
20508   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20509             "    i++;\n"
20510             "}",
20511             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20512 
20513   // Don't automatically break all macro definitions (llvm.org/PR17842).
20514   verifyFormat("#define aNumber 10", Style);
20515   // However, generally keep the line breaks that the user authored.
20516   EXPECT_EQ("#define aNumber \\\n"
20517             "    10",
20518             format("#define aNumber \\\n"
20519                    " 10",
20520                    Style));
20521 
20522   // Keep empty and one-element array literals on a single line.
20523   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20524             "                                  copyItems:YES];",
20525             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20526                    "copyItems:YES];",
20527                    Style));
20528   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20529             "                                  copyItems:YES];",
20530             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20531                    "             copyItems:YES];",
20532                    Style));
20533   // FIXME: This does not seem right, there should be more indentation before
20534   // the array literal's entries. Nested blocks have the same problem.
20535   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20536             "    @\"a\",\n"
20537             "    @\"a\"\n"
20538             "]\n"
20539             "                                  copyItems:YES];",
20540             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20541                    "     @\"a\",\n"
20542                    "     @\"a\"\n"
20543                    "     ]\n"
20544                    "       copyItems:YES];",
20545                    Style));
20546   EXPECT_EQ(
20547       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20548       "                                  copyItems:YES];",
20549       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20550              "   copyItems:YES];",
20551              Style));
20552 
20553   verifyFormat("[self.a b:c c:d];", Style);
20554   EXPECT_EQ("[self.a b:c\n"
20555             "        c:d];",
20556             format("[self.a b:c\n"
20557                    "c:d];",
20558                    Style));
20559 }
20560 
20561 TEST_F(FormatTest, FormatsLambdas) {
20562   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20563   verifyFormat(
20564       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20565   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20566   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20567   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20568   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20569   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20570   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20571   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20572   verifyFormat("int x = f(*+[] {});");
20573   verifyFormat("void f() {\n"
20574                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20575                "}\n");
20576   verifyFormat("void f() {\n"
20577                "  other(x.begin(), //\n"
20578                "        x.end(),   //\n"
20579                "        [&](int, int) { return 1; });\n"
20580                "}\n");
20581   verifyFormat("void f() {\n"
20582                "  other.other.other.other.other(\n"
20583                "      x.begin(), x.end(),\n"
20584                "      [something, rather](int, int, int, int, int, int, int) { "
20585                "return 1; });\n"
20586                "}\n");
20587   verifyFormat(
20588       "void f() {\n"
20589       "  other.other.other.other.other(\n"
20590       "      x.begin(), x.end(),\n"
20591       "      [something, rather](int, int, int, int, int, int, int) {\n"
20592       "        //\n"
20593       "      });\n"
20594       "}\n");
20595   verifyFormat("SomeFunction([]() { // A cool function...\n"
20596                "  return 43;\n"
20597                "});");
20598   EXPECT_EQ("SomeFunction([]() {\n"
20599             "#define A a\n"
20600             "  return 43;\n"
20601             "});",
20602             format("SomeFunction([](){\n"
20603                    "#define A a\n"
20604                    "return 43;\n"
20605                    "});"));
20606   verifyFormat("void f() {\n"
20607                "  SomeFunction([](decltype(x), A *a) {});\n"
20608                "  SomeFunction([](typeof(x), A *a) {});\n"
20609                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20610                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20611                "}");
20612   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20613                "    [](const aaaaaaaaaa &a) { return a; });");
20614   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20615                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20616                "});");
20617   verifyFormat("Constructor()\n"
20618                "    : Field([] { // comment\n"
20619                "        int i;\n"
20620                "      }) {}");
20621   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20622                "  return some_parameter.size();\n"
20623                "};");
20624   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20625                "    [](const string &s) { return s; };");
20626   verifyFormat("int i = aaaaaa ? 1 //\n"
20627                "               : [] {\n"
20628                "                   return 2; //\n"
20629                "                 }();");
20630   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20631                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20632                "                  return x == 2; // force break\n"
20633                "                });");
20634   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20635                "    [=](int iiiiiiiiiiii) {\n"
20636                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20637                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20638                "    });",
20639                getLLVMStyleWithColumns(60));
20640 
20641   verifyFormat("SomeFunction({[&] {\n"
20642                "                // comment\n"
20643                "              },\n"
20644                "              [&] {\n"
20645                "                // comment\n"
20646                "              }});");
20647   verifyFormat("SomeFunction({[&] {\n"
20648                "  // comment\n"
20649                "}});");
20650   verifyFormat(
20651       "virtual aaaaaaaaaaaaaaaa(\n"
20652       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20653       "    aaaaa aaaaaaaaa);");
20654 
20655   // Lambdas with return types.
20656   verifyFormat("int c = []() -> int { return 2; }();\n");
20657   verifyFormat("int c = []() -> int * { return 2; }();\n");
20658   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20659   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20660   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20661   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20662   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20663   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20664   verifyFormat("[a, a]() -> a<1> {};");
20665   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20666   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20667   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20668   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20669   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20670   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20671   verifyFormat("[]() -> foo<!5> { return {}; };");
20672   verifyFormat("[]() -> foo<~5> { return {}; };");
20673   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20674   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20675   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20676   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20677   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20678   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20679   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20680   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20681   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20682   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20683   verifyFormat("namespace bar {\n"
20684                "// broken:\n"
20685                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20686                "} // namespace bar");
20687   verifyFormat("namespace bar {\n"
20688                "// broken:\n"
20689                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20690                "} // namespace bar");
20691   verifyFormat("namespace bar {\n"
20692                "// broken:\n"
20693                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20694                "} // namespace bar");
20695   verifyFormat("namespace bar {\n"
20696                "// broken:\n"
20697                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20698                "} // namespace bar");
20699   verifyFormat("namespace bar {\n"
20700                "// broken:\n"
20701                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20702                "} // namespace bar");
20703   verifyFormat("namespace bar {\n"
20704                "// broken:\n"
20705                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20706                "} // namespace bar");
20707   verifyFormat("namespace bar {\n"
20708                "// broken:\n"
20709                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20710                "} // namespace bar");
20711   verifyFormat("namespace bar {\n"
20712                "// broken:\n"
20713                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20714                "} // namespace bar");
20715   verifyFormat("namespace bar {\n"
20716                "// broken:\n"
20717                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20718                "} // namespace bar");
20719   verifyFormat("namespace bar {\n"
20720                "// broken:\n"
20721                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20722                "} // namespace bar");
20723   verifyFormat("namespace bar {\n"
20724                "// broken:\n"
20725                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20726                "} // namespace bar");
20727   verifyFormat("namespace bar {\n"
20728                "// broken:\n"
20729                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20730                "} // namespace bar");
20731   verifyFormat("namespace bar {\n"
20732                "// broken:\n"
20733                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20734                "} // namespace bar");
20735   verifyFormat("namespace bar {\n"
20736                "// broken:\n"
20737                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20738                "} // namespace bar");
20739   verifyFormat("namespace bar {\n"
20740                "// broken:\n"
20741                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20742                "} // namespace bar");
20743   verifyFormat("namespace bar {\n"
20744                "// broken:\n"
20745                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20746                "} // namespace bar");
20747   verifyFormat("namespace bar {\n"
20748                "// broken:\n"
20749                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20750                "} // namespace bar");
20751   verifyFormat("namespace bar {\n"
20752                "// broken:\n"
20753                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20754                "} // namespace bar");
20755   verifyFormat("[]() -> a<1> {};");
20756   verifyFormat("[]() -> a<1> { ; };");
20757   verifyFormat("[]() -> a<1> { ; }();");
20758   verifyFormat("[a, a]() -> a<true> {};");
20759   verifyFormat("[]() -> a<true> {};");
20760   verifyFormat("[]() -> a<true> { ; };");
20761   verifyFormat("[]() -> a<true> { ; }();");
20762   verifyFormat("[a, a]() -> a<false> {};");
20763   verifyFormat("[]() -> a<false> {};");
20764   verifyFormat("[]() -> a<false> { ; };");
20765   verifyFormat("[]() -> a<false> { ; }();");
20766   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20767   verifyFormat("namespace bar {\n"
20768                "auto foo{[]() -> foo<false> { ; }};\n"
20769                "} // namespace bar");
20770   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20771                "                   int j) -> int {\n"
20772                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20773                "};");
20774   verifyFormat(
20775       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20776       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20777       "      return aaaaaaaaaaaaaaaaa;\n"
20778       "    });",
20779       getLLVMStyleWithColumns(70));
20780   verifyFormat("[]() //\n"
20781                "    -> int {\n"
20782                "  return 1; //\n"
20783                "};");
20784   verifyFormat("[]() -> Void<T...> {};");
20785   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20786   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20787   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20788   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20789   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20790   verifyFormat("return int{[x = x]() { return x; }()};");
20791 
20792   // Lambdas with explicit template argument lists.
20793   verifyFormat(
20794       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20795   verifyFormat("auto L = []<class T>(T) {\n"
20796                "  {\n"
20797                "    f();\n"
20798                "    g();\n"
20799                "  }\n"
20800                "};\n");
20801   verifyFormat("auto L = []<class... T>(T...) {\n"
20802                "  {\n"
20803                "    f();\n"
20804                "    g();\n"
20805                "  }\n"
20806                "};\n");
20807   verifyFormat("auto L = []<typename... T>(T...) {\n"
20808                "  {\n"
20809                "    f();\n"
20810                "    g();\n"
20811                "  }\n"
20812                "};\n");
20813   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
20814                "  {\n"
20815                "    f();\n"
20816                "    g();\n"
20817                "  }\n"
20818                "};\n");
20819   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
20820                "  {\n"
20821                "    f();\n"
20822                "    g();\n"
20823                "  }\n"
20824                "};\n");
20825 
20826   // Multiple lambdas in the same parentheses change indentation rules. These
20827   // lambdas are forced to start on new lines.
20828   verifyFormat("SomeFunction(\n"
20829                "    []() {\n"
20830                "      //\n"
20831                "    },\n"
20832                "    []() {\n"
20833                "      //\n"
20834                "    });");
20835 
20836   // A lambda passed as arg0 is always pushed to the next line.
20837   verifyFormat("SomeFunction(\n"
20838                "    [this] {\n"
20839                "      //\n"
20840                "    },\n"
20841                "    1);\n");
20842 
20843   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20844   // the arg0 case above.
20845   auto Style = getGoogleStyle();
20846   Style.BinPackArguments = false;
20847   verifyFormat("SomeFunction(\n"
20848                "    a,\n"
20849                "    [this] {\n"
20850                "      //\n"
20851                "    },\n"
20852                "    b);\n",
20853                Style);
20854   verifyFormat("SomeFunction(\n"
20855                "    a,\n"
20856                "    [this] {\n"
20857                "      //\n"
20858                "    },\n"
20859                "    b);\n");
20860 
20861   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20862   // the BinPackArguments value (as long as the code is wide enough).
20863   verifyFormat(
20864       "something->SomeFunction(\n"
20865       "    a,\n"
20866       "    [this] {\n"
20867       "      "
20868       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20869       "    },\n"
20870       "    b);\n");
20871 
20872   // A multi-line lambda is pulled up as long as the introducer fits on the
20873   // previous line and there are no further args.
20874   verifyFormat("function(1, [this, that] {\n"
20875                "  //\n"
20876                "});\n");
20877   verifyFormat("function([this, that] {\n"
20878                "  //\n"
20879                "});\n");
20880   // FIXME: this format is not ideal and we should consider forcing the first
20881   // arg onto its own line.
20882   verifyFormat("function(a, b, c, //\n"
20883                "         d, [this, that] {\n"
20884                "           //\n"
20885                "         });\n");
20886 
20887   // Multiple lambdas are treated correctly even when there is a short arg0.
20888   verifyFormat("SomeFunction(\n"
20889                "    1,\n"
20890                "    [this] {\n"
20891                "      //\n"
20892                "    },\n"
20893                "    [this] {\n"
20894                "      //\n"
20895                "    },\n"
20896                "    1);\n");
20897 
20898   // More complex introducers.
20899   verifyFormat("return [i, args...] {};");
20900 
20901   // Not lambdas.
20902   verifyFormat("constexpr char hello[]{\"hello\"};");
20903   verifyFormat("double &operator[](int i) { return 0; }\n"
20904                "int i;");
20905   verifyFormat("std::unique_ptr<int[]> foo() {}");
20906   verifyFormat("int i = a[a][a]->f();");
20907   verifyFormat("int i = (*b)[a]->f();");
20908 
20909   // Other corner cases.
20910   verifyFormat("void f() {\n"
20911                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20912                "  );\n"
20913                "}");
20914 
20915   // Lambdas created through weird macros.
20916   verifyFormat("void f() {\n"
20917                "  MACRO((const AA &a) { return 1; });\n"
20918                "  MACRO((AA &a) { return 1; });\n"
20919                "}");
20920 
20921   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20922                "      doo_dah();\n"
20923                "      doo_dah();\n"
20924                "    })) {\n"
20925                "}");
20926   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20927                "                doo_dah();\n"
20928                "                doo_dah();\n"
20929                "              })) {\n"
20930                "}");
20931   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20932                "                doo_dah();\n"
20933                "                doo_dah();\n"
20934                "              })) {\n"
20935                "}");
20936   verifyFormat("auto lambda = []() {\n"
20937                "  int a = 2\n"
20938                "#if A\n"
20939                "          + 2\n"
20940                "#endif\n"
20941                "      ;\n"
20942                "};");
20943 
20944   // Lambdas with complex multiline introducers.
20945   verifyFormat(
20946       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20947       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20948       "        -> ::std::unordered_set<\n"
20949       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20950       "      //\n"
20951       "    });");
20952 
20953   FormatStyle DoNotMerge = getLLVMStyle();
20954   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20955   verifyFormat("auto c = []() {\n"
20956                "  return b;\n"
20957                "};",
20958                "auto c = []() { return b; };", DoNotMerge);
20959   verifyFormat("auto c = []() {\n"
20960                "};",
20961                " auto c = []() {};", DoNotMerge);
20962 
20963   FormatStyle MergeEmptyOnly = getLLVMStyle();
20964   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20965   verifyFormat("auto c = []() {\n"
20966                "  return b;\n"
20967                "};",
20968                "auto c = []() {\n"
20969                "  return b;\n"
20970                " };",
20971                MergeEmptyOnly);
20972   verifyFormat("auto c = []() {};",
20973                "auto c = []() {\n"
20974                "};",
20975                MergeEmptyOnly);
20976 
20977   FormatStyle MergeInline = getLLVMStyle();
20978   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20979   verifyFormat("auto c = []() {\n"
20980                "  return b;\n"
20981                "};",
20982                "auto c = []() { return b; };", MergeInline);
20983   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20984                MergeInline);
20985   verifyFormat("function([]() { return b; }, a)",
20986                "function([]() { return b; }, a)", MergeInline);
20987   verifyFormat("function(a, []() { return b; })",
20988                "function(a, []() { return b; })", MergeInline);
20989 
20990   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20991   // AllowShortLambdasOnASingleLine
20992   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20993   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20994   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20995   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20996       FormatStyle::ShortLambdaStyle::SLS_None;
20997   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20998                "    []()\n"
20999                "    {\n"
21000                "      return 17;\n"
21001                "    });",
21002                LLVMWithBeforeLambdaBody);
21003   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
21004                "    []()\n"
21005                "    {\n"
21006                "    });",
21007                LLVMWithBeforeLambdaBody);
21008   verifyFormat("auto fct_SLS_None = []()\n"
21009                "{\n"
21010                "  return 17;\n"
21011                "};",
21012                LLVMWithBeforeLambdaBody);
21013   verifyFormat("TwoNestedLambdas_SLS_None(\n"
21014                "    []()\n"
21015                "    {\n"
21016                "      return Call(\n"
21017                "          []()\n"
21018                "          {\n"
21019                "            return 17;\n"
21020                "          });\n"
21021                "    });",
21022                LLVMWithBeforeLambdaBody);
21023   verifyFormat("void Fct() {\n"
21024                "  return {[]()\n"
21025                "          {\n"
21026                "            return 17;\n"
21027                "          }};\n"
21028                "}",
21029                LLVMWithBeforeLambdaBody);
21030 
21031   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21032       FormatStyle::ShortLambdaStyle::SLS_Empty;
21033   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
21034                "    []()\n"
21035                "    {\n"
21036                "      return 17;\n"
21037                "    });",
21038                LLVMWithBeforeLambdaBody);
21039   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
21040                LLVMWithBeforeLambdaBody);
21041   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
21042                "ongFunctionName_SLS_Empty(\n"
21043                "    []() {});",
21044                LLVMWithBeforeLambdaBody);
21045   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
21046                "                                []()\n"
21047                "                                {\n"
21048                "                                  return 17;\n"
21049                "                                });",
21050                LLVMWithBeforeLambdaBody);
21051   verifyFormat("auto fct_SLS_Empty = []()\n"
21052                "{\n"
21053                "  return 17;\n"
21054                "};",
21055                LLVMWithBeforeLambdaBody);
21056   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
21057                "    []()\n"
21058                "    {\n"
21059                "      return Call([]() {});\n"
21060                "    });",
21061                LLVMWithBeforeLambdaBody);
21062   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
21063                "                           []()\n"
21064                "                           {\n"
21065                "                             return Call([]() {});\n"
21066                "                           });",
21067                LLVMWithBeforeLambdaBody);
21068   verifyFormat(
21069       "FctWithLongLineInLambda_SLS_Empty(\n"
21070       "    []()\n"
21071       "    {\n"
21072       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21073       "                               AndShouldNotBeConsiderAsInline,\n"
21074       "                               LambdaBodyMustBeBreak);\n"
21075       "    });",
21076       LLVMWithBeforeLambdaBody);
21077 
21078   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21079       FormatStyle::ShortLambdaStyle::SLS_Inline;
21080   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
21081                LLVMWithBeforeLambdaBody);
21082   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
21083                LLVMWithBeforeLambdaBody);
21084   verifyFormat("auto fct_SLS_Inline = []()\n"
21085                "{\n"
21086                "  return 17;\n"
21087                "};",
21088                LLVMWithBeforeLambdaBody);
21089   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
21090                "17; }); });",
21091                LLVMWithBeforeLambdaBody);
21092   verifyFormat(
21093       "FctWithLongLineInLambda_SLS_Inline(\n"
21094       "    []()\n"
21095       "    {\n"
21096       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21097       "                               AndShouldNotBeConsiderAsInline,\n"
21098       "                               LambdaBodyMustBeBreak);\n"
21099       "    });",
21100       LLVMWithBeforeLambdaBody);
21101   verifyFormat("FctWithMultipleParams_SLS_Inline("
21102                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21103                "                                 []() { return 17; });",
21104                LLVMWithBeforeLambdaBody);
21105   verifyFormat(
21106       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21107       LLVMWithBeforeLambdaBody);
21108 
21109   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21110       FormatStyle::ShortLambdaStyle::SLS_All;
21111   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21112                LLVMWithBeforeLambdaBody);
21113   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21114                LLVMWithBeforeLambdaBody);
21115   verifyFormat("auto fct_SLS_All = []() { return 17; };",
21116                LLVMWithBeforeLambdaBody);
21117   verifyFormat("FctWithOneParam_SLS_All(\n"
21118                "    []()\n"
21119                "    {\n"
21120                "      // A cool function...\n"
21121                "      return 43;\n"
21122                "    });",
21123                LLVMWithBeforeLambdaBody);
21124   verifyFormat("FctWithMultipleParams_SLS_All("
21125                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21126                "                              []() { return 17; });",
21127                LLVMWithBeforeLambdaBody);
21128   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21129                LLVMWithBeforeLambdaBody);
21130   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21131                LLVMWithBeforeLambdaBody);
21132   verifyFormat(
21133       "FctWithLongLineInLambda_SLS_All(\n"
21134       "    []()\n"
21135       "    {\n"
21136       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21137       "                               AndShouldNotBeConsiderAsInline,\n"
21138       "                               LambdaBodyMustBeBreak);\n"
21139       "    });",
21140       LLVMWithBeforeLambdaBody);
21141   verifyFormat(
21142       "auto fct_SLS_All = []()\n"
21143       "{\n"
21144       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21145       "                           AndShouldNotBeConsiderAsInline,\n"
21146       "                           LambdaBodyMustBeBreak);\n"
21147       "};",
21148       LLVMWithBeforeLambdaBody);
21149   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21150   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21151                LLVMWithBeforeLambdaBody);
21152   verifyFormat(
21153       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21154       "                                FirstParam,\n"
21155       "                                SecondParam,\n"
21156       "                                ThirdParam,\n"
21157       "                                FourthParam);",
21158       LLVMWithBeforeLambdaBody);
21159   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21160                "    []() { return "
21161                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21162                "    FirstParam,\n"
21163                "    SecondParam,\n"
21164                "    ThirdParam,\n"
21165                "    FourthParam);",
21166                LLVMWithBeforeLambdaBody);
21167   verifyFormat(
21168       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21169       "                                SecondParam,\n"
21170       "                                ThirdParam,\n"
21171       "                                FourthParam,\n"
21172       "                                []() { return SomeValueNotSoLong; });",
21173       LLVMWithBeforeLambdaBody);
21174   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21175                "    []()\n"
21176                "    {\n"
21177                "      return "
21178                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21179                "eConsiderAsInline;\n"
21180                "    });",
21181                LLVMWithBeforeLambdaBody);
21182   verifyFormat(
21183       "FctWithLongLineInLambda_SLS_All(\n"
21184       "    []()\n"
21185       "    {\n"
21186       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21187       "                               AndShouldNotBeConsiderAsInline,\n"
21188       "                               LambdaBodyMustBeBreak);\n"
21189       "    });",
21190       LLVMWithBeforeLambdaBody);
21191   verifyFormat("FctWithTwoParams_SLS_All(\n"
21192                "    []()\n"
21193                "    {\n"
21194                "      // A cool function...\n"
21195                "      return 43;\n"
21196                "    },\n"
21197                "    87);",
21198                LLVMWithBeforeLambdaBody);
21199   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21200                LLVMWithBeforeLambdaBody);
21201   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21202                LLVMWithBeforeLambdaBody);
21203   verifyFormat(
21204       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21205       LLVMWithBeforeLambdaBody);
21206   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21207                "}); }, x);",
21208                LLVMWithBeforeLambdaBody);
21209   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21210                "    []()\n"
21211                "    {\n"
21212                "      // A cool function...\n"
21213                "      return Call([]() { return 17; });\n"
21214                "    });",
21215                LLVMWithBeforeLambdaBody);
21216   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21217                "    []()\n"
21218                "    {\n"
21219                "      return Call(\n"
21220                "          []()\n"
21221                "          {\n"
21222                "            // A cool function...\n"
21223                "            return 17;\n"
21224                "          });\n"
21225                "    });",
21226                LLVMWithBeforeLambdaBody);
21227 
21228   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21229       FormatStyle::ShortLambdaStyle::SLS_None;
21230 
21231   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21232                "{\n"
21233                "  return MyAssignment::SelectFromList(this);\n"
21234                "};\n",
21235                LLVMWithBeforeLambdaBody);
21236 
21237   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21238                "{\n"
21239                "  return MyAssignment::SelectFromList(this);\n"
21240                "};\n",
21241                LLVMWithBeforeLambdaBody);
21242 
21243   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21244                "{\n"
21245                "  return MyAssignment::SelectFromList(this);\n"
21246                "};\n",
21247                LLVMWithBeforeLambdaBody);
21248 
21249   verifyFormat("namespace test {\n"
21250                "class Test {\n"
21251                "public:\n"
21252                "  Test() = default;\n"
21253                "};\n"
21254                "} // namespace test",
21255                LLVMWithBeforeLambdaBody);
21256 
21257   // Lambdas with different indentation styles.
21258   Style = getLLVMStyleWithColumns(100);
21259   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21260             "  return promise.then(\n"
21261             "      [this, &someVariable, someObject = "
21262             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21263             "        return someObject.startAsyncAction().then(\n"
21264             "            [this, &someVariable](AsyncActionResult result) "
21265             "mutable { result.processMore(); });\n"
21266             "      });\n"
21267             "}\n",
21268             format("SomeResult doSomething(SomeObject promise) {\n"
21269                    "  return promise.then([this, &someVariable, someObject = "
21270                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21271                    "    return someObject.startAsyncAction().then([this, "
21272                    "&someVariable](AsyncActionResult result) mutable {\n"
21273                    "      result.processMore();\n"
21274                    "    });\n"
21275                    "  });\n"
21276                    "}\n",
21277                    Style));
21278   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21279   verifyFormat("test() {\n"
21280                "  ([]() -> {\n"
21281                "    int b = 32;\n"
21282                "    return 3;\n"
21283                "  }).foo();\n"
21284                "}",
21285                Style);
21286   verifyFormat("test() {\n"
21287                "  []() -> {\n"
21288                "    int b = 32;\n"
21289                "    return 3;\n"
21290                "  }\n"
21291                "}",
21292                Style);
21293   verifyFormat("std::sort(v.begin(), v.end(),\n"
21294                "          [](const auto &someLongArgumentName, const auto "
21295                "&someOtherLongArgumentName) {\n"
21296                "  return someLongArgumentName.someMemberVariable < "
21297                "someOtherLongArgumentName.someMemberVariable;\n"
21298                "});",
21299                Style);
21300   verifyFormat("test() {\n"
21301                "  (\n"
21302                "      []() -> {\n"
21303                "        int b = 32;\n"
21304                "        return 3;\n"
21305                "      },\n"
21306                "      foo, bar)\n"
21307                "      .foo();\n"
21308                "}",
21309                Style);
21310   verifyFormat("test() {\n"
21311                "  ([]() -> {\n"
21312                "    int b = 32;\n"
21313                "    return 3;\n"
21314                "  })\n"
21315                "      .foo()\n"
21316                "      .bar();\n"
21317                "}",
21318                Style);
21319   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21320             "  return promise.then(\n"
21321             "      [this, &someVariable, someObject = "
21322             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21323             "    return someObject.startAsyncAction().then(\n"
21324             "        [this, &someVariable](AsyncActionResult result) mutable { "
21325             "result.processMore(); });\n"
21326             "  });\n"
21327             "}\n",
21328             format("SomeResult doSomething(SomeObject promise) {\n"
21329                    "  return promise.then([this, &someVariable, someObject = "
21330                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21331                    "    return someObject.startAsyncAction().then([this, "
21332                    "&someVariable](AsyncActionResult result) mutable {\n"
21333                    "      result.processMore();\n"
21334                    "    });\n"
21335                    "  });\n"
21336                    "}\n",
21337                    Style));
21338   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21339             "  return promise.then([this, &someVariable] {\n"
21340             "    return someObject.startAsyncAction().then(\n"
21341             "        [this, &someVariable](AsyncActionResult result) mutable { "
21342             "result.processMore(); });\n"
21343             "  });\n"
21344             "}\n",
21345             format("SomeResult doSomething(SomeObject promise) {\n"
21346                    "  return promise.then([this, &someVariable] {\n"
21347                    "    return someObject.startAsyncAction().then([this, "
21348                    "&someVariable](AsyncActionResult result) mutable {\n"
21349                    "      result.processMore();\n"
21350                    "    });\n"
21351                    "  });\n"
21352                    "}\n",
21353                    Style));
21354   Style = getGoogleStyle();
21355   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21356   EXPECT_EQ("#define A                                       \\\n"
21357             "  [] {                                          \\\n"
21358             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21359             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21360             "      }",
21361             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21362                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21363                    Style));
21364   // TODO: The current formatting has a minor issue that's not worth fixing
21365   // right now whereby the closing brace is indented relative to the signature
21366   // instead of being aligned. This only happens with macros.
21367 }
21368 
21369 TEST_F(FormatTest, LambdaWithLineComments) {
21370   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21371   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21372   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21373   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21374       FormatStyle::ShortLambdaStyle::SLS_All;
21375 
21376   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21377   verifyFormat("auto k = []() // comment\n"
21378                "{ return; }",
21379                LLVMWithBeforeLambdaBody);
21380   verifyFormat("auto k = []() /* comment */ { return; }",
21381                LLVMWithBeforeLambdaBody);
21382   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21383                LLVMWithBeforeLambdaBody);
21384   verifyFormat("auto k = []() // X\n"
21385                "{ return; }",
21386                LLVMWithBeforeLambdaBody);
21387   verifyFormat(
21388       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21389       "{ return; }",
21390       LLVMWithBeforeLambdaBody);
21391 }
21392 
21393 TEST_F(FormatTest, EmptyLinesInLambdas) {
21394   verifyFormat("auto lambda = []() {\n"
21395                "  x(); //\n"
21396                "};",
21397                "auto lambda = []() {\n"
21398                "\n"
21399                "  x(); //\n"
21400                "\n"
21401                "};");
21402 }
21403 
21404 TEST_F(FormatTest, FormatsBlocks) {
21405   FormatStyle ShortBlocks = getLLVMStyle();
21406   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21407   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21408   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21409   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21410   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21411   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21412   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21413 
21414   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21415   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21416   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21417 
21418   verifyFormat("[operation setCompletionBlock:^{\n"
21419                "  [self onOperationDone];\n"
21420                "}];");
21421   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21422                "  [self onOperationDone];\n"
21423                "}]};");
21424   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21425                "  f();\n"
21426                "}];");
21427   verifyFormat("int a = [operation block:^int(int *i) {\n"
21428                "  return 1;\n"
21429                "}];");
21430   verifyFormat("[myObject doSomethingWith:arg1\n"
21431                "                      aaa:^int(int *a) {\n"
21432                "                        return 1;\n"
21433                "                      }\n"
21434                "                      bbb:f(a * bbbbbbbb)];");
21435 
21436   verifyFormat("[operation setCompletionBlock:^{\n"
21437                "  [self.delegate newDataAvailable];\n"
21438                "}];",
21439                getLLVMStyleWithColumns(60));
21440   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21441                "  NSString *path = [self sessionFilePath];\n"
21442                "  if (path) {\n"
21443                "    // ...\n"
21444                "  }\n"
21445                "});");
21446   verifyFormat("[[SessionService sharedService]\n"
21447                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21448                "      if (window) {\n"
21449                "        [self windowDidLoad:window];\n"
21450                "      } else {\n"
21451                "        [self errorLoadingWindow];\n"
21452                "      }\n"
21453                "    }];");
21454   verifyFormat("void (^largeBlock)(void) = ^{\n"
21455                "  // ...\n"
21456                "};\n",
21457                getLLVMStyleWithColumns(40));
21458   verifyFormat("[[SessionService sharedService]\n"
21459                "    loadWindowWithCompletionBlock: //\n"
21460                "        ^(SessionWindow *window) {\n"
21461                "          if (window) {\n"
21462                "            [self windowDidLoad:window];\n"
21463                "          } else {\n"
21464                "            [self errorLoadingWindow];\n"
21465                "          }\n"
21466                "        }];",
21467                getLLVMStyleWithColumns(60));
21468   verifyFormat("[myObject doSomethingWith:arg1\n"
21469                "    firstBlock:^(Foo *a) {\n"
21470                "      // ...\n"
21471                "      int i;\n"
21472                "    }\n"
21473                "    secondBlock:^(Bar *b) {\n"
21474                "      // ...\n"
21475                "      int i;\n"
21476                "    }\n"
21477                "    thirdBlock:^Foo(Bar *b) {\n"
21478                "      // ...\n"
21479                "      int i;\n"
21480                "    }];");
21481   verifyFormat("[myObject doSomethingWith:arg1\n"
21482                "               firstBlock:-1\n"
21483                "              secondBlock:^(Bar *b) {\n"
21484                "                // ...\n"
21485                "                int i;\n"
21486                "              }];");
21487 
21488   verifyFormat("f(^{\n"
21489                "  @autoreleasepool {\n"
21490                "    if (a) {\n"
21491                "      g();\n"
21492                "    }\n"
21493                "  }\n"
21494                "});");
21495   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21496   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21497                "};");
21498 
21499   FormatStyle FourIndent = getLLVMStyle();
21500   FourIndent.ObjCBlockIndentWidth = 4;
21501   verifyFormat("[operation setCompletionBlock:^{\n"
21502                "    [self onOperationDone];\n"
21503                "}];",
21504                FourIndent);
21505 }
21506 
21507 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21508   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21509 
21510   verifyFormat("[[SessionService sharedService] "
21511                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21512                "  if (window) {\n"
21513                "    [self windowDidLoad:window];\n"
21514                "  } else {\n"
21515                "    [self errorLoadingWindow];\n"
21516                "  }\n"
21517                "}];",
21518                ZeroColumn);
21519   EXPECT_EQ("[[SessionService sharedService]\n"
21520             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21521             "      if (window) {\n"
21522             "        [self windowDidLoad:window];\n"
21523             "      } else {\n"
21524             "        [self errorLoadingWindow];\n"
21525             "      }\n"
21526             "    }];",
21527             format("[[SessionService sharedService]\n"
21528                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21529                    "                if (window) {\n"
21530                    "    [self windowDidLoad:window];\n"
21531                    "  } else {\n"
21532                    "    [self errorLoadingWindow];\n"
21533                    "  }\n"
21534                    "}];",
21535                    ZeroColumn));
21536   verifyFormat("[myObject doSomethingWith:arg1\n"
21537                "    firstBlock:^(Foo *a) {\n"
21538                "      // ...\n"
21539                "      int i;\n"
21540                "    }\n"
21541                "    secondBlock:^(Bar *b) {\n"
21542                "      // ...\n"
21543                "      int i;\n"
21544                "    }\n"
21545                "    thirdBlock:^Foo(Bar *b) {\n"
21546                "      // ...\n"
21547                "      int i;\n"
21548                "    }];",
21549                ZeroColumn);
21550   verifyFormat("f(^{\n"
21551                "  @autoreleasepool {\n"
21552                "    if (a) {\n"
21553                "      g();\n"
21554                "    }\n"
21555                "  }\n"
21556                "});",
21557                ZeroColumn);
21558   verifyFormat("void (^largeBlock)(void) = ^{\n"
21559                "  // ...\n"
21560                "};",
21561                ZeroColumn);
21562 
21563   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21564   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21565             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21566   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21567   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21568             "  int i;\n"
21569             "};",
21570             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21571 }
21572 
21573 TEST_F(FormatTest, SupportsCRLF) {
21574   EXPECT_EQ("int a;\r\n"
21575             "int b;\r\n"
21576             "int c;\r\n",
21577             format("int a;\r\n"
21578                    "  int b;\r\n"
21579                    "    int c;\r\n",
21580                    getLLVMStyle()));
21581   EXPECT_EQ("int a;\r\n"
21582             "int b;\r\n"
21583             "int c;\r\n",
21584             format("int a;\r\n"
21585                    "  int b;\n"
21586                    "    int c;\r\n",
21587                    getLLVMStyle()));
21588   EXPECT_EQ("int a;\n"
21589             "int b;\n"
21590             "int c;\n",
21591             format("int a;\r\n"
21592                    "  int b;\n"
21593                    "    int c;\n",
21594                    getLLVMStyle()));
21595   EXPECT_EQ("\"aaaaaaa \"\r\n"
21596             "\"bbbbbbb\";\r\n",
21597             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21598   EXPECT_EQ("#define A \\\r\n"
21599             "  b;      \\\r\n"
21600             "  c;      \\\r\n"
21601             "  d;\r\n",
21602             format("#define A \\\r\n"
21603                    "  b; \\\r\n"
21604                    "  c; d; \r\n",
21605                    getGoogleStyle()));
21606 
21607   EXPECT_EQ("/*\r\n"
21608             "multi line block comments\r\n"
21609             "should not introduce\r\n"
21610             "an extra carriage return\r\n"
21611             "*/\r\n",
21612             format("/*\r\n"
21613                    "multi line block comments\r\n"
21614                    "should not introduce\r\n"
21615                    "an extra carriage return\r\n"
21616                    "*/\r\n"));
21617   EXPECT_EQ("/*\r\n"
21618             "\r\n"
21619             "*/",
21620             format("/*\r\n"
21621                    "    \r\r\r\n"
21622                    "*/"));
21623 
21624   FormatStyle style = getLLVMStyle();
21625 
21626   style.DeriveLineEnding = true;
21627   style.UseCRLF = false;
21628   EXPECT_EQ("union FooBarBazQux {\n"
21629             "  int foo;\n"
21630             "  int bar;\n"
21631             "  int baz;\n"
21632             "};",
21633             format("union FooBarBazQux {\r\n"
21634                    "  int foo;\n"
21635                    "  int bar;\r\n"
21636                    "  int baz;\n"
21637                    "};",
21638                    style));
21639   style.UseCRLF = true;
21640   EXPECT_EQ("union FooBarBazQux {\r\n"
21641             "  int foo;\r\n"
21642             "  int bar;\r\n"
21643             "  int baz;\r\n"
21644             "};",
21645             format("union FooBarBazQux {\r\n"
21646                    "  int foo;\n"
21647                    "  int bar;\r\n"
21648                    "  int baz;\n"
21649                    "};",
21650                    style));
21651 
21652   style.DeriveLineEnding = false;
21653   style.UseCRLF = false;
21654   EXPECT_EQ("union FooBarBazQux {\n"
21655             "  int foo;\n"
21656             "  int bar;\n"
21657             "  int baz;\n"
21658             "  int qux;\n"
21659             "};",
21660             format("union FooBarBazQux {\r\n"
21661                    "  int foo;\n"
21662                    "  int bar;\r\n"
21663                    "  int baz;\n"
21664                    "  int qux;\r\n"
21665                    "};",
21666                    style));
21667   style.UseCRLF = true;
21668   EXPECT_EQ("union FooBarBazQux {\r\n"
21669             "  int foo;\r\n"
21670             "  int bar;\r\n"
21671             "  int baz;\r\n"
21672             "  int qux;\r\n"
21673             "};",
21674             format("union FooBarBazQux {\r\n"
21675                    "  int foo;\n"
21676                    "  int bar;\r\n"
21677                    "  int baz;\n"
21678                    "  int qux;\n"
21679                    "};",
21680                    style));
21681 
21682   style.DeriveLineEnding = true;
21683   style.UseCRLF = false;
21684   EXPECT_EQ("union FooBarBazQux {\r\n"
21685             "  int foo;\r\n"
21686             "  int bar;\r\n"
21687             "  int baz;\r\n"
21688             "  int qux;\r\n"
21689             "};",
21690             format("union FooBarBazQux {\r\n"
21691                    "  int foo;\n"
21692                    "  int bar;\r\n"
21693                    "  int baz;\n"
21694                    "  int qux;\r\n"
21695                    "};",
21696                    style));
21697   style.UseCRLF = true;
21698   EXPECT_EQ("union FooBarBazQux {\n"
21699             "  int foo;\n"
21700             "  int bar;\n"
21701             "  int baz;\n"
21702             "  int qux;\n"
21703             "};",
21704             format("union FooBarBazQux {\r\n"
21705                    "  int foo;\n"
21706                    "  int bar;\r\n"
21707                    "  int baz;\n"
21708                    "  int qux;\n"
21709                    "};",
21710                    style));
21711 }
21712 
21713 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21714   verifyFormat("MY_CLASS(C) {\n"
21715                "  int i;\n"
21716                "  int j;\n"
21717                "};");
21718 }
21719 
21720 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21721   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21722   TwoIndent.ContinuationIndentWidth = 2;
21723 
21724   EXPECT_EQ("int i =\n"
21725             "  longFunction(\n"
21726             "    arg);",
21727             format("int i = longFunction(arg);", TwoIndent));
21728 
21729   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21730   SixIndent.ContinuationIndentWidth = 6;
21731 
21732   EXPECT_EQ("int i =\n"
21733             "      longFunction(\n"
21734             "            arg);",
21735             format("int i = longFunction(arg);", SixIndent));
21736 }
21737 
21738 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21739   FormatStyle Style = getLLVMStyle();
21740   verifyFormat("int Foo::getter(\n"
21741                "    //\n"
21742                ") const {\n"
21743                "  return foo;\n"
21744                "}",
21745                Style);
21746   verifyFormat("void Foo::setter(\n"
21747                "    //\n"
21748                ") {\n"
21749                "  foo = 1;\n"
21750                "}",
21751                Style);
21752 }
21753 
21754 TEST_F(FormatTest, SpacesInAngles) {
21755   FormatStyle Spaces = getLLVMStyle();
21756   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21757 
21758   verifyFormat("vector< ::std::string > x1;", Spaces);
21759   verifyFormat("Foo< int, Bar > x2;", Spaces);
21760   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21761 
21762   verifyFormat("static_cast< int >(arg);", Spaces);
21763   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21764   verifyFormat("f< int, float >();", Spaces);
21765   verifyFormat("template <> g() {}", Spaces);
21766   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21767   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21768   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21769                Spaces);
21770 
21771   Spaces.Standard = FormatStyle::LS_Cpp03;
21772   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21773   verifyFormat("A< A< int > >();", Spaces);
21774 
21775   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21776   verifyFormat("A<A<int> >();", Spaces);
21777 
21778   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21779   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21780                Spaces);
21781   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21782                Spaces);
21783 
21784   verifyFormat("A<A<int> >();", Spaces);
21785   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21786   verifyFormat("A< A< int > >();", Spaces);
21787 
21788   Spaces.Standard = FormatStyle::LS_Cpp11;
21789   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21790   verifyFormat("A< A< int > >();", Spaces);
21791 
21792   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21793   verifyFormat("vector<::std::string> x4;", Spaces);
21794   verifyFormat("vector<int> x5;", Spaces);
21795   verifyFormat("Foo<int, Bar> x6;", Spaces);
21796   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21797 
21798   verifyFormat("A<A<int>>();", Spaces);
21799 
21800   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21801   verifyFormat("vector<::std::string> x4;", Spaces);
21802   verifyFormat("vector< ::std::string > x4;", Spaces);
21803   verifyFormat("vector<int> x5;", Spaces);
21804   verifyFormat("vector< int > x5;", Spaces);
21805   verifyFormat("Foo<int, Bar> x6;", Spaces);
21806   verifyFormat("Foo< int, Bar > x6;", Spaces);
21807   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21808   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21809 
21810   verifyFormat("A<A<int>>();", Spaces);
21811   verifyFormat("A< A< int > >();", Spaces);
21812   verifyFormat("A<A<int > >();", Spaces);
21813   verifyFormat("A< A< int>>();", Spaces);
21814 
21815   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21816   verifyFormat("// clang-format off\n"
21817                "foo<<<1, 1>>>();\n"
21818                "// clang-format on\n",
21819                Spaces);
21820   verifyFormat("// clang-format off\n"
21821                "foo< < <1, 1> > >();\n"
21822                "// clang-format on\n",
21823                Spaces);
21824 }
21825 
21826 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21827   FormatStyle Style = getLLVMStyle();
21828   Style.SpaceAfterTemplateKeyword = false;
21829   verifyFormat("template<int> void foo();", Style);
21830 }
21831 
21832 TEST_F(FormatTest, TripleAngleBrackets) {
21833   verifyFormat("f<<<1, 1>>>();");
21834   verifyFormat("f<<<1, 1, 1, s>>>();");
21835   verifyFormat("f<<<a, b, c, d>>>();");
21836   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21837   verifyFormat("f<param><<<1, 1>>>();");
21838   verifyFormat("f<1><<<1, 1>>>();");
21839   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21840   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21841                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21842   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21843                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21844 }
21845 
21846 TEST_F(FormatTest, MergeLessLessAtEnd) {
21847   verifyFormat("<<");
21848   EXPECT_EQ("< < <", format("\\\n<<<"));
21849   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21850                "aaallvm::outs() <<");
21851   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21852                "aaaallvm::outs()\n    <<");
21853 }
21854 
21855 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21856   std::string code = "#if A\n"
21857                      "#if B\n"
21858                      "a.\n"
21859                      "#endif\n"
21860                      "    a = 1;\n"
21861                      "#else\n"
21862                      "#endif\n"
21863                      "#if C\n"
21864                      "#else\n"
21865                      "#endif\n";
21866   EXPECT_EQ(code, format(code));
21867 }
21868 
21869 TEST_F(FormatTest, HandleConflictMarkers) {
21870   // Git/SVN conflict markers.
21871   EXPECT_EQ("int a;\n"
21872             "void f() {\n"
21873             "  callme(some(parameter1,\n"
21874             "<<<<<<< text by the vcs\n"
21875             "              parameter2),\n"
21876             "||||||| text by the vcs\n"
21877             "              parameter2),\n"
21878             "         parameter3,\n"
21879             "======= text by the vcs\n"
21880             "              parameter2, parameter3),\n"
21881             ">>>>>>> text by the vcs\n"
21882             "         otherparameter);\n",
21883             format("int a;\n"
21884                    "void f() {\n"
21885                    "  callme(some(parameter1,\n"
21886                    "<<<<<<< text by the vcs\n"
21887                    "  parameter2),\n"
21888                    "||||||| text by the vcs\n"
21889                    "  parameter2),\n"
21890                    "  parameter3,\n"
21891                    "======= text by the vcs\n"
21892                    "  parameter2,\n"
21893                    "  parameter3),\n"
21894                    ">>>>>>> text by the vcs\n"
21895                    "  otherparameter);\n"));
21896 
21897   // Perforce markers.
21898   EXPECT_EQ("void f() {\n"
21899             "  function(\n"
21900             ">>>> text by the vcs\n"
21901             "      parameter,\n"
21902             "==== text by the vcs\n"
21903             "      parameter,\n"
21904             "==== text by the vcs\n"
21905             "      parameter,\n"
21906             "<<<< text by the vcs\n"
21907             "      parameter);\n",
21908             format("void f() {\n"
21909                    "  function(\n"
21910                    ">>>> text by the vcs\n"
21911                    "  parameter,\n"
21912                    "==== text by the vcs\n"
21913                    "  parameter,\n"
21914                    "==== text by the vcs\n"
21915                    "  parameter,\n"
21916                    "<<<< text by the vcs\n"
21917                    "  parameter);\n"));
21918 
21919   EXPECT_EQ("<<<<<<<\n"
21920             "|||||||\n"
21921             "=======\n"
21922             ">>>>>>>",
21923             format("<<<<<<<\n"
21924                    "|||||||\n"
21925                    "=======\n"
21926                    ">>>>>>>"));
21927 
21928   EXPECT_EQ("<<<<<<<\n"
21929             "|||||||\n"
21930             "int i;\n"
21931             "=======\n"
21932             ">>>>>>>",
21933             format("<<<<<<<\n"
21934                    "|||||||\n"
21935                    "int i;\n"
21936                    "=======\n"
21937                    ">>>>>>>"));
21938 
21939   // FIXME: Handle parsing of macros around conflict markers correctly:
21940   EXPECT_EQ("#define Macro \\\n"
21941             "<<<<<<<\n"
21942             "Something \\\n"
21943             "|||||||\n"
21944             "Else \\\n"
21945             "=======\n"
21946             "Other \\\n"
21947             ">>>>>>>\n"
21948             "    End int i;\n",
21949             format("#define Macro \\\n"
21950                    "<<<<<<<\n"
21951                    "  Something \\\n"
21952                    "|||||||\n"
21953                    "  Else \\\n"
21954                    "=======\n"
21955                    "  Other \\\n"
21956                    ">>>>>>>\n"
21957                    "  End\n"
21958                    "int i;\n"));
21959 
21960   verifyFormat(R"(====
21961 #ifdef A
21962 a
21963 #else
21964 b
21965 #endif
21966 )");
21967 }
21968 
21969 TEST_F(FormatTest, DisableRegions) {
21970   EXPECT_EQ("int i;\n"
21971             "// clang-format off\n"
21972             "  int j;\n"
21973             "// clang-format on\n"
21974             "int k;",
21975             format(" int  i;\n"
21976                    "   // clang-format off\n"
21977                    "  int j;\n"
21978                    " // clang-format on\n"
21979                    "   int   k;"));
21980   EXPECT_EQ("int i;\n"
21981             "/* clang-format off */\n"
21982             "  int j;\n"
21983             "/* clang-format on */\n"
21984             "int k;",
21985             format(" int  i;\n"
21986                    "   /* clang-format off */\n"
21987                    "  int j;\n"
21988                    " /* clang-format on */\n"
21989                    "   int   k;"));
21990 
21991   // Don't reflow comments within disabled regions.
21992   EXPECT_EQ("// clang-format off\n"
21993             "// long long long long long long line\n"
21994             "/* clang-format on */\n"
21995             "/* long long long\n"
21996             " * long long long\n"
21997             " * line */\n"
21998             "int i;\n"
21999             "/* clang-format off */\n"
22000             "/* long long long long long long line */\n",
22001             format("// clang-format off\n"
22002                    "// long long long long long long line\n"
22003                    "/* clang-format on */\n"
22004                    "/* long long long long long long line */\n"
22005                    "int i;\n"
22006                    "/* clang-format off */\n"
22007                    "/* long long long long long long line */\n",
22008                    getLLVMStyleWithColumns(20)));
22009 }
22010 
22011 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
22012   format("? ) =");
22013   verifyNoCrash("#define a\\\n /**/}");
22014 }
22015 
22016 TEST_F(FormatTest, FormatsTableGenCode) {
22017   FormatStyle Style = getLLVMStyle();
22018   Style.Language = FormatStyle::LK_TableGen;
22019   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
22020 }
22021 
22022 TEST_F(FormatTest, ArrayOfTemplates) {
22023   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
22024             format("auto a = new unique_ptr<int > [ 10];"));
22025 
22026   FormatStyle Spaces = getLLVMStyle();
22027   Spaces.SpacesInSquareBrackets = true;
22028   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
22029             format("auto a = new unique_ptr<int > [10];", Spaces));
22030 }
22031 
22032 TEST_F(FormatTest, ArrayAsTemplateType) {
22033   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
22034             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
22035 
22036   FormatStyle Spaces = getLLVMStyle();
22037   Spaces.SpacesInSquareBrackets = true;
22038   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
22039             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
22040 }
22041 
22042 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
22043 
22044 TEST(FormatStyle, GetStyleWithEmptyFileName) {
22045   llvm::vfs::InMemoryFileSystem FS;
22046   auto Style1 = getStyle("file", "", "Google", "", &FS);
22047   ASSERT_TRUE((bool)Style1);
22048   ASSERT_EQ(*Style1, getGoogleStyle());
22049 }
22050 
22051 TEST(FormatStyle, GetStyleOfFile) {
22052   llvm::vfs::InMemoryFileSystem FS;
22053   // Test 1: format file in the same directory.
22054   ASSERT_TRUE(
22055       FS.addFile("/a/.clang-format", 0,
22056                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22057   ASSERT_TRUE(
22058       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22059   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
22060   ASSERT_TRUE((bool)Style1);
22061   ASSERT_EQ(*Style1, getLLVMStyle());
22062 
22063   // Test 2.1: fallback to default.
22064   ASSERT_TRUE(
22065       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22066   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
22067   ASSERT_TRUE((bool)Style2);
22068   ASSERT_EQ(*Style2, getMozillaStyle());
22069 
22070   // Test 2.2: no format on 'none' fallback style.
22071   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22072   ASSERT_TRUE((bool)Style2);
22073   ASSERT_EQ(*Style2, getNoStyle());
22074 
22075   // Test 2.3: format if config is found with no based style while fallback is
22076   // 'none'.
22077   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
22078                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
22079   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22080   ASSERT_TRUE((bool)Style2);
22081   ASSERT_EQ(*Style2, getLLVMStyle());
22082 
22083   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
22084   Style2 = getStyle("{}", "a.h", "none", "", &FS);
22085   ASSERT_TRUE((bool)Style2);
22086   ASSERT_EQ(*Style2, getLLVMStyle());
22087 
22088   // Test 3: format file in parent directory.
22089   ASSERT_TRUE(
22090       FS.addFile("/c/.clang-format", 0,
22091                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22092   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
22093                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22094   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22095   ASSERT_TRUE((bool)Style3);
22096   ASSERT_EQ(*Style3, getGoogleStyle());
22097 
22098   // Test 4: error on invalid fallback style
22099   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
22100   ASSERT_FALSE((bool)Style4);
22101   llvm::consumeError(Style4.takeError());
22102 
22103   // Test 5: error on invalid yaml on command line
22104   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
22105   ASSERT_FALSE((bool)Style5);
22106   llvm::consumeError(Style5.takeError());
22107 
22108   // Test 6: error on invalid style
22109   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
22110   ASSERT_FALSE((bool)Style6);
22111   llvm::consumeError(Style6.takeError());
22112 
22113   // Test 7: found config file, error on parsing it
22114   ASSERT_TRUE(
22115       FS.addFile("/d/.clang-format", 0,
22116                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
22117                                                   "InvalidKey: InvalidValue")));
22118   ASSERT_TRUE(
22119       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22120   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
22121   ASSERT_FALSE((bool)Style7a);
22122   llvm::consumeError(Style7a.takeError());
22123 
22124   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
22125   ASSERT_TRUE((bool)Style7b);
22126 
22127   // Test 8: inferred per-language defaults apply.
22128   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
22129   ASSERT_TRUE((bool)StyleTd);
22130   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
22131 
22132   // Test 9.1.1: overwriting a file style, when no parent file exists with no
22133   // fallback style.
22134   ASSERT_TRUE(FS.addFile(
22135       "/e/sub/.clang-format", 0,
22136       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
22137                                        "ColumnLimit: 20")));
22138   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
22139                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22140   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22141   ASSERT_TRUE(static_cast<bool>(Style9));
22142   ASSERT_EQ(*Style9, [] {
22143     auto Style = getNoStyle();
22144     Style.ColumnLimit = 20;
22145     return Style;
22146   }());
22147 
22148   // Test 9.1.2: propagate more than one level with no parent file.
22149   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22150                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22151   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22152                          llvm::MemoryBuffer::getMemBuffer(
22153                              "BasedOnStyle: InheritParentConfig\n"
22154                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22155   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22156 
22157   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22158   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22159   ASSERT_TRUE(static_cast<bool>(Style9));
22160   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22161     auto Style = getNoStyle();
22162     Style.ColumnLimit = 20;
22163     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22164     return Style;
22165   }());
22166 
22167   // Test 9.2: with LLVM fallback style
22168   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22169   ASSERT_TRUE(static_cast<bool>(Style9));
22170   ASSERT_EQ(*Style9, [] {
22171     auto Style = getLLVMStyle();
22172     Style.ColumnLimit = 20;
22173     return Style;
22174   }());
22175 
22176   // Test 9.3: with a parent file
22177   ASSERT_TRUE(
22178       FS.addFile("/e/.clang-format", 0,
22179                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22180                                                   "UseTab: Always")));
22181   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22182   ASSERT_TRUE(static_cast<bool>(Style9));
22183   ASSERT_EQ(*Style9, [] {
22184     auto Style = getGoogleStyle();
22185     Style.ColumnLimit = 20;
22186     Style.UseTab = FormatStyle::UT_Always;
22187     return Style;
22188   }());
22189 
22190   // Test 9.4: propagate more than one level with a parent file.
22191   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22192     auto Style = getGoogleStyle();
22193     Style.ColumnLimit = 20;
22194     Style.UseTab = FormatStyle::UT_Always;
22195     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22196     return Style;
22197   }();
22198 
22199   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22200   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22201   ASSERT_TRUE(static_cast<bool>(Style9));
22202   ASSERT_EQ(*Style9, SubSubStyle);
22203 
22204   // Test 9.5: use InheritParentConfig as style name
22205   Style9 =
22206       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22207   ASSERT_TRUE(static_cast<bool>(Style9));
22208   ASSERT_EQ(*Style9, SubSubStyle);
22209 
22210   // Test 9.6: use command line style with inheritance
22211   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22212                     "none", "", &FS);
22213   ASSERT_TRUE(static_cast<bool>(Style9));
22214   ASSERT_EQ(*Style9, SubSubStyle);
22215 
22216   // Test 9.7: use command line style with inheritance and own config
22217   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22218                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22219                     "/e/sub/code.cpp", "none", "", &FS);
22220   ASSERT_TRUE(static_cast<bool>(Style9));
22221   ASSERT_EQ(*Style9, SubSubStyle);
22222 
22223   // Test 9.8: use inheritance from a file without BasedOnStyle
22224   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22225                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22226   ASSERT_TRUE(
22227       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22228                  llvm::MemoryBuffer::getMemBuffer(
22229                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22230   // Make sure we do not use the fallback style
22231   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22232   ASSERT_TRUE(static_cast<bool>(Style9));
22233   ASSERT_EQ(*Style9, [] {
22234     auto Style = getLLVMStyle();
22235     Style.ColumnLimit = 123;
22236     return Style;
22237   }());
22238 
22239   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22240   ASSERT_TRUE(static_cast<bool>(Style9));
22241   ASSERT_EQ(*Style9, [] {
22242     auto Style = getLLVMStyle();
22243     Style.ColumnLimit = 123;
22244     Style.IndentWidth = 7;
22245     return Style;
22246   }());
22247 
22248   // Test 9.9: use inheritance from a specific config file.
22249   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22250                     "none", "", &FS);
22251   ASSERT_TRUE(static_cast<bool>(Style9));
22252   ASSERT_EQ(*Style9, SubSubStyle);
22253 }
22254 
22255 TEST(FormatStyle, GetStyleOfSpecificFile) {
22256   llvm::vfs::InMemoryFileSystem FS;
22257   // Specify absolute path to a format file in a parent directory.
22258   ASSERT_TRUE(
22259       FS.addFile("/e/.clang-format", 0,
22260                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22261   ASSERT_TRUE(
22262       FS.addFile("/e/explicit.clang-format", 0,
22263                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22264   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22265                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22266   auto Style = getStyle("file:/e/explicit.clang-format",
22267                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22268   ASSERT_TRUE(static_cast<bool>(Style));
22269   ASSERT_EQ(*Style, getGoogleStyle());
22270 
22271   // Specify relative path to a format file.
22272   ASSERT_TRUE(
22273       FS.addFile("../../e/explicit.clang-format", 0,
22274                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22275   Style = getStyle("file:../../e/explicit.clang-format",
22276                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22277   ASSERT_TRUE(static_cast<bool>(Style));
22278   ASSERT_EQ(*Style, getGoogleStyle());
22279 
22280   // Specify path to a format file that does not exist.
22281   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22282                    "LLVM", "", &FS);
22283   ASSERT_FALSE(static_cast<bool>(Style));
22284   llvm::consumeError(Style.takeError());
22285 
22286   // Specify path to a file on the filesystem.
22287   SmallString<128> FormatFilePath;
22288   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22289       "FormatFileTest", "tpl", FormatFilePath);
22290   EXPECT_FALSE((bool)ECF);
22291   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22292   EXPECT_FALSE((bool)ECF);
22293   FormatFileTest << "BasedOnStyle: Google\n";
22294   FormatFileTest.close();
22295 
22296   SmallString<128> TestFilePath;
22297   std::error_code ECT =
22298       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22299   EXPECT_FALSE((bool)ECT);
22300   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22301   CodeFileTest << "int i;\n";
22302   CodeFileTest.close();
22303 
22304   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22305   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22306 
22307   llvm::sys::fs::remove(FormatFilePath.c_str());
22308   llvm::sys::fs::remove(TestFilePath.c_str());
22309   ASSERT_TRUE(static_cast<bool>(Style));
22310   ASSERT_EQ(*Style, getGoogleStyle());
22311 }
22312 
22313 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22314   // Column limit is 20.
22315   std::string Code = "Type *a =\n"
22316                      "    new Type();\n"
22317                      "g(iiiii, 0, jjjjj,\n"
22318                      "  0, kkkkk, 0, mm);\n"
22319                      "int  bad     = format   ;";
22320   std::string Expected = "auto a = new Type();\n"
22321                          "g(iiiii, nullptr,\n"
22322                          "  jjjjj, nullptr,\n"
22323                          "  kkkkk, nullptr,\n"
22324                          "  mm);\n"
22325                          "int  bad     = format   ;";
22326   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22327   tooling::Replacements Replaces = toReplacements(
22328       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22329                             "auto "),
22330        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22331                             "nullptr"),
22332        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22333                             "nullptr"),
22334        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22335                             "nullptr")});
22336 
22337   FormatStyle Style = getLLVMStyle();
22338   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22339   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22340   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22341       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22342   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22343   EXPECT_TRUE(static_cast<bool>(Result));
22344   EXPECT_EQ(Expected, *Result);
22345 }
22346 
22347 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22348   std::string Code = "#include \"a.h\"\n"
22349                      "#include \"c.h\"\n"
22350                      "\n"
22351                      "int main() {\n"
22352                      "  return 0;\n"
22353                      "}";
22354   std::string Expected = "#include \"a.h\"\n"
22355                          "#include \"b.h\"\n"
22356                          "#include \"c.h\"\n"
22357                          "\n"
22358                          "int main() {\n"
22359                          "  return 0;\n"
22360                          "}";
22361   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22362   tooling::Replacements Replaces = toReplacements(
22363       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22364                             "#include \"b.h\"\n")});
22365 
22366   FormatStyle Style = getLLVMStyle();
22367   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22368   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22369   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22370       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22371   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22372   EXPECT_TRUE(static_cast<bool>(Result));
22373   EXPECT_EQ(Expected, *Result);
22374 }
22375 
22376 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22377   EXPECT_EQ("using std::cin;\n"
22378             "using std::cout;",
22379             format("using std::cout;\n"
22380                    "using std::cin;",
22381                    getGoogleStyle()));
22382 }
22383 
22384 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22385   FormatStyle Style = getLLVMStyle();
22386   Style.Standard = FormatStyle::LS_Cpp03;
22387   // cpp03 recognize this string as identifier u8 and literal character 'a'
22388   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22389 }
22390 
22391 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22392   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22393   // all modes, including C++11, C++14 and C++17
22394   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22395 }
22396 
22397 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22398   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22399   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22400 }
22401 
22402 TEST_F(FormatTest, StructuredBindings) {
22403   // Structured bindings is a C++17 feature.
22404   // all modes, including C++11, C++14 and C++17
22405   verifyFormat("auto [a, b] = f();");
22406   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22407   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22408   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22409   EXPECT_EQ("auto const volatile [a, b] = f();",
22410             format("auto  const   volatile[a, b] = f();"));
22411   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22412   EXPECT_EQ("auto &[a, b, c] = f();",
22413             format("auto   &[  a  ,  b,c   ] = f();"));
22414   EXPECT_EQ("auto &&[a, b, c] = f();",
22415             format("auto   &&[  a  ,  b,c   ] = f();"));
22416   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22417   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22418             format("auto  const  volatile  &&[a, b] = f();"));
22419   EXPECT_EQ("auto const &&[a, b] = f();",
22420             format("auto  const   &&  [a, b] = f();"));
22421   EXPECT_EQ("const auto &[a, b] = f();",
22422             format("const  auto  &  [a, b] = f();"));
22423   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22424             format("const  auto   volatile  &&[a, b] = f();"));
22425   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22426             format("volatile  const  auto   &&[a, b] = f();"));
22427   EXPECT_EQ("const auto &&[a, b] = f();",
22428             format("const  auto  &&  [a, b] = f();"));
22429 
22430   // Make sure we don't mistake structured bindings for lambdas.
22431   FormatStyle PointerMiddle = getLLVMStyle();
22432   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22433   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22434   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22435   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22436   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22437   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22438   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22439   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22440   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22441   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22442   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22443   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22444   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22445 
22446   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22447             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22448   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22449             format("for (const auto   &   [a, b] : some_range) {\n}"));
22450   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22451             format("for (const auto[a, b] : some_range) {\n}"));
22452   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22453   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22454   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22455   EXPECT_EQ("auto const &[x, y](expr);",
22456             format("auto  const  &  [x,y]  (expr);"));
22457   EXPECT_EQ("auto const &&[x, y](expr);",
22458             format("auto  const  &&  [x,y]  (expr);"));
22459   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22460   EXPECT_EQ("auto const &[x, y]{expr};",
22461             format("auto  const  &  [x,y]  {expr};"));
22462   EXPECT_EQ("auto const &&[x, y]{expr};",
22463             format("auto  const  &&  [x,y]  {expr};"));
22464 
22465   FormatStyle Spaces = getLLVMStyle();
22466   Spaces.SpacesInSquareBrackets = true;
22467   verifyFormat("auto [ a, b ] = f();", Spaces);
22468   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22469   verifyFormat("auto &[ a, b ] = f();", Spaces);
22470   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22471   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22472 }
22473 
22474 TEST_F(FormatTest, FileAndCode) {
22475   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22476   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22477   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22478   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22479   EXPECT_EQ(FormatStyle::LK_ObjC,
22480             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22481   EXPECT_EQ(
22482       FormatStyle::LK_ObjC,
22483       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22484   EXPECT_EQ(FormatStyle::LK_ObjC,
22485             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22486   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22487   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22488   EXPECT_EQ(FormatStyle::LK_ObjC,
22489             guessLanguage("foo", "@interface Foo\n@end\n"));
22490   EXPECT_EQ(FormatStyle::LK_ObjC,
22491             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22492   EXPECT_EQ(
22493       FormatStyle::LK_ObjC,
22494       guessLanguage("foo.h",
22495                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22496   EXPECT_EQ(
22497       FormatStyle::LK_Cpp,
22498       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22499 }
22500 
22501 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22502   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22503   EXPECT_EQ(FormatStyle::LK_ObjC,
22504             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22505   EXPECT_EQ(FormatStyle::LK_Cpp,
22506             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22507   EXPECT_EQ(
22508       FormatStyle::LK_Cpp,
22509       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22510   EXPECT_EQ(FormatStyle::LK_ObjC,
22511             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22512   EXPECT_EQ(FormatStyle::LK_Cpp,
22513             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22514   EXPECT_EQ(FormatStyle::LK_ObjC,
22515             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22516   EXPECT_EQ(FormatStyle::LK_Cpp,
22517             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22518   EXPECT_EQ(FormatStyle::LK_Cpp,
22519             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22520   EXPECT_EQ(FormatStyle::LK_ObjC,
22521             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22522   EXPECT_EQ(FormatStyle::LK_Cpp,
22523             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22524   EXPECT_EQ(
22525       FormatStyle::LK_Cpp,
22526       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22527   EXPECT_EQ(
22528       FormatStyle::LK_Cpp,
22529       guessLanguage("foo.h",
22530                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22531   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22532 }
22533 
22534 TEST_F(FormatTest, GuessLanguageWithCaret) {
22535   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22536   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22537   EXPECT_EQ(FormatStyle::LK_ObjC,
22538             guessLanguage("foo.h", "int(^)(char, float);"));
22539   EXPECT_EQ(FormatStyle::LK_ObjC,
22540             guessLanguage("foo.h", "int(^foo)(char, float);"));
22541   EXPECT_EQ(FormatStyle::LK_ObjC,
22542             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22543   EXPECT_EQ(FormatStyle::LK_ObjC,
22544             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22545   EXPECT_EQ(
22546       FormatStyle::LK_ObjC,
22547       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22548 }
22549 
22550 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22551   EXPECT_EQ(FormatStyle::LK_Cpp,
22552             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22553   EXPECT_EQ(FormatStyle::LK_Cpp,
22554             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22555   EXPECT_EQ(FormatStyle::LK_Cpp,
22556             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22557 }
22558 
22559 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22560   // ASM symbolic names are identifiers that must be surrounded by [] without
22561   // space in between:
22562   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22563 
22564   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22565   verifyFormat(R"(//
22566 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22567 )");
22568 
22569   // A list of several ASM symbolic names.
22570   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22571 
22572   // ASM symbolic names in inline ASM with inputs and outputs.
22573   verifyFormat(R"(//
22574 asm("cmoveq %1, %2, %[result]"
22575     : [result] "=r"(result)
22576     : "r"(test), "r"(new), "[result]"(old));
22577 )");
22578 
22579   // ASM symbolic names in inline ASM with no outputs.
22580   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22581 }
22582 
22583 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22584   EXPECT_EQ(FormatStyle::LK_Cpp,
22585             guessLanguage("foo.h", "void f() {\n"
22586                                    "  asm (\"mov %[e], %[d]\"\n"
22587                                    "     : [d] \"=rm\" (d)\n"
22588                                    "       [e] \"rm\" (*e));\n"
22589                                    "}"));
22590   EXPECT_EQ(FormatStyle::LK_Cpp,
22591             guessLanguage("foo.h", "void f() {\n"
22592                                    "  _asm (\"mov %[e], %[d]\"\n"
22593                                    "     : [d] \"=rm\" (d)\n"
22594                                    "       [e] \"rm\" (*e));\n"
22595                                    "}"));
22596   EXPECT_EQ(FormatStyle::LK_Cpp,
22597             guessLanguage("foo.h", "void f() {\n"
22598                                    "  __asm (\"mov %[e], %[d]\"\n"
22599                                    "     : [d] \"=rm\" (d)\n"
22600                                    "       [e] \"rm\" (*e));\n"
22601                                    "}"));
22602   EXPECT_EQ(FormatStyle::LK_Cpp,
22603             guessLanguage("foo.h", "void f() {\n"
22604                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22605                                    "     : [d] \"=rm\" (d)\n"
22606                                    "       [e] \"rm\" (*e));\n"
22607                                    "}"));
22608   EXPECT_EQ(FormatStyle::LK_Cpp,
22609             guessLanguage("foo.h", "void f() {\n"
22610                                    "  asm (\"mov %[e], %[d]\"\n"
22611                                    "     : [d] \"=rm\" (d),\n"
22612                                    "       [e] \"rm\" (*e));\n"
22613                                    "}"));
22614   EXPECT_EQ(FormatStyle::LK_Cpp,
22615             guessLanguage("foo.h", "void f() {\n"
22616                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22617                                    "     : [d] \"=rm\" (d)\n"
22618                                    "       [e] \"rm\" (*e));\n"
22619                                    "}"));
22620 }
22621 
22622 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22623   EXPECT_EQ(FormatStyle::LK_Cpp,
22624             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22625   EXPECT_EQ(FormatStyle::LK_ObjC,
22626             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22627   EXPECT_EQ(
22628       FormatStyle::LK_Cpp,
22629       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22630   EXPECT_EQ(
22631       FormatStyle::LK_ObjC,
22632       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22633 }
22634 
22635 TEST_F(FormatTest, TypenameMacros) {
22636   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22637 
22638   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22639   FormatStyle Google = getGoogleStyleWithColumns(0);
22640   Google.TypenameMacros = TypenameMacros;
22641   verifyFormat("struct foo {\n"
22642                "  int bar;\n"
22643                "  TAILQ_ENTRY(a) bleh;\n"
22644                "};",
22645                Google);
22646 
22647   FormatStyle Macros = getLLVMStyle();
22648   Macros.TypenameMacros = TypenameMacros;
22649 
22650   verifyFormat("STACK_OF(int) a;", Macros);
22651   verifyFormat("STACK_OF(int) *a;", Macros);
22652   verifyFormat("STACK_OF(int const *) *a;", Macros);
22653   verifyFormat("STACK_OF(int *const) *a;", Macros);
22654   verifyFormat("STACK_OF(int, string) a;", Macros);
22655   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22656   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22657   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22658   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22659   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22660   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22661 
22662   Macros.PointerAlignment = FormatStyle::PAS_Left;
22663   verifyFormat("STACK_OF(int)* a;", Macros);
22664   verifyFormat("STACK_OF(int*)* a;", Macros);
22665   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22666   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22667   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22668 }
22669 
22670 TEST_F(FormatTest, AtomicQualifier) {
22671   // Check that we treate _Atomic as a type and not a function call
22672   FormatStyle Google = getGoogleStyleWithColumns(0);
22673   verifyFormat("struct foo {\n"
22674                "  int a1;\n"
22675                "  _Atomic(a) a2;\n"
22676                "  _Atomic(_Atomic(int) *const) a3;\n"
22677                "};",
22678                Google);
22679   verifyFormat("_Atomic(uint64_t) a;");
22680   verifyFormat("_Atomic(uint64_t) *a;");
22681   verifyFormat("_Atomic(uint64_t const *) *a;");
22682   verifyFormat("_Atomic(uint64_t *const) *a;");
22683   verifyFormat("_Atomic(const uint64_t *) *a;");
22684   verifyFormat("_Atomic(uint64_t) a;");
22685   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22686   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22687   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22688   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22689 
22690   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22691   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22692   FormatStyle Style = getLLVMStyle();
22693   Style.PointerAlignment = FormatStyle::PAS_Left;
22694   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22695   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22696   verifyFormat("_Atomic(int)* a;", Style);
22697   verifyFormat("_Atomic(int*)* a;", Style);
22698   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22699 
22700   Style.SpacesInCStyleCastParentheses = true;
22701   Style.SpacesInParentheses = false;
22702   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22703   Style.SpacesInCStyleCastParentheses = false;
22704   Style.SpacesInParentheses = true;
22705   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22706   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22707 }
22708 
22709 TEST_F(FormatTest, AmbersandInLamda) {
22710   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22711   FormatStyle AlignStyle = getLLVMStyle();
22712   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22713   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22714   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22715   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22716 }
22717 
22718 TEST_F(FormatTest, SpacesInConditionalStatement) {
22719   FormatStyle Spaces = getLLVMStyle();
22720   Spaces.IfMacros.clear();
22721   Spaces.IfMacros.push_back("MYIF");
22722   Spaces.SpacesInConditionalStatement = true;
22723   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22724   verifyFormat("if ( !a )\n  return;", Spaces);
22725   verifyFormat("if ( a )\n  return;", Spaces);
22726   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22727   verifyFormat("MYIF ( a )\n  return;", Spaces);
22728   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22729   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22730   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22731   verifyFormat("while ( a )\n  return;", Spaces);
22732   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22733   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22734   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22735   // Check that space on the left of "::" is inserted as expected at beginning
22736   // of condition.
22737   verifyFormat("while ( ::func() )\n  return;", Spaces);
22738 
22739   // Check impact of ControlStatementsExceptControlMacros is honored.
22740   Spaces.SpaceBeforeParens =
22741       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22742   verifyFormat("MYIF( a )\n  return;", Spaces);
22743   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22744   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22745 }
22746 
22747 TEST_F(FormatTest, AlternativeOperators) {
22748   // Test case for ensuring alternate operators are not
22749   // combined with their right most neighbour.
22750   verifyFormat("int a and b;");
22751   verifyFormat("int a and_eq b;");
22752   verifyFormat("int a bitand b;");
22753   verifyFormat("int a bitor b;");
22754   verifyFormat("int a compl b;");
22755   verifyFormat("int a not b;");
22756   verifyFormat("int a not_eq b;");
22757   verifyFormat("int a or b;");
22758   verifyFormat("int a xor b;");
22759   verifyFormat("int a xor_eq b;");
22760   verifyFormat("return this not_eq bitand other;");
22761   verifyFormat("bool operator not_eq(const X bitand other)");
22762 
22763   verifyFormat("int a and 5;");
22764   verifyFormat("int a and_eq 5;");
22765   verifyFormat("int a bitand 5;");
22766   verifyFormat("int a bitor 5;");
22767   verifyFormat("int a compl 5;");
22768   verifyFormat("int a not 5;");
22769   verifyFormat("int a not_eq 5;");
22770   verifyFormat("int a or 5;");
22771   verifyFormat("int a xor 5;");
22772   verifyFormat("int a xor_eq 5;");
22773 
22774   verifyFormat("int a compl(5);");
22775   verifyFormat("int a not(5);");
22776 
22777   /* FIXME handle alternate tokens
22778    * https://en.cppreference.com/w/cpp/language/operator_alternative
22779   // alternative tokens
22780   verifyFormat("compl foo();");     //  ~foo();
22781   verifyFormat("foo() <%%>;");      // foo();
22782   verifyFormat("void foo() <%%>;"); // void foo(){}
22783   verifyFormat("int a <:1:>;");     // int a[1];[
22784   verifyFormat("%:define ABC abc"); // #define ABC abc
22785   verifyFormat("%:%:");             // ##
22786   */
22787 }
22788 
22789 TEST_F(FormatTest, STLWhileNotDefineChed) {
22790   verifyFormat("#if defined(while)\n"
22791                "#define while EMIT WARNING C4005\n"
22792                "#endif // while");
22793 }
22794 
22795 TEST_F(FormatTest, OperatorSpacing) {
22796   FormatStyle Style = getLLVMStyle();
22797   Style.PointerAlignment = FormatStyle::PAS_Right;
22798   verifyFormat("Foo::operator*();", Style);
22799   verifyFormat("Foo::operator void *();", Style);
22800   verifyFormat("Foo::operator void **();", Style);
22801   verifyFormat("Foo::operator void *&();", Style);
22802   verifyFormat("Foo::operator void *&&();", Style);
22803   verifyFormat("Foo::operator void const *();", Style);
22804   verifyFormat("Foo::operator void const **();", Style);
22805   verifyFormat("Foo::operator void const *&();", Style);
22806   verifyFormat("Foo::operator void const *&&();", Style);
22807   verifyFormat("Foo::operator()(void *);", Style);
22808   verifyFormat("Foo::operator*(void *);", Style);
22809   verifyFormat("Foo::operator*();", Style);
22810   verifyFormat("Foo::operator**();", Style);
22811   verifyFormat("Foo::operator&();", Style);
22812   verifyFormat("Foo::operator<int> *();", Style);
22813   verifyFormat("Foo::operator<Foo> *();", Style);
22814   verifyFormat("Foo::operator<int> **();", Style);
22815   verifyFormat("Foo::operator<Foo> **();", Style);
22816   verifyFormat("Foo::operator<int> &();", Style);
22817   verifyFormat("Foo::operator<Foo> &();", Style);
22818   verifyFormat("Foo::operator<int> &&();", Style);
22819   verifyFormat("Foo::operator<Foo> &&();", Style);
22820   verifyFormat("Foo::operator<int> *&();", Style);
22821   verifyFormat("Foo::operator<Foo> *&();", Style);
22822   verifyFormat("Foo::operator<int> *&&();", Style);
22823   verifyFormat("Foo::operator<Foo> *&&();", Style);
22824   verifyFormat("operator*(int (*)(), class Foo);", Style);
22825 
22826   verifyFormat("Foo::operator&();", Style);
22827   verifyFormat("Foo::operator void &();", Style);
22828   verifyFormat("Foo::operator void const &();", Style);
22829   verifyFormat("Foo::operator()(void &);", Style);
22830   verifyFormat("Foo::operator&(void &);", Style);
22831   verifyFormat("Foo::operator&();", Style);
22832   verifyFormat("operator&(int (&)(), class Foo);", Style);
22833   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22834 
22835   verifyFormat("Foo::operator&&();", Style);
22836   verifyFormat("Foo::operator**();", Style);
22837   verifyFormat("Foo::operator void &&();", Style);
22838   verifyFormat("Foo::operator void const &&();", Style);
22839   verifyFormat("Foo::operator()(void &&);", Style);
22840   verifyFormat("Foo::operator&&(void &&);", Style);
22841   verifyFormat("Foo::operator&&();", Style);
22842   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22843   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22844   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22845                Style);
22846   verifyFormat("operator void **()", Style);
22847   verifyFormat("operator const FooRight<Object> &()", Style);
22848   verifyFormat("operator const FooRight<Object> *()", Style);
22849   verifyFormat("operator const FooRight<Object> **()", Style);
22850   verifyFormat("operator const FooRight<Object> *&()", Style);
22851   verifyFormat("operator const FooRight<Object> *&&()", Style);
22852 
22853   Style.PointerAlignment = FormatStyle::PAS_Left;
22854   verifyFormat("Foo::operator*();", Style);
22855   verifyFormat("Foo::operator**();", Style);
22856   verifyFormat("Foo::operator void*();", Style);
22857   verifyFormat("Foo::operator void**();", Style);
22858   verifyFormat("Foo::operator void*&();", Style);
22859   verifyFormat("Foo::operator void*&&();", Style);
22860   verifyFormat("Foo::operator void const*();", Style);
22861   verifyFormat("Foo::operator void const**();", Style);
22862   verifyFormat("Foo::operator void const*&();", Style);
22863   verifyFormat("Foo::operator void const*&&();", Style);
22864   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22865   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22866   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22867   verifyFormat("Foo::operator()(void*);", Style);
22868   verifyFormat("Foo::operator*(void*);", Style);
22869   verifyFormat("Foo::operator*();", Style);
22870   verifyFormat("Foo::operator<int>*();", Style);
22871   verifyFormat("Foo::operator<Foo>*();", Style);
22872   verifyFormat("Foo::operator<int>**();", Style);
22873   verifyFormat("Foo::operator<Foo>**();", Style);
22874   verifyFormat("Foo::operator<Foo>*&();", Style);
22875   verifyFormat("Foo::operator<int>&();", Style);
22876   verifyFormat("Foo::operator<Foo>&();", Style);
22877   verifyFormat("Foo::operator<int>&&();", Style);
22878   verifyFormat("Foo::operator<Foo>&&();", Style);
22879   verifyFormat("Foo::operator<int>*&();", Style);
22880   verifyFormat("Foo::operator<Foo>*&();", Style);
22881   verifyFormat("operator*(int (*)(), class Foo);", Style);
22882 
22883   verifyFormat("Foo::operator&();", Style);
22884   verifyFormat("Foo::operator void&();", Style);
22885   verifyFormat("Foo::operator void const&();", Style);
22886   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22887   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22888   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22889   verifyFormat("Foo::operator()(void&);", Style);
22890   verifyFormat("Foo::operator&(void&);", Style);
22891   verifyFormat("Foo::operator&();", Style);
22892   verifyFormat("operator&(int (&)(), class Foo);", Style);
22893   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22894   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22895 
22896   verifyFormat("Foo::operator&&();", Style);
22897   verifyFormat("Foo::operator void&&();", Style);
22898   verifyFormat("Foo::operator void const&&();", Style);
22899   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22900   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22901   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22902   verifyFormat("Foo::operator()(void&&);", Style);
22903   verifyFormat("Foo::operator&&(void&&);", Style);
22904   verifyFormat("Foo::operator&&();", Style);
22905   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22906   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22907   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22908                Style);
22909   verifyFormat("operator void**()", Style);
22910   verifyFormat("operator const FooLeft<Object>&()", Style);
22911   verifyFormat("operator const FooLeft<Object>*()", Style);
22912   verifyFormat("operator const FooLeft<Object>**()", Style);
22913   verifyFormat("operator const FooLeft<Object>*&()", Style);
22914   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22915 
22916   // PR45107
22917   verifyFormat("operator Vector<String>&();", Style);
22918   verifyFormat("operator const Vector<String>&();", Style);
22919   verifyFormat("operator foo::Bar*();", Style);
22920   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22921   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22922                Style);
22923 
22924   Style.PointerAlignment = FormatStyle::PAS_Middle;
22925   verifyFormat("Foo::operator*();", Style);
22926   verifyFormat("Foo::operator void *();", Style);
22927   verifyFormat("Foo::operator()(void *);", Style);
22928   verifyFormat("Foo::operator*(void *);", Style);
22929   verifyFormat("Foo::operator*();", Style);
22930   verifyFormat("operator*(int (*)(), class Foo);", Style);
22931 
22932   verifyFormat("Foo::operator&();", Style);
22933   verifyFormat("Foo::operator void &();", Style);
22934   verifyFormat("Foo::operator void const &();", Style);
22935   verifyFormat("Foo::operator()(void &);", Style);
22936   verifyFormat("Foo::operator&(void &);", Style);
22937   verifyFormat("Foo::operator&();", Style);
22938   verifyFormat("operator&(int (&)(), class Foo);", Style);
22939 
22940   verifyFormat("Foo::operator&&();", Style);
22941   verifyFormat("Foo::operator void &&();", Style);
22942   verifyFormat("Foo::operator void const &&();", Style);
22943   verifyFormat("Foo::operator()(void &&);", Style);
22944   verifyFormat("Foo::operator&&(void &&);", Style);
22945   verifyFormat("Foo::operator&&();", Style);
22946   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22947 }
22948 
22949 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22950   FormatStyle Style = getLLVMStyle();
22951   // PR46157
22952   verifyFormat("foo(operator+, -42);", Style);
22953   verifyFormat("foo(operator++, -42);", Style);
22954   verifyFormat("foo(operator--, -42);", Style);
22955   verifyFormat("foo(-42, operator--);", Style);
22956   verifyFormat("foo(-42, operator, );", Style);
22957   verifyFormat("foo(operator, , -42);", Style);
22958 }
22959 
22960 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22961   FormatStyle Style = getLLVMStyle();
22962   Style.WhitespaceSensitiveMacros.push_back("FOO");
22963 
22964   // Don't use the helpers here, since 'mess up' will change the whitespace
22965   // and these are all whitespace sensitive by definition
22966   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22967             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22968   EXPECT_EQ(
22969       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22970       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22971   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22972             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22973   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22974             "       Still=Intentional);",
22975             format("FOO(String-ized&Messy+But,: :\n"
22976                    "       Still=Intentional);",
22977                    Style));
22978   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22979   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22980             "       Still=Intentional);",
22981             format("FOO(String-ized=&Messy+But,: :\n"
22982                    "       Still=Intentional);",
22983                    Style));
22984 
22985   Style.ColumnLimit = 21;
22986   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22987             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22988 }
22989 
22990 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22991   // These tests are not in NamespaceFixer because that doesn't
22992   // test its interaction with line wrapping
22993   FormatStyle Style = getLLVMStyleWithColumns(80);
22994   verifyFormat("namespace {\n"
22995                "int i;\n"
22996                "int j;\n"
22997                "} // namespace",
22998                Style);
22999 
23000   verifyFormat("namespace AAA {\n"
23001                "int i;\n"
23002                "int j;\n"
23003                "} // namespace AAA",
23004                Style);
23005 
23006   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
23007             "int i;\n"
23008             "int j;\n"
23009             "} // namespace Averyveryveryverylongnamespace",
23010             format("namespace Averyveryveryverylongnamespace {\n"
23011                    "int i;\n"
23012                    "int j;\n"
23013                    "}",
23014                    Style));
23015 
23016   EXPECT_EQ(
23017       "namespace "
23018       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23019       "    went::mad::now {\n"
23020       "int i;\n"
23021       "int j;\n"
23022       "} // namespace\n"
23023       "  // "
23024       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23025       "went::mad::now",
23026       format("namespace "
23027              "would::it::save::you::a::lot::of::time::if_::i::"
23028              "just::gave::up::and_::went::mad::now {\n"
23029              "int i;\n"
23030              "int j;\n"
23031              "}",
23032              Style));
23033 
23034   // This used to duplicate the comment again and again on subsequent runs
23035   EXPECT_EQ(
23036       "namespace "
23037       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23038       "    went::mad::now {\n"
23039       "int i;\n"
23040       "int j;\n"
23041       "} // namespace\n"
23042       "  // "
23043       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23044       "went::mad::now",
23045       format("namespace "
23046              "would::it::save::you::a::lot::of::time::if_::i::"
23047              "just::gave::up::and_::went::mad::now {\n"
23048              "int i;\n"
23049              "int j;\n"
23050              "} // namespace\n"
23051              "  // "
23052              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
23053              "and_::went::mad::now",
23054              Style));
23055 }
23056 
23057 TEST_F(FormatTest, LikelyUnlikely) {
23058   FormatStyle Style = getLLVMStyle();
23059 
23060   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23061                "  return 29;\n"
23062                "}",
23063                Style);
23064 
23065   verifyFormat("if (argc > 5) [[likely]] {\n"
23066                "  return 29;\n"
23067                "}",
23068                Style);
23069 
23070   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23071                "  return 29;\n"
23072                "} else [[likely]] {\n"
23073                "  return 42;\n"
23074                "}\n",
23075                Style);
23076 
23077   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23078                "  return 29;\n"
23079                "} else if (argc > 10) [[likely]] {\n"
23080                "  return 99;\n"
23081                "} else {\n"
23082                "  return 42;\n"
23083                "}\n",
23084                Style);
23085 
23086   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
23087                "  return 29;\n"
23088                "}",
23089                Style);
23090 
23091   verifyFormat("if (argc > 5) [[unlikely]]\n"
23092                "  return 29;\n",
23093                Style);
23094   verifyFormat("if (argc > 5) [[likely]]\n"
23095                "  return 29;\n",
23096                Style);
23097 
23098   Style.AttributeMacros.push_back("UNLIKELY");
23099   Style.AttributeMacros.push_back("LIKELY");
23100   verifyFormat("if (argc > 5) UNLIKELY\n"
23101                "  return 29;\n",
23102                Style);
23103 
23104   verifyFormat("if (argc > 5) UNLIKELY {\n"
23105                "  return 29;\n"
23106                "}",
23107                Style);
23108   verifyFormat("if (argc > 5) UNLIKELY {\n"
23109                "  return 29;\n"
23110                "} else [[likely]] {\n"
23111                "  return 42;\n"
23112                "}\n",
23113                Style);
23114   verifyFormat("if (argc > 5) UNLIKELY {\n"
23115                "  return 29;\n"
23116                "} else LIKELY {\n"
23117                "  return 42;\n"
23118                "}\n",
23119                Style);
23120   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23121                "  return 29;\n"
23122                "} else LIKELY {\n"
23123                "  return 42;\n"
23124                "}\n",
23125                Style);
23126 }
23127 
23128 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23129   verifyFormat("Constructor()\n"
23130                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23131                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
23132                "aaaaaaaaaaaaaaaaaat))");
23133   verifyFormat("Constructor()\n"
23134                "    : aaaaaaaaaaaaa(aaaaaa), "
23135                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23136 
23137   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23138   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23139   verifyFormat("Constructor()\n"
23140                "    : aaaaaa(aaaaaa),\n"
23141                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23142                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23143                StyleWithWhitespacePenalty);
23144   verifyFormat("Constructor()\n"
23145                "    : aaaaaaaaaaaaa(aaaaaa), "
23146                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23147                StyleWithWhitespacePenalty);
23148 }
23149 
23150 TEST_F(FormatTest, LLVMDefaultStyle) {
23151   FormatStyle Style = getLLVMStyle();
23152   verifyFormat("extern \"C\" {\n"
23153                "int foo();\n"
23154                "}",
23155                Style);
23156 }
23157 TEST_F(FormatTest, GNUDefaultStyle) {
23158   FormatStyle Style = getGNUStyle();
23159   verifyFormat("extern \"C\"\n"
23160                "{\n"
23161                "  int foo ();\n"
23162                "}",
23163                Style);
23164 }
23165 TEST_F(FormatTest, MozillaDefaultStyle) {
23166   FormatStyle Style = getMozillaStyle();
23167   verifyFormat("extern \"C\"\n"
23168                "{\n"
23169                "  int foo();\n"
23170                "}",
23171                Style);
23172 }
23173 TEST_F(FormatTest, GoogleDefaultStyle) {
23174   FormatStyle Style = getGoogleStyle();
23175   verifyFormat("extern \"C\" {\n"
23176                "int foo();\n"
23177                "}",
23178                Style);
23179 }
23180 TEST_F(FormatTest, ChromiumDefaultStyle) {
23181   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23182   verifyFormat("extern \"C\" {\n"
23183                "int foo();\n"
23184                "}",
23185                Style);
23186 }
23187 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23188   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23189   verifyFormat("extern \"C\"\n"
23190                "{\n"
23191                "    int foo();\n"
23192                "}",
23193                Style);
23194 }
23195 TEST_F(FormatTest, WebKitDefaultStyle) {
23196   FormatStyle Style = getWebKitStyle();
23197   verifyFormat("extern \"C\" {\n"
23198                "int foo();\n"
23199                "}",
23200                Style);
23201 }
23202 
23203 TEST_F(FormatTest, ConceptsAndRequires) {
23204   FormatStyle Style = getLLVMStyle();
23205   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23206 
23207   verifyFormat("template <typename T>\n"
23208                "concept Hashable = requires(T a) {\n"
23209                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23210                "};",
23211                Style);
23212   verifyFormat("template <typename T>\n"
23213                "concept EqualityComparable = requires(T a, T b) {\n"
23214                "  { a == b } -> bool;\n"
23215                "};",
23216                Style);
23217   verifyFormat("template <typename T>\n"
23218                "concept EqualityComparable = requires(T a, T b) {\n"
23219                "  { a == b } -> bool;\n"
23220                "  { a != b } -> bool;\n"
23221                "};",
23222                Style);
23223   verifyFormat("template <typename T>\n"
23224                "concept EqualityComparable = requires(T a, T b) {\n"
23225                "  { a == b } -> bool;\n"
23226                "  { a != b } -> bool;\n"
23227                "};",
23228                Style);
23229 
23230   verifyFormat("template <typename It>\n"
23231                "requires Iterator<It>\n"
23232                "void sort(It begin, It end) {\n"
23233                "  //....\n"
23234                "}",
23235                Style);
23236 
23237   verifyFormat("template <typename T>\n"
23238                "concept Large = sizeof(T) > 10;",
23239                Style);
23240 
23241   verifyFormat("template <typename T, typename U>\n"
23242                "concept FooableWith = requires(T t, U u) {\n"
23243                "  typename T::foo_type;\n"
23244                "  { t.foo(u) } -> typename T::foo_type;\n"
23245                "  t++;\n"
23246                "};\n"
23247                "void doFoo(FooableWith<int> auto t) {\n"
23248                "  t.foo(3);\n"
23249                "}",
23250                Style);
23251   verifyFormat("template <typename T>\n"
23252                "concept Context = sizeof(T) == 1;",
23253                Style);
23254   verifyFormat("template <typename T>\n"
23255                "concept Context = is_specialization_of_v<context, T>;",
23256                Style);
23257   verifyFormat("template <typename T>\n"
23258                "concept Node = std::is_object_v<T>;",
23259                Style);
23260   verifyFormat("template <typename T>\n"
23261                "concept Tree = true;",
23262                Style);
23263 
23264   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
23265                "  //...\n"
23266                "}",
23267                Style);
23268 
23269   verifyFormat(
23270       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
23271       "  //...\n"
23272       "}",
23273       Style);
23274 
23275   verifyFormat(
23276       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
23277       "  //...\n"
23278       "}",
23279       Style);
23280 
23281   verifyFormat("template <typename T>\n"
23282                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
23283                "Concept2<I> {\n"
23284                "  //...\n"
23285                "}",
23286                Style);
23287 
23288   verifyFormat("template <typename T>\n"
23289                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
23290                "Concept2<I> {\n"
23291                "  //...\n"
23292                "}",
23293                Style);
23294 
23295   verifyFormat(
23296       "template <typename T>\n"
23297       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
23298       "  //...\n"
23299       "}",
23300       Style);
23301 
23302   verifyFormat(
23303       "template <typename T>\n"
23304       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
23305       "  //...\n"
23306       "}",
23307       Style);
23308 
23309   verifyFormat("template <typename It>\n"
23310                "requires Foo<It>() && Bar<It> {\n"
23311                "  //....\n"
23312                "}",
23313                Style);
23314 
23315   verifyFormat("template <typename It>\n"
23316                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
23317                "  //....\n"
23318                "}",
23319                Style);
23320 
23321   verifyFormat("template <typename It>\n"
23322                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
23323                "  //....\n"
23324                "}",
23325                Style);
23326 
23327   verifyFormat(
23328       "template <typename It>\n"
23329       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
23330       "  //....\n"
23331       "}",
23332       Style);
23333 
23334   Style.IndentRequires = true;
23335   verifyFormat("template <typename It>\n"
23336                "  requires Iterator<It>\n"
23337                "void sort(It begin, It end) {\n"
23338                "  //....\n"
23339                "}",
23340                Style);
23341   verifyFormat("template <std::size index_>\n"
23342                "  requires(index_ < sizeof...(Children_))\n"
23343                "Tree auto &child() {\n"
23344                "  // ...\n"
23345                "}",
23346                Style);
23347 
23348   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
23349   verifyFormat("template <typename T>\n"
23350                "concept Hashable = requires (T a) {\n"
23351                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23352                "};",
23353                Style);
23354 
23355   verifyFormat("template <class T = void>\n"
23356                "  requires EqualityComparable<T> || Same<T, void>\n"
23357                "struct equal_to;",
23358                Style);
23359 
23360   verifyFormat("template <class T>\n"
23361                "  requires requires {\n"
23362                "    T{};\n"
23363                "    T (int);\n"
23364                "  }\n",
23365                Style);
23366 
23367   Style.ColumnLimit = 78;
23368   verifyFormat("template <typename T>\n"
23369                "concept Context = Traits<typename T::traits_type> and\n"
23370                "    Interface<typename T::interface_type> and\n"
23371                "    Request<typename T::request_type> and\n"
23372                "    Response<typename T::response_type> and\n"
23373                "    ContextExtension<typename T::extension_type> and\n"
23374                "    ::std::is_copy_constructable<T> and "
23375                "::std::is_move_constructable<T> and\n"
23376                "    requires (T c) {\n"
23377                "  { c.response; } -> Response;\n"
23378                "} and requires (T c) {\n"
23379                "  { c.request; } -> Request;\n"
23380                "}\n",
23381                Style);
23382 
23383   verifyFormat("template <typename T>\n"
23384                "concept Context = Traits<typename T::traits_type> or\n"
23385                "    Interface<typename T::interface_type> or\n"
23386                "    Request<typename T::request_type> or\n"
23387                "    Response<typename T::response_type> or\n"
23388                "    ContextExtension<typename T::extension_type> or\n"
23389                "    ::std::is_copy_constructable<T> or "
23390                "::std::is_move_constructable<T> or\n"
23391                "    requires (T c) {\n"
23392                "  { c.response; } -> Response;\n"
23393                "} or requires (T c) {\n"
23394                "  { c.request; } -> Request;\n"
23395                "}\n",
23396                Style);
23397 
23398   verifyFormat("template <typename T>\n"
23399                "concept Context = Traits<typename T::traits_type> &&\n"
23400                "    Interface<typename T::interface_type> &&\n"
23401                "    Request<typename T::request_type> &&\n"
23402                "    Response<typename T::response_type> &&\n"
23403                "    ContextExtension<typename T::extension_type> &&\n"
23404                "    ::std::is_copy_constructable<T> && "
23405                "::std::is_move_constructable<T> &&\n"
23406                "    requires (T c) {\n"
23407                "  { c.response; } -> Response;\n"
23408                "} && requires (T c) {\n"
23409                "  { c.request; } -> Request;\n"
23410                "}\n",
23411                Style);
23412 
23413   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
23414                "Constraint2<T>;");
23415 
23416   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23417   Style.BraceWrapping.AfterFunction = true;
23418   Style.BraceWrapping.AfterClass = true;
23419   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
23420   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
23421   verifyFormat("void Foo () requires (std::copyable<T>)\n"
23422                "{\n"
23423                "  return\n"
23424                "}\n",
23425                Style);
23426 
23427   verifyFormat("void Foo () requires std::copyable<T>\n"
23428                "{\n"
23429                "  return\n"
23430                "}\n",
23431                Style);
23432 
23433   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23434                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
23435                "struct constant;",
23436                Style);
23437 
23438   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23439                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
23440                "struct constant;",
23441                Style);
23442 
23443   verifyFormat("template <class T>\n"
23444                "class plane_with_very_very_very_long_name\n"
23445                "{\n"
23446                "  constexpr plane_with_very_very_very_long_name () requires "
23447                "std::copyable<T>\n"
23448                "      : plane_with_very_very_very_long_name (1)\n"
23449                "  {\n"
23450                "  }\n"
23451                "}\n",
23452                Style);
23453 
23454   verifyFormat("template <class T>\n"
23455                "class plane_with_long_name\n"
23456                "{\n"
23457                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
23458                "      : plane_with_long_name (1)\n"
23459                "  {\n"
23460                "  }\n"
23461                "}\n",
23462                Style);
23463 
23464   Style.BreakBeforeConceptDeclarations = false;
23465   verifyFormat("template <typename T> concept Tree = true;", Style);
23466 
23467   Style.IndentRequires = false;
23468   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23469                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
23470                "struct constant;",
23471                Style);
23472 }
23473 
23474 TEST_F(FormatTest, StatementAttributeLikeMacros) {
23475   FormatStyle Style = getLLVMStyle();
23476   StringRef Source = "void Foo::slot() {\n"
23477                      "  unsigned char MyChar = 'x';\n"
23478                      "  emit signal(MyChar);\n"
23479                      "  Q_EMIT signal(MyChar);\n"
23480                      "}";
23481 
23482   EXPECT_EQ(Source, format(Source, Style));
23483 
23484   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
23485   EXPECT_EQ("void Foo::slot() {\n"
23486             "  unsigned char MyChar = 'x';\n"
23487             "  emit          signal(MyChar);\n"
23488             "  Q_EMIT signal(MyChar);\n"
23489             "}",
23490             format(Source, Style));
23491 
23492   Style.StatementAttributeLikeMacros.push_back("emit");
23493   EXPECT_EQ(Source, format(Source, Style));
23494 
23495   Style.StatementAttributeLikeMacros = {};
23496   EXPECT_EQ("void Foo::slot() {\n"
23497             "  unsigned char MyChar = 'x';\n"
23498             "  emit          signal(MyChar);\n"
23499             "  Q_EMIT        signal(MyChar);\n"
23500             "}",
23501             format(Source, Style));
23502 }
23503 
23504 TEST_F(FormatTest, IndentAccessModifiers) {
23505   FormatStyle Style = getLLVMStyle();
23506   Style.IndentAccessModifiers = true;
23507   // Members are *two* levels below the record;
23508   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
23509   verifyFormat("class C {\n"
23510                "    int i;\n"
23511                "};\n",
23512                Style);
23513   verifyFormat("union C {\n"
23514                "    int i;\n"
23515                "    unsigned u;\n"
23516                "};\n",
23517                Style);
23518   // Access modifiers should be indented one level below the record.
23519   verifyFormat("class C {\n"
23520                "  public:\n"
23521                "    int i;\n"
23522                "};\n",
23523                Style);
23524   verifyFormat("struct S {\n"
23525                "  private:\n"
23526                "    class C {\n"
23527                "        int j;\n"
23528                "\n"
23529                "      public:\n"
23530                "        C();\n"
23531                "    };\n"
23532                "\n"
23533                "  public:\n"
23534                "    int i;\n"
23535                "};\n",
23536                Style);
23537   // Enumerations are not records and should be unaffected.
23538   Style.AllowShortEnumsOnASingleLine = false;
23539   verifyFormat("enum class E {\n"
23540                "  A,\n"
23541                "  B\n"
23542                "};\n",
23543                Style);
23544   // Test with a different indentation width;
23545   // also proves that the result is Style.AccessModifierOffset agnostic.
23546   Style.IndentWidth = 3;
23547   verifyFormat("class C {\n"
23548                "   public:\n"
23549                "      int i;\n"
23550                "};\n",
23551                Style);
23552 }
23553 
23554 TEST_F(FormatTest, LimitlessStringsAndComments) {
23555   auto Style = getLLVMStyleWithColumns(0);
23556   constexpr StringRef Code =
23557       "/**\n"
23558       " * This is a multiline comment with quite some long lines, at least for "
23559       "the LLVM Style.\n"
23560       " * We will redo this with strings and line comments. Just to  check if "
23561       "everything is working.\n"
23562       " */\n"
23563       "bool foo() {\n"
23564       "  /* Single line multi line comment. */\n"
23565       "  const std::string String = \"This is a multiline string with quite "
23566       "some long lines, at least for the LLVM Style.\"\n"
23567       "                             \"We already did it with multi line "
23568       "comments, and we will do it with line comments. Just to check if "
23569       "everything is working.\";\n"
23570       "  // This is a line comment (block) with quite some long lines, at "
23571       "least for the LLVM Style.\n"
23572       "  // We already did this with multi line comments and strings. Just to "
23573       "check if everything is working.\n"
23574       "  const std::string SmallString = \"Hello World\";\n"
23575       "  // Small line comment\n"
23576       "  return String.size() > SmallString.size();\n"
23577       "}";
23578   EXPECT_EQ(Code, format(Code, Style));
23579 }
23580 
23581 TEST_F(FormatTest, FormatDecayCopy) {
23582   // error cases from unit tests
23583   verifyFormat("foo(auto())");
23584   verifyFormat("foo(auto{})");
23585   verifyFormat("foo(auto({}))");
23586   verifyFormat("foo(auto{{}})");
23587 
23588   verifyFormat("foo(auto(1))");
23589   verifyFormat("foo(auto{1})");
23590   verifyFormat("foo(new auto(1))");
23591   verifyFormat("foo(new auto{1})");
23592   verifyFormat("decltype(auto(1)) x;");
23593   verifyFormat("decltype(auto{1}) x;");
23594   verifyFormat("auto(x);");
23595   verifyFormat("auto{x};");
23596   verifyFormat("new auto{x};");
23597   verifyFormat("auto{x} = y;");
23598   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23599                                 // the user's own fault
23600   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23601                                          // clearly the user's own fault
23602   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23603 }
23604 
23605 TEST_F(FormatTest, Cpp20ModulesSupport) {
23606   FormatStyle Style = getLLVMStyle();
23607   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23608   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23609 
23610   verifyFormat("export import foo;", Style);
23611   verifyFormat("export import foo:bar;", Style);
23612   verifyFormat("export import foo.bar;", Style);
23613   verifyFormat("export import foo.bar:baz;", Style);
23614   verifyFormat("export import :bar;", Style);
23615   verifyFormat("export module foo:bar;", Style);
23616   verifyFormat("export module foo;", Style);
23617   verifyFormat("export module foo.bar;", Style);
23618   verifyFormat("export module foo.bar:baz;", Style);
23619   verifyFormat("export import <string_view>;", Style);
23620 
23621   verifyFormat("export type_name var;", Style);
23622   verifyFormat("template <class T> export using A = B<T>;", Style);
23623   verifyFormat("export using A = B;", Style);
23624   verifyFormat("export int func() {\n"
23625                "  foo();\n"
23626                "}",
23627                Style);
23628   verifyFormat("export struct {\n"
23629                "  int foo;\n"
23630                "};",
23631                Style);
23632   verifyFormat("export {\n"
23633                "  int foo;\n"
23634                "};",
23635                Style);
23636   verifyFormat("export export char const *hello() { return \"hello\"; }");
23637 
23638   verifyFormat("import bar;", Style);
23639   verifyFormat("import foo.bar;", Style);
23640   verifyFormat("import foo:bar;", Style);
23641   verifyFormat("import :bar;", Style);
23642   verifyFormat("import <ctime>;", Style);
23643   verifyFormat("import \"header\";", Style);
23644 
23645   verifyFormat("module foo;", Style);
23646   verifyFormat("module foo:bar;", Style);
23647   verifyFormat("module foo.bar;", Style);
23648   verifyFormat("module;", Style);
23649 
23650   verifyFormat("export namespace hi {\n"
23651                "const char *sayhi();\n"
23652                "}",
23653                Style);
23654 
23655   verifyFormat("module :private;", Style);
23656   verifyFormat("import <foo/bar.h>;", Style);
23657   verifyFormat("import foo...bar;", Style);
23658   verifyFormat("import ..........;", Style);
23659   verifyFormat("module foo:private;", Style);
23660   verifyFormat("import a", Style);
23661   verifyFormat("module a", Style);
23662   verifyFormat("export import a", Style);
23663   verifyFormat("export module a", Style);
23664 
23665   verifyFormat("import", Style);
23666   verifyFormat("module", Style);
23667   verifyFormat("export", Style);
23668 }
23669 
23670 TEST_F(FormatTest, CoroutineForCoawait) {
23671   FormatStyle Style = getLLVMStyle();
23672   verifyFormat("for co_await (auto x : range())\n  ;");
23673   verifyFormat("for (auto i : arr) {\n"
23674                "}",
23675                Style);
23676   verifyFormat("for co_await (auto i : arr) {\n"
23677                "}",
23678                Style);
23679   verifyFormat("for co_await (auto i : foo(T{})) {\n"
23680                "}",
23681                Style);
23682 }
23683 
23684 TEST_F(FormatTest, CoroutineCoAwait) {
23685   verifyFormat("int x = co_await foo();");
23686   verifyFormat("int x = (co_await foo());");
23687   verifyFormat("co_await (42);");
23688   verifyFormat("void operator co_await(int);");
23689   verifyFormat("void operator co_await(a);");
23690   verifyFormat("co_await a;");
23691   verifyFormat("co_await missing_await_resume{};");
23692   verifyFormat("co_await a; // comment");
23693   verifyFormat("void test0() { co_await a; }");
23694   verifyFormat("co_await co_await co_await foo();");
23695   verifyFormat("co_await foo().bar();");
23696   verifyFormat("co_await [this]() -> Task { co_return x; }");
23697   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
23698                "foo(); }(x, y);");
23699 
23700   FormatStyle Style = getLLVMStyleWithColumns(40);
23701   verifyFormat("co_await [this](int a, int b) -> Task {\n"
23702                "  co_return co_await foo();\n"
23703                "}(x, y);",
23704                Style);
23705   verifyFormat("co_await;");
23706 }
23707 
23708 TEST_F(FormatTest, CoroutineCoYield) {
23709   verifyFormat("int x = co_yield foo();");
23710   verifyFormat("int x = (co_yield foo());");
23711   verifyFormat("co_yield (42);");
23712   verifyFormat("co_yield {42};");
23713   verifyFormat("co_yield 42;");
23714   verifyFormat("co_yield n++;");
23715   verifyFormat("co_yield ++n;");
23716   verifyFormat("co_yield;");
23717 }
23718 
23719 TEST_F(FormatTest, CoroutineCoReturn) {
23720   verifyFormat("co_return (42);");
23721   verifyFormat("co_return;");
23722   verifyFormat("co_return {};");
23723   verifyFormat("co_return x;");
23724   verifyFormat("co_return co_await foo();");
23725   verifyFormat("co_return co_yield foo();");
23726 }
23727 
23728 TEST_F(FormatTest, EmptyShortBlock) {
23729   auto Style = getLLVMStyle();
23730   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
23731 
23732   verifyFormat("try {\n"
23733                "  doA();\n"
23734                "} catch (Exception &e) {\n"
23735                "  e.printStackTrace();\n"
23736                "}\n",
23737                Style);
23738 
23739   verifyFormat("try {\n"
23740                "  doA();\n"
23741                "} catch (Exception &e) {}\n",
23742                Style);
23743 }
23744 
23745 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
23746   auto Style = getLLVMStyle();
23747 
23748   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
23749   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
23750   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
23751   verifyFormat("struct Y<[] { return 0; }> {};", Style);
23752 
23753   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
23754   verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
23755 }
23756 
23757 TEST_F(FormatTest, RemoveBraces) {
23758   FormatStyle Style = getLLVMStyle();
23759   Style.RemoveBracesLLVM = true;
23760 
23761   // The following eight test cases are fully-braced versions of the examples at
23762   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
23763   // statement-bodies-of-if-else-loop-statements".
23764 
23765   // 1. Omit the braces, since the body is simple and clearly associated with
23766   // the if.
23767   verifyFormat("if (isa<FunctionDecl>(D))\n"
23768                "  handleFunctionDecl(D);\n"
23769                "else if (isa<VarDecl>(D))\n"
23770                "  handleVarDecl(D);",
23771                "if (isa<FunctionDecl>(D)) {\n"
23772                "  handleFunctionDecl(D);\n"
23773                "} else if (isa<VarDecl>(D)) {\n"
23774                "  handleVarDecl(D);\n"
23775                "}",
23776                Style);
23777 
23778   // 2. Here we document the condition itself and not the body.
23779   verifyFormat("if (isa<VarDecl>(D)) {\n"
23780                "  // It is necessary that we explain the situation with this\n"
23781                "  // surprisingly long comment, so it would be unclear\n"
23782                "  // without the braces whether the following statement is in\n"
23783                "  // the scope of the `if`.\n"
23784                "  // Because the condition is documented, we can't really\n"
23785                "  // hoist this comment that applies to the body above the\n"
23786                "  // if.\n"
23787                "  handleOtherDecl(D);\n"
23788                "}",
23789                Style);
23790 
23791   // 3. Use braces on the outer `if` to avoid a potential dangling else
23792   // situation.
23793   verifyFormat("if (isa<VarDecl>(D)) {\n"
23794                "  for (auto *A : D.attrs())\n"
23795                "    if (shouldProcessAttr(A))\n"
23796                "      handleAttr(A);\n"
23797                "}",
23798                "if (isa<VarDecl>(D)) {\n"
23799                "  for (auto *A : D.attrs()) {\n"
23800                "    if (shouldProcessAttr(A)) {\n"
23801                "      handleAttr(A);\n"
23802                "    }\n"
23803                "  }\n"
23804                "}",
23805                Style);
23806 
23807   // 4. Use braces for the `if` block to keep it uniform with the else block.
23808   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23809                "  handleFunctionDecl(D);\n"
23810                "} else {\n"
23811                "  // In this else case, it is necessary that we explain the\n"
23812                "  // situation with this surprisingly long comment, so it\n"
23813                "  // would be unclear without the braces whether the\n"
23814                "  // following statement is in the scope of the `if`.\n"
23815                "  handleOtherDecl(D);\n"
23816                "}",
23817                Style);
23818 
23819   // 5. This should also omit braces.  The `for` loop contains only a single
23820   // statement, so it shouldn't have braces.  The `if` also only contains a
23821   // single simple statement (the for loop), so it also should omit braces.
23822   verifyFormat("if (isa<FunctionDecl>(D))\n"
23823                "  for (auto *A : D.attrs())\n"
23824                "    handleAttr(A);",
23825                "if (isa<FunctionDecl>(D)) {\n"
23826                "  for (auto *A : D.attrs()) {\n"
23827                "    handleAttr(A);\n"
23828                "  }\n"
23829                "}",
23830                Style);
23831 
23832   // 6. Use braces for the outer `if` since the nested `for` is braced.
23833   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23834                "  for (auto *A : D.attrs()) {\n"
23835                "    // In this for loop body, it is necessary that we explain\n"
23836                "    // the situation with this surprisingly long comment,\n"
23837                "    // forcing braces on the `for` block.\n"
23838                "    handleAttr(A);\n"
23839                "  }\n"
23840                "}",
23841                Style);
23842 
23843   // 7. Use braces on the outer block because there are more than two levels of
23844   // nesting.
23845   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23846                "  for (auto *A : D.attrs())\n"
23847                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
23848                "      handleAttrOnDecl(D, A, i);\n"
23849                "}",
23850                "if (isa<FunctionDecl>(D)) {\n"
23851                "  for (auto *A : D.attrs()) {\n"
23852                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
23853                "      handleAttrOnDecl(D, A, i);\n"
23854                "    }\n"
23855                "  }\n"
23856                "}",
23857                Style);
23858 
23859   // 8. Use braces on the outer block because of a nested `if`, otherwise the
23860   // compiler would warn: `add explicit braces to avoid dangling else`
23861   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23862                "  if (shouldProcess(D))\n"
23863                "    handleVarDecl(D);\n"
23864                "  else\n"
23865                "    markAsIgnored(D);\n"
23866                "}",
23867                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23868                "  if (shouldProcess(D)) {\n"
23869                "    handleVarDecl(D);\n"
23870                "  } else {\n"
23871                "    markAsIgnored(D);\n"
23872                "  }\n"
23873                "}",
23874                Style);
23875 
23876   verifyFormat("if (a)\n"
23877                "  b; // comment\n"
23878                "else if (c)\n"
23879                "  d; /* comment */\n"
23880                "else\n"
23881                "  e;",
23882                "if (a) {\n"
23883                "  b; // comment\n"
23884                "} else if (c) {\n"
23885                "  d; /* comment */\n"
23886                "} else {\n"
23887                "  e;\n"
23888                "}",
23889                Style);
23890 
23891   verifyFormat("if (a) {\n"
23892                "  b;\n"
23893                "  c;\n"
23894                "} else if (d) {\n"
23895                "  e;\n"
23896                "}",
23897                Style);
23898 
23899   verifyFormat("if (a) {\n"
23900                "#undef NDEBUG\n"
23901                "  b;\n"
23902                "} else {\n"
23903                "  c;\n"
23904                "}",
23905                Style);
23906 
23907   verifyFormat("if (a) {\n"
23908                "  // comment\n"
23909                "} else if (b) {\n"
23910                "  c;\n"
23911                "}",
23912                Style);
23913 
23914   verifyFormat("if (a) {\n"
23915                "  b;\n"
23916                "} else {\n"
23917                "  { c; }\n"
23918                "}",
23919                Style);
23920 
23921   verifyFormat("if (a) {\n"
23922                "  if (b) // comment\n"
23923                "    c;\n"
23924                "} else if (d) {\n"
23925                "  e;\n"
23926                "}",
23927                "if (a) {\n"
23928                "  if (b) { // comment\n"
23929                "    c;\n"
23930                "  }\n"
23931                "} else if (d) {\n"
23932                "  e;\n"
23933                "}",
23934                Style);
23935 
23936   verifyFormat("if (a) {\n"
23937                "  if (b) {\n"
23938                "    c;\n"
23939                "    // comment\n"
23940                "  } else if (d) {\n"
23941                "    e;\n"
23942                "  }\n"
23943                "}",
23944                Style);
23945 
23946   verifyFormat("if (a) {\n"
23947                "  if (b)\n"
23948                "    c;\n"
23949                "}",
23950                "if (a) {\n"
23951                "  if (b) {\n"
23952                "    c;\n"
23953                "  }\n"
23954                "}",
23955                Style);
23956 
23957   verifyFormat("if (a)\n"
23958                "  if (b)\n"
23959                "    c;\n"
23960                "  else\n"
23961                "    d;\n"
23962                "else\n"
23963                "  e;",
23964                "if (a) {\n"
23965                "  if (b) {\n"
23966                "    c;\n"
23967                "  } else {\n"
23968                "    d;\n"
23969                "  }\n"
23970                "} else {\n"
23971                "  e;\n"
23972                "}",
23973                Style);
23974 
23975   verifyFormat("if (a) {\n"
23976                "  // comment\n"
23977                "  if (b)\n"
23978                "    c;\n"
23979                "  else if (d)\n"
23980                "    e;\n"
23981                "} else {\n"
23982                "  g;\n"
23983                "}",
23984                "if (a) {\n"
23985                "  // comment\n"
23986                "  if (b) {\n"
23987                "    c;\n"
23988                "  } else if (d) {\n"
23989                "    e;\n"
23990                "  }\n"
23991                "} else {\n"
23992                "  g;\n"
23993                "}",
23994                Style);
23995 
23996   verifyFormat("if (a)\n"
23997                "  b;\n"
23998                "else if (c)\n"
23999                "  d;\n"
24000                "else\n"
24001                "  e;",
24002                "if (a) {\n"
24003                "  b;\n"
24004                "} else {\n"
24005                "  if (c) {\n"
24006                "    d;\n"
24007                "  } else {\n"
24008                "    e;\n"
24009                "  }\n"
24010                "}",
24011                Style);
24012 
24013   verifyFormat("if (a) {\n"
24014                "  if (b)\n"
24015                "    c;\n"
24016                "  else if (d)\n"
24017                "    e;\n"
24018                "} else {\n"
24019                "  g;\n"
24020                "}",
24021                "if (a) {\n"
24022                "  if (b)\n"
24023                "    c;\n"
24024                "  else {\n"
24025                "    if (d)\n"
24026                "      e;\n"
24027                "  }\n"
24028                "} else {\n"
24029                "  g;\n"
24030                "}",
24031                Style);
24032 
24033   verifyFormat("if (a)\n"
24034                "  b;\n"
24035                "else if (c)\n"
24036                "  while (d)\n"
24037                "    e;\n"
24038                "// comment",
24039                "if (a)\n"
24040                "{\n"
24041                "  b;\n"
24042                "} else if (c) {\n"
24043                "  while (d) {\n"
24044                "    e;\n"
24045                "  }\n"
24046                "}\n"
24047                "// comment",
24048                Style);
24049 
24050   verifyFormat("if (a) {\n"
24051                "  b;\n"
24052                "} else if (c) {\n"
24053                "  d;\n"
24054                "} else {\n"
24055                "  e;\n"
24056                "  g;\n"
24057                "}",
24058                Style);
24059 
24060   verifyFormat("if (a) {\n"
24061                "  b;\n"
24062                "} else if (c) {\n"
24063                "  d;\n"
24064                "} else {\n"
24065                "  e;\n"
24066                "} // comment",
24067                Style);
24068 
24069   verifyFormat("int abs = [](int i) {\n"
24070                "  if (i >= 0)\n"
24071                "    return i;\n"
24072                "  return -i;\n"
24073                "};",
24074                "int abs = [](int i) {\n"
24075                "  if (i >= 0) {\n"
24076                "    return i;\n"
24077                "  }\n"
24078                "  return -i;\n"
24079                "};",
24080                Style);
24081 
24082   // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
24083 #if 0
24084   Style.ColumnLimit = 65;
24085 
24086   verifyFormat("if (condition) {\n"
24087                "  ff(Indices,\n"
24088                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24089                "} else {\n"
24090                "  ff(Indices,\n"
24091                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24092                "}",
24093                Style);
24094 
24095   Style.ColumnLimit = 20;
24096 
24097   verifyFormat("if (a) {\n"
24098                "  b = c + // 1 -\n"
24099                "      d;\n"
24100                "}",
24101                Style);
24102 
24103   verifyFormat("if (a) {\n"
24104                "  b = c >= 0 ? d\n"
24105                "             : e;\n"
24106                "}",
24107                "if (a) {\n"
24108                "  b = c >= 0 ? d : e;\n"
24109                "}",
24110                Style);
24111 #endif
24112 
24113   Style.ColumnLimit = 20;
24114 
24115   verifyFormat("if (a)\n"
24116                "  b = c > 0 ? d : e;",
24117                "if (a) {\n"
24118                "  b = c > 0 ? d : e;\n"
24119                "}",
24120                Style);
24121 
24122   Style.ColumnLimit = 0;
24123 
24124   verifyFormat("if (a)\n"
24125                "  b234567890223456789032345678904234567890 = "
24126                "c234567890223456789032345678904234567890;",
24127                "if (a) {\n"
24128                "  b234567890223456789032345678904234567890 = "
24129                "c234567890223456789032345678904234567890;\n"
24130                "}",
24131                Style);
24132 }
24133 
24134 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24135   auto Style = getLLVMStyle();
24136 
24137   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24138                     "void functionDecl(int a, int b, int c);";
24139 
24140   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24141                      "paramF, paramG, paramH, paramI);\n"
24142                      "void functionDecl(int argumentA, int argumentB, int "
24143                      "argumentC, int argumentD, int argumentE);";
24144 
24145   verifyFormat(Short, Style);
24146 
24147   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24148                       "paramF, paramG, paramH,\n"
24149                       "             paramI);\n"
24150                       "void functionDecl(int argumentA, int argumentB, int "
24151                       "argumentC, int argumentD,\n"
24152                       "                  int argumentE);";
24153 
24154   verifyFormat(NoBreak, Medium, Style);
24155   verifyFormat(NoBreak,
24156                "functionCall(\n"
24157                "    paramA,\n"
24158                "    paramB,\n"
24159                "    paramC,\n"
24160                "    paramD,\n"
24161                "    paramE,\n"
24162                "    paramF,\n"
24163                "    paramG,\n"
24164                "    paramH,\n"
24165                "    paramI\n"
24166                ");\n"
24167                "void functionDecl(\n"
24168                "    int argumentA,\n"
24169                "    int argumentB,\n"
24170                "    int argumentC,\n"
24171                "    int argumentD,\n"
24172                "    int argumentE\n"
24173                ");",
24174                Style);
24175 
24176   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24177                "                  nestedLongFunctionCall(argument1, "
24178                "argument2, argument3,\n"
24179                "                                         argument4, "
24180                "argument5));",
24181                Style);
24182 
24183   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24184 
24185   verifyFormat(Short, Style);
24186   verifyFormat(
24187       "functionCall(\n"
24188       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24189       "paramI\n"
24190       ");\n"
24191       "void functionDecl(\n"
24192       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24193       "argumentE\n"
24194       ");",
24195       Medium, Style);
24196 
24197   Style.AllowAllArgumentsOnNextLine = false;
24198   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24199 
24200   verifyFormat(Short, Style);
24201   verifyFormat(
24202       "functionCall(\n"
24203       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24204       "paramI\n"
24205       ");\n"
24206       "void functionDecl(\n"
24207       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24208       "argumentE\n"
24209       ");",
24210       Medium, Style);
24211 
24212   Style.BinPackArguments = false;
24213   Style.BinPackParameters = false;
24214 
24215   verifyFormat(Short, Style);
24216 
24217   verifyFormat("functionCall(\n"
24218                "    paramA,\n"
24219                "    paramB,\n"
24220                "    paramC,\n"
24221                "    paramD,\n"
24222                "    paramE,\n"
24223                "    paramF,\n"
24224                "    paramG,\n"
24225                "    paramH,\n"
24226                "    paramI\n"
24227                ");\n"
24228                "void functionDecl(\n"
24229                "    int argumentA,\n"
24230                "    int argumentB,\n"
24231                "    int argumentC,\n"
24232                "    int argumentD,\n"
24233                "    int argumentE\n"
24234                ");",
24235                Medium, Style);
24236 
24237   verifyFormat("outerFunctionCall(\n"
24238                "    nestedFunctionCall(argument1),\n"
24239                "    nestedLongFunctionCall(\n"
24240                "        argument1,\n"
24241                "        argument2,\n"
24242                "        argument3,\n"
24243                "        argument4,\n"
24244                "        argument5\n"
24245                "    )\n"
24246                ");",
24247                Style);
24248 
24249   verifyFormat("int a = (int)b;", Style);
24250   verifyFormat("int a = (int)b;",
24251                "int a = (\n"
24252                "    int\n"
24253                ") b;",
24254                Style);
24255 
24256   verifyFormat("return (true);", Style);
24257   verifyFormat("return (true);",
24258                "return (\n"
24259                "    true\n"
24260                ");",
24261                Style);
24262 
24263   verifyFormat("void foo();", Style);
24264   verifyFormat("void foo();",
24265                "void foo(\n"
24266                ");",
24267                Style);
24268 
24269   verifyFormat("void foo() {}", Style);
24270   verifyFormat("void foo() {}",
24271                "void foo(\n"
24272                ") {\n"
24273                "}",
24274                Style);
24275 
24276   verifyFormat("auto string = std::string();", Style);
24277   verifyFormat("auto string = std::string();",
24278                "auto string = std::string(\n"
24279                ");",
24280                Style);
24281 
24282   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24283   verifyFormat("void (*functionPointer)() = nullptr;",
24284                "void (\n"
24285                "    *functionPointer\n"
24286                ")\n"
24287                "(\n"
24288                ") = nullptr;",
24289                Style);
24290 }
24291 
24292 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24293   auto Style = getLLVMStyle();
24294 
24295   verifyFormat("if (foo()) {\n"
24296                "  return;\n"
24297                "}",
24298                Style);
24299 
24300   verifyFormat("if (quitelongarg !=\n"
24301                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24302                "comment\n"
24303                "  return;\n"
24304                "}",
24305                Style);
24306 
24307   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24308 
24309   verifyFormat("if (foo()) {\n"
24310                "  return;\n"
24311                "}",
24312                Style);
24313 
24314   verifyFormat("if (quitelongarg !=\n"
24315                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24316                "comment\n"
24317                "  return;\n"
24318                "}",
24319                Style);
24320 }
24321 
24322 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24323   auto Style = getLLVMStyle();
24324 
24325   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24326                "  doSomething();\n"
24327                "}",
24328                Style);
24329 
24330   verifyFormat("for (int myReallyLongCountVariable = 0; "
24331                "myReallyLongCountVariable < count;\n"
24332                "     myReallyLongCountVariable++) {\n"
24333                "  doSomething();\n"
24334                "}",
24335                Style);
24336 
24337   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24338 
24339   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24340                "  doSomething();\n"
24341                "}",
24342                Style);
24343 
24344   verifyFormat("for (int myReallyLongCountVariable = 0; "
24345                "myReallyLongCountVariable < count;\n"
24346                "     myReallyLongCountVariable++) {\n"
24347                "  doSomething();\n"
24348                "}",
24349                Style);
24350 }
24351 
24352 TEST_F(FormatTest, UnderstandsDigraphs) {
24353   verifyFormat("int arr<:5:> = {};");
24354   verifyFormat("int arr[5] = <%%>;");
24355   verifyFormat("int arr<:::qualified_variable:> = {};");
24356   verifyFormat("int arr[::qualified_variable] = <%%>;");
24357   verifyFormat("%:include <header>");
24358   verifyFormat("%:define A x##y");
24359   verifyFormat("#define A x%:%:y");
24360 }
24361 
24362 } // namespace
24363 } // namespace format
24364 } // namespace clang
24365