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 = clang::format::getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = clang::format::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("while (true)\n"
1468                "  ;",
1469                AllowsMergedLoops);
1470   verifyFormat("for (;;)\n"
1471                "  ;",
1472                AllowsMergedLoops);
1473   verifyFormat("for (;;)\n"
1474                "  for (;;) continue;",
1475                AllowsMergedLoops);
1476   verifyFormat("for (;;) // Can't merge this\n"
1477                "  continue;",
1478                AllowsMergedLoops);
1479   verifyFormat("for (;;) /* still don't merge */\n"
1480                "  continue;",
1481                AllowsMergedLoops);
1482   verifyFormat("do a++;\n"
1483                "while (true);",
1484                AllowsMergedLoops);
1485   verifyFormat("do /* Don't merge */\n"
1486                "  a++;\n"
1487                "while (true);",
1488                AllowsMergedLoops);
1489   verifyFormat("do // Don't merge\n"
1490                "  a++;\n"
1491                "while (true);",
1492                AllowsMergedLoops);
1493   verifyFormat("do\n"
1494                "  // Don't merge\n"
1495                "  a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   // Without braces labels are interpreted differently.
1499   verifyFormat("{\n"
1500                "  do\n"
1501                "  label:\n"
1502                "    a++;\n"
1503                "  while (true);\n"
1504                "}",
1505                AllowsMergedLoops);
1506 }
1507 
1508 TEST_F(FormatTest, FormatShortBracedStatements) {
1509   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1510   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1511   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1512   // Not IF to avoid any confusion that IF is somehow special.
1513   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1514   AllowSimpleBracedStatements.ColumnLimit = 40;
1515   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1516       FormatStyle::SBS_Always;
1517 
1518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1519       FormatStyle::SIS_WithoutElse;
1520   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1521 
1522   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1523   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1524   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1525 
1526   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1527   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1528   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1529   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1530   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1531   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1532   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1533   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1534   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1535   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1536   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1537   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1538   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1539   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1540   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1541   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1542   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1543                AllowSimpleBracedStatements);
1544   verifyFormat("if (true) {\n"
1545                "  ffffffffffffffffffffffff();\n"
1546                "}",
1547                AllowSimpleBracedStatements);
1548   verifyFormat("if (true) {\n"
1549                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1550                "}",
1551                AllowSimpleBracedStatements);
1552   verifyFormat("if (true) { //\n"
1553                "  f();\n"
1554                "}",
1555                AllowSimpleBracedStatements);
1556   verifyFormat("if (true) {\n"
1557                "  f();\n"
1558                "  f();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  f();\n"
1563                "} else {\n"
1564                "  f();\n"
1565                "}",
1566                AllowSimpleBracedStatements);
1567   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("MYIF (true) {\n"
1570                "  ffffffffffffffffffffffff();\n"
1571                "}",
1572                AllowSimpleBracedStatements);
1573   verifyFormat("MYIF (true) {\n"
1574                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1575                "}",
1576                AllowSimpleBracedStatements);
1577   verifyFormat("MYIF (true) { //\n"
1578                "  f();\n"
1579                "}",
1580                AllowSimpleBracedStatements);
1581   verifyFormat("MYIF (true) {\n"
1582                "  f();\n"
1583                "  f();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  f();\n"
1588                "} else {\n"
1589                "  f();\n"
1590                "}",
1591                AllowSimpleBracedStatements);
1592 
1593   verifyFormat("struct A2 {\n"
1594                "  int X;\n"
1595                "};",
1596                AllowSimpleBracedStatements);
1597   verifyFormat("typedef struct A2 {\n"
1598                "  int X;\n"
1599                "} A2_t;",
1600                AllowSimpleBracedStatements);
1601   verifyFormat("template <int> struct A2 {\n"
1602                "  struct B {};\n"
1603                "};",
1604                AllowSimpleBracedStatements);
1605 
1606   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1607       FormatStyle::SIS_Never;
1608   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1609   verifyFormat("if (true) {\n"
1610                "  f();\n"
1611                "}",
1612                AllowSimpleBracedStatements);
1613   verifyFormat("if (true) {\n"
1614                "  f();\n"
1615                "} else {\n"
1616                "  f();\n"
1617                "}",
1618                AllowSimpleBracedStatements);
1619   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1620   verifyFormat("MYIF (true) {\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624   verifyFormat("MYIF (true) {\n"
1625                "  f();\n"
1626                "} else {\n"
1627                "  f();\n"
1628                "}",
1629                AllowSimpleBracedStatements);
1630 
1631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1632   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("while (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1638   verifyFormat("for (;;) {\n"
1639                "  f();\n"
1640                "}",
1641                AllowSimpleBracedStatements);
1642 
1643   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1644       FormatStyle::SIS_WithoutElse;
1645   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1646   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1647       FormatStyle::BWACS_Always;
1648 
1649   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1650   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1651   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1652   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1653   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1654   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1656   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1657   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1658   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1659   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1660   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1661   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1664   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1665   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1666                AllowSimpleBracedStatements);
1667   verifyFormat("if (true)\n"
1668                "{\n"
1669                "  ffffffffffffffffffffffff();\n"
1670                "}",
1671                AllowSimpleBracedStatements);
1672   verifyFormat("if (true)\n"
1673                "{\n"
1674                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1675                "}",
1676                AllowSimpleBracedStatements);
1677   verifyFormat("if (true)\n"
1678                "{ //\n"
1679                "  f();\n"
1680                "}",
1681                AllowSimpleBracedStatements);
1682   verifyFormat("if (true)\n"
1683                "{\n"
1684                "  f();\n"
1685                "  f();\n"
1686                "}",
1687                AllowSimpleBracedStatements);
1688   verifyFormat("if (true)\n"
1689                "{\n"
1690                "  f();\n"
1691                "} else\n"
1692                "{\n"
1693                "  f();\n"
1694                "}",
1695                AllowSimpleBracedStatements);
1696   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1697                AllowSimpleBracedStatements);
1698   verifyFormat("MYIF (true)\n"
1699                "{\n"
1700                "  ffffffffffffffffffffffff();\n"
1701                "}",
1702                AllowSimpleBracedStatements);
1703   verifyFormat("MYIF (true)\n"
1704                "{\n"
1705                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1706                "}",
1707                AllowSimpleBracedStatements);
1708   verifyFormat("MYIF (true)\n"
1709                "{ //\n"
1710                "  f();\n"
1711                "}",
1712                AllowSimpleBracedStatements);
1713   verifyFormat("MYIF (true)\n"
1714                "{\n"
1715                "  f();\n"
1716                "  f();\n"
1717                "}",
1718                AllowSimpleBracedStatements);
1719   verifyFormat("MYIF (true)\n"
1720                "{\n"
1721                "  f();\n"
1722                "} else\n"
1723                "{\n"
1724                "  f();\n"
1725                "}",
1726                AllowSimpleBracedStatements);
1727 
1728   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1729       FormatStyle::SIS_Never;
1730   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1731   verifyFormat("if (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "}",
1735                AllowSimpleBracedStatements);
1736   verifyFormat("if (true)\n"
1737                "{\n"
1738                "  f();\n"
1739                "} else\n"
1740                "{\n"
1741                "  f();\n"
1742                "}",
1743                AllowSimpleBracedStatements);
1744   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1745   verifyFormat("MYIF (true)\n"
1746                "{\n"
1747                "  f();\n"
1748                "}",
1749                AllowSimpleBracedStatements);
1750   verifyFormat("MYIF (true)\n"
1751                "{\n"
1752                "  f();\n"
1753                "} else\n"
1754                "{\n"
1755                "  f();\n"
1756                "}",
1757                AllowSimpleBracedStatements);
1758 
1759   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1760   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1761   verifyFormat("while (true)\n"
1762                "{\n"
1763                "  f();\n"
1764                "}",
1765                AllowSimpleBracedStatements);
1766   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1767   verifyFormat("for (;;)\n"
1768                "{\n"
1769                "  f();\n"
1770                "}",
1771                AllowSimpleBracedStatements);
1772 }
1773 
1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1775   FormatStyle Style = getLLVMStyleWithColumns(60);
1776   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1777   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1778   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1779   EXPECT_EQ("#define A                                                  \\\n"
1780             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1781             "  {                                                        \\\n"
1782             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1783             "  }\n"
1784             "X;",
1785             format("#define A \\\n"
1786                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1787                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1788                    "   }\n"
1789                    "X;",
1790                    Style));
1791 }
1792 
1793 TEST_F(FormatTest, ParseIfElse) {
1794   verifyFormat("if (true)\n"
1795                "  if (true)\n"
1796                "    if (true)\n"
1797                "      f();\n"
1798                "    else\n"
1799                "      g();\n"
1800                "  else\n"
1801                "    h();\n"
1802                "else\n"
1803                "  i();");
1804   verifyFormat("if (true)\n"
1805                "  if (true)\n"
1806                "    if (true) {\n"
1807                "      if (true)\n"
1808                "        f();\n"
1809                "    } else {\n"
1810                "      g();\n"
1811                "    }\n"
1812                "  else\n"
1813                "    h();\n"
1814                "else {\n"
1815                "  i();\n"
1816                "}");
1817   verifyFormat("if (true)\n"
1818                "  if constexpr (true)\n"
1819                "    if (true) {\n"
1820                "      if constexpr (true)\n"
1821                "        f();\n"
1822                "    } else {\n"
1823                "      g();\n"
1824                "    }\n"
1825                "  else\n"
1826                "    h();\n"
1827                "else {\n"
1828                "  i();\n"
1829                "}");
1830   verifyFormat("if (true)\n"
1831                "  if CONSTEXPR (true)\n"
1832                "    if (true) {\n"
1833                "      if CONSTEXPR (true)\n"
1834                "        f();\n"
1835                "    } else {\n"
1836                "      g();\n"
1837                "    }\n"
1838                "  else\n"
1839                "    h();\n"
1840                "else {\n"
1841                "  i();\n"
1842                "}");
1843   verifyFormat("void f() {\n"
1844                "  if (a) {\n"
1845                "  } else {\n"
1846                "  }\n"
1847                "}");
1848 }
1849 
1850 TEST_F(FormatTest, ElseIf) {
1851   verifyFormat("if (a) {\n} else if (b) {\n}");
1852   verifyFormat("if (a)\n"
1853                "  f();\n"
1854                "else if (b)\n"
1855                "  g();\n"
1856                "else\n"
1857                "  h();");
1858   verifyFormat("if (a)\n"
1859                "  f();\n"
1860                "else // comment\n"
1861                "  if (b) {\n"
1862                "    g();\n"
1863                "    h();\n"
1864                "  }");
1865   verifyFormat("if constexpr (a)\n"
1866                "  f();\n"
1867                "else if constexpr (b)\n"
1868                "  g();\n"
1869                "else\n"
1870                "  h();");
1871   verifyFormat("if CONSTEXPR (a)\n"
1872                "  f();\n"
1873                "else if CONSTEXPR (b)\n"
1874                "  g();\n"
1875                "else\n"
1876                "  h();");
1877   verifyFormat("if (a) {\n"
1878                "  f();\n"
1879                "}\n"
1880                "// or else ..\n"
1881                "else {\n"
1882                "  g()\n"
1883                "}");
1884 
1885   verifyFormat("if (a) {\n"
1886                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1887                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1888                "}");
1889   verifyFormat("if (a) {\n"
1890                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1891                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1892                "}");
1893   verifyFormat("if (a) {\n"
1894                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1895                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1896                "}");
1897   verifyFormat("if (a) {\n"
1898                "} else if (\n"
1899                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1900                "}",
1901                getLLVMStyleWithColumns(62));
1902   verifyFormat("if (a) {\n"
1903                "} else if constexpr (\n"
1904                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1905                "}",
1906                getLLVMStyleWithColumns(62));
1907   verifyFormat("if (a) {\n"
1908                "} else if CONSTEXPR (\n"
1909                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1910                "}",
1911                getLLVMStyleWithColumns(62));
1912 }
1913 
1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1915   FormatStyle Style = getLLVMStyle();
1916   // Check first the default LLVM style
1917   // Style.PointerAlignment = FormatStyle::PAS_Right;
1918   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1919   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1920   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1921   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1922   verifyFormat("int *f1(int &a) const &;", Style);
1923   verifyFormat("int *f1(int &a) const & = 0;", Style);
1924   verifyFormat("int *a = f1();", Style);
1925   verifyFormat("int &b = f2();", Style);
1926   verifyFormat("int &&c = f3();", Style);
1927 
1928   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1929   verifyFormat("Const unsigned int *c;\n"
1930                "const unsigned int *d;\n"
1931                "Const unsigned int &e;\n"
1932                "const unsigned int &f;\n"
1933                "const unsigned    &&g;\n"
1934                "Const unsigned      h;",
1935                Style);
1936 
1937   Style.PointerAlignment = FormatStyle::PAS_Left;
1938   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1939   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
1940   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
1941   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
1942   verifyFormat("int* f1(int& a) const& = 0;", Style);
1943   verifyFormat("int* a = f1();", Style);
1944   verifyFormat("int& b = f2();", Style);
1945   verifyFormat("int&& c = f3();", Style);
1946 
1947   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1948   verifyFormat("Const unsigned int* c;\n"
1949                "const unsigned int* d;\n"
1950                "Const unsigned int& e;\n"
1951                "const unsigned int& f;\n"
1952                "const unsigned&&    g;\n"
1953                "Const unsigned      h;",
1954                Style);
1955 
1956   Style.PointerAlignment = FormatStyle::PAS_Right;
1957   Style.ReferenceAlignment = FormatStyle::RAS_Left;
1958   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
1959   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
1960   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
1961   verifyFormat("int *a = f1();", Style);
1962   verifyFormat("int& b = f2();", Style);
1963   verifyFormat("int&& c = f3();", Style);
1964 
1965   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1966   verifyFormat("Const unsigned int *c;\n"
1967                "const unsigned int *d;\n"
1968                "Const unsigned int& e;\n"
1969                "const unsigned int& f;\n"
1970                "const unsigned      g;\n"
1971                "Const unsigned      h;",
1972                Style);
1973 
1974   Style.PointerAlignment = FormatStyle::PAS_Left;
1975   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
1976   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
1977   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
1978   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
1979   verifyFormat("int* a = f1();", Style);
1980   verifyFormat("int & b = f2();", Style);
1981   verifyFormat("int && c = f3();", Style);
1982 
1983   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1984   verifyFormat("Const unsigned int*  c;\n"
1985                "const unsigned int*  d;\n"
1986                "Const unsigned int & e;\n"
1987                "const unsigned int & f;\n"
1988                "const unsigned &&    g;\n"
1989                "Const unsigned       h;",
1990                Style);
1991 
1992   Style.PointerAlignment = FormatStyle::PAS_Middle;
1993   Style.ReferenceAlignment = FormatStyle::RAS_Right;
1994   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
1995   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
1996   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
1997   verifyFormat("int * a = f1();", Style);
1998   verifyFormat("int &b = f2();", Style);
1999   verifyFormat("int &&c = f3();", Style);
2000 
2001   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2002   // specifically handled
2003   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2004 }
2005 
2006 TEST_F(FormatTest, FormatsForLoop) {
2007   verifyFormat(
2008       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2009       "     ++VeryVeryLongLoopVariable)\n"
2010       "  ;");
2011   verifyFormat("for (;;)\n"
2012                "  f();");
2013   verifyFormat("for (;;) {\n}");
2014   verifyFormat("for (;;) {\n"
2015                "  f();\n"
2016                "}");
2017   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2018 
2019   verifyFormat(
2020       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2021       "                                          E = UnwrappedLines.end();\n"
2022       "     I != E; ++I) {\n}");
2023 
2024   verifyFormat(
2025       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2026       "     ++IIIII) {\n}");
2027   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2028                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2029                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2030   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2031                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2032                "         E = FD->getDeclsInPrototypeScope().end();\n"
2033                "     I != E; ++I) {\n}");
2034   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2035                "         I = Container.begin(),\n"
2036                "         E = Container.end();\n"
2037                "     I != E; ++I) {\n}",
2038                getLLVMStyleWithColumns(76));
2039 
2040   verifyFormat(
2041       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2042       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2043       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2044       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2045       "     ++aaaaaaaaaaa) {\n}");
2046   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2047                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2048                "     ++i) {\n}");
2049   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2050                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2051                "}");
2052   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2053                "         aaaaaaaaaa);\n"
2054                "     iter; ++iter) {\n"
2055                "}");
2056   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2057                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2058                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2059                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2060 
2061   // These should not be formatted as Objective-C for-in loops.
2062   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2063   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2064   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2065   verifyFormat(
2066       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2067 
2068   FormatStyle NoBinPacking = getLLVMStyle();
2069   NoBinPacking.BinPackParameters = false;
2070   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2071                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2072                "                                           aaaaaaaaaaaaaaaa,\n"
2073                "                                           aaaaaaaaaaaaaaaa,\n"
2074                "                                           aaaaaaaaaaaaaaaa);\n"
2075                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2076                "}",
2077                NoBinPacking);
2078   verifyFormat(
2079       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2080       "                                          E = UnwrappedLines.end();\n"
2081       "     I != E;\n"
2082       "     ++I) {\n}",
2083       NoBinPacking);
2084 
2085   FormatStyle AlignLeft = getLLVMStyle();
2086   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2087   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2088 }
2089 
2090 TEST_F(FormatTest, RangeBasedForLoops) {
2091   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2092                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2093   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2094                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2095   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2096                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2097   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2098                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2099 }
2100 
2101 TEST_F(FormatTest, ForEachLoops) {
2102   verifyFormat("void f() {\n"
2103                "  foreach (Item *item, itemlist) {}\n"
2104                "  Q_FOREACH (Item *item, itemlist) {}\n"
2105                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
2106                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2107                "}");
2108 
2109   FormatStyle Style = getLLVMStyle();
2110   Style.SpaceBeforeParens =
2111       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2112   verifyFormat("void f() {\n"
2113                "  foreach(Item *item, itemlist) {}\n"
2114                "  Q_FOREACH(Item *item, itemlist) {}\n"
2115                "  BOOST_FOREACH(Item *item, itemlist) {}\n"
2116                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2117                "}",
2118                Style);
2119 
2120   // As function-like macros.
2121   verifyFormat("#define foreach(x, y)\n"
2122                "#define Q_FOREACH(x, y)\n"
2123                "#define BOOST_FOREACH(x, y)\n"
2124                "#define UNKNOWN_FOREACH(x, y)\n");
2125 
2126   // Not as function-like macros.
2127   verifyFormat("#define foreach (x, y)\n"
2128                "#define Q_FOREACH (x, y)\n"
2129                "#define BOOST_FOREACH (x, y)\n"
2130                "#define UNKNOWN_FOREACH (x, y)\n");
2131 
2132   // handle microsoft non standard extension
2133   verifyFormat("for each (char c in x->MyStringProperty)");
2134 }
2135 
2136 TEST_F(FormatTest, FormatsWhileLoop) {
2137   verifyFormat("while (true) {\n}");
2138   verifyFormat("while (true)\n"
2139                "  f();");
2140   verifyFormat("while () {\n}");
2141   verifyFormat("while () {\n"
2142                "  f();\n"
2143                "}");
2144 }
2145 
2146 TEST_F(FormatTest, FormatsDoWhile) {
2147   verifyFormat("do {\n"
2148                "  do_something();\n"
2149                "} while (something());");
2150   verifyFormat("do\n"
2151                "  do_something();\n"
2152                "while (something());");
2153 }
2154 
2155 TEST_F(FormatTest, FormatsSwitchStatement) {
2156   verifyFormat("switch (x) {\n"
2157                "case 1:\n"
2158                "  f();\n"
2159                "  break;\n"
2160                "case kFoo:\n"
2161                "case ns::kBar:\n"
2162                "case kBaz:\n"
2163                "  break;\n"
2164                "default:\n"
2165                "  g();\n"
2166                "  break;\n"
2167                "}");
2168   verifyFormat("switch (x) {\n"
2169                "case 1: {\n"
2170                "  f();\n"
2171                "  break;\n"
2172                "}\n"
2173                "case 2: {\n"
2174                "  break;\n"
2175                "}\n"
2176                "}");
2177   verifyFormat("switch (x) {\n"
2178                "case 1: {\n"
2179                "  f();\n"
2180                "  {\n"
2181                "    g();\n"
2182                "    h();\n"
2183                "  }\n"
2184                "  break;\n"
2185                "}\n"
2186                "}");
2187   verifyFormat("switch (x) {\n"
2188                "case 1: {\n"
2189                "  f();\n"
2190                "  if (foo) {\n"
2191                "    g();\n"
2192                "    h();\n"
2193                "  }\n"
2194                "  break;\n"
2195                "}\n"
2196                "}");
2197   verifyFormat("switch (x) {\n"
2198                "case 1: {\n"
2199                "  f();\n"
2200                "  g();\n"
2201                "} break;\n"
2202                "}");
2203   verifyFormat("switch (test)\n"
2204                "  ;");
2205   verifyFormat("switch (x) {\n"
2206                "default: {\n"
2207                "  // Do nothing.\n"
2208                "}\n"
2209                "}");
2210   verifyFormat("switch (x) {\n"
2211                "// comment\n"
2212                "// if 1, do f()\n"
2213                "case 1:\n"
2214                "  f();\n"
2215                "}");
2216   verifyFormat("switch (x) {\n"
2217                "case 1:\n"
2218                "  // Do amazing stuff\n"
2219                "  {\n"
2220                "    f();\n"
2221                "    g();\n"
2222                "  }\n"
2223                "  break;\n"
2224                "}");
2225   verifyFormat("#define A          \\\n"
2226                "  switch (x) {     \\\n"
2227                "  case a:          \\\n"
2228                "    foo = b;       \\\n"
2229                "  }",
2230                getLLVMStyleWithColumns(20));
2231   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2232                "  case OP_name:                        \\\n"
2233                "    return operations::Operation##name\n",
2234                getLLVMStyleWithColumns(40));
2235   verifyFormat("switch (x) {\n"
2236                "case 1:;\n"
2237                "default:;\n"
2238                "  int i;\n"
2239                "}");
2240 
2241   verifyGoogleFormat("switch (x) {\n"
2242                      "  case 1:\n"
2243                      "    f();\n"
2244                      "    break;\n"
2245                      "  case kFoo:\n"
2246                      "  case ns::kBar:\n"
2247                      "  case kBaz:\n"
2248                      "    break;\n"
2249                      "  default:\n"
2250                      "    g();\n"
2251                      "    break;\n"
2252                      "}");
2253   verifyGoogleFormat("switch (x) {\n"
2254                      "  case 1: {\n"
2255                      "    f();\n"
2256                      "    break;\n"
2257                      "  }\n"
2258                      "}");
2259   verifyGoogleFormat("switch (test)\n"
2260                      "  ;");
2261 
2262   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2263                      "  case OP_name:              \\\n"
2264                      "    return operations::Operation##name\n");
2265   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2266                      "  // Get the correction operation class.\n"
2267                      "  switch (OpCode) {\n"
2268                      "    CASE(Add);\n"
2269                      "    CASE(Subtract);\n"
2270                      "    default:\n"
2271                      "      return operations::Unknown;\n"
2272                      "  }\n"
2273                      "#undef OPERATION_CASE\n"
2274                      "}");
2275   verifyFormat("DEBUG({\n"
2276                "  switch (x) {\n"
2277                "  case A:\n"
2278                "    f();\n"
2279                "    break;\n"
2280                "    // fallthrough\n"
2281                "  case B:\n"
2282                "    g();\n"
2283                "    break;\n"
2284                "  }\n"
2285                "});");
2286   EXPECT_EQ("DEBUG({\n"
2287             "  switch (x) {\n"
2288             "  case A:\n"
2289             "    f();\n"
2290             "    break;\n"
2291             "  // On B:\n"
2292             "  case B:\n"
2293             "    g();\n"
2294             "    break;\n"
2295             "  }\n"
2296             "});",
2297             format("DEBUG({\n"
2298                    "  switch (x) {\n"
2299                    "  case A:\n"
2300                    "    f();\n"
2301                    "    break;\n"
2302                    "  // On B:\n"
2303                    "  case B:\n"
2304                    "    g();\n"
2305                    "    break;\n"
2306                    "  }\n"
2307                    "});",
2308                    getLLVMStyle()));
2309   EXPECT_EQ("switch (n) {\n"
2310             "case 0: {\n"
2311             "  return false;\n"
2312             "}\n"
2313             "default: {\n"
2314             "  return true;\n"
2315             "}\n"
2316             "}",
2317             format("switch (n)\n"
2318                    "{\n"
2319                    "case 0: {\n"
2320                    "  return false;\n"
2321                    "}\n"
2322                    "default: {\n"
2323                    "  return true;\n"
2324                    "}\n"
2325                    "}",
2326                    getLLVMStyle()));
2327   verifyFormat("switch (a) {\n"
2328                "case (b):\n"
2329                "  return;\n"
2330                "}");
2331 
2332   verifyFormat("switch (a) {\n"
2333                "case some_namespace::\n"
2334                "    some_constant:\n"
2335                "  return;\n"
2336                "}",
2337                getLLVMStyleWithColumns(34));
2338 
2339   FormatStyle Style = getLLVMStyle();
2340   Style.IndentCaseLabels = true;
2341   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2342   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2343   Style.BraceWrapping.AfterCaseLabel = true;
2344   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2345   EXPECT_EQ("switch (n)\n"
2346             "{\n"
2347             "  case 0:\n"
2348             "  {\n"
2349             "    return false;\n"
2350             "  }\n"
2351             "  default:\n"
2352             "  {\n"
2353             "    return true;\n"
2354             "  }\n"
2355             "}",
2356             format("switch (n) {\n"
2357                    "  case 0: {\n"
2358                    "    return false;\n"
2359                    "  }\n"
2360                    "  default: {\n"
2361                    "    return true;\n"
2362                    "  }\n"
2363                    "}",
2364                    Style));
2365   Style.BraceWrapping.AfterCaseLabel = false;
2366   EXPECT_EQ("switch (n)\n"
2367             "{\n"
2368             "  case 0: {\n"
2369             "    return false;\n"
2370             "  }\n"
2371             "  default: {\n"
2372             "    return true;\n"
2373             "  }\n"
2374             "}",
2375             format("switch (n) {\n"
2376                    "  case 0:\n"
2377                    "  {\n"
2378                    "    return false;\n"
2379                    "  }\n"
2380                    "  default:\n"
2381                    "  {\n"
2382                    "    return true;\n"
2383                    "  }\n"
2384                    "}",
2385                    Style));
2386   Style.IndentCaseLabels = false;
2387   Style.IndentCaseBlocks = true;
2388   EXPECT_EQ("switch (n)\n"
2389             "{\n"
2390             "case 0:\n"
2391             "  {\n"
2392             "    return false;\n"
2393             "  }\n"
2394             "case 1:\n"
2395             "  break;\n"
2396             "default:\n"
2397             "  {\n"
2398             "    return true;\n"
2399             "  }\n"
2400             "}",
2401             format("switch (n) {\n"
2402                    "case 0: {\n"
2403                    "  return false;\n"
2404                    "}\n"
2405                    "case 1:\n"
2406                    "  break;\n"
2407                    "default: {\n"
2408                    "  return true;\n"
2409                    "}\n"
2410                    "}",
2411                    Style));
2412   Style.IndentCaseLabels = true;
2413   Style.IndentCaseBlocks = true;
2414   EXPECT_EQ("switch (n)\n"
2415             "{\n"
2416             "  case 0:\n"
2417             "    {\n"
2418             "      return false;\n"
2419             "    }\n"
2420             "  case 1:\n"
2421             "    break;\n"
2422             "  default:\n"
2423             "    {\n"
2424             "      return true;\n"
2425             "    }\n"
2426             "}",
2427             format("switch (n) {\n"
2428                    "case 0: {\n"
2429                    "  return false;\n"
2430                    "}\n"
2431                    "case 1:\n"
2432                    "  break;\n"
2433                    "default: {\n"
2434                    "  return true;\n"
2435                    "}\n"
2436                    "}",
2437                    Style));
2438 }
2439 
2440 TEST_F(FormatTest, CaseRanges) {
2441   verifyFormat("switch (x) {\n"
2442                "case 'A' ... 'Z':\n"
2443                "case 1 ... 5:\n"
2444                "case a ... b:\n"
2445                "  break;\n"
2446                "}");
2447 }
2448 
2449 TEST_F(FormatTest, ShortEnums) {
2450   FormatStyle Style = getLLVMStyle();
2451   Style.AllowShortEnumsOnASingleLine = true;
2452   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2453   Style.AllowShortEnumsOnASingleLine = false;
2454   verifyFormat("enum {\n"
2455                "  A,\n"
2456                "  B,\n"
2457                "  C\n"
2458                "} ShortEnum1, ShortEnum2;",
2459                Style);
2460   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2461   Style.BraceWrapping.AfterEnum = true;
2462   verifyFormat("enum\n"
2463                "{\n"
2464                "  A,\n"
2465                "  B,\n"
2466                "  C\n"
2467                "} ShortEnum1, ShortEnum2;",
2468                Style);
2469 }
2470 
2471 TEST_F(FormatTest, ShortCaseLabels) {
2472   FormatStyle Style = getLLVMStyle();
2473   Style.AllowShortCaseLabelsOnASingleLine = true;
2474   verifyFormat("switch (a) {\n"
2475                "case 1: x = 1; break;\n"
2476                "case 2: return;\n"
2477                "case 3:\n"
2478                "case 4:\n"
2479                "case 5: return;\n"
2480                "case 6: // comment\n"
2481                "  return;\n"
2482                "case 7:\n"
2483                "  // comment\n"
2484                "  return;\n"
2485                "case 8:\n"
2486                "  x = 8; // comment\n"
2487                "  break;\n"
2488                "default: y = 1; break;\n"
2489                "}",
2490                Style);
2491   verifyFormat("switch (a) {\n"
2492                "case 0: return; // comment\n"
2493                "case 1: break;  // comment\n"
2494                "case 2: return;\n"
2495                "// comment\n"
2496                "case 3: return;\n"
2497                "// comment 1\n"
2498                "// comment 2\n"
2499                "// comment 3\n"
2500                "case 4: break; /* comment */\n"
2501                "case 5:\n"
2502                "  // comment\n"
2503                "  break;\n"
2504                "case 6: /* comment */ x = 1; break;\n"
2505                "case 7: x = /* comment */ 1; break;\n"
2506                "case 8:\n"
2507                "  x = 1; /* comment */\n"
2508                "  break;\n"
2509                "case 9:\n"
2510                "  break; // comment line 1\n"
2511                "         // comment line 2\n"
2512                "}",
2513                Style);
2514   EXPECT_EQ("switch (a) {\n"
2515             "case 1:\n"
2516             "  x = 8;\n"
2517             "  // fall through\n"
2518             "case 2: x = 8;\n"
2519             "// comment\n"
2520             "case 3:\n"
2521             "  return; /* comment line 1\n"
2522             "           * comment line 2 */\n"
2523             "case 4: i = 8;\n"
2524             "// something else\n"
2525             "#if FOO\n"
2526             "case 5: break;\n"
2527             "#endif\n"
2528             "}",
2529             format("switch (a) {\n"
2530                    "case 1: x = 8;\n"
2531                    "  // fall through\n"
2532                    "case 2:\n"
2533                    "  x = 8;\n"
2534                    "// comment\n"
2535                    "case 3:\n"
2536                    "  return; /* comment line 1\n"
2537                    "           * comment line 2 */\n"
2538                    "case 4:\n"
2539                    "  i = 8;\n"
2540                    "// something else\n"
2541                    "#if FOO\n"
2542                    "case 5: break;\n"
2543                    "#endif\n"
2544                    "}",
2545                    Style));
2546   EXPECT_EQ("switch (a) {\n"
2547             "case 0:\n"
2548             "  return; // long long long long long long long long long long "
2549             "long long comment\n"
2550             "          // line\n"
2551             "}",
2552             format("switch (a) {\n"
2553                    "case 0: return; // long long long long long long long long "
2554                    "long long long long comment line\n"
2555                    "}",
2556                    Style));
2557   EXPECT_EQ("switch (a) {\n"
2558             "case 0:\n"
2559             "  return; /* long long long long long long long long long long "
2560             "long long comment\n"
2561             "             line */\n"
2562             "}",
2563             format("switch (a) {\n"
2564                    "case 0: return; /* long long long long long long long long "
2565                    "long long long long comment line */\n"
2566                    "}",
2567                    Style));
2568   verifyFormat("switch (a) {\n"
2569                "#if FOO\n"
2570                "case 0: return 0;\n"
2571                "#endif\n"
2572                "}",
2573                Style);
2574   verifyFormat("switch (a) {\n"
2575                "case 1: {\n"
2576                "}\n"
2577                "case 2: {\n"
2578                "  return;\n"
2579                "}\n"
2580                "case 3: {\n"
2581                "  x = 1;\n"
2582                "  return;\n"
2583                "}\n"
2584                "case 4:\n"
2585                "  if (x)\n"
2586                "    return;\n"
2587                "}",
2588                Style);
2589   Style.ColumnLimit = 21;
2590   verifyFormat("switch (a) {\n"
2591                "case 1: x = 1; break;\n"
2592                "case 2: return;\n"
2593                "case 3:\n"
2594                "case 4:\n"
2595                "case 5: return;\n"
2596                "default:\n"
2597                "  y = 1;\n"
2598                "  break;\n"
2599                "}",
2600                Style);
2601   Style.ColumnLimit = 80;
2602   Style.AllowShortCaseLabelsOnASingleLine = false;
2603   Style.IndentCaseLabels = true;
2604   EXPECT_EQ("switch (n) {\n"
2605             "  default /*comments*/:\n"
2606             "    return true;\n"
2607             "  case 0:\n"
2608             "    return false;\n"
2609             "}",
2610             format("switch (n) {\n"
2611                    "default/*comments*/:\n"
2612                    "  return true;\n"
2613                    "case 0:\n"
2614                    "  return false;\n"
2615                    "}",
2616                    Style));
2617   Style.AllowShortCaseLabelsOnASingleLine = true;
2618   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2619   Style.BraceWrapping.AfterCaseLabel = true;
2620   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2621   EXPECT_EQ("switch (n)\n"
2622             "{\n"
2623             "  case 0:\n"
2624             "  {\n"
2625             "    return false;\n"
2626             "  }\n"
2627             "  default:\n"
2628             "  {\n"
2629             "    return true;\n"
2630             "  }\n"
2631             "}",
2632             format("switch (n) {\n"
2633                    "  case 0: {\n"
2634                    "    return false;\n"
2635                    "  }\n"
2636                    "  default:\n"
2637                    "  {\n"
2638                    "    return true;\n"
2639                    "  }\n"
2640                    "}",
2641                    Style));
2642 }
2643 
2644 TEST_F(FormatTest, FormatsLabels) {
2645   verifyFormat("void f() {\n"
2646                "  some_code();\n"
2647                "test_label:\n"
2648                "  some_other_code();\n"
2649                "  {\n"
2650                "    some_more_code();\n"
2651                "  another_label:\n"
2652                "    some_more_code();\n"
2653                "  }\n"
2654                "}");
2655   verifyFormat("{\n"
2656                "  some_code();\n"
2657                "test_label:\n"
2658                "  some_other_code();\n"
2659                "}");
2660   verifyFormat("{\n"
2661                "  some_code();\n"
2662                "test_label:;\n"
2663                "  int i = 0;\n"
2664                "}");
2665   FormatStyle Style = getLLVMStyle();
2666   Style.IndentGotoLabels = false;
2667   verifyFormat("void f() {\n"
2668                "  some_code();\n"
2669                "test_label:\n"
2670                "  some_other_code();\n"
2671                "  {\n"
2672                "    some_more_code();\n"
2673                "another_label:\n"
2674                "    some_more_code();\n"
2675                "  }\n"
2676                "}",
2677                Style);
2678   verifyFormat("{\n"
2679                "  some_code();\n"
2680                "test_label:\n"
2681                "  some_other_code();\n"
2682                "}",
2683                Style);
2684   verifyFormat("{\n"
2685                "  some_code();\n"
2686                "test_label:;\n"
2687                "  int i = 0;\n"
2688                "}");
2689 }
2690 
2691 TEST_F(FormatTest, MultiLineControlStatements) {
2692   FormatStyle Style = getLLVMStyle();
2693   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2694   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2695   Style.ColumnLimit = 20;
2696   // Short lines should keep opening brace on same line.
2697   EXPECT_EQ("if (foo) {\n"
2698             "  bar();\n"
2699             "}",
2700             format("if(foo){bar();}", Style));
2701   EXPECT_EQ("if (foo) {\n"
2702             "  bar();\n"
2703             "} else {\n"
2704             "  baz();\n"
2705             "}",
2706             format("if(foo){bar();}else{baz();}", Style));
2707   EXPECT_EQ("if (foo && bar) {\n"
2708             "  baz();\n"
2709             "}",
2710             format("if(foo&&bar){baz();}", Style));
2711   EXPECT_EQ("if (foo) {\n"
2712             "  bar();\n"
2713             "} else if (baz) {\n"
2714             "  quux();\n"
2715             "}",
2716             format("if(foo){bar();}else if(baz){quux();}", Style));
2717   EXPECT_EQ(
2718       "if (foo) {\n"
2719       "  bar();\n"
2720       "} else if (baz) {\n"
2721       "  quux();\n"
2722       "} else {\n"
2723       "  foobar();\n"
2724       "}",
2725       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2726   EXPECT_EQ("for (;;) {\n"
2727             "  foo();\n"
2728             "}",
2729             format("for(;;){foo();}"));
2730   EXPECT_EQ("while (1) {\n"
2731             "  foo();\n"
2732             "}",
2733             format("while(1){foo();}", Style));
2734   EXPECT_EQ("switch (foo) {\n"
2735             "case bar:\n"
2736             "  return;\n"
2737             "}",
2738             format("switch(foo){case bar:return;}", Style));
2739   EXPECT_EQ("try {\n"
2740             "  foo();\n"
2741             "} catch (...) {\n"
2742             "  bar();\n"
2743             "}",
2744             format("try{foo();}catch(...){bar();}", Style));
2745   EXPECT_EQ("do {\n"
2746             "  foo();\n"
2747             "} while (bar &&\n"
2748             "         baz);",
2749             format("do{foo();}while(bar&&baz);", Style));
2750   // Long lines should put opening brace on new line.
2751   EXPECT_EQ("if (foo && bar &&\n"
2752             "    baz)\n"
2753             "{\n"
2754             "  quux();\n"
2755             "}",
2756             format("if(foo&&bar&&baz){quux();}", Style));
2757   EXPECT_EQ("if (foo && bar &&\n"
2758             "    baz)\n"
2759             "{\n"
2760             "  quux();\n"
2761             "}",
2762             format("if (foo && bar &&\n"
2763                    "    baz) {\n"
2764                    "  quux();\n"
2765                    "}",
2766                    Style));
2767   EXPECT_EQ("if (foo) {\n"
2768             "  bar();\n"
2769             "} else if (baz ||\n"
2770             "           quux)\n"
2771             "{\n"
2772             "  foobar();\n"
2773             "}",
2774             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2775   EXPECT_EQ(
2776       "if (foo) {\n"
2777       "  bar();\n"
2778       "} else if (baz ||\n"
2779       "           quux)\n"
2780       "{\n"
2781       "  foobar();\n"
2782       "} else {\n"
2783       "  barbaz();\n"
2784       "}",
2785       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2786              Style));
2787   EXPECT_EQ("for (int i = 0;\n"
2788             "     i < 10; ++i)\n"
2789             "{\n"
2790             "  foo();\n"
2791             "}",
2792             format("for(int i=0;i<10;++i){foo();}", Style));
2793   EXPECT_EQ("foreach (int i,\n"
2794             "         list)\n"
2795             "{\n"
2796             "  foo();\n"
2797             "}",
2798             format("foreach(int i, list){foo();}", Style));
2799   Style.ColumnLimit =
2800       40; // to concentrate at brace wrapping, not line wrap due to column limit
2801   EXPECT_EQ("foreach (int i, list) {\n"
2802             "  foo();\n"
2803             "}",
2804             format("foreach(int i, list){foo();}", Style));
2805   Style.ColumnLimit =
2806       20; // to concentrate at brace wrapping, not line wrap due to column limit
2807   EXPECT_EQ("while (foo || bar ||\n"
2808             "       baz)\n"
2809             "{\n"
2810             "  quux();\n"
2811             "}",
2812             format("while(foo||bar||baz){quux();}", Style));
2813   EXPECT_EQ("switch (\n"
2814             "    foo = barbaz)\n"
2815             "{\n"
2816             "case quux:\n"
2817             "  return;\n"
2818             "}",
2819             format("switch(foo=barbaz){case quux:return;}", Style));
2820   EXPECT_EQ("try {\n"
2821             "  foo();\n"
2822             "} catch (\n"
2823             "    Exception &bar)\n"
2824             "{\n"
2825             "  baz();\n"
2826             "}",
2827             format("try{foo();}catch(Exception&bar){baz();}", Style));
2828   Style.ColumnLimit =
2829       40; // to concentrate at brace wrapping, not line wrap due to column limit
2830   EXPECT_EQ("try {\n"
2831             "  foo();\n"
2832             "} catch (Exception &bar) {\n"
2833             "  baz();\n"
2834             "}",
2835             format("try{foo();}catch(Exception&bar){baz();}", Style));
2836   Style.ColumnLimit =
2837       20; // to concentrate at brace wrapping, not line wrap due to column limit
2838 
2839   Style.BraceWrapping.BeforeElse = true;
2840   EXPECT_EQ(
2841       "if (foo) {\n"
2842       "  bar();\n"
2843       "}\n"
2844       "else if (baz ||\n"
2845       "         quux)\n"
2846       "{\n"
2847       "  foobar();\n"
2848       "}\n"
2849       "else {\n"
2850       "  barbaz();\n"
2851       "}",
2852       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2853              Style));
2854 
2855   Style.BraceWrapping.BeforeCatch = true;
2856   EXPECT_EQ("try {\n"
2857             "  foo();\n"
2858             "}\n"
2859             "catch (...) {\n"
2860             "  baz();\n"
2861             "}",
2862             format("try{foo();}catch(...){baz();}", Style));
2863 
2864   Style.BraceWrapping.AfterFunction = true;
2865   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2866   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
2867   Style.ColumnLimit = 80;
2868   verifyFormat("void shortfunction() { bar(); }", Style);
2869 
2870   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
2871   verifyFormat("void shortfunction()\n"
2872                "{\n"
2873                "  bar();\n"
2874                "}",
2875                Style);
2876 }
2877 
2878 TEST_F(FormatTest, BeforeWhile) {
2879   FormatStyle Style = getLLVMStyle();
2880   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2881 
2882   verifyFormat("do {\n"
2883                "  foo();\n"
2884                "} while (1);",
2885                Style);
2886   Style.BraceWrapping.BeforeWhile = true;
2887   verifyFormat("do {\n"
2888                "  foo();\n"
2889                "}\n"
2890                "while (1);",
2891                Style);
2892 }
2893 
2894 //===----------------------------------------------------------------------===//
2895 // Tests for classes, namespaces, etc.
2896 //===----------------------------------------------------------------------===//
2897 
2898 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2899   verifyFormat("class A {};");
2900 }
2901 
2902 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2903   verifyFormat("class A {\n"
2904                "public:\n"
2905                "public: // comment\n"
2906                "protected:\n"
2907                "private:\n"
2908                "  void f() {}\n"
2909                "};");
2910   verifyFormat("export class A {\n"
2911                "public:\n"
2912                "public: // comment\n"
2913                "protected:\n"
2914                "private:\n"
2915                "  void f() {}\n"
2916                "};");
2917   verifyGoogleFormat("class A {\n"
2918                      " public:\n"
2919                      " protected:\n"
2920                      " private:\n"
2921                      "  void f() {}\n"
2922                      "};");
2923   verifyGoogleFormat("export class A {\n"
2924                      " public:\n"
2925                      " protected:\n"
2926                      " private:\n"
2927                      "  void f() {}\n"
2928                      "};");
2929   verifyFormat("class A {\n"
2930                "public slots:\n"
2931                "  void f1() {}\n"
2932                "public Q_SLOTS:\n"
2933                "  void f2() {}\n"
2934                "protected slots:\n"
2935                "  void f3() {}\n"
2936                "protected Q_SLOTS:\n"
2937                "  void f4() {}\n"
2938                "private slots:\n"
2939                "  void f5() {}\n"
2940                "private Q_SLOTS:\n"
2941                "  void f6() {}\n"
2942                "signals:\n"
2943                "  void g1();\n"
2944                "Q_SIGNALS:\n"
2945                "  void g2();\n"
2946                "};");
2947 
2948   // Don't interpret 'signals' the wrong way.
2949   verifyFormat("signals.set();");
2950   verifyFormat("for (Signals signals : f()) {\n}");
2951   verifyFormat("{\n"
2952                "  signals.set(); // This needs indentation.\n"
2953                "}");
2954   verifyFormat("void f() {\n"
2955                "label:\n"
2956                "  signals.baz();\n"
2957                "}");
2958 }
2959 
2960 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2961   EXPECT_EQ("class A {\n"
2962             "public:\n"
2963             "  void f();\n"
2964             "\n"
2965             "private:\n"
2966             "  void g() {}\n"
2967             "  // test\n"
2968             "protected:\n"
2969             "  int h;\n"
2970             "};",
2971             format("class A {\n"
2972                    "public:\n"
2973                    "void f();\n"
2974                    "private:\n"
2975                    "void g() {}\n"
2976                    "// test\n"
2977                    "protected:\n"
2978                    "int h;\n"
2979                    "};"));
2980   EXPECT_EQ("class A {\n"
2981             "protected:\n"
2982             "public:\n"
2983             "  void f();\n"
2984             "};",
2985             format("class A {\n"
2986                    "protected:\n"
2987                    "\n"
2988                    "public:\n"
2989                    "\n"
2990                    "  void f();\n"
2991                    "};"));
2992 
2993   // Even ensure proper spacing inside macros.
2994   EXPECT_EQ("#define B     \\\n"
2995             "  class A {   \\\n"
2996             "   protected: \\\n"
2997             "   public:    \\\n"
2998             "    void f(); \\\n"
2999             "  };",
3000             format("#define B     \\\n"
3001                    "  class A {   \\\n"
3002                    "   protected: \\\n"
3003                    "              \\\n"
3004                    "   public:    \\\n"
3005                    "              \\\n"
3006                    "    void f(); \\\n"
3007                    "  };",
3008                    getGoogleStyle()));
3009   // But don't remove empty lines after macros ending in access specifiers.
3010   EXPECT_EQ("#define A private:\n"
3011             "\n"
3012             "int i;",
3013             format("#define A         private:\n"
3014                    "\n"
3015                    "int              i;"));
3016 }
3017 
3018 TEST_F(FormatTest, FormatsClasses) {
3019   verifyFormat("class A : public B {};");
3020   verifyFormat("class A : public ::B {};");
3021 
3022   verifyFormat(
3023       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3024       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3025   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3026                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3027                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3028   verifyFormat(
3029       "class A : public B, public C, public D, public E, public F {};");
3030   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3031                "                     public C,\n"
3032                "                     public D,\n"
3033                "                     public E,\n"
3034                "                     public F,\n"
3035                "                     public G {};");
3036 
3037   verifyFormat("class\n"
3038                "    ReallyReallyLongClassName {\n"
3039                "  int i;\n"
3040                "};",
3041                getLLVMStyleWithColumns(32));
3042   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3043                "                           aaaaaaaaaaaaaaaa> {};");
3044   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3045                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3046                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3047   verifyFormat("template <class R, class C>\n"
3048                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3049                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3050   verifyFormat("class ::A::B {};");
3051 }
3052 
3053 TEST_F(FormatTest, BreakInheritanceStyle) {
3054   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3055   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3056       FormatStyle::BILS_BeforeComma;
3057   verifyFormat("class MyClass : public X {};",
3058                StyleWithInheritanceBreakBeforeComma);
3059   verifyFormat("class MyClass\n"
3060                "    : public X\n"
3061                "    , public Y {};",
3062                StyleWithInheritanceBreakBeforeComma);
3063   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3064                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3065                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3066                StyleWithInheritanceBreakBeforeComma);
3067   verifyFormat("struct aaaaaaaaaaaaa\n"
3068                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3069                "          aaaaaaaaaaaaaaaa> {};",
3070                StyleWithInheritanceBreakBeforeComma);
3071 
3072   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3073   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3074       FormatStyle::BILS_AfterColon;
3075   verifyFormat("class MyClass : public X {};",
3076                StyleWithInheritanceBreakAfterColon);
3077   verifyFormat("class MyClass : public X, public Y {};",
3078                StyleWithInheritanceBreakAfterColon);
3079   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3080                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3081                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3082                StyleWithInheritanceBreakAfterColon);
3083   verifyFormat("struct aaaaaaaaaaaaa :\n"
3084                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3085                "        aaaaaaaaaaaaaaaa> {};",
3086                StyleWithInheritanceBreakAfterColon);
3087 
3088   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3089   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3090       FormatStyle::BILS_AfterComma;
3091   verifyFormat("class MyClass : public X {};",
3092                StyleWithInheritanceBreakAfterComma);
3093   verifyFormat("class MyClass : public X,\n"
3094                "                public Y {};",
3095                StyleWithInheritanceBreakAfterComma);
3096   verifyFormat(
3097       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3098       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3099       "{};",
3100       StyleWithInheritanceBreakAfterComma);
3101   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3102                "                           aaaaaaaaaaaaaaaa> {};",
3103                StyleWithInheritanceBreakAfterComma);
3104   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3105                "    : public OnceBreak,\n"
3106                "      public AlwaysBreak,\n"
3107                "      EvenBasesFitInOneLine {};",
3108                StyleWithInheritanceBreakAfterComma);
3109 }
3110 
3111 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3112   verifyFormat("class A {\n} a, b;");
3113   verifyFormat("struct A {\n} a, b;");
3114   verifyFormat("union A {\n} a;");
3115 }
3116 
3117 TEST_F(FormatTest, FormatsEnum) {
3118   verifyFormat("enum {\n"
3119                "  Zero,\n"
3120                "  One = 1,\n"
3121                "  Two = One + 1,\n"
3122                "  Three = (One + Two),\n"
3123                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3124                "  Five = (One, Two, Three, Four, 5)\n"
3125                "};");
3126   verifyGoogleFormat("enum {\n"
3127                      "  Zero,\n"
3128                      "  One = 1,\n"
3129                      "  Two = One + 1,\n"
3130                      "  Three = (One + Two),\n"
3131                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3132                      "  Five = (One, Two, Three, Four, 5)\n"
3133                      "};");
3134   verifyFormat("enum Enum {};");
3135   verifyFormat("enum {};");
3136   verifyFormat("enum X E {} d;");
3137   verifyFormat("enum __attribute__((...)) E {} d;");
3138   verifyFormat("enum __declspec__((...)) E {} d;");
3139   verifyFormat("enum {\n"
3140                "  Bar = Foo<int, int>::value\n"
3141                "};",
3142                getLLVMStyleWithColumns(30));
3143 
3144   verifyFormat("enum ShortEnum { A, B, C };");
3145   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3146 
3147   EXPECT_EQ("enum KeepEmptyLines {\n"
3148             "  ONE,\n"
3149             "\n"
3150             "  TWO,\n"
3151             "\n"
3152             "  THREE\n"
3153             "}",
3154             format("enum KeepEmptyLines {\n"
3155                    "  ONE,\n"
3156                    "\n"
3157                    "  TWO,\n"
3158                    "\n"
3159                    "\n"
3160                    "  THREE\n"
3161                    "}"));
3162   verifyFormat("enum E { // comment\n"
3163                "  ONE,\n"
3164                "  TWO\n"
3165                "};\n"
3166                "int i;");
3167 
3168   FormatStyle EightIndent = getLLVMStyle();
3169   EightIndent.IndentWidth = 8;
3170   verifyFormat("enum {\n"
3171                "        VOID,\n"
3172                "        CHAR,\n"
3173                "        SHORT,\n"
3174                "        INT,\n"
3175                "        LONG,\n"
3176                "        SIGNED,\n"
3177                "        UNSIGNED,\n"
3178                "        BOOL,\n"
3179                "        FLOAT,\n"
3180                "        DOUBLE,\n"
3181                "        COMPLEX\n"
3182                "};",
3183                EightIndent);
3184 
3185   // Not enums.
3186   verifyFormat("enum X f() {\n"
3187                "  a();\n"
3188                "  return 42;\n"
3189                "}");
3190   verifyFormat("enum X Type::f() {\n"
3191                "  a();\n"
3192                "  return 42;\n"
3193                "}");
3194   verifyFormat("enum ::X f() {\n"
3195                "  a();\n"
3196                "  return 42;\n"
3197                "}");
3198   verifyFormat("enum ns::X f() {\n"
3199                "  a();\n"
3200                "  return 42;\n"
3201                "}");
3202 }
3203 
3204 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3205   verifyFormat("enum Type {\n"
3206                "  One = 0; // These semicolons should be commas.\n"
3207                "  Two = 1;\n"
3208                "};");
3209   verifyFormat("namespace n {\n"
3210                "enum Type {\n"
3211                "  One,\n"
3212                "  Two, // missing };\n"
3213                "  int i;\n"
3214                "}\n"
3215                "void g() {}");
3216 }
3217 
3218 TEST_F(FormatTest, FormatsEnumStruct) {
3219   verifyFormat("enum struct {\n"
3220                "  Zero,\n"
3221                "  One = 1,\n"
3222                "  Two = One + 1,\n"
3223                "  Three = (One + Two),\n"
3224                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3225                "  Five = (One, Two, Three, Four, 5)\n"
3226                "};");
3227   verifyFormat("enum struct Enum {};");
3228   verifyFormat("enum struct {};");
3229   verifyFormat("enum struct X E {} d;");
3230   verifyFormat("enum struct __attribute__((...)) E {} d;");
3231   verifyFormat("enum struct __declspec__((...)) E {} d;");
3232   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3233 }
3234 
3235 TEST_F(FormatTest, FormatsEnumClass) {
3236   verifyFormat("enum class {\n"
3237                "  Zero,\n"
3238                "  One = 1,\n"
3239                "  Two = One + 1,\n"
3240                "  Three = (One + Two),\n"
3241                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3242                "  Five = (One, Two, Three, Four, 5)\n"
3243                "};");
3244   verifyFormat("enum class Enum {};");
3245   verifyFormat("enum class {};");
3246   verifyFormat("enum class X E {} d;");
3247   verifyFormat("enum class __attribute__((...)) E {} d;");
3248   verifyFormat("enum class __declspec__((...)) E {} d;");
3249   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3250 }
3251 
3252 TEST_F(FormatTest, FormatsEnumTypes) {
3253   verifyFormat("enum X : int {\n"
3254                "  A, // Force multiple lines.\n"
3255                "  B\n"
3256                "};");
3257   verifyFormat("enum X : int { A, B };");
3258   verifyFormat("enum X : std::uint32_t { A, B };");
3259 }
3260 
3261 TEST_F(FormatTest, FormatsTypedefEnum) {
3262   FormatStyle Style = getLLVMStyle();
3263   Style.ColumnLimit = 40;
3264   verifyFormat("typedef enum {} EmptyEnum;");
3265   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3266   verifyFormat("typedef enum {\n"
3267                "  ZERO = 0,\n"
3268                "  ONE = 1,\n"
3269                "  TWO = 2,\n"
3270                "  THREE = 3\n"
3271                "} LongEnum;",
3272                Style);
3273   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3274   Style.BraceWrapping.AfterEnum = true;
3275   verifyFormat("typedef enum {} EmptyEnum;");
3276   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3277   verifyFormat("typedef enum\n"
3278                "{\n"
3279                "  ZERO = 0,\n"
3280                "  ONE = 1,\n"
3281                "  TWO = 2,\n"
3282                "  THREE = 3\n"
3283                "} LongEnum;",
3284                Style);
3285 }
3286 
3287 TEST_F(FormatTest, FormatsNSEnums) {
3288   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3289   verifyGoogleFormat(
3290       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3291   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3292                      "  // Information about someDecentlyLongValue.\n"
3293                      "  someDecentlyLongValue,\n"
3294                      "  // Information about anotherDecentlyLongValue.\n"
3295                      "  anotherDecentlyLongValue,\n"
3296                      "  // Information about aThirdDecentlyLongValue.\n"
3297                      "  aThirdDecentlyLongValue\n"
3298                      "};");
3299   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3300                      "  // Information about someDecentlyLongValue.\n"
3301                      "  someDecentlyLongValue,\n"
3302                      "  // Information about anotherDecentlyLongValue.\n"
3303                      "  anotherDecentlyLongValue,\n"
3304                      "  // Information about aThirdDecentlyLongValue.\n"
3305                      "  aThirdDecentlyLongValue\n"
3306                      "};");
3307   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3308                      "  a = 1,\n"
3309                      "  b = 2,\n"
3310                      "  c = 3,\n"
3311                      "};");
3312   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3313                      "  a = 1,\n"
3314                      "  b = 2,\n"
3315                      "  c = 3,\n"
3316                      "};");
3317   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3318                      "  a = 1,\n"
3319                      "  b = 2,\n"
3320                      "  c = 3,\n"
3321                      "};");
3322   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3323                      "  a = 1,\n"
3324                      "  b = 2,\n"
3325                      "  c = 3,\n"
3326                      "};");
3327 }
3328 
3329 TEST_F(FormatTest, FormatsBitfields) {
3330   verifyFormat("struct Bitfields {\n"
3331                "  unsigned sClass : 8;\n"
3332                "  unsigned ValueKind : 2;\n"
3333                "};");
3334   verifyFormat("struct A {\n"
3335                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3336                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3337                "};");
3338   verifyFormat("struct MyStruct {\n"
3339                "  uchar data;\n"
3340                "  uchar : 8;\n"
3341                "  uchar : 8;\n"
3342                "  uchar other;\n"
3343                "};");
3344   FormatStyle Style = getLLVMStyle();
3345   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3346   verifyFormat("struct Bitfields {\n"
3347                "  unsigned sClass:8;\n"
3348                "  unsigned ValueKind:2;\n"
3349                "  uchar other;\n"
3350                "};",
3351                Style);
3352   verifyFormat("struct A {\n"
3353                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3354                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3355                "};",
3356                Style);
3357   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3358   verifyFormat("struct Bitfields {\n"
3359                "  unsigned sClass :8;\n"
3360                "  unsigned ValueKind :2;\n"
3361                "  uchar other;\n"
3362                "};",
3363                Style);
3364   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3365   verifyFormat("struct Bitfields {\n"
3366                "  unsigned sClass: 8;\n"
3367                "  unsigned ValueKind: 2;\n"
3368                "  uchar other;\n"
3369                "};",
3370                Style);
3371 }
3372 
3373 TEST_F(FormatTest, FormatsNamespaces) {
3374   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3375   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3376 
3377   verifyFormat("namespace some_namespace {\n"
3378                "class A {};\n"
3379                "void f() { f(); }\n"
3380                "}",
3381                LLVMWithNoNamespaceFix);
3382   verifyFormat("namespace N::inline D {\n"
3383                "class A {};\n"
3384                "void f() { f(); }\n"
3385                "}",
3386                LLVMWithNoNamespaceFix);
3387   verifyFormat("namespace N::inline D::E {\n"
3388                "class A {};\n"
3389                "void f() { f(); }\n"
3390                "}",
3391                LLVMWithNoNamespaceFix);
3392   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3393                "class A {};\n"
3394                "void f() { f(); }\n"
3395                "}",
3396                LLVMWithNoNamespaceFix);
3397   verifyFormat("/* something */ namespace some_namespace {\n"
3398                "class A {};\n"
3399                "void f() { f(); }\n"
3400                "}",
3401                LLVMWithNoNamespaceFix);
3402   verifyFormat("namespace {\n"
3403                "class A {};\n"
3404                "void f() { f(); }\n"
3405                "}",
3406                LLVMWithNoNamespaceFix);
3407   verifyFormat("/* something */ namespace {\n"
3408                "class A {};\n"
3409                "void f() { f(); }\n"
3410                "}",
3411                LLVMWithNoNamespaceFix);
3412   verifyFormat("inline namespace X {\n"
3413                "class A {};\n"
3414                "void f() { f(); }\n"
3415                "}",
3416                LLVMWithNoNamespaceFix);
3417   verifyFormat("/* something */ inline namespace X {\n"
3418                "class A {};\n"
3419                "void f() { f(); }\n"
3420                "}",
3421                LLVMWithNoNamespaceFix);
3422   verifyFormat("export namespace X {\n"
3423                "class A {};\n"
3424                "void f() { f(); }\n"
3425                "}",
3426                LLVMWithNoNamespaceFix);
3427   verifyFormat("using namespace some_namespace;\n"
3428                "class A {};\n"
3429                "void f() { f(); }",
3430                LLVMWithNoNamespaceFix);
3431 
3432   // This code is more common than we thought; if we
3433   // layout this correctly the semicolon will go into
3434   // its own line, which is undesirable.
3435   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3436   verifyFormat("namespace {\n"
3437                "class A {};\n"
3438                "};",
3439                LLVMWithNoNamespaceFix);
3440 
3441   verifyFormat("namespace {\n"
3442                "int SomeVariable = 0; // comment\n"
3443                "} // namespace",
3444                LLVMWithNoNamespaceFix);
3445   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3446             "#define HEADER_GUARD\n"
3447             "namespace my_namespace {\n"
3448             "int i;\n"
3449             "} // my_namespace\n"
3450             "#endif // HEADER_GUARD",
3451             format("#ifndef HEADER_GUARD\n"
3452                    " #define HEADER_GUARD\n"
3453                    "   namespace my_namespace {\n"
3454                    "int i;\n"
3455                    "}    // my_namespace\n"
3456                    "#endif    // HEADER_GUARD",
3457                    LLVMWithNoNamespaceFix));
3458 
3459   EXPECT_EQ("namespace A::B {\n"
3460             "class C {};\n"
3461             "}",
3462             format("namespace A::B {\n"
3463                    "class C {};\n"
3464                    "}",
3465                    LLVMWithNoNamespaceFix));
3466 
3467   FormatStyle Style = getLLVMStyle();
3468   Style.NamespaceIndentation = FormatStyle::NI_All;
3469   EXPECT_EQ("namespace out {\n"
3470             "  int i;\n"
3471             "  namespace in {\n"
3472             "    int i;\n"
3473             "  } // namespace in\n"
3474             "} // namespace out",
3475             format("namespace out {\n"
3476                    "int i;\n"
3477                    "namespace in {\n"
3478                    "int i;\n"
3479                    "} // namespace in\n"
3480                    "} // namespace out",
3481                    Style));
3482 
3483   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3484   EXPECT_EQ("namespace out {\n"
3485             "int i;\n"
3486             "namespace in {\n"
3487             "  int i;\n"
3488             "} // namespace in\n"
3489             "} // namespace out",
3490             format("namespace out {\n"
3491                    "int i;\n"
3492                    "namespace in {\n"
3493                    "int i;\n"
3494                    "} // namespace in\n"
3495                    "} // namespace out",
3496                    Style));
3497 }
3498 
3499 TEST_F(FormatTest, NamespaceMacros) {
3500   FormatStyle Style = getLLVMStyle();
3501   Style.NamespaceMacros.push_back("TESTSUITE");
3502 
3503   verifyFormat("TESTSUITE(A) {\n"
3504                "int foo();\n"
3505                "} // TESTSUITE(A)",
3506                Style);
3507 
3508   verifyFormat("TESTSUITE(A, B) {\n"
3509                "int foo();\n"
3510                "} // TESTSUITE(A)",
3511                Style);
3512 
3513   // Properly indent according to NamespaceIndentation style
3514   Style.NamespaceIndentation = FormatStyle::NI_All;
3515   verifyFormat("TESTSUITE(A) {\n"
3516                "  int foo();\n"
3517                "} // TESTSUITE(A)",
3518                Style);
3519   verifyFormat("TESTSUITE(A) {\n"
3520                "  namespace B {\n"
3521                "    int foo();\n"
3522                "  } // namespace B\n"
3523                "} // TESTSUITE(A)",
3524                Style);
3525   verifyFormat("namespace A {\n"
3526                "  TESTSUITE(B) {\n"
3527                "    int foo();\n"
3528                "  } // TESTSUITE(B)\n"
3529                "} // namespace A",
3530                Style);
3531 
3532   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3533   verifyFormat("TESTSUITE(A) {\n"
3534                "TESTSUITE(B) {\n"
3535                "  int foo();\n"
3536                "} // TESTSUITE(B)\n"
3537                "} // TESTSUITE(A)",
3538                Style);
3539   verifyFormat("TESTSUITE(A) {\n"
3540                "namespace B {\n"
3541                "  int foo();\n"
3542                "} // namespace B\n"
3543                "} // TESTSUITE(A)",
3544                Style);
3545   verifyFormat("namespace A {\n"
3546                "TESTSUITE(B) {\n"
3547                "  int foo();\n"
3548                "} // TESTSUITE(B)\n"
3549                "} // namespace A",
3550                Style);
3551 
3552   // Properly merge namespace-macros blocks in CompactNamespaces mode
3553   Style.NamespaceIndentation = FormatStyle::NI_None;
3554   Style.CompactNamespaces = true;
3555   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3556                "}} // TESTSUITE(A::B)",
3557                Style);
3558 
3559   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3560             "}} // TESTSUITE(out::in)",
3561             format("TESTSUITE(out) {\n"
3562                    "TESTSUITE(in) {\n"
3563                    "} // TESTSUITE(in)\n"
3564                    "} // TESTSUITE(out)",
3565                    Style));
3566 
3567   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3568             "}} // TESTSUITE(out::in)",
3569             format("TESTSUITE(out) {\n"
3570                    "TESTSUITE(in) {\n"
3571                    "} // TESTSUITE(in)\n"
3572                    "} // TESTSUITE(out)",
3573                    Style));
3574 
3575   // Do not merge different namespaces/macros
3576   EXPECT_EQ("namespace out {\n"
3577             "TESTSUITE(in) {\n"
3578             "} // TESTSUITE(in)\n"
3579             "} // namespace out",
3580             format("namespace out {\n"
3581                    "TESTSUITE(in) {\n"
3582                    "} // TESTSUITE(in)\n"
3583                    "} // namespace out",
3584                    Style));
3585   EXPECT_EQ("TESTSUITE(out) {\n"
3586             "namespace in {\n"
3587             "} // namespace in\n"
3588             "} // TESTSUITE(out)",
3589             format("TESTSUITE(out) {\n"
3590                    "namespace in {\n"
3591                    "} // namespace in\n"
3592                    "} // TESTSUITE(out)",
3593                    Style));
3594   Style.NamespaceMacros.push_back("FOOBAR");
3595   EXPECT_EQ("TESTSUITE(out) {\n"
3596             "FOOBAR(in) {\n"
3597             "} // FOOBAR(in)\n"
3598             "} // TESTSUITE(out)",
3599             format("TESTSUITE(out) {\n"
3600                    "FOOBAR(in) {\n"
3601                    "} // FOOBAR(in)\n"
3602                    "} // TESTSUITE(out)",
3603                    Style));
3604 }
3605 
3606 TEST_F(FormatTest, FormatsCompactNamespaces) {
3607   FormatStyle Style = getLLVMStyle();
3608   Style.CompactNamespaces = true;
3609   Style.NamespaceMacros.push_back("TESTSUITE");
3610 
3611   verifyFormat("namespace A { namespace B {\n"
3612                "}} // namespace A::B",
3613                Style);
3614 
3615   EXPECT_EQ("namespace out { namespace in {\n"
3616             "}} // namespace out::in",
3617             format("namespace out {\n"
3618                    "namespace in {\n"
3619                    "} // namespace in\n"
3620                    "} // namespace out",
3621                    Style));
3622 
3623   // Only namespaces which have both consecutive opening and end get compacted
3624   EXPECT_EQ("namespace out {\n"
3625             "namespace in1 {\n"
3626             "} // namespace in1\n"
3627             "namespace in2 {\n"
3628             "} // namespace in2\n"
3629             "} // namespace out",
3630             format("namespace out {\n"
3631                    "namespace in1 {\n"
3632                    "} // namespace in1\n"
3633                    "namespace in2 {\n"
3634                    "} // namespace in2\n"
3635                    "} // namespace out",
3636                    Style));
3637 
3638   EXPECT_EQ("namespace out {\n"
3639             "int i;\n"
3640             "namespace in {\n"
3641             "int j;\n"
3642             "} // namespace in\n"
3643             "int k;\n"
3644             "} // namespace out",
3645             format("namespace out { int i;\n"
3646                    "namespace in { int j; } // namespace in\n"
3647                    "int k; } // namespace out",
3648                    Style));
3649 
3650   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3651             "}}} // namespace A::B::C\n",
3652             format("namespace A { namespace B {\n"
3653                    "namespace C {\n"
3654                    "}} // namespace B::C\n"
3655                    "} // namespace A\n",
3656                    Style));
3657 
3658   Style.ColumnLimit = 40;
3659   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3660             "namespace bbbbbbbbbb {\n"
3661             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3662             format("namespace aaaaaaaaaa {\n"
3663                    "namespace bbbbbbbbbb {\n"
3664                    "} // namespace bbbbbbbbbb\n"
3665                    "} // namespace aaaaaaaaaa",
3666                    Style));
3667 
3668   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3669             "namespace cccccc {\n"
3670             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3671             format("namespace aaaaaa {\n"
3672                    "namespace bbbbbb {\n"
3673                    "namespace cccccc {\n"
3674                    "} // namespace cccccc\n"
3675                    "} // namespace bbbbbb\n"
3676                    "} // namespace aaaaaa",
3677                    Style));
3678   Style.ColumnLimit = 80;
3679 
3680   // Extra semicolon after 'inner' closing brace prevents merging
3681   EXPECT_EQ("namespace out { namespace in {\n"
3682             "}; } // namespace out::in",
3683             format("namespace out {\n"
3684                    "namespace in {\n"
3685                    "}; // namespace in\n"
3686                    "} // namespace out",
3687                    Style));
3688 
3689   // Extra semicolon after 'outer' closing brace is conserved
3690   EXPECT_EQ("namespace out { namespace in {\n"
3691             "}}; // namespace out::in",
3692             format("namespace out {\n"
3693                    "namespace in {\n"
3694                    "} // namespace in\n"
3695                    "}; // namespace out",
3696                    Style));
3697 
3698   Style.NamespaceIndentation = FormatStyle::NI_All;
3699   EXPECT_EQ("namespace out { namespace in {\n"
3700             "  int i;\n"
3701             "}} // namespace out::in",
3702             format("namespace out {\n"
3703                    "namespace in {\n"
3704                    "int i;\n"
3705                    "} // namespace in\n"
3706                    "} // namespace out",
3707                    Style));
3708   EXPECT_EQ("namespace out { namespace mid {\n"
3709             "  namespace in {\n"
3710             "    int j;\n"
3711             "  } // namespace in\n"
3712             "  int k;\n"
3713             "}} // namespace out::mid",
3714             format("namespace out { namespace mid {\n"
3715                    "namespace in { int j; } // namespace in\n"
3716                    "int k; }} // namespace out::mid",
3717                    Style));
3718 
3719   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3720   EXPECT_EQ("namespace out { namespace in {\n"
3721             "  int i;\n"
3722             "}} // namespace out::in",
3723             format("namespace out {\n"
3724                    "namespace in {\n"
3725                    "int i;\n"
3726                    "} // namespace in\n"
3727                    "} // namespace out",
3728                    Style));
3729   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3730             "  int i;\n"
3731             "}}} // namespace out::mid::in",
3732             format("namespace out {\n"
3733                    "namespace mid {\n"
3734                    "namespace in {\n"
3735                    "int i;\n"
3736                    "} // namespace in\n"
3737                    "} // namespace mid\n"
3738                    "} // namespace out",
3739                    Style));
3740 }
3741 
3742 TEST_F(FormatTest, FormatsExternC) {
3743   verifyFormat("extern \"C\" {\nint a;");
3744   verifyFormat("extern \"C\" {}");
3745   verifyFormat("extern \"C\" {\n"
3746                "int foo();\n"
3747                "}");
3748   verifyFormat("extern \"C\" int foo() {}");
3749   verifyFormat("extern \"C\" int foo();");
3750   verifyFormat("extern \"C\" int foo() {\n"
3751                "  int i = 42;\n"
3752                "  return i;\n"
3753                "}");
3754 
3755   FormatStyle Style = getLLVMStyle();
3756   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3757   Style.BraceWrapping.AfterFunction = true;
3758   verifyFormat("extern \"C\" int foo() {}", Style);
3759   verifyFormat("extern \"C\" int foo();", Style);
3760   verifyFormat("extern \"C\" int foo()\n"
3761                "{\n"
3762                "  int i = 42;\n"
3763                "  return i;\n"
3764                "}",
3765                Style);
3766 
3767   Style.BraceWrapping.AfterExternBlock = true;
3768   Style.BraceWrapping.SplitEmptyRecord = false;
3769   verifyFormat("extern \"C\"\n"
3770                "{}",
3771                Style);
3772   verifyFormat("extern \"C\"\n"
3773                "{\n"
3774                "  int foo();\n"
3775                "}",
3776                Style);
3777 }
3778 
3779 TEST_F(FormatTest, IndentExternBlockStyle) {
3780   FormatStyle Style = getLLVMStyle();
3781   Style.IndentWidth = 2;
3782 
3783   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3784   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3785   verifyFormat("extern \"C\" {\n"
3786                "  int foo10();\n"
3787                "}",
3788                Style);
3789 
3790   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3791   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3792   verifyFormat("extern \"C\" {\n"
3793                "int foo12();\n"
3794                "}",
3795                Style);
3796 
3797   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3798   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3799   Style.BraceWrapping.AfterExternBlock = true;
3800   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3801   verifyFormat("extern \"C\"\n{\n"
3802                "  int foo14();\n"
3803                "}",
3804                Style);
3805 
3806   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3807   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3808   Style.BraceWrapping.AfterExternBlock = false;
3809   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3810   verifyFormat("extern \"C\" {\n"
3811                "int foo16();\n"
3812                "}",
3813                Style);
3814 }
3815 
3816 TEST_F(FormatTest, FormatsInlineASM) {
3817   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3818   verifyFormat("asm(\"nop\" ::: \"memory\");");
3819   verifyFormat(
3820       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3821       "    \"cpuid\\n\\t\"\n"
3822       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3823       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3824       "    : \"a\"(value));");
3825   EXPECT_EQ(
3826       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3827       "  __asm {\n"
3828       "        mov     edx,[that] // vtable in edx\n"
3829       "        mov     eax,methodIndex\n"
3830       "        call    [edx][eax*4] // stdcall\n"
3831       "  }\n"
3832       "}",
3833       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3834              "    __asm {\n"
3835              "        mov     edx,[that] // vtable in edx\n"
3836              "        mov     eax,methodIndex\n"
3837              "        call    [edx][eax*4] // stdcall\n"
3838              "    }\n"
3839              "}"));
3840   EXPECT_EQ("_asm {\n"
3841             "  xor eax, eax;\n"
3842             "  cpuid;\n"
3843             "}",
3844             format("_asm {\n"
3845                    "  xor eax, eax;\n"
3846                    "  cpuid;\n"
3847                    "}"));
3848   verifyFormat("void function() {\n"
3849                "  // comment\n"
3850                "  asm(\"\");\n"
3851                "}");
3852   EXPECT_EQ("__asm {\n"
3853             "}\n"
3854             "int i;",
3855             format("__asm   {\n"
3856                    "}\n"
3857                    "int   i;"));
3858 }
3859 
3860 TEST_F(FormatTest, FormatTryCatch) {
3861   verifyFormat("try {\n"
3862                "  throw a * b;\n"
3863                "} catch (int a) {\n"
3864                "  // Do nothing.\n"
3865                "} catch (...) {\n"
3866                "  exit(42);\n"
3867                "}");
3868 
3869   // Function-level try statements.
3870   verifyFormat("int f() try { return 4; } catch (...) {\n"
3871                "  return 5;\n"
3872                "}");
3873   verifyFormat("class A {\n"
3874                "  int a;\n"
3875                "  A() try : a(0) {\n"
3876                "  } catch (...) {\n"
3877                "    throw;\n"
3878                "  }\n"
3879                "};\n");
3880   verifyFormat("class A {\n"
3881                "  int a;\n"
3882                "  A() try : a(0), b{1} {\n"
3883                "  } catch (...) {\n"
3884                "    throw;\n"
3885                "  }\n"
3886                "};\n");
3887   verifyFormat("class A {\n"
3888                "  int a;\n"
3889                "  A() try : a(0), b{1}, c{2} {\n"
3890                "  } catch (...) {\n"
3891                "    throw;\n"
3892                "  }\n"
3893                "};\n");
3894   verifyFormat("class A {\n"
3895                "  int a;\n"
3896                "  A() try : a(0), b{1}, c{2} {\n"
3897                "    { // New scope.\n"
3898                "    }\n"
3899                "  } catch (...) {\n"
3900                "    throw;\n"
3901                "  }\n"
3902                "};\n");
3903 
3904   // Incomplete try-catch blocks.
3905   verifyIncompleteFormat("try {} catch (");
3906 }
3907 
3908 TEST_F(FormatTest, FormatTryAsAVariable) {
3909   verifyFormat("int try;");
3910   verifyFormat("int try, size;");
3911   verifyFormat("try = foo();");
3912   verifyFormat("if (try < size) {\n  return true;\n}");
3913 
3914   verifyFormat("int catch;");
3915   verifyFormat("int catch, size;");
3916   verifyFormat("catch = foo();");
3917   verifyFormat("if (catch < size) {\n  return true;\n}");
3918 
3919   FormatStyle Style = getLLVMStyle();
3920   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3921   Style.BraceWrapping.AfterFunction = true;
3922   Style.BraceWrapping.BeforeCatch = true;
3923   verifyFormat("try {\n"
3924                "  int bar = 1;\n"
3925                "}\n"
3926                "catch (...) {\n"
3927                "  int bar = 1;\n"
3928                "}",
3929                Style);
3930   verifyFormat("#if NO_EX\n"
3931                "try\n"
3932                "#endif\n"
3933                "{\n"
3934                "}\n"
3935                "#if NO_EX\n"
3936                "catch (...) {\n"
3937                "}",
3938                Style);
3939   verifyFormat("try /* abc */ {\n"
3940                "  int bar = 1;\n"
3941                "}\n"
3942                "catch (...) {\n"
3943                "  int bar = 1;\n"
3944                "}",
3945                Style);
3946   verifyFormat("try\n"
3947                "// abc\n"
3948                "{\n"
3949                "  int bar = 1;\n"
3950                "}\n"
3951                "catch (...) {\n"
3952                "  int bar = 1;\n"
3953                "}",
3954                Style);
3955 }
3956 
3957 TEST_F(FormatTest, FormatSEHTryCatch) {
3958   verifyFormat("__try {\n"
3959                "  int a = b * c;\n"
3960                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3961                "  // Do nothing.\n"
3962                "}");
3963 
3964   verifyFormat("__try {\n"
3965                "  int a = b * c;\n"
3966                "} __finally {\n"
3967                "  // Do nothing.\n"
3968                "}");
3969 
3970   verifyFormat("DEBUG({\n"
3971                "  __try {\n"
3972                "  } __finally {\n"
3973                "  }\n"
3974                "});\n");
3975 }
3976 
3977 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3978   verifyFormat("try {\n"
3979                "  f();\n"
3980                "} catch {\n"
3981                "  g();\n"
3982                "}");
3983   verifyFormat("try {\n"
3984                "  f();\n"
3985                "} catch (A a) MACRO(x) {\n"
3986                "  g();\n"
3987                "} catch (B b) MACRO(x) {\n"
3988                "  g();\n"
3989                "}");
3990 }
3991 
3992 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3993   FormatStyle Style = getLLVMStyle();
3994   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3995                           FormatStyle::BS_WebKit}) {
3996     Style.BreakBeforeBraces = BraceStyle;
3997     verifyFormat("try {\n"
3998                  "  // something\n"
3999                  "} catch (...) {\n"
4000                  "  // something\n"
4001                  "}",
4002                  Style);
4003   }
4004   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4005   verifyFormat("try {\n"
4006                "  // something\n"
4007                "}\n"
4008                "catch (...) {\n"
4009                "  // something\n"
4010                "}",
4011                Style);
4012   verifyFormat("__try {\n"
4013                "  // something\n"
4014                "}\n"
4015                "__finally {\n"
4016                "  // something\n"
4017                "}",
4018                Style);
4019   verifyFormat("@try {\n"
4020                "  // something\n"
4021                "}\n"
4022                "@finally {\n"
4023                "  // something\n"
4024                "}",
4025                Style);
4026   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4027   verifyFormat("try\n"
4028                "{\n"
4029                "  // something\n"
4030                "}\n"
4031                "catch (...)\n"
4032                "{\n"
4033                "  // something\n"
4034                "}",
4035                Style);
4036   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4037   verifyFormat("try\n"
4038                "  {\n"
4039                "  // something white\n"
4040                "  }\n"
4041                "catch (...)\n"
4042                "  {\n"
4043                "  // something white\n"
4044                "  }",
4045                Style);
4046   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4047   verifyFormat("try\n"
4048                "  {\n"
4049                "    // something\n"
4050                "  }\n"
4051                "catch (...)\n"
4052                "  {\n"
4053                "    // something\n"
4054                "  }",
4055                Style);
4056   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4057   Style.BraceWrapping.BeforeCatch = true;
4058   verifyFormat("try {\n"
4059                "  // something\n"
4060                "}\n"
4061                "catch (...) {\n"
4062                "  // something\n"
4063                "}",
4064                Style);
4065 }
4066 
4067 TEST_F(FormatTest, StaticInitializers) {
4068   verifyFormat("static SomeClass SC = {1, 'a'};");
4069 
4070   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4071                "    100000000, "
4072                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4073 
4074   // Here, everything other than the "}" would fit on a line.
4075   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4076                "    10000000000000000000000000};");
4077   EXPECT_EQ("S s = {a,\n"
4078             "\n"
4079             "       b};",
4080             format("S s = {\n"
4081                    "  a,\n"
4082                    "\n"
4083                    "  b\n"
4084                    "};"));
4085 
4086   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4087   // line. However, the formatting looks a bit off and this probably doesn't
4088   // happen often in practice.
4089   verifyFormat("static int Variable[1] = {\n"
4090                "    {1000000000000000000000000000000000000}};",
4091                getLLVMStyleWithColumns(40));
4092 }
4093 
4094 TEST_F(FormatTest, DesignatedInitializers) {
4095   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4096   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4097                "                    .bbbbbbbbbb = 2,\n"
4098                "                    .cccccccccc = 3,\n"
4099                "                    .dddddddddd = 4,\n"
4100                "                    .eeeeeeeeee = 5};");
4101   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4102                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4103                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4104                "    .ccccccccccccccccccccccccccc = 3,\n"
4105                "    .ddddddddddddddddddddddddddd = 4,\n"
4106                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4107 
4108   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4109 
4110   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4111   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4112                "                    [2] = bbbbbbbbbb,\n"
4113                "                    [3] = cccccccccc,\n"
4114                "                    [4] = dddddddddd,\n"
4115                "                    [5] = eeeeeeeeee};");
4116   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4117                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4118                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4119                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4120                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4121                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4122 }
4123 
4124 TEST_F(FormatTest, NestedStaticInitializers) {
4125   verifyFormat("static A x = {{{}}};\n");
4126   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4127                "               {init1, init2, init3, init4}}};",
4128                getLLVMStyleWithColumns(50));
4129 
4130   verifyFormat("somes Status::global_reps[3] = {\n"
4131                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4132                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4133                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4134                getLLVMStyleWithColumns(60));
4135   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4136                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4137                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4138                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4139   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4140                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4141                "rect.fTop}};");
4142 
4143   verifyFormat(
4144       "SomeArrayOfSomeType a = {\n"
4145       "    {{1, 2, 3},\n"
4146       "     {1, 2, 3},\n"
4147       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4148       "      333333333333333333333333333333},\n"
4149       "     {1, 2, 3},\n"
4150       "     {1, 2, 3}}};");
4151   verifyFormat(
4152       "SomeArrayOfSomeType a = {\n"
4153       "    {{1, 2, 3}},\n"
4154       "    {{1, 2, 3}},\n"
4155       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4156       "      333333333333333333333333333333}},\n"
4157       "    {{1, 2, 3}},\n"
4158       "    {{1, 2, 3}}};");
4159 
4160   verifyFormat("struct {\n"
4161                "  unsigned bit;\n"
4162                "  const char *const name;\n"
4163                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4164                "                 {kOsWin, \"Windows\"},\n"
4165                "                 {kOsLinux, \"Linux\"},\n"
4166                "                 {kOsCrOS, \"Chrome OS\"}};");
4167   verifyFormat("struct {\n"
4168                "  unsigned bit;\n"
4169                "  const char *const name;\n"
4170                "} kBitsToOs[] = {\n"
4171                "    {kOsMac, \"Mac\"},\n"
4172                "    {kOsWin, \"Windows\"},\n"
4173                "    {kOsLinux, \"Linux\"},\n"
4174                "    {kOsCrOS, \"Chrome OS\"},\n"
4175                "};");
4176 }
4177 
4178 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4179   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4180                "                      \\\n"
4181                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4182 }
4183 
4184 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4185   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4186                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4187 
4188   // Do break defaulted and deleted functions.
4189   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4190                "    default;",
4191                getLLVMStyleWithColumns(40));
4192   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4193                "    delete;",
4194                getLLVMStyleWithColumns(40));
4195 }
4196 
4197 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4198   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4199                getLLVMStyleWithColumns(40));
4200   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4201                getLLVMStyleWithColumns(40));
4202   EXPECT_EQ("#define Q                              \\\n"
4203             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4204             "  \"aaaaaaaa.cpp\"",
4205             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4206                    getLLVMStyleWithColumns(40)));
4207 }
4208 
4209 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4210   EXPECT_EQ("# 123 \"A string literal\"",
4211             format("   #     123    \"A string literal\""));
4212 }
4213 
4214 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4215   EXPECT_EQ("#;", format("#;"));
4216   verifyFormat("#\n;\n;\n;");
4217 }
4218 
4219 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4220   EXPECT_EQ("#line 42 \"test\"\n",
4221             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4222   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4223                                     getLLVMStyleWithColumns(12)));
4224 }
4225 
4226 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4227   EXPECT_EQ("#line 42 \"test\"",
4228             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4229   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4230 }
4231 
4232 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4233   verifyFormat("#define A \\x20");
4234   verifyFormat("#define A \\ x20");
4235   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4236   verifyFormat("#define A ''");
4237   verifyFormat("#define A ''qqq");
4238   verifyFormat("#define A `qqq");
4239   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4240   EXPECT_EQ("const char *c = STRINGIFY(\n"
4241             "\\na : b);",
4242             format("const char * c = STRINGIFY(\n"
4243                    "\\na : b);"));
4244 
4245   verifyFormat("a\r\\");
4246   verifyFormat("a\v\\");
4247   verifyFormat("a\f\\");
4248 }
4249 
4250 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4251   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4252   style.IndentWidth = 4;
4253   style.PPIndentWidth = 1;
4254 
4255   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4256   verifyFormat("#ifdef __linux__\n"
4257                "void foo() {\n"
4258                "    int x = 0;\n"
4259                "}\n"
4260                "#define FOO\n"
4261                "#endif\n"
4262                "void bar() {\n"
4263                "    int y = 0;\n"
4264                "}\n",
4265                style);
4266 
4267   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4268   verifyFormat("#ifdef __linux__\n"
4269                "void foo() {\n"
4270                "    int x = 0;\n"
4271                "}\n"
4272                "# define FOO foo\n"
4273                "#endif\n"
4274                "void bar() {\n"
4275                "    int y = 0;\n"
4276                "}\n",
4277                style);
4278 
4279   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4280   verifyFormat("#ifdef __linux__\n"
4281                "void foo() {\n"
4282                "    int x = 0;\n"
4283                "}\n"
4284                " #define FOO foo\n"
4285                "#endif\n"
4286                "void bar() {\n"
4287                "    int y = 0;\n"
4288                "}\n",
4289                style);
4290 }
4291 
4292 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4293   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4294   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4295   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4296   // FIXME: We never break before the macro name.
4297   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4298 
4299   verifyFormat("#define A A\n#define A A");
4300   verifyFormat("#define A(X) A\n#define A A");
4301 
4302   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4303   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4304 }
4305 
4306 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4307   EXPECT_EQ("// somecomment\n"
4308             "#include \"a.h\"\n"
4309             "#define A(  \\\n"
4310             "    A, B)\n"
4311             "#include \"b.h\"\n"
4312             "// somecomment\n",
4313             format("  // somecomment\n"
4314                    "  #include \"a.h\"\n"
4315                    "#define A(A,\\\n"
4316                    "    B)\n"
4317                    "    #include \"b.h\"\n"
4318                    " // somecomment\n",
4319                    getLLVMStyleWithColumns(13)));
4320 }
4321 
4322 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4323 
4324 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4325   EXPECT_EQ("#define A    \\\n"
4326             "  c;         \\\n"
4327             "  e;\n"
4328             "f;",
4329             format("#define A c; e;\n"
4330                    "f;",
4331                    getLLVMStyleWithColumns(14)));
4332 }
4333 
4334 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4335 
4336 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4337   EXPECT_EQ("int x,\n"
4338             "#define A\n"
4339             "    y;",
4340             format("int x,\n#define A\ny;"));
4341 }
4342 
4343 TEST_F(FormatTest, HashInMacroDefinition) {
4344   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4345   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4346   verifyFormat("#define A  \\\n"
4347                "  {        \\\n"
4348                "    f(#c); \\\n"
4349                "  }",
4350                getLLVMStyleWithColumns(11));
4351 
4352   verifyFormat("#define A(X)         \\\n"
4353                "  void function##X()",
4354                getLLVMStyleWithColumns(22));
4355 
4356   verifyFormat("#define A(a, b, c)   \\\n"
4357                "  void a##b##c()",
4358                getLLVMStyleWithColumns(22));
4359 
4360   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4361 }
4362 
4363 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4364   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4365   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4366 
4367   FormatStyle Style = getLLVMStyle();
4368   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4369   verifyFormat("#define true ((foo)1)", Style);
4370   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4371   verifyFormat("#define false((foo)0)", Style);
4372 }
4373 
4374 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4375   EXPECT_EQ("#define A b;", format("#define A \\\n"
4376                                    "          \\\n"
4377                                    "  b;",
4378                                    getLLVMStyleWithColumns(25)));
4379   EXPECT_EQ("#define A \\\n"
4380             "          \\\n"
4381             "  a;      \\\n"
4382             "  b;",
4383             format("#define A \\\n"
4384                    "          \\\n"
4385                    "  a;      \\\n"
4386                    "  b;",
4387                    getLLVMStyleWithColumns(11)));
4388   EXPECT_EQ("#define A \\\n"
4389             "  a;      \\\n"
4390             "          \\\n"
4391             "  b;",
4392             format("#define A \\\n"
4393                    "  a;      \\\n"
4394                    "          \\\n"
4395                    "  b;",
4396                    getLLVMStyleWithColumns(11)));
4397 }
4398 
4399 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4400   verifyIncompleteFormat("#define A :");
4401   verifyFormat("#define SOMECASES  \\\n"
4402                "  case 1:          \\\n"
4403                "  case 2\n",
4404                getLLVMStyleWithColumns(20));
4405   verifyFormat("#define MACRO(a) \\\n"
4406                "  if (a)         \\\n"
4407                "    f();         \\\n"
4408                "  else           \\\n"
4409                "    g()",
4410                getLLVMStyleWithColumns(18));
4411   verifyFormat("#define A template <typename T>");
4412   verifyIncompleteFormat("#define STR(x) #x\n"
4413                          "f(STR(this_is_a_string_literal{));");
4414   verifyFormat("#pragma omp threadprivate( \\\n"
4415                "    y)), // expected-warning",
4416                getLLVMStyleWithColumns(28));
4417   verifyFormat("#d, = };");
4418   verifyFormat("#if \"a");
4419   verifyIncompleteFormat("({\n"
4420                          "#define b     \\\n"
4421                          "  }           \\\n"
4422                          "  a\n"
4423                          "a",
4424                          getLLVMStyleWithColumns(15));
4425   verifyFormat("#define A     \\\n"
4426                "  {           \\\n"
4427                "    {\n"
4428                "#define B     \\\n"
4429                "  }           \\\n"
4430                "  }",
4431                getLLVMStyleWithColumns(15));
4432   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4433   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4434   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4435   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4436 }
4437 
4438 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4439   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4440   EXPECT_EQ("class A : public QObject {\n"
4441             "  Q_OBJECT\n"
4442             "\n"
4443             "  A() {}\n"
4444             "};",
4445             format("class A  :  public QObject {\n"
4446                    "     Q_OBJECT\n"
4447                    "\n"
4448                    "  A() {\n}\n"
4449                    "}  ;"));
4450   EXPECT_EQ("MACRO\n"
4451             "/*static*/ int i;",
4452             format("MACRO\n"
4453                    " /*static*/ int   i;"));
4454   EXPECT_EQ("SOME_MACRO\n"
4455             "namespace {\n"
4456             "void f();\n"
4457             "} // namespace",
4458             format("SOME_MACRO\n"
4459                    "  namespace    {\n"
4460                    "void   f(  );\n"
4461                    "} // namespace"));
4462   // Only if the identifier contains at least 5 characters.
4463   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4464   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4465   // Only if everything is upper case.
4466   EXPECT_EQ("class A : public QObject {\n"
4467             "  Q_Object A() {}\n"
4468             "};",
4469             format("class A  :  public QObject {\n"
4470                    "     Q_Object\n"
4471                    "  A() {\n}\n"
4472                    "}  ;"));
4473 
4474   // Only if the next line can actually start an unwrapped line.
4475   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4476             format("SOME_WEIRD_LOG_MACRO\n"
4477                    "<< SomeThing;"));
4478 
4479   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4480                "(n, buffers))\n",
4481                getChromiumStyle(FormatStyle::LK_Cpp));
4482 
4483   // See PR41483
4484   EXPECT_EQ("/**/ FOO(a)\n"
4485             "FOO(b)",
4486             format("/**/ FOO(a)\n"
4487                    "FOO(b)"));
4488 }
4489 
4490 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4491   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4492             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4493             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4494             "class X {};\n"
4495             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4496             "int *createScopDetectionPass() { return 0; }",
4497             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4498                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4499                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4500                    "  class X {};\n"
4501                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4502                    "  int *createScopDetectionPass() { return 0; }"));
4503   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4504   // braces, so that inner block is indented one level more.
4505   EXPECT_EQ("int q() {\n"
4506             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4507             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4508             "  IPC_END_MESSAGE_MAP()\n"
4509             "}",
4510             format("int q() {\n"
4511                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4512                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4513                    "  IPC_END_MESSAGE_MAP()\n"
4514                    "}"));
4515 
4516   // Same inside macros.
4517   EXPECT_EQ("#define LIST(L) \\\n"
4518             "  L(A)          \\\n"
4519             "  L(B)          \\\n"
4520             "  L(C)",
4521             format("#define LIST(L) \\\n"
4522                    "  L(A) \\\n"
4523                    "  L(B) \\\n"
4524                    "  L(C)",
4525                    getGoogleStyle()));
4526 
4527   // These must not be recognized as macros.
4528   EXPECT_EQ("int q() {\n"
4529             "  f(x);\n"
4530             "  f(x) {}\n"
4531             "  f(x)->g();\n"
4532             "  f(x)->*g();\n"
4533             "  f(x).g();\n"
4534             "  f(x) = x;\n"
4535             "  f(x) += x;\n"
4536             "  f(x) -= x;\n"
4537             "  f(x) *= x;\n"
4538             "  f(x) /= x;\n"
4539             "  f(x) %= x;\n"
4540             "  f(x) &= x;\n"
4541             "  f(x) |= x;\n"
4542             "  f(x) ^= x;\n"
4543             "  f(x) >>= x;\n"
4544             "  f(x) <<= x;\n"
4545             "  f(x)[y].z();\n"
4546             "  LOG(INFO) << x;\n"
4547             "  ifstream(x) >> x;\n"
4548             "}\n",
4549             format("int q() {\n"
4550                    "  f(x)\n;\n"
4551                    "  f(x)\n {}\n"
4552                    "  f(x)\n->g();\n"
4553                    "  f(x)\n->*g();\n"
4554                    "  f(x)\n.g();\n"
4555                    "  f(x)\n = x;\n"
4556                    "  f(x)\n += x;\n"
4557                    "  f(x)\n -= x;\n"
4558                    "  f(x)\n *= x;\n"
4559                    "  f(x)\n /= x;\n"
4560                    "  f(x)\n %= x;\n"
4561                    "  f(x)\n &= x;\n"
4562                    "  f(x)\n |= x;\n"
4563                    "  f(x)\n ^= x;\n"
4564                    "  f(x)\n >>= x;\n"
4565                    "  f(x)\n <<= x;\n"
4566                    "  f(x)\n[y].z();\n"
4567                    "  LOG(INFO)\n << x;\n"
4568                    "  ifstream(x)\n >> x;\n"
4569                    "}\n"));
4570   EXPECT_EQ("int q() {\n"
4571             "  F(x)\n"
4572             "  if (1) {\n"
4573             "  }\n"
4574             "  F(x)\n"
4575             "  while (1) {\n"
4576             "  }\n"
4577             "  F(x)\n"
4578             "  G(x);\n"
4579             "  F(x)\n"
4580             "  try {\n"
4581             "    Q();\n"
4582             "  } catch (...) {\n"
4583             "  }\n"
4584             "}\n",
4585             format("int q() {\n"
4586                    "F(x)\n"
4587                    "if (1) {}\n"
4588                    "F(x)\n"
4589                    "while (1) {}\n"
4590                    "F(x)\n"
4591                    "G(x);\n"
4592                    "F(x)\n"
4593                    "try { Q(); } catch (...) {}\n"
4594                    "}\n"));
4595   EXPECT_EQ("class A {\n"
4596             "  A() : t(0) {}\n"
4597             "  A(int i) noexcept() : {}\n"
4598             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4599             "  try : t(0) {\n"
4600             "  } catch (...) {\n"
4601             "  }\n"
4602             "};",
4603             format("class A {\n"
4604                    "  A()\n : t(0) {}\n"
4605                    "  A(int i)\n noexcept() : {}\n"
4606                    "  A(X x)\n"
4607                    "  try : t(0) {} catch (...) {}\n"
4608                    "};"));
4609   FormatStyle Style = getLLVMStyle();
4610   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4611   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4612   Style.BraceWrapping.AfterFunction = true;
4613   EXPECT_EQ("void f()\n"
4614             "try\n"
4615             "{\n"
4616             "}",
4617             format("void f() try {\n"
4618                    "}",
4619                    Style));
4620   EXPECT_EQ("class SomeClass {\n"
4621             "public:\n"
4622             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4623             "};",
4624             format("class SomeClass {\n"
4625                    "public:\n"
4626                    "  SomeClass()\n"
4627                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4628                    "};"));
4629   EXPECT_EQ("class SomeClass {\n"
4630             "public:\n"
4631             "  SomeClass()\n"
4632             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4633             "};",
4634             format("class SomeClass {\n"
4635                    "public:\n"
4636                    "  SomeClass()\n"
4637                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4638                    "};",
4639                    getLLVMStyleWithColumns(40)));
4640 
4641   verifyFormat("MACRO(>)");
4642 
4643   // Some macros contain an implicit semicolon.
4644   Style = getLLVMStyle();
4645   Style.StatementMacros.push_back("FOO");
4646   verifyFormat("FOO(a) int b = 0;");
4647   verifyFormat("FOO(a)\n"
4648                "int b = 0;",
4649                Style);
4650   verifyFormat("FOO(a);\n"
4651                "int b = 0;",
4652                Style);
4653   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4654                "int b = 0;",
4655                Style);
4656   verifyFormat("FOO()\n"
4657                "int b = 0;",
4658                Style);
4659   verifyFormat("FOO\n"
4660                "int b = 0;",
4661                Style);
4662   verifyFormat("void f() {\n"
4663                "  FOO(a)\n"
4664                "  return a;\n"
4665                "}",
4666                Style);
4667   verifyFormat("FOO(a)\n"
4668                "FOO(b)",
4669                Style);
4670   verifyFormat("int a = 0;\n"
4671                "FOO(b)\n"
4672                "int c = 0;",
4673                Style);
4674   verifyFormat("int a = 0;\n"
4675                "int x = FOO(a)\n"
4676                "int b = 0;",
4677                Style);
4678   verifyFormat("void foo(int a) { FOO(a) }\n"
4679                "uint32_t bar() {}",
4680                Style);
4681 }
4682 
4683 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4684   verifyFormat("#define A \\\n"
4685                "  f({     \\\n"
4686                "    g();  \\\n"
4687                "  });",
4688                getLLVMStyleWithColumns(11));
4689 }
4690 
4691 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4692   FormatStyle Style = getLLVMStyle();
4693   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4694   Style.ColumnLimit = 40;
4695   verifyFormat("#ifdef _WIN32\n"
4696                "#define A 0\n"
4697                "#ifdef VAR2\n"
4698                "#define B 1\n"
4699                "#include <someheader.h>\n"
4700                "#define MACRO                          \\\n"
4701                "  some_very_long_func_aaaaaaaaaa();\n"
4702                "#endif\n"
4703                "#else\n"
4704                "#define A 1\n"
4705                "#endif",
4706                Style);
4707   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4708   verifyFormat("#ifdef _WIN32\n"
4709                "#  define A 0\n"
4710                "#  ifdef VAR2\n"
4711                "#    define B 1\n"
4712                "#    include <someheader.h>\n"
4713                "#    define MACRO                      \\\n"
4714                "      some_very_long_func_aaaaaaaaaa();\n"
4715                "#  endif\n"
4716                "#else\n"
4717                "#  define A 1\n"
4718                "#endif",
4719                Style);
4720   verifyFormat("#if A\n"
4721                "#  define MACRO                        \\\n"
4722                "    void a(int x) {                    \\\n"
4723                "      b();                             \\\n"
4724                "      c();                             \\\n"
4725                "      d();                             \\\n"
4726                "      e();                             \\\n"
4727                "      f();                             \\\n"
4728                "    }\n"
4729                "#endif",
4730                Style);
4731   // Comments before include guard.
4732   verifyFormat("// file comment\n"
4733                "// file comment\n"
4734                "#ifndef HEADER_H\n"
4735                "#define HEADER_H\n"
4736                "code();\n"
4737                "#endif",
4738                Style);
4739   // Test with include guards.
4740   verifyFormat("#ifndef HEADER_H\n"
4741                "#define HEADER_H\n"
4742                "code();\n"
4743                "#endif",
4744                Style);
4745   // Include guards must have a #define with the same variable immediately
4746   // after #ifndef.
4747   verifyFormat("#ifndef NOT_GUARD\n"
4748                "#  define FOO\n"
4749                "code();\n"
4750                "#endif",
4751                Style);
4752 
4753   // Include guards must cover the entire file.
4754   verifyFormat("code();\n"
4755                "code();\n"
4756                "#ifndef NOT_GUARD\n"
4757                "#  define NOT_GUARD\n"
4758                "code();\n"
4759                "#endif",
4760                Style);
4761   verifyFormat("#ifndef NOT_GUARD\n"
4762                "#  define NOT_GUARD\n"
4763                "code();\n"
4764                "#endif\n"
4765                "code();",
4766                Style);
4767   // Test with trailing blank lines.
4768   verifyFormat("#ifndef HEADER_H\n"
4769                "#define HEADER_H\n"
4770                "code();\n"
4771                "#endif\n",
4772                Style);
4773   // Include guards don't have #else.
4774   verifyFormat("#ifndef NOT_GUARD\n"
4775                "#  define NOT_GUARD\n"
4776                "code();\n"
4777                "#else\n"
4778                "#endif",
4779                Style);
4780   verifyFormat("#ifndef NOT_GUARD\n"
4781                "#  define NOT_GUARD\n"
4782                "code();\n"
4783                "#elif FOO\n"
4784                "#endif",
4785                Style);
4786   // Non-identifier #define after potential include guard.
4787   verifyFormat("#ifndef FOO\n"
4788                "#  define 1\n"
4789                "#endif\n",
4790                Style);
4791   // #if closes past last non-preprocessor line.
4792   verifyFormat("#ifndef FOO\n"
4793                "#define FOO\n"
4794                "#if 1\n"
4795                "int i;\n"
4796                "#  define A 0\n"
4797                "#endif\n"
4798                "#endif\n",
4799                Style);
4800   // Don't crash if there is an #elif directive without a condition.
4801   verifyFormat("#if 1\n"
4802                "int x;\n"
4803                "#elif\n"
4804                "int y;\n"
4805                "#else\n"
4806                "int z;\n"
4807                "#endif",
4808                Style);
4809   // FIXME: This doesn't handle the case where there's code between the
4810   // #ifndef and #define but all other conditions hold. This is because when
4811   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4812   // previous code line yet, so we can't detect it.
4813   EXPECT_EQ("#ifndef NOT_GUARD\n"
4814             "code();\n"
4815             "#define NOT_GUARD\n"
4816             "code();\n"
4817             "#endif",
4818             format("#ifndef NOT_GUARD\n"
4819                    "code();\n"
4820                    "#  define NOT_GUARD\n"
4821                    "code();\n"
4822                    "#endif",
4823                    Style));
4824   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4825   // be outside an include guard. Examples are #pragma once and
4826   // #pragma GCC diagnostic, or anything else that does not change the meaning
4827   // of the file if it's included multiple times.
4828   EXPECT_EQ("#ifdef WIN32\n"
4829             "#  pragma once\n"
4830             "#endif\n"
4831             "#ifndef HEADER_H\n"
4832             "#  define HEADER_H\n"
4833             "code();\n"
4834             "#endif",
4835             format("#ifdef WIN32\n"
4836                    "#  pragma once\n"
4837                    "#endif\n"
4838                    "#ifndef HEADER_H\n"
4839                    "#define HEADER_H\n"
4840                    "code();\n"
4841                    "#endif",
4842                    Style));
4843   // FIXME: This does not detect when there is a single non-preprocessor line
4844   // in front of an include-guard-like structure where other conditions hold
4845   // because ScopedLineState hides the line.
4846   EXPECT_EQ("code();\n"
4847             "#ifndef HEADER_H\n"
4848             "#define HEADER_H\n"
4849             "code();\n"
4850             "#endif",
4851             format("code();\n"
4852                    "#ifndef HEADER_H\n"
4853                    "#  define HEADER_H\n"
4854                    "code();\n"
4855                    "#endif",
4856                    Style));
4857   // Keep comments aligned with #, otherwise indent comments normally. These
4858   // tests cannot use verifyFormat because messUp manipulates leading
4859   // whitespace.
4860   {
4861     const char *Expected = ""
4862                            "void f() {\n"
4863                            "#if 1\n"
4864                            "// Preprocessor aligned.\n"
4865                            "#  define A 0\n"
4866                            "  // Code. Separated by blank line.\n"
4867                            "\n"
4868                            "#  define B 0\n"
4869                            "  // Code. Not aligned with #\n"
4870                            "#  define C 0\n"
4871                            "#endif";
4872     const char *ToFormat = ""
4873                            "void f() {\n"
4874                            "#if 1\n"
4875                            "// Preprocessor aligned.\n"
4876                            "#  define A 0\n"
4877                            "// Code. Separated by blank line.\n"
4878                            "\n"
4879                            "#  define B 0\n"
4880                            "   // Code. Not aligned with #\n"
4881                            "#  define C 0\n"
4882                            "#endif";
4883     EXPECT_EQ(Expected, format(ToFormat, Style));
4884     EXPECT_EQ(Expected, format(Expected, Style));
4885   }
4886   // Keep block quotes aligned.
4887   {
4888     const char *Expected = ""
4889                            "void f() {\n"
4890                            "#if 1\n"
4891                            "/* Preprocessor aligned. */\n"
4892                            "#  define A 0\n"
4893                            "  /* Code. Separated by blank line. */\n"
4894                            "\n"
4895                            "#  define B 0\n"
4896                            "  /* Code. Not aligned with # */\n"
4897                            "#  define C 0\n"
4898                            "#endif";
4899     const char *ToFormat = ""
4900                            "void f() {\n"
4901                            "#if 1\n"
4902                            "/* Preprocessor aligned. */\n"
4903                            "#  define A 0\n"
4904                            "/* Code. Separated by blank line. */\n"
4905                            "\n"
4906                            "#  define B 0\n"
4907                            "   /* Code. Not aligned with # */\n"
4908                            "#  define C 0\n"
4909                            "#endif";
4910     EXPECT_EQ(Expected, format(ToFormat, Style));
4911     EXPECT_EQ(Expected, format(Expected, Style));
4912   }
4913   // Keep comments aligned with un-indented directives.
4914   {
4915     const char *Expected = ""
4916                            "void f() {\n"
4917                            "// Preprocessor aligned.\n"
4918                            "#define A 0\n"
4919                            "  // Code. Separated by blank line.\n"
4920                            "\n"
4921                            "#define B 0\n"
4922                            "  // Code. Not aligned with #\n"
4923                            "#define C 0\n";
4924     const char *ToFormat = ""
4925                            "void f() {\n"
4926                            "// Preprocessor aligned.\n"
4927                            "#define A 0\n"
4928                            "// Code. Separated by blank line.\n"
4929                            "\n"
4930                            "#define B 0\n"
4931                            "   // Code. Not aligned with #\n"
4932                            "#define C 0\n";
4933     EXPECT_EQ(Expected, format(ToFormat, Style));
4934     EXPECT_EQ(Expected, format(Expected, Style));
4935   }
4936   // Test AfterHash with tabs.
4937   {
4938     FormatStyle Tabbed = Style;
4939     Tabbed.UseTab = FormatStyle::UT_Always;
4940     Tabbed.IndentWidth = 8;
4941     Tabbed.TabWidth = 8;
4942     verifyFormat("#ifdef _WIN32\n"
4943                  "#\tdefine A 0\n"
4944                  "#\tifdef VAR2\n"
4945                  "#\t\tdefine B 1\n"
4946                  "#\t\tinclude <someheader.h>\n"
4947                  "#\t\tdefine MACRO          \\\n"
4948                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4949                  "#\tendif\n"
4950                  "#else\n"
4951                  "#\tdefine A 1\n"
4952                  "#endif",
4953                  Tabbed);
4954   }
4955 
4956   // Regression test: Multiline-macro inside include guards.
4957   verifyFormat("#ifndef HEADER_H\n"
4958                "#define HEADER_H\n"
4959                "#define A()        \\\n"
4960                "  int i;           \\\n"
4961                "  int j;\n"
4962                "#endif // HEADER_H",
4963                getLLVMStyleWithColumns(20));
4964 
4965   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4966   // Basic before hash indent tests
4967   verifyFormat("#ifdef _WIN32\n"
4968                "  #define A 0\n"
4969                "  #ifdef VAR2\n"
4970                "    #define B 1\n"
4971                "    #include <someheader.h>\n"
4972                "    #define MACRO                      \\\n"
4973                "      some_very_long_func_aaaaaaaaaa();\n"
4974                "  #endif\n"
4975                "#else\n"
4976                "  #define A 1\n"
4977                "#endif",
4978                Style);
4979   verifyFormat("#if A\n"
4980                "  #define MACRO                        \\\n"
4981                "    void a(int x) {                    \\\n"
4982                "      b();                             \\\n"
4983                "      c();                             \\\n"
4984                "      d();                             \\\n"
4985                "      e();                             \\\n"
4986                "      f();                             \\\n"
4987                "    }\n"
4988                "#endif",
4989                Style);
4990   // Keep comments aligned with indented directives. These
4991   // tests cannot use verifyFormat because messUp manipulates leading
4992   // whitespace.
4993   {
4994     const char *Expected = "void f() {\n"
4995                            "// Aligned to preprocessor.\n"
4996                            "#if 1\n"
4997                            "  // Aligned to code.\n"
4998                            "  int a;\n"
4999                            "  #if 1\n"
5000                            "    // Aligned to preprocessor.\n"
5001                            "    #define A 0\n"
5002                            "  // Aligned to code.\n"
5003                            "  int b;\n"
5004                            "  #endif\n"
5005                            "#endif\n"
5006                            "}";
5007     const char *ToFormat = "void f() {\n"
5008                            "// Aligned to preprocessor.\n"
5009                            "#if 1\n"
5010                            "// Aligned to code.\n"
5011                            "int a;\n"
5012                            "#if 1\n"
5013                            "// Aligned to preprocessor.\n"
5014                            "#define A 0\n"
5015                            "// Aligned to code.\n"
5016                            "int b;\n"
5017                            "#endif\n"
5018                            "#endif\n"
5019                            "}";
5020     EXPECT_EQ(Expected, format(ToFormat, Style));
5021     EXPECT_EQ(Expected, format(Expected, Style));
5022   }
5023   {
5024     const char *Expected = "void f() {\n"
5025                            "/* Aligned to preprocessor. */\n"
5026                            "#if 1\n"
5027                            "  /* Aligned to code. */\n"
5028                            "  int a;\n"
5029                            "  #if 1\n"
5030                            "    /* Aligned to preprocessor. */\n"
5031                            "    #define A 0\n"
5032                            "  /* Aligned to code. */\n"
5033                            "  int b;\n"
5034                            "  #endif\n"
5035                            "#endif\n"
5036                            "}";
5037     const char *ToFormat = "void f() {\n"
5038                            "/* Aligned to preprocessor. */\n"
5039                            "#if 1\n"
5040                            "/* Aligned to code. */\n"
5041                            "int a;\n"
5042                            "#if 1\n"
5043                            "/* Aligned to preprocessor. */\n"
5044                            "#define A 0\n"
5045                            "/* Aligned to code. */\n"
5046                            "int b;\n"
5047                            "#endif\n"
5048                            "#endif\n"
5049                            "}";
5050     EXPECT_EQ(Expected, format(ToFormat, Style));
5051     EXPECT_EQ(Expected, format(Expected, Style));
5052   }
5053 
5054   // Test single comment before preprocessor
5055   verifyFormat("// Comment\n"
5056                "\n"
5057                "#if 1\n"
5058                "#endif",
5059                Style);
5060 }
5061 
5062 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5063   verifyFormat("{\n  { a #c; }\n}");
5064 }
5065 
5066 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5067   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5068             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5069   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5070             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5071 }
5072 
5073 TEST_F(FormatTest, EscapedNewlines) {
5074   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5075   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5076             format("#define A \\\nint i;\\\n  int j;", Narrow));
5077   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5078   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5079   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5080   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5081 
5082   FormatStyle AlignLeft = getLLVMStyle();
5083   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5084   EXPECT_EQ("#define MACRO(x) \\\n"
5085             "private:         \\\n"
5086             "  int x(int a);\n",
5087             format("#define MACRO(x) \\\n"
5088                    "private:         \\\n"
5089                    "  int x(int a);\n",
5090                    AlignLeft));
5091 
5092   // CRLF line endings
5093   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5094             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5095   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5096   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5097   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5098   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5099   EXPECT_EQ("#define MACRO(x) \\\r\n"
5100             "private:         \\\r\n"
5101             "  int x(int a);\r\n",
5102             format("#define MACRO(x) \\\r\n"
5103                    "private:         \\\r\n"
5104                    "  int x(int a);\r\n",
5105                    AlignLeft));
5106 
5107   FormatStyle DontAlign = getLLVMStyle();
5108   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5109   DontAlign.MaxEmptyLinesToKeep = 3;
5110   // FIXME: can't use verifyFormat here because the newline before
5111   // "public:" is not inserted the first time it's reformatted
5112   EXPECT_EQ("#define A \\\n"
5113             "  class Foo { \\\n"
5114             "    void bar(); \\\n"
5115             "\\\n"
5116             "\\\n"
5117             "\\\n"
5118             "  public: \\\n"
5119             "    void baz(); \\\n"
5120             "  };",
5121             format("#define A \\\n"
5122                    "  class Foo { \\\n"
5123                    "    void bar(); \\\n"
5124                    "\\\n"
5125                    "\\\n"
5126                    "\\\n"
5127                    "  public: \\\n"
5128                    "    void baz(); \\\n"
5129                    "  };",
5130                    DontAlign));
5131 }
5132 
5133 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5134   verifyFormat("#define A \\\n"
5135                "  int v(  \\\n"
5136                "      a); \\\n"
5137                "  int i;",
5138                getLLVMStyleWithColumns(11));
5139 }
5140 
5141 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5142   EXPECT_EQ(
5143       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5144       "                      \\\n"
5145       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5146       "\n"
5147       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5148       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5149       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5150              "\\\n"
5151              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5152              "  \n"
5153              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5154              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5155 }
5156 
5157 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5158   EXPECT_EQ("int\n"
5159             "#define A\n"
5160             "    a;",
5161             format("int\n#define A\na;"));
5162   verifyFormat("functionCallTo(\n"
5163                "    someOtherFunction(\n"
5164                "        withSomeParameters, whichInSequence,\n"
5165                "        areLongerThanALine(andAnotherCall,\n"
5166                "#define A B\n"
5167                "                           withMoreParamters,\n"
5168                "                           whichStronglyInfluenceTheLayout),\n"
5169                "        andMoreParameters),\n"
5170                "    trailing);",
5171                getLLVMStyleWithColumns(69));
5172   verifyFormat("Foo::Foo()\n"
5173                "#ifdef BAR\n"
5174                "    : baz(0)\n"
5175                "#endif\n"
5176                "{\n"
5177                "}");
5178   verifyFormat("void f() {\n"
5179                "  if (true)\n"
5180                "#ifdef A\n"
5181                "    f(42);\n"
5182                "  x();\n"
5183                "#else\n"
5184                "    g();\n"
5185                "  x();\n"
5186                "#endif\n"
5187                "}");
5188   verifyFormat("void f(param1, param2,\n"
5189                "       param3,\n"
5190                "#ifdef A\n"
5191                "       param4(param5,\n"
5192                "#ifdef A1\n"
5193                "              param6,\n"
5194                "#ifdef A2\n"
5195                "              param7),\n"
5196                "#else\n"
5197                "              param8),\n"
5198                "       param9,\n"
5199                "#endif\n"
5200                "       param10,\n"
5201                "#endif\n"
5202                "       param11)\n"
5203                "#else\n"
5204                "       param12)\n"
5205                "#endif\n"
5206                "{\n"
5207                "  x();\n"
5208                "}",
5209                getLLVMStyleWithColumns(28));
5210   verifyFormat("#if 1\n"
5211                "int i;");
5212   verifyFormat("#if 1\n"
5213                "#endif\n"
5214                "#if 1\n"
5215                "#else\n"
5216                "#endif\n");
5217   verifyFormat("DEBUG({\n"
5218                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5219                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5220                "});\n"
5221                "#if a\n"
5222                "#else\n"
5223                "#endif");
5224 
5225   verifyIncompleteFormat("void f(\n"
5226                          "#if A\n"
5227                          ");\n"
5228                          "#else\n"
5229                          "#endif");
5230 }
5231 
5232 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5233   verifyFormat("#endif\n"
5234                "#if B");
5235 }
5236 
5237 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5238   FormatStyle SingleLine = getLLVMStyle();
5239   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5240   verifyFormat("#if 0\n"
5241                "#elif 1\n"
5242                "#endif\n"
5243                "void foo() {\n"
5244                "  if (test) foo2();\n"
5245                "}",
5246                SingleLine);
5247 }
5248 
5249 TEST_F(FormatTest, LayoutBlockInsideParens) {
5250   verifyFormat("functionCall({ int i; });");
5251   verifyFormat("functionCall({\n"
5252                "  int i;\n"
5253                "  int j;\n"
5254                "});");
5255   verifyFormat("functionCall(\n"
5256                "    {\n"
5257                "      int i;\n"
5258                "      int j;\n"
5259                "    },\n"
5260                "    aaaa, bbbb, cccc);");
5261   verifyFormat("functionA(functionB({\n"
5262                "            int i;\n"
5263                "            int j;\n"
5264                "          }),\n"
5265                "          aaaa, bbbb, cccc);");
5266   verifyFormat("functionCall(\n"
5267                "    {\n"
5268                "      int i;\n"
5269                "      int j;\n"
5270                "    },\n"
5271                "    aaaa, bbbb, // comment\n"
5272                "    cccc);");
5273   verifyFormat("functionA(functionB({\n"
5274                "            int i;\n"
5275                "            int j;\n"
5276                "          }),\n"
5277                "          aaaa, bbbb, // comment\n"
5278                "          cccc);");
5279   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5280   verifyFormat("functionCall(aaaa, bbbb, {\n"
5281                "  int i;\n"
5282                "  int j;\n"
5283                "});");
5284   verifyFormat(
5285       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5286       "    {\n"
5287       "      int i; // break\n"
5288       "    },\n"
5289       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5290       "                                     ccccccccccccccccc));");
5291   verifyFormat("DEBUG({\n"
5292                "  if (a)\n"
5293                "    f();\n"
5294                "});");
5295 }
5296 
5297 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5298   EXPECT_EQ("SOME_MACRO { int i; }\n"
5299             "int i;",
5300             format("  SOME_MACRO  {int i;}  int i;"));
5301 }
5302 
5303 TEST_F(FormatTest, LayoutNestedBlocks) {
5304   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5305                "  struct s {\n"
5306                "    int i;\n"
5307                "  };\n"
5308                "  s kBitsToOs[] = {{10}};\n"
5309                "  for (int i = 0; i < 10; ++i)\n"
5310                "    return;\n"
5311                "}");
5312   verifyFormat("call(parameter, {\n"
5313                "  something();\n"
5314                "  // Comment using all columns.\n"
5315                "  somethingelse();\n"
5316                "});",
5317                getLLVMStyleWithColumns(40));
5318   verifyFormat("DEBUG( //\n"
5319                "    { f(); }, a);");
5320   verifyFormat("DEBUG( //\n"
5321                "    {\n"
5322                "      f(); //\n"
5323                "    },\n"
5324                "    a);");
5325 
5326   EXPECT_EQ("call(parameter, {\n"
5327             "  something();\n"
5328             "  // Comment too\n"
5329             "  // looooooooooong.\n"
5330             "  somethingElse();\n"
5331             "});",
5332             format("call(parameter, {\n"
5333                    "  something();\n"
5334                    "  // Comment too looooooooooong.\n"
5335                    "  somethingElse();\n"
5336                    "});",
5337                    getLLVMStyleWithColumns(29)));
5338   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5339   EXPECT_EQ("DEBUG({ // comment\n"
5340             "  int i;\n"
5341             "});",
5342             format("DEBUG({ // comment\n"
5343                    "int  i;\n"
5344                    "});"));
5345   EXPECT_EQ("DEBUG({\n"
5346             "  int i;\n"
5347             "\n"
5348             "  // comment\n"
5349             "  int j;\n"
5350             "});",
5351             format("DEBUG({\n"
5352                    "  int  i;\n"
5353                    "\n"
5354                    "  // comment\n"
5355                    "  int  j;\n"
5356                    "});"));
5357 
5358   verifyFormat("DEBUG({\n"
5359                "  if (a)\n"
5360                "    return;\n"
5361                "});");
5362   verifyGoogleFormat("DEBUG({\n"
5363                      "  if (a) return;\n"
5364                      "});");
5365   FormatStyle Style = getGoogleStyle();
5366   Style.ColumnLimit = 45;
5367   verifyFormat("Debug(\n"
5368                "    aaaaa,\n"
5369                "    {\n"
5370                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5371                "    },\n"
5372                "    a);",
5373                Style);
5374 
5375   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5376 
5377   verifyNoCrash("^{v^{a}}");
5378 }
5379 
5380 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5381   EXPECT_EQ("#define MACRO()                     \\\n"
5382             "  Debug(aaa, /* force line break */ \\\n"
5383             "        {                           \\\n"
5384             "          int i;                    \\\n"
5385             "          int j;                    \\\n"
5386             "        })",
5387             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5388                    "          {  int   i;  int  j;   })",
5389                    getGoogleStyle()));
5390 
5391   EXPECT_EQ("#define A                                       \\\n"
5392             "  [] {                                          \\\n"
5393             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5394             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5395             "  }",
5396             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5397                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5398                    getGoogleStyle()));
5399 }
5400 
5401 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5402   EXPECT_EQ("{}", format("{}"));
5403   verifyFormat("enum E {};");
5404   verifyFormat("enum E {}");
5405   FormatStyle Style = getLLVMStyle();
5406   Style.SpaceInEmptyBlock = true;
5407   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5408   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5409   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5410   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5411   Style.BraceWrapping.BeforeElse = false;
5412   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5413   verifyFormat("if (a)\n"
5414                "{\n"
5415                "} else if (b)\n"
5416                "{\n"
5417                "} else\n"
5418                "{ }",
5419                Style);
5420   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5421   verifyFormat("if (a) {\n"
5422                "} else if (b) {\n"
5423                "} else {\n"
5424                "}",
5425                Style);
5426   Style.BraceWrapping.BeforeElse = true;
5427   verifyFormat("if (a) { }\n"
5428                "else if (b) { }\n"
5429                "else { }",
5430                Style);
5431 }
5432 
5433 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5434   FormatStyle Style = getLLVMStyle();
5435   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5436   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5437   verifyFormat("FOO_BEGIN\n"
5438                "  FOO_ENTRY\n"
5439                "FOO_END",
5440                Style);
5441   verifyFormat("FOO_BEGIN\n"
5442                "  NESTED_FOO_BEGIN\n"
5443                "    NESTED_FOO_ENTRY\n"
5444                "  NESTED_FOO_END\n"
5445                "FOO_END",
5446                Style);
5447   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5448                "  int x;\n"
5449                "  x = 1;\n"
5450                "FOO_END(Baz)",
5451                Style);
5452 }
5453 
5454 //===----------------------------------------------------------------------===//
5455 // Line break tests.
5456 //===----------------------------------------------------------------------===//
5457 
5458 TEST_F(FormatTest, PreventConfusingIndents) {
5459   verifyFormat(
5460       "void f() {\n"
5461       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5462       "                         parameter, parameter, parameter)),\n"
5463       "                     SecondLongCall(parameter));\n"
5464       "}");
5465   verifyFormat(
5466       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5467       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5468       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5469       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5470   verifyFormat(
5471       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5472       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5473       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5474       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5475   verifyFormat(
5476       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5477       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5478       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5479       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5480   verifyFormat("int a = bbbb && ccc &&\n"
5481                "        fffff(\n"
5482                "#define A Just forcing a new line\n"
5483                "            ddd);");
5484 }
5485 
5486 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5487   verifyFormat(
5488       "bool aaaaaaa =\n"
5489       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5490       "    bbbbbbbb();");
5491   verifyFormat(
5492       "bool aaaaaaa =\n"
5493       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5494       "    bbbbbbbb();");
5495 
5496   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5497                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5498                "    ccccccccc == ddddddddddd;");
5499   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5500                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5501                "    ccccccccc == ddddddddddd;");
5502   verifyFormat(
5503       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5504       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5505       "    ccccccccc == ddddddddddd;");
5506 
5507   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5508                "                 aaaaaa) &&\n"
5509                "         bbbbbb && cccccc;");
5510   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5511                "                 aaaaaa) >>\n"
5512                "         bbbbbb;");
5513   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5514                "    SourceMgr.getSpellingColumnNumber(\n"
5515                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5516                "    1);");
5517 
5518   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5519                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5520                "    cccccc) {\n}");
5521   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5522                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5523                "              cccccc) {\n}");
5524   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5525                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5526                "              cccccc) {\n}");
5527   verifyFormat("b = a &&\n"
5528                "    // Comment\n"
5529                "    b.c && d;");
5530 
5531   // If the LHS of a comparison is not a binary expression itself, the
5532   // additional linebreak confuses many people.
5533   verifyFormat(
5534       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5535       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5536       "}");
5537   verifyFormat(
5538       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5539       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5540       "}");
5541   verifyFormat(
5542       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5543       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5544       "}");
5545   verifyFormat(
5546       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5547       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5548       "}");
5549   // Even explicit parentheses stress the precedence enough to make the
5550   // additional break unnecessary.
5551   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5552                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5553                "}");
5554   // This cases is borderline, but with the indentation it is still readable.
5555   verifyFormat(
5556       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5557       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5558       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5559       "}",
5560       getLLVMStyleWithColumns(75));
5561 
5562   // If the LHS is a binary expression, we should still use the additional break
5563   // as otherwise the formatting hides the operator precedence.
5564   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5565                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5566                "    5) {\n"
5567                "}");
5568   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5569                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5570                "    5) {\n"
5571                "}");
5572 
5573   FormatStyle OnePerLine = getLLVMStyle();
5574   OnePerLine.BinPackParameters = false;
5575   verifyFormat(
5576       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5577       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5578       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5579       OnePerLine);
5580 
5581   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5582                "                .aaa(aaaaaaaaaaaaa) *\n"
5583                "            aaaaaaa +\n"
5584                "        aaaaaaa;",
5585                getLLVMStyleWithColumns(40));
5586 }
5587 
5588 TEST_F(FormatTest, ExpressionIndentation) {
5589   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5590                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5591                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5592                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5593                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5594                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5595                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5596                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5597                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5598   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5599                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5600                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5601                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5602   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5603                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5604                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5605                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5606   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5607                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5608                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5609                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5610   verifyFormat("if () {\n"
5611                "} else if (aaaaa && bbbbb > // break\n"
5612                "                        ccccc) {\n"
5613                "}");
5614   verifyFormat("if () {\n"
5615                "} else if constexpr (aaaaa && bbbbb > // break\n"
5616                "                                  ccccc) {\n"
5617                "}");
5618   verifyFormat("if () {\n"
5619                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5620                "                                  ccccc) {\n"
5621                "}");
5622   verifyFormat("if () {\n"
5623                "} else if (aaaaa &&\n"
5624                "           bbbbb > // break\n"
5625                "               ccccc &&\n"
5626                "           ddddd) {\n"
5627                "}");
5628 
5629   // Presence of a trailing comment used to change indentation of b.
5630   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5631                "       b;\n"
5632                "return aaaaaaaaaaaaaaaaaaa +\n"
5633                "       b; //",
5634                getLLVMStyleWithColumns(30));
5635 }
5636 
5637 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5638   // Not sure what the best system is here. Like this, the LHS can be found
5639   // immediately above an operator (everything with the same or a higher
5640   // indent). The RHS is aligned right of the operator and so compasses
5641   // everything until something with the same indent as the operator is found.
5642   // FIXME: Is this a good system?
5643   FormatStyle Style = getLLVMStyle();
5644   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5645   verifyFormat(
5646       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5647       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5648       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5649       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5650       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5651       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5652       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5653       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5654       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5655       Style);
5656   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5657                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5658                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5659                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5660                Style);
5661   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5662                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5663                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5664                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5665                Style);
5666   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5667                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5668                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5669                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5670                Style);
5671   verifyFormat("if () {\n"
5672                "} else if (aaaaa\n"
5673                "           && bbbbb // break\n"
5674                "                  > ccccc) {\n"
5675                "}",
5676                Style);
5677   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5678                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5679                Style);
5680   verifyFormat("return (a)\n"
5681                "       // comment\n"
5682                "       + b;",
5683                Style);
5684   verifyFormat(
5685       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5686       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5687       "             + cc;",
5688       Style);
5689 
5690   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5691                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5692                Style);
5693 
5694   // Forced by comments.
5695   verifyFormat(
5696       "unsigned ContentSize =\n"
5697       "    sizeof(int16_t)   // DWARF ARange version number\n"
5698       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5699       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5700       "    + sizeof(int8_t); // Segment Size (in bytes)");
5701 
5702   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5703                "       == boost::fusion::at_c<1>(iiii).second;",
5704                Style);
5705 
5706   Style.ColumnLimit = 60;
5707   verifyFormat("zzzzzzzzzz\n"
5708                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5709                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5710                Style);
5711 
5712   Style.ColumnLimit = 80;
5713   Style.IndentWidth = 4;
5714   Style.TabWidth = 4;
5715   Style.UseTab = FormatStyle::UT_Always;
5716   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5717   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5718   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5719             "\t&& (someOtherLongishConditionPart1\n"
5720             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5721             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5722                    "(someOtherLongishConditionPart1 || "
5723                    "someOtherEvenLongerNestedConditionPart2);",
5724                    Style));
5725 }
5726 
5727 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5728   FormatStyle Style = getLLVMStyle();
5729   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5730   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5731 
5732   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5733                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5734                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5735                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5736                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5737                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5738                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5739                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5740                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5741                Style);
5742   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5743                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5744                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5745                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5746                Style);
5747   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5748                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5749                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5750                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5751                Style);
5752   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5753                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5754                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5755                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5756                Style);
5757   verifyFormat("if () {\n"
5758                "} else if (aaaaa\n"
5759                "           && bbbbb // break\n"
5760                "                  > ccccc) {\n"
5761                "}",
5762                Style);
5763   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5764                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5765                Style);
5766   verifyFormat("return (a)\n"
5767                "     // comment\n"
5768                "     + b;",
5769                Style);
5770   verifyFormat(
5771       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5772       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5773       "           + cc;",
5774       Style);
5775   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5776                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5777                "                        : 3333333333333333;",
5778                Style);
5779   verifyFormat(
5780       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5781       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5782       "                                             : eeeeeeeeeeeeeeeeee)\n"
5783       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5784       "                        : 3333333333333333;",
5785       Style);
5786   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5787                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5788                Style);
5789 
5790   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5791                "    == boost::fusion::at_c<1>(iiii).second;",
5792                Style);
5793 
5794   Style.ColumnLimit = 60;
5795   verifyFormat("zzzzzzzzzzzzz\n"
5796                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5797                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5798                Style);
5799 
5800   // Forced by comments.
5801   Style.ColumnLimit = 80;
5802   verifyFormat(
5803       "unsigned ContentSize\n"
5804       "    = sizeof(int16_t) // DWARF ARange version number\n"
5805       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5806       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5807       "    + sizeof(int8_t); // Segment Size (in bytes)",
5808       Style);
5809 
5810   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5811   verifyFormat(
5812       "unsigned ContentSize =\n"
5813       "    sizeof(int16_t)   // DWARF ARange version number\n"
5814       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5815       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5816       "    + sizeof(int8_t); // Segment Size (in bytes)",
5817       Style);
5818 
5819   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5820   verifyFormat(
5821       "unsigned ContentSize =\n"
5822       "    sizeof(int16_t)   // DWARF ARange version number\n"
5823       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5824       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5825       "    + sizeof(int8_t); // Segment Size (in bytes)",
5826       Style);
5827 }
5828 
5829 TEST_F(FormatTest, EnforcedOperatorWraps) {
5830   // Here we'd like to wrap after the || operators, but a comment is forcing an
5831   // earlier wrap.
5832   verifyFormat("bool x = aaaaa //\n"
5833                "         || bbbbb\n"
5834                "         //\n"
5835                "         || cccc;");
5836 }
5837 
5838 TEST_F(FormatTest, NoOperandAlignment) {
5839   FormatStyle Style = getLLVMStyle();
5840   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5841   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5842                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5843                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5844                Style);
5845   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5846   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5847                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5848                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5849                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5850                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5851                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5852                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5853                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5854                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5855                Style);
5856 
5857   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5858                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5859                "    + cc;",
5860                Style);
5861   verifyFormat("int a = aa\n"
5862                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5863                "        * cccccccccccccccccccccccccccccccccccc;\n",
5864                Style);
5865 
5866   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5867   verifyFormat("return (a > b\n"
5868                "    // comment1\n"
5869                "    // comment2\n"
5870                "    || c);",
5871                Style);
5872 }
5873 
5874 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5875   FormatStyle Style = getLLVMStyle();
5876   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5877   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5878                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5879                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5880                Style);
5881 }
5882 
5883 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5884   FormatStyle Style = getLLVMStyle();
5885   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5886   Style.BinPackArguments = false;
5887   Style.ColumnLimit = 40;
5888   verifyFormat("void test() {\n"
5889                "  someFunction(\n"
5890                "      this + argument + is + quite\n"
5891                "      + long + so + it + gets + wrapped\n"
5892                "      + but + remains + bin - packed);\n"
5893                "}",
5894                Style);
5895   verifyFormat("void test() {\n"
5896                "  someFunction(arg1,\n"
5897                "               this + argument + is\n"
5898                "                   + quite + long + so\n"
5899                "                   + it + gets + wrapped\n"
5900                "                   + but + remains + bin\n"
5901                "                   - packed,\n"
5902                "               arg3);\n"
5903                "}",
5904                Style);
5905   verifyFormat("void test() {\n"
5906                "  someFunction(\n"
5907                "      arg1,\n"
5908                "      this + argument + has\n"
5909                "          + anotherFunc(nested,\n"
5910                "                        calls + whose\n"
5911                "                            + arguments\n"
5912                "                            + are + also\n"
5913                "                            + wrapped,\n"
5914                "                        in + addition)\n"
5915                "          + to + being + bin - packed,\n"
5916                "      arg3);\n"
5917                "}",
5918                Style);
5919 
5920   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5921   verifyFormat("void test() {\n"
5922                "  someFunction(\n"
5923                "      arg1,\n"
5924                "      this + argument + has +\n"
5925                "          anotherFunc(nested,\n"
5926                "                      calls + whose +\n"
5927                "                          arguments +\n"
5928                "                          are + also +\n"
5929                "                          wrapped,\n"
5930                "                      in + addition) +\n"
5931                "          to + being + bin - packed,\n"
5932                "      arg3);\n"
5933                "}",
5934                Style);
5935 }
5936 
5937 TEST_F(FormatTest, ConstructorInitializers) {
5938   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5939   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5940                getLLVMStyleWithColumns(45));
5941   verifyFormat("Constructor()\n"
5942                "    : Inttializer(FitsOnTheLine) {}",
5943                getLLVMStyleWithColumns(44));
5944   verifyFormat("Constructor()\n"
5945                "    : Inttializer(FitsOnTheLine) {}",
5946                getLLVMStyleWithColumns(43));
5947 
5948   verifyFormat("template <typename T>\n"
5949                "Constructor() : Initializer(FitsOnTheLine) {}",
5950                getLLVMStyleWithColumns(45));
5951 
5952   verifyFormat(
5953       "SomeClass::Constructor()\n"
5954       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5955 
5956   verifyFormat(
5957       "SomeClass::Constructor()\n"
5958       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5959       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5960   verifyFormat(
5961       "SomeClass::Constructor()\n"
5962       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5963       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5964   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5965                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5966                "    : aaaaaaaaaa(aaaaaa) {}");
5967 
5968   verifyFormat("Constructor()\n"
5969                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5970                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5971                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5972                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5973 
5974   verifyFormat("Constructor()\n"
5975                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5976                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5977 
5978   verifyFormat("Constructor(int Parameter = 0)\n"
5979                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5980                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5981   verifyFormat("Constructor()\n"
5982                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5983                "}",
5984                getLLVMStyleWithColumns(60));
5985   verifyFormat("Constructor()\n"
5986                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5987                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5988 
5989   // Here a line could be saved by splitting the second initializer onto two
5990   // lines, but that is not desirable.
5991   verifyFormat("Constructor()\n"
5992                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5993                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5994                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5995 
5996   FormatStyle OnePerLine = getLLVMStyle();
5997   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
5998   verifyFormat("MyClass::MyClass()\n"
5999                "    : a(a),\n"
6000                "      b(b),\n"
6001                "      c(c) {}",
6002                OnePerLine);
6003   verifyFormat("MyClass::MyClass()\n"
6004                "    : a(a), // comment\n"
6005                "      b(b),\n"
6006                "      c(c) {}",
6007                OnePerLine);
6008   verifyFormat("MyClass::MyClass(int a)\n"
6009                "    : b(a),      // comment\n"
6010                "      c(a + 1) { // lined up\n"
6011                "}",
6012                OnePerLine);
6013   verifyFormat("Constructor()\n"
6014                "    : a(b, b, b) {}",
6015                OnePerLine);
6016   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6017   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6018   verifyFormat("SomeClass::Constructor()\n"
6019                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6020                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6021                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6022                OnePerLine);
6023   verifyFormat("SomeClass::Constructor()\n"
6024                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6025                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6026                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6027                OnePerLine);
6028   verifyFormat("MyClass::MyClass(int var)\n"
6029                "    : some_var_(var),            // 4 space indent\n"
6030                "      some_other_var_(var + 1) { // lined up\n"
6031                "}",
6032                OnePerLine);
6033   verifyFormat("Constructor()\n"
6034                "    : aaaaa(aaaaaa),\n"
6035                "      aaaaa(aaaaaa),\n"
6036                "      aaaaa(aaaaaa),\n"
6037                "      aaaaa(aaaaaa),\n"
6038                "      aaaaa(aaaaaa) {}",
6039                OnePerLine);
6040   verifyFormat("Constructor()\n"
6041                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6042                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6043                OnePerLine);
6044   OnePerLine.BinPackParameters = false;
6045   verifyFormat(
6046       "Constructor()\n"
6047       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6048       "          aaaaaaaaaaa().aaa(),\n"
6049       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6050       OnePerLine);
6051   OnePerLine.ColumnLimit = 60;
6052   verifyFormat("Constructor()\n"
6053                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6054                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6055                OnePerLine);
6056 
6057   EXPECT_EQ("Constructor()\n"
6058             "    : // Comment forcing unwanted break.\n"
6059             "      aaaa(aaaa) {}",
6060             format("Constructor() :\n"
6061                    "    // Comment forcing unwanted break.\n"
6062                    "    aaaa(aaaa) {}"));
6063 }
6064 
6065 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6066   FormatStyle Style = getLLVMStyle();
6067   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6068   Style.ColumnLimit = 60;
6069   Style.BinPackParameters = false;
6070 
6071   for (int i = 0; i < 4; ++i) {
6072     // Test all combinations of parameters that should not have an effect.
6073     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6074     Style.AllowAllArgumentsOnNextLine = i & 2;
6075 
6076     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6077     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6078     verifyFormat("Constructor()\n"
6079                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6080                  Style);
6081     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6082 
6083     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6084     verifyFormat("Constructor()\n"
6085                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6086                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6087                  Style);
6088     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6089 
6090     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6091     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6092     verifyFormat("Constructor()\n"
6093                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6094                  Style);
6095 
6096     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6097     verifyFormat("Constructor()\n"
6098                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6099                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6100                  Style);
6101 
6102     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6103     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6104     verifyFormat("Constructor() :\n"
6105                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6106                  Style);
6107 
6108     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6109     verifyFormat("Constructor() :\n"
6110                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6111                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6112                  Style);
6113   }
6114 
6115   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6116   // AllowAllConstructorInitializersOnNextLine in all
6117   // BreakConstructorInitializers modes
6118   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6119   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6120   verifyFormat("SomeClassWithALongName::Constructor(\n"
6121                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6122                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6123                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6124                Style);
6125 
6126   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6127   verifyFormat("SomeClassWithALongName::Constructor(\n"
6128                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6129                "    int bbbbbbbbbbbbb,\n"
6130                "    int cccccccccccccccc)\n"
6131                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6132                Style);
6133 
6134   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6135   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6136   verifyFormat("SomeClassWithALongName::Constructor(\n"
6137                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6138                "    int bbbbbbbbbbbbb)\n"
6139                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6140                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6141                Style);
6142 
6143   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6144 
6145   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6146   verifyFormat("SomeClassWithALongName::Constructor(\n"
6147                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6148                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6149                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6150                Style);
6151 
6152   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6153   verifyFormat("SomeClassWithALongName::Constructor(\n"
6154                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6155                "    int bbbbbbbbbbbbb,\n"
6156                "    int cccccccccccccccc)\n"
6157                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6158                Style);
6159 
6160   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6161   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6162   verifyFormat("SomeClassWithALongName::Constructor(\n"
6163                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6164                "    int bbbbbbbbbbbbb)\n"
6165                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6166                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6167                Style);
6168 
6169   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6170   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6171   verifyFormat("SomeClassWithALongName::Constructor(\n"
6172                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6173                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6174                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6175                Style);
6176 
6177   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6178   verifyFormat("SomeClassWithALongName::Constructor(\n"
6179                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6180                "    int bbbbbbbbbbbbb,\n"
6181                "    int cccccccccccccccc) :\n"
6182                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6183                Style);
6184 
6185   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6186   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6187   verifyFormat("SomeClassWithALongName::Constructor(\n"
6188                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6189                "    int bbbbbbbbbbbbb) :\n"
6190                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6191                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6192                Style);
6193 }
6194 
6195 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6196   FormatStyle Style = getLLVMStyle();
6197   Style.ColumnLimit = 60;
6198   Style.BinPackArguments = false;
6199   for (int i = 0; i < 4; ++i) {
6200     // Test all combinations of parameters that should not have an effect.
6201     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6202     Style.PackConstructorInitializers =
6203         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6204 
6205     Style.AllowAllArgumentsOnNextLine = true;
6206     verifyFormat("void foo() {\n"
6207                  "  FunctionCallWithReallyLongName(\n"
6208                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6209                  "}",
6210                  Style);
6211     Style.AllowAllArgumentsOnNextLine = false;
6212     verifyFormat("void foo() {\n"
6213                  "  FunctionCallWithReallyLongName(\n"
6214                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6215                  "      bbbbbbbbbbbb);\n"
6216                  "}",
6217                  Style);
6218 
6219     Style.AllowAllArgumentsOnNextLine = true;
6220     verifyFormat("void foo() {\n"
6221                  "  auto VariableWithReallyLongName = {\n"
6222                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6223                  "}",
6224                  Style);
6225     Style.AllowAllArgumentsOnNextLine = false;
6226     verifyFormat("void foo() {\n"
6227                  "  auto VariableWithReallyLongName = {\n"
6228                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6229                  "      bbbbbbbbbbbb};\n"
6230                  "}",
6231                  Style);
6232   }
6233 
6234   // This parameter should not affect declarations.
6235   Style.BinPackParameters = false;
6236   Style.AllowAllArgumentsOnNextLine = false;
6237   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6238   verifyFormat("void FunctionCallWithReallyLongName(\n"
6239                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6240                Style);
6241   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6242   verifyFormat("void FunctionCallWithReallyLongName(\n"
6243                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6244                "    int bbbbbbbbbbbb);",
6245                Style);
6246 }
6247 
6248 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6249   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6250   // and BAS_Align.
6251   auto Style = getLLVMStyle();
6252   Style.ColumnLimit = 35;
6253   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6254                     "void functionDecl(int A, int B, int C);";
6255   Style.AllowAllArgumentsOnNextLine = false;
6256   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6257   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6258                       "    paramC);\n"
6259                       "void functionDecl(int A, int B,\n"
6260                       "    int C);"),
6261             format(Input, Style));
6262   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6263   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6264                       "             paramC);\n"
6265                       "void functionDecl(int A, int B,\n"
6266                       "                  int C);"),
6267             format(Input, Style));
6268   // However, BAS_AlwaysBreak should take precedence over
6269   // AllowAllArgumentsOnNextLine.
6270   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6271   EXPECT_EQ(StringRef("functionCall(\n"
6272                       "    paramA, paramB, paramC);\n"
6273                       "void functionDecl(\n"
6274                       "    int A, int B, int C);"),
6275             format(Input, Style));
6276 
6277   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6278   // first argument.
6279   Style.AllowAllArgumentsOnNextLine = true;
6280   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6281   EXPECT_EQ(StringRef("functionCall(\n"
6282                       "    paramA, paramB, paramC);\n"
6283                       "void functionDecl(\n"
6284                       "    int A, int B, int C);"),
6285             format(Input, Style));
6286   // It wouldn't fit on one line with aligned parameters so this setting
6287   // doesn't change anything for BAS_Align.
6288   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6289   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6290                       "             paramC);\n"
6291                       "void functionDecl(int A, int B,\n"
6292                       "                  int C);"),
6293             format(Input, Style));
6294   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6295   EXPECT_EQ(StringRef("functionCall(\n"
6296                       "    paramA, paramB, paramC);\n"
6297                       "void functionDecl(\n"
6298                       "    int A, int B, int C);"),
6299             format(Input, Style));
6300 }
6301 
6302 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6303   FormatStyle Style = getLLVMStyle();
6304   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6305 
6306   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6307   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6308                getStyleWithColumns(Style, 45));
6309   verifyFormat("Constructor() :\n"
6310                "    Initializer(FitsOnTheLine) {}",
6311                getStyleWithColumns(Style, 44));
6312   verifyFormat("Constructor() :\n"
6313                "    Initializer(FitsOnTheLine) {}",
6314                getStyleWithColumns(Style, 43));
6315 
6316   verifyFormat("template <typename T>\n"
6317                "Constructor() : Initializer(FitsOnTheLine) {}",
6318                getStyleWithColumns(Style, 50));
6319   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6320   verifyFormat(
6321       "SomeClass::Constructor() :\n"
6322       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6323       Style);
6324 
6325   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6326   verifyFormat(
6327       "SomeClass::Constructor() :\n"
6328       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6329       Style);
6330 
6331   verifyFormat(
6332       "SomeClass::Constructor() :\n"
6333       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6334       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6335       Style);
6336   verifyFormat(
6337       "SomeClass::Constructor() :\n"
6338       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6339       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6340       Style);
6341   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6342                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6343                "    aaaaaaaaaa(aaaaaa) {}",
6344                Style);
6345 
6346   verifyFormat("Constructor() :\n"
6347                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6348                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6349                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6350                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6351                Style);
6352 
6353   verifyFormat("Constructor() :\n"
6354                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6355                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6356                Style);
6357 
6358   verifyFormat("Constructor(int Parameter = 0) :\n"
6359                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6360                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6361                Style);
6362   verifyFormat("Constructor() :\n"
6363                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6364                "}",
6365                getStyleWithColumns(Style, 60));
6366   verifyFormat("Constructor() :\n"
6367                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6368                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6369                Style);
6370 
6371   // Here a line could be saved by splitting the second initializer onto two
6372   // lines, but that is not desirable.
6373   verifyFormat("Constructor() :\n"
6374                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6375                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6376                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6377                Style);
6378 
6379   FormatStyle OnePerLine = Style;
6380   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6381   verifyFormat("SomeClass::Constructor() :\n"
6382                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6383                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6384                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6385                OnePerLine);
6386   verifyFormat("SomeClass::Constructor() :\n"
6387                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6388                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6389                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6390                OnePerLine);
6391   verifyFormat("MyClass::MyClass(int var) :\n"
6392                "    some_var_(var),            // 4 space indent\n"
6393                "    some_other_var_(var + 1) { // lined up\n"
6394                "}",
6395                OnePerLine);
6396   verifyFormat("Constructor() :\n"
6397                "    aaaaa(aaaaaa),\n"
6398                "    aaaaa(aaaaaa),\n"
6399                "    aaaaa(aaaaaa),\n"
6400                "    aaaaa(aaaaaa),\n"
6401                "    aaaaa(aaaaaa) {}",
6402                OnePerLine);
6403   verifyFormat("Constructor() :\n"
6404                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6405                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6406                OnePerLine);
6407   OnePerLine.BinPackParameters = false;
6408   verifyFormat("Constructor() :\n"
6409                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6410                "        aaaaaaaaaaa().aaa(),\n"
6411                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6412                OnePerLine);
6413   OnePerLine.ColumnLimit = 60;
6414   verifyFormat("Constructor() :\n"
6415                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6416                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6417                OnePerLine);
6418 
6419   EXPECT_EQ("Constructor() :\n"
6420             "    // Comment forcing unwanted break.\n"
6421             "    aaaa(aaaa) {}",
6422             format("Constructor() :\n"
6423                    "    // Comment forcing unwanted break.\n"
6424                    "    aaaa(aaaa) {}",
6425                    Style));
6426 
6427   Style.ColumnLimit = 0;
6428   verifyFormat("SomeClass::Constructor() :\n"
6429                "    a(a) {}",
6430                Style);
6431   verifyFormat("SomeClass::Constructor() noexcept :\n"
6432                "    a(a) {}",
6433                Style);
6434   verifyFormat("SomeClass::Constructor() :\n"
6435                "    a(a), b(b), c(c) {}",
6436                Style);
6437   verifyFormat("SomeClass::Constructor() :\n"
6438                "    a(a) {\n"
6439                "  foo();\n"
6440                "  bar();\n"
6441                "}",
6442                Style);
6443 
6444   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6445   verifyFormat("SomeClass::Constructor() :\n"
6446                "    a(a), b(b), c(c) {\n"
6447                "}",
6448                Style);
6449   verifyFormat("SomeClass::Constructor() :\n"
6450                "    a(a) {\n"
6451                "}",
6452                Style);
6453 
6454   Style.ColumnLimit = 80;
6455   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6456   Style.ConstructorInitializerIndentWidth = 2;
6457   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6458   verifyFormat("SomeClass::Constructor() :\n"
6459                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6460                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6461                Style);
6462 
6463   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6464   // well
6465   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6466   verifyFormat(
6467       "class SomeClass\n"
6468       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6469       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6470       Style);
6471   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6472   verifyFormat(
6473       "class SomeClass\n"
6474       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6475       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6476       Style);
6477   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6478   verifyFormat(
6479       "class SomeClass :\n"
6480       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6481       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6482       Style);
6483   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6484   verifyFormat(
6485       "class SomeClass\n"
6486       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6487       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6488       Style);
6489 }
6490 
6491 #ifndef EXPENSIVE_CHECKS
6492 // Expensive checks enables libstdc++ checking which includes validating the
6493 // state of ranges used in std::priority_queue - this blows out the
6494 // runtime/scalability of the function and makes this test unacceptably slow.
6495 TEST_F(FormatTest, MemoizationTests) {
6496   // This breaks if the memoization lookup does not take \c Indent and
6497   // \c LastSpace into account.
6498   verifyFormat(
6499       "extern CFRunLoopTimerRef\n"
6500       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6501       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6502       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6503       "                     CFRunLoopTimerContext *context) {}");
6504 
6505   // Deep nesting somewhat works around our memoization.
6506   verifyFormat(
6507       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6508       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6509       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6510       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6511       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6512       getLLVMStyleWithColumns(65));
6513   verifyFormat(
6514       "aaaaa(\n"
6515       "    aaaaa,\n"
6516       "    aaaaa(\n"
6517       "        aaaaa,\n"
6518       "        aaaaa(\n"
6519       "            aaaaa,\n"
6520       "            aaaaa(\n"
6521       "                aaaaa,\n"
6522       "                aaaaa(\n"
6523       "                    aaaaa,\n"
6524       "                    aaaaa(\n"
6525       "                        aaaaa,\n"
6526       "                        aaaaa(\n"
6527       "                            aaaaa,\n"
6528       "                            aaaaa(\n"
6529       "                                aaaaa,\n"
6530       "                                aaaaa(\n"
6531       "                                    aaaaa,\n"
6532       "                                    aaaaa(\n"
6533       "                                        aaaaa,\n"
6534       "                                        aaaaa(\n"
6535       "                                            aaaaa,\n"
6536       "                                            aaaaa(\n"
6537       "                                                aaaaa,\n"
6538       "                                                aaaaa))))))))))));",
6539       getLLVMStyleWithColumns(65));
6540   verifyFormat(
6541       "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"
6542       "                                  a),\n"
6543       "                                a),\n"
6544       "                              a),\n"
6545       "                            a),\n"
6546       "                          a),\n"
6547       "                        a),\n"
6548       "                      a),\n"
6549       "                    a),\n"
6550       "                  a),\n"
6551       "                a),\n"
6552       "              a),\n"
6553       "            a),\n"
6554       "          a),\n"
6555       "        a),\n"
6556       "      a),\n"
6557       "    a),\n"
6558       "  a)",
6559       getLLVMStyleWithColumns(65));
6560 
6561   // This test takes VERY long when memoization is broken.
6562   FormatStyle OnePerLine = getLLVMStyle();
6563   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6564   OnePerLine.BinPackParameters = false;
6565   std::string input = "Constructor()\n"
6566                       "    : aaaa(a,\n";
6567   for (unsigned i = 0, e = 80; i != e; ++i) {
6568     input += "           a,\n";
6569   }
6570   input += "           a) {}";
6571   verifyFormat(input, OnePerLine);
6572 }
6573 #endif
6574 
6575 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6576   verifyFormat(
6577       "void f() {\n"
6578       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6579       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6580       "    f();\n"
6581       "}");
6582   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6583                "    Intervals[i - 1].getRange().getLast()) {\n}");
6584 }
6585 
6586 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6587   // Principially, we break function declarations in a certain order:
6588   // 1) break amongst arguments.
6589   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6590                "                              Cccccccccccccc cccccccccccccc);");
6591   verifyFormat("template <class TemplateIt>\n"
6592                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6593                "                            TemplateIt *stop) {}");
6594 
6595   // 2) break after return type.
6596   verifyFormat(
6597       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6598       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6599       getGoogleStyle());
6600 
6601   // 3) break after (.
6602   verifyFormat(
6603       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6604       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6605       getGoogleStyle());
6606 
6607   // 4) break before after nested name specifiers.
6608   verifyFormat(
6609       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6610       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6611       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6612       getGoogleStyle());
6613 
6614   // However, there are exceptions, if a sufficient amount of lines can be
6615   // saved.
6616   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6617   // more adjusting.
6618   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6619                "                                  Cccccccccccccc cccccccccc,\n"
6620                "                                  Cccccccccccccc cccccccccc,\n"
6621                "                                  Cccccccccccccc cccccccccc,\n"
6622                "                                  Cccccccccccccc cccccccccc);");
6623   verifyFormat(
6624       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6625       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6626       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6627       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6628       getGoogleStyle());
6629   verifyFormat(
6630       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6631       "                                          Cccccccccccccc cccccccccc,\n"
6632       "                                          Cccccccccccccc cccccccccc,\n"
6633       "                                          Cccccccccccccc cccccccccc,\n"
6634       "                                          Cccccccccccccc cccccccccc,\n"
6635       "                                          Cccccccccccccc cccccccccc,\n"
6636       "                                          Cccccccccccccc cccccccccc);");
6637   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6638                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6639                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6640                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6641                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6642 
6643   // Break after multi-line parameters.
6644   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6645                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6646                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6647                "    bbbb bbbb);");
6648   verifyFormat("void SomeLoooooooooooongFunction(\n"
6649                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6650                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6651                "    int bbbbbbbbbbbbb);");
6652 
6653   // Treat overloaded operators like other functions.
6654   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6655                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6656   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6657                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6658   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6659                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6660   verifyGoogleFormat(
6661       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6662       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6663   verifyGoogleFormat(
6664       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6665       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6666   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6667                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6668   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6669                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6670   verifyGoogleFormat(
6671       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6672       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6673       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6674   verifyGoogleFormat("template <typename T>\n"
6675                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6676                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6677                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6678 
6679   FormatStyle Style = getLLVMStyle();
6680   Style.PointerAlignment = FormatStyle::PAS_Left;
6681   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6682                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6683                Style);
6684   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6685                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6686                Style);
6687 }
6688 
6689 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6690   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6691   // Prefer keeping `::` followed by `operator` together.
6692   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6693             "ccccccccc::operator++() {\n"
6694             "  stuff();\n"
6695             "}",
6696             format("const aaaa::bbbbbbb\n"
6697                    "&ccccccccc::operator++() { stuff(); }",
6698                    getLLVMStyleWithColumns(40)));
6699 }
6700 
6701 TEST_F(FormatTest, TrailingReturnType) {
6702   verifyFormat("auto foo() -> int;\n");
6703   // correct trailing return type spacing
6704   verifyFormat("auto operator->() -> int;\n");
6705   verifyFormat("auto operator++(int) -> int;\n");
6706 
6707   verifyFormat("struct S {\n"
6708                "  auto bar() const -> int;\n"
6709                "};");
6710   verifyFormat("template <size_t Order, typename T>\n"
6711                "auto load_img(const std::string &filename)\n"
6712                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6713   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6714                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6715   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6716   verifyFormat("template <typename T>\n"
6717                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6718                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6719 
6720   // Not trailing return types.
6721   verifyFormat("void f() { auto a = b->c(); }");
6722 }
6723 
6724 TEST_F(FormatTest, DeductionGuides) {
6725   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6726   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6727   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6728   verifyFormat(
6729       "template <class... T>\n"
6730       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6731   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6732   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6733   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6734   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6735   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6736   verifyFormat("template <class T> x() -> x<1>;");
6737   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6738 
6739   // Ensure not deduction guides.
6740   verifyFormat("c()->f<int>();");
6741   verifyFormat("x()->foo<1>;");
6742   verifyFormat("x = p->foo<3>();");
6743   verifyFormat("x()->x<1>();");
6744   verifyFormat("x()->x<1>;");
6745 }
6746 
6747 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6748   // Avoid breaking before trailing 'const' or other trailing annotations, if
6749   // they are not function-like.
6750   FormatStyle Style = getGoogleStyle();
6751   Style.ColumnLimit = 47;
6752   verifyFormat("void someLongFunction(\n"
6753                "    int someLoooooooooooooongParameter) const {\n}",
6754                getLLVMStyleWithColumns(47));
6755   verifyFormat("LoooooongReturnType\n"
6756                "someLoooooooongFunction() const {}",
6757                getLLVMStyleWithColumns(47));
6758   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6759                "    const {}",
6760                Style);
6761   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6762                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6763   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6764                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6765   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6766                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6767   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6768                "                   aaaaaaaaaaa aaaaa) const override;");
6769   verifyGoogleFormat(
6770       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6771       "    const override;");
6772 
6773   // Even if the first parameter has to be wrapped.
6774   verifyFormat("void someLongFunction(\n"
6775                "    int someLongParameter) const {}",
6776                getLLVMStyleWithColumns(46));
6777   verifyFormat("void someLongFunction(\n"
6778                "    int someLongParameter) const {}",
6779                Style);
6780   verifyFormat("void someLongFunction(\n"
6781                "    int someLongParameter) override {}",
6782                Style);
6783   verifyFormat("void someLongFunction(\n"
6784                "    int someLongParameter) OVERRIDE {}",
6785                Style);
6786   verifyFormat("void someLongFunction(\n"
6787                "    int someLongParameter) final {}",
6788                Style);
6789   verifyFormat("void someLongFunction(\n"
6790                "    int someLongParameter) FINAL {}",
6791                Style);
6792   verifyFormat("void someLongFunction(\n"
6793                "    int parameter) const override {}",
6794                Style);
6795 
6796   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6797   verifyFormat("void someLongFunction(\n"
6798                "    int someLongParameter) const\n"
6799                "{\n"
6800                "}",
6801                Style);
6802 
6803   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6804   verifyFormat("void someLongFunction(\n"
6805                "    int someLongParameter) const\n"
6806                "  {\n"
6807                "  }",
6808                Style);
6809 
6810   // Unless these are unknown annotations.
6811   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6812                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6813                "    LONG_AND_UGLY_ANNOTATION;");
6814 
6815   // Breaking before function-like trailing annotations is fine to keep them
6816   // close to their arguments.
6817   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6818                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6819   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6820                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6821   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6822                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6823   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6824                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6825   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6826 
6827   verifyFormat(
6828       "void aaaaaaaaaaaaaaaaaa()\n"
6829       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6830       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6831   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6832                "    __attribute__((unused));");
6833   verifyGoogleFormat(
6834       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6835       "    GUARDED_BY(aaaaaaaaaaaa);");
6836   verifyGoogleFormat(
6837       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6838       "    GUARDED_BY(aaaaaaaaaaaa);");
6839   verifyGoogleFormat(
6840       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6841       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6842   verifyGoogleFormat(
6843       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6844       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6845 }
6846 
6847 TEST_F(FormatTest, FunctionAnnotations) {
6848   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6849                "int OldFunction(const string &parameter) {}");
6850   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6851                "string OldFunction(const string &parameter) {}");
6852   verifyFormat("template <typename T>\n"
6853                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6854                "string OldFunction(const string &parameter) {}");
6855 
6856   // Not function annotations.
6857   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6858                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6859   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6860                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6861   verifyFormat("MACRO(abc).function() // wrap\n"
6862                "    << abc;");
6863   verifyFormat("MACRO(abc)->function() // wrap\n"
6864                "    << abc;");
6865   verifyFormat("MACRO(abc)::function() // wrap\n"
6866                "    << abc;");
6867 }
6868 
6869 TEST_F(FormatTest, BreaksDesireably) {
6870   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6871                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6872                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6873   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6874                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6875                "}");
6876 
6877   verifyFormat(
6878       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6879       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6880 
6881   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6882                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6883                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6884 
6885   verifyFormat(
6886       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6887       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6888       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6889       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6890       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6891 
6892   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6893                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6894 
6895   verifyFormat(
6896       "void f() {\n"
6897       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6898       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6899       "}");
6900   verifyFormat(
6901       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6902       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6903   verifyFormat(
6904       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6905       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6906   verifyFormat(
6907       "aaaaaa(aaa,\n"
6908       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6909       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6910       "       aaaa);");
6911   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6912                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6913                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6914 
6915   // Indent consistently independent of call expression and unary operator.
6916   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6917                "    dddddddddddddddddddddddddddddd));");
6918   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6919                "    dddddddddddddddddddddddddddddd));");
6920   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6921                "    dddddddddddddddddddddddddddddd));");
6922 
6923   // This test case breaks on an incorrect memoization, i.e. an optimization not
6924   // taking into account the StopAt value.
6925   verifyFormat(
6926       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6927       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6928       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6929       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6930 
6931   verifyFormat("{\n  {\n    {\n"
6932                "      Annotation.SpaceRequiredBefore =\n"
6933                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6934                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6935                "    }\n  }\n}");
6936 
6937   // Break on an outer level if there was a break on an inner level.
6938   EXPECT_EQ("f(g(h(a, // comment\n"
6939             "      b, c),\n"
6940             "    d, e),\n"
6941             "  x, y);",
6942             format("f(g(h(a, // comment\n"
6943                    "    b, c), d, e), x, y);"));
6944 
6945   // Prefer breaking similar line breaks.
6946   verifyFormat(
6947       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6948       "                             NSTrackingMouseEnteredAndExited |\n"
6949       "                             NSTrackingActiveAlways;");
6950 }
6951 
6952 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6953   FormatStyle NoBinPacking = getGoogleStyle();
6954   NoBinPacking.BinPackParameters = false;
6955   NoBinPacking.BinPackArguments = true;
6956   verifyFormat("void f() {\n"
6957                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6958                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6959                "}",
6960                NoBinPacking);
6961   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6962                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6963                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6964                NoBinPacking);
6965 
6966   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6967   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6968                "                        vector<int> bbbbbbbbbbbbbbb);",
6969                NoBinPacking);
6970   // FIXME: This behavior difference is probably not wanted. However, currently
6971   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6972   // template arguments from BreakBeforeParameter being set because of the
6973   // one-per-line formatting.
6974   verifyFormat(
6975       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6976       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6977       NoBinPacking);
6978   verifyFormat(
6979       "void fffffffffff(\n"
6980       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6981       "        aaaaaaaaaa);");
6982 }
6983 
6984 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6985   FormatStyle NoBinPacking = getGoogleStyle();
6986   NoBinPacking.BinPackParameters = false;
6987   NoBinPacking.BinPackArguments = false;
6988   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6989                "  aaaaaaaaaaaaaaaaaaaa,\n"
6990                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6991                NoBinPacking);
6992   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6993                "        aaaaaaaaaaaaa,\n"
6994                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6995                NoBinPacking);
6996   verifyFormat(
6997       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6998       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6999       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7000       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7001       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7002       NoBinPacking);
7003   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7004                "    .aaaaaaaaaaaaaaaaaa();",
7005                NoBinPacking);
7006   verifyFormat("void f() {\n"
7007                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7008                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7009                "}",
7010                NoBinPacking);
7011 
7012   verifyFormat(
7013       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7014       "             aaaaaaaaaaaa,\n"
7015       "             aaaaaaaaaaaa);",
7016       NoBinPacking);
7017   verifyFormat(
7018       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7019       "                               ddddddddddddddddddddddddddddd),\n"
7020       "             test);",
7021       NoBinPacking);
7022 
7023   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7024                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7025                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7026                "    aaaaaaaaaaaaaaaaaa;",
7027                NoBinPacking);
7028   verifyFormat("a(\"a\"\n"
7029                "  \"a\",\n"
7030                "  a);");
7031 
7032   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7033   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7034                "                aaaaaaaaa,\n"
7035                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7036                NoBinPacking);
7037   verifyFormat(
7038       "void f() {\n"
7039       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7040       "      .aaaaaaa();\n"
7041       "}",
7042       NoBinPacking);
7043   verifyFormat(
7044       "template <class SomeType, class SomeOtherType>\n"
7045       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7046       NoBinPacking);
7047 }
7048 
7049 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7050   FormatStyle Style = getLLVMStyleWithColumns(15);
7051   Style.ExperimentalAutoDetectBinPacking = true;
7052   EXPECT_EQ("aaa(aaaa,\n"
7053             "    aaaa,\n"
7054             "    aaaa);\n"
7055             "aaa(aaaa,\n"
7056             "    aaaa,\n"
7057             "    aaaa);",
7058             format("aaa(aaaa,\n" // one-per-line
7059                    "  aaaa,\n"
7060                    "    aaaa  );\n"
7061                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7062                    Style));
7063   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7064             "    aaaa);\n"
7065             "aaa(aaaa, aaaa,\n"
7066             "    aaaa);",
7067             format("aaa(aaaa,  aaaa,\n" // bin-packed
7068                    "    aaaa  );\n"
7069                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7070                    Style));
7071 }
7072 
7073 TEST_F(FormatTest, FormatsBuilderPattern) {
7074   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7075                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7076                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7077                "    .StartsWith(\".init\", ORDER_INIT)\n"
7078                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7079                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7080                "    .Default(ORDER_TEXT);\n");
7081 
7082   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7083                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7084   verifyFormat("aaaaaaa->aaaaaaa\n"
7085                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7086                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7087                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7088   verifyFormat(
7089       "aaaaaaa->aaaaaaa\n"
7090       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7091       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7092   verifyFormat(
7093       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7094       "    aaaaaaaaaaaaaa);");
7095   verifyFormat(
7096       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7097       "    aaaaaa->aaaaaaaaaaaa()\n"
7098       "        ->aaaaaaaaaaaaaaaa(\n"
7099       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7100       "        ->aaaaaaaaaaaaaaaaa();");
7101   verifyGoogleFormat(
7102       "void f() {\n"
7103       "  someo->Add((new util::filetools::Handler(dir))\n"
7104       "                 ->OnEvent1(NewPermanentCallback(\n"
7105       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7106       "                 ->OnEvent2(NewPermanentCallback(\n"
7107       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7108       "                 ->OnEvent3(NewPermanentCallback(\n"
7109       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7110       "                 ->OnEvent5(NewPermanentCallback(\n"
7111       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7112       "                 ->OnEvent6(NewPermanentCallback(\n"
7113       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7114       "}");
7115 
7116   verifyFormat(
7117       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7118   verifyFormat("aaaaaaaaaaaaaaa()\n"
7119                "    .aaaaaaaaaaaaaaa()\n"
7120                "    .aaaaaaaaaaaaaaa()\n"
7121                "    .aaaaaaaaaaaaaaa()\n"
7122                "    .aaaaaaaaaaaaaaa();");
7123   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7124                "    .aaaaaaaaaaaaaaa()\n"
7125                "    .aaaaaaaaaaaaaaa()\n"
7126                "    .aaaaaaaaaaaaaaa();");
7127   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7128                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7129                "    .aaaaaaaaaaaaaaa();");
7130   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7131                "    ->aaaaaaaaaaaaaae(0)\n"
7132                "    ->aaaaaaaaaaaaaaa();");
7133 
7134   // Don't linewrap after very short segments.
7135   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7136                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7137                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7138   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7139                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7140                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7141   verifyFormat("aaa()\n"
7142                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7143                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7144                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7145 
7146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7147                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7148                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7149   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7150                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7151                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7152 
7153   // Prefer not to break after empty parentheses.
7154   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7155                "    First->LastNewlineOffset);");
7156 
7157   // Prefer not to create "hanging" indents.
7158   verifyFormat(
7159       "return !soooooooooooooome_map\n"
7160       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7161       "            .second;");
7162   verifyFormat(
7163       "return aaaaaaaaaaaaaaaa\n"
7164       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7165       "    .aaaa(aaaaaaaaaaaaaa);");
7166   // No hanging indent here.
7167   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7168                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7169   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7170                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7171   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7172                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7173                getLLVMStyleWithColumns(60));
7174   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7175                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7176                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7177                getLLVMStyleWithColumns(59));
7178   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7179                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7180                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7181 
7182   // Dont break if only closing statements before member call
7183   verifyFormat("test() {\n"
7184                "  ([]() -> {\n"
7185                "    int b = 32;\n"
7186                "    return 3;\n"
7187                "  }).foo();\n"
7188                "}");
7189   verifyFormat("test() {\n"
7190                "  (\n"
7191                "      []() -> {\n"
7192                "        int b = 32;\n"
7193                "        return 3;\n"
7194                "      },\n"
7195                "      foo, bar)\n"
7196                "      .foo();\n"
7197                "}");
7198   verifyFormat("test() {\n"
7199                "  ([]() -> {\n"
7200                "    int b = 32;\n"
7201                "    return 3;\n"
7202                "  })\n"
7203                "      .foo()\n"
7204                "      .bar();\n"
7205                "}");
7206   verifyFormat("test() {\n"
7207                "  ([]() -> {\n"
7208                "    int b = 32;\n"
7209                "    return 3;\n"
7210                "  })\n"
7211                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7212                "           \"bbbb\");\n"
7213                "}",
7214                getLLVMStyleWithColumns(30));
7215 }
7216 
7217 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7218   verifyFormat(
7219       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7220       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7221   verifyFormat(
7222       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7223       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7224 
7225   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7226                "    ccccccccccccccccccccccccc) {\n}");
7227   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7228                "    ccccccccccccccccccccccccc) {\n}");
7229 
7230   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7231                "    ccccccccccccccccccccccccc) {\n}");
7232   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7233                "    ccccccccccccccccccccccccc) {\n}");
7234 
7235   verifyFormat(
7236       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7237       "    ccccccccccccccccccccccccc) {\n}");
7238   verifyFormat(
7239       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7240       "    ccccccccccccccccccccccccc) {\n}");
7241 
7242   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7243                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7244                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7245                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7246   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7247                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7248                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7249                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7250 
7251   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7252                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7253                "    aaaaaaaaaaaaaaa != aa) {\n}");
7254   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7255                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7256                "    aaaaaaaaaaaaaaa != aa) {\n}");
7257 }
7258 
7259 TEST_F(FormatTest, BreaksAfterAssignments) {
7260   verifyFormat(
7261       "unsigned Cost =\n"
7262       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7263       "                        SI->getPointerAddressSpaceee());\n");
7264   verifyFormat(
7265       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7266       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7267 
7268   verifyFormat(
7269       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7270       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7271   verifyFormat("unsigned OriginalStartColumn =\n"
7272                "    SourceMgr.getSpellingColumnNumber(\n"
7273                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7274                "    1;");
7275 }
7276 
7277 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7278   FormatStyle Style = getLLVMStyle();
7279   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7280                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7281                Style);
7282 
7283   Style.PenaltyBreakAssignment = 20;
7284   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7285                "                                 cccccccccccccccccccccccccc;",
7286                Style);
7287 }
7288 
7289 TEST_F(FormatTest, AlignsAfterAssignments) {
7290   verifyFormat(
7291       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7292       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7293   verifyFormat(
7294       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7295       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7296   verifyFormat(
7297       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7298       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7299   verifyFormat(
7300       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7301       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7302   verifyFormat(
7303       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7304       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7305       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7306 }
7307 
7308 TEST_F(FormatTest, AlignsAfterReturn) {
7309   verifyFormat(
7310       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7311       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7312   verifyFormat(
7313       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7314       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7315   verifyFormat(
7316       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7317       "       aaaaaaaaaaaaaaaaaaaaaa();");
7318   verifyFormat(
7319       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7320       "        aaaaaaaaaaaaaaaaaaaaaa());");
7321   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7322                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7323   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7324                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7325                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7326   verifyFormat("return\n"
7327                "    // true if code is one of a or b.\n"
7328                "    code == a || code == b;");
7329 }
7330 
7331 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7332   verifyFormat(
7333       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7334       "                                                aaaaaaaaa aaaaaaa) {}");
7335   verifyFormat(
7336       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7337       "                                               aaaaaaaaaaa aaaaaaaaa);");
7338   verifyFormat(
7339       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7340       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7341   FormatStyle Style = getLLVMStyle();
7342   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7343   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7344                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7345                Style);
7346   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7347                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7348                Style);
7349   verifyFormat("SomeLongVariableName->someFunction(\n"
7350                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7351                Style);
7352   verifyFormat(
7353       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7354       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7355       Style);
7356   verifyFormat(
7357       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7358       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7359       Style);
7360   verifyFormat(
7361       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7362       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7363       Style);
7364 
7365   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7366                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7367                "        b));",
7368                Style);
7369 
7370   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7371   Style.BinPackArguments = false;
7372   Style.BinPackParameters = false;
7373   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7374                "    aaaaaaaaaaa aaaaaaaa,\n"
7375                "    aaaaaaaaa aaaaaaa,\n"
7376                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7377                Style);
7378   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7379                "    aaaaaaaaaaa aaaaaaaaa,\n"
7380                "    aaaaaaaaaaa aaaaaaaaa,\n"
7381                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7382                Style);
7383   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7384                "    aaaaaaaaaaaaaaa,\n"
7385                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7386                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7387                Style);
7388   verifyFormat(
7389       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7390       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7391       Style);
7392   verifyFormat(
7393       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7394       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7395       Style);
7396   verifyFormat(
7397       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7398       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7399       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7400       "    aaaaaaaaaaaaaaaa);",
7401       Style);
7402   verifyFormat(
7403       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7404       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7405       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7406       "    aaaaaaaaaaaaaaaa);",
7407       Style);
7408 }
7409 
7410 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7411   FormatStyle Style = getLLVMStyleWithColumns(40);
7412   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7413                "          bbbbbbbbbbbbbbbbbbbbbb);",
7414                Style);
7415   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7416   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7417   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7418                "          bbbbbbbbbbbbbbbbbbbbbb);",
7419                Style);
7420   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7421   Style.AlignOperands = FormatStyle::OAS_Align;
7422   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7423                "          bbbbbbbbbbbbbbbbbbbbbb);",
7424                Style);
7425   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7426   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7427   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7428                "    bbbbbbbbbbbbbbbbbbbbbb);",
7429                Style);
7430 }
7431 
7432 TEST_F(FormatTest, BreaksConditionalExpressions) {
7433   verifyFormat(
7434       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7435       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7436       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7437   verifyFormat(
7438       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7439       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7440       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7441   verifyFormat(
7442       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7443       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7444   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7445                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7446                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7447   verifyFormat(
7448       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7449       "                                                    : aaaaaaaaaaaaa);");
7450   verifyFormat(
7451       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7452       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7453       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7454       "                   aaaaaaaaaaaaa);");
7455   verifyFormat(
7456       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7457       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7458       "                   aaaaaaaaaaaaa);");
7459   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7460                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7461                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7462                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7463                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7464   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7465                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7466                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7467                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7468                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7469                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7470                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7471   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7472                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7473                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7474                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7475                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7476   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7477                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7478                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7479   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7480                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7481                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7482                "        : aaaaaaaaaaaaaaaa;");
7483   verifyFormat(
7484       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7485       "    ? aaaaaaaaaaaaaaa\n"
7486       "    : aaaaaaaaaaaaaaa;");
7487   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7488                "          aaaaaaaaa\n"
7489                "      ? b\n"
7490                "      : c);");
7491   verifyFormat("return aaaa == bbbb\n"
7492                "           // comment\n"
7493                "           ? aaaa\n"
7494                "           : bbbb;");
7495   verifyFormat("unsigned Indent =\n"
7496                "    format(TheLine.First,\n"
7497                "           IndentForLevel[TheLine.Level] >= 0\n"
7498                "               ? IndentForLevel[TheLine.Level]\n"
7499                "               : TheLine * 2,\n"
7500                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7501                getLLVMStyleWithColumns(60));
7502   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7503                "                  ? aaaaaaaaaaaaaaa\n"
7504                "                  : bbbbbbbbbbbbbbb //\n"
7505                "                        ? ccccccccccccccc\n"
7506                "                        : ddddddddddddddd;");
7507   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7508                "                  ? aaaaaaaaaaaaaaa\n"
7509                "                  : (bbbbbbbbbbbbbbb //\n"
7510                "                         ? ccccccccccccccc\n"
7511                "                         : ddddddddddddddd);");
7512   verifyFormat(
7513       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7515       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7516       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7517       "                                      : aaaaaaaaaa;");
7518   verifyFormat(
7519       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7520       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7521       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7522 
7523   FormatStyle NoBinPacking = getLLVMStyle();
7524   NoBinPacking.BinPackArguments = false;
7525   verifyFormat(
7526       "void f() {\n"
7527       "  g(aaa,\n"
7528       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7529       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7530       "        ? aaaaaaaaaaaaaaa\n"
7531       "        : aaaaaaaaaaaaaaa);\n"
7532       "}",
7533       NoBinPacking);
7534   verifyFormat(
7535       "void f() {\n"
7536       "  g(aaa,\n"
7537       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7538       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7539       "        ?: aaaaaaaaaaaaaaa);\n"
7540       "}",
7541       NoBinPacking);
7542 
7543   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7544                "             // comment.\n"
7545                "             ccccccccccccccccccccccccccccccccccccccc\n"
7546                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7547                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7548 
7549   // Assignments in conditional expressions. Apparently not uncommon :-(.
7550   verifyFormat("return a != b\n"
7551                "           // comment\n"
7552                "           ? a = b\n"
7553                "           : a = b;");
7554   verifyFormat("return a != b\n"
7555                "           // comment\n"
7556                "           ? a = a != b\n"
7557                "                     // comment\n"
7558                "                     ? a = b\n"
7559                "                     : a\n"
7560                "           : a;\n");
7561   verifyFormat("return a != b\n"
7562                "           // comment\n"
7563                "           ? a\n"
7564                "           : a = a != b\n"
7565                "                     // comment\n"
7566                "                     ? a = b\n"
7567                "                     : a;");
7568 
7569   // Chained conditionals
7570   FormatStyle Style = getLLVMStyle();
7571   Style.ColumnLimit = 70;
7572   Style.AlignOperands = FormatStyle::OAS_Align;
7573   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7574                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7575                "                        : 3333333333333333;",
7576                Style);
7577   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7578                "       : bbbbbbbbbb     ? 2222222222222222\n"
7579                "                        : 3333333333333333;",
7580                Style);
7581   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7582                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7583                "                          : 3333333333333333;",
7584                Style);
7585   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7586                "       : bbbbbbbbbbbbbb ? 222222\n"
7587                "                        : 333333;",
7588                Style);
7589   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7590                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7591                "       : cccccccccccccc ? 3333333333333333\n"
7592                "                        : 4444444444444444;",
7593                Style);
7594   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7595                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7596                "                        : 3333333333333333;",
7597                Style);
7598   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7599                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7600                "                        : (aaa ? bbb : ccc);",
7601                Style);
7602   verifyFormat(
7603       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7604       "                                             : cccccccccccccccccc)\n"
7605       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7606       "                        : 3333333333333333;",
7607       Style);
7608   verifyFormat(
7609       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7610       "                                             : cccccccccccccccccc)\n"
7611       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7612       "                        : 3333333333333333;",
7613       Style);
7614   verifyFormat(
7615       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7616       "                                             : dddddddddddddddddd)\n"
7617       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7618       "                        : 3333333333333333;",
7619       Style);
7620   verifyFormat(
7621       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7622       "                                             : dddddddddddddddddd)\n"
7623       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7624       "                        : 3333333333333333;",
7625       Style);
7626   verifyFormat(
7627       "return aaaaaaaaa        ? 1111111111111111\n"
7628       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7629       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7630       "                                             : dddddddddddddddddd)\n",
7631       Style);
7632   verifyFormat(
7633       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7634       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7635       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7636       "                                             : cccccccccccccccccc);",
7637       Style);
7638   verifyFormat(
7639       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7640       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7641       "                                             : eeeeeeeeeeeeeeeeee)\n"
7642       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7643       "                        : 3333333333333333;",
7644       Style);
7645   verifyFormat(
7646       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7647       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7648       "                                             : eeeeeeeeeeeeeeeeee)\n"
7649       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7650       "                        : 3333333333333333;",
7651       Style);
7652   verifyFormat(
7653       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7654       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7655       "                                             : eeeeeeeeeeeeeeeeee)\n"
7656       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7657       "                        : 3333333333333333;",
7658       Style);
7659   verifyFormat(
7660       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7661       "                                             : cccccccccccccccccc\n"
7662       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7663       "                        : 3333333333333333;",
7664       Style);
7665   verifyFormat(
7666       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7667       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7668       "                                             : eeeeeeeeeeeeeeeeee\n"
7669       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7670       "                        : 3333333333333333;",
7671       Style);
7672   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7673                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7674                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7675                "                                   : eeeeeeeeeeeeeeeeee)\n"
7676                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7677                "                             : 3333333333333333;",
7678                Style);
7679   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7680                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7681                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7682                "                                : eeeeeeeeeeeeeeeeee\n"
7683                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7684                "                                 : 3333333333333333;",
7685                Style);
7686 
7687   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7688   Style.BreakBeforeTernaryOperators = false;
7689   // FIXME: Aligning the question marks is weird given DontAlign.
7690   // Consider disabling this alignment in this case. Also check whether this
7691   // will render the adjustment from https://reviews.llvm.org/D82199
7692   // unnecessary.
7693   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7694                "    bbbb                ? cccccccccccccccccc :\n"
7695                "                          ddddd;\n",
7696                Style);
7697 
7698   EXPECT_EQ(
7699       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7700       "    /*\n"
7701       "     */\n"
7702       "    function() {\n"
7703       "      try {\n"
7704       "        return JJJJJJJJJJJJJJ(\n"
7705       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7706       "      }\n"
7707       "    } :\n"
7708       "    function() {};",
7709       format(
7710           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7711           "     /*\n"
7712           "      */\n"
7713           "     function() {\n"
7714           "      try {\n"
7715           "        return JJJJJJJJJJJJJJ(\n"
7716           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7717           "      }\n"
7718           "    } :\n"
7719           "    function() {};",
7720           getGoogleStyle(FormatStyle::LK_JavaScript)));
7721 }
7722 
7723 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7724   FormatStyle Style = getLLVMStyle();
7725   Style.BreakBeforeTernaryOperators = false;
7726   Style.ColumnLimit = 70;
7727   verifyFormat(
7728       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7729       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7730       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7731       Style);
7732   verifyFormat(
7733       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7734       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7735       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7736       Style);
7737   verifyFormat(
7738       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7739       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7740       Style);
7741   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7742                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7743                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7744                Style);
7745   verifyFormat(
7746       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7747       "                                                      aaaaaaaaaaaaa);",
7748       Style);
7749   verifyFormat(
7750       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7751       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7752       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7753       "                   aaaaaaaaaaaaa);",
7754       Style);
7755   verifyFormat(
7756       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7757       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7758       "                   aaaaaaaaaaaaa);",
7759       Style);
7760   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7761                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7762                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7763                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7764                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7765                Style);
7766   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7767                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7768                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7769                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7770                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7771                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7772                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7773                Style);
7774   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7775                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7776                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7777                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7778                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7779                Style);
7780   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7781                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7782                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7783                Style);
7784   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7785                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7786                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7787                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7788                Style);
7789   verifyFormat(
7790       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7791       "    aaaaaaaaaaaaaaa :\n"
7792       "    aaaaaaaaaaaaaaa;",
7793       Style);
7794   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7795                "          aaaaaaaaa ?\n"
7796                "      b :\n"
7797                "      c);",
7798                Style);
7799   verifyFormat("unsigned Indent =\n"
7800                "    format(TheLine.First,\n"
7801                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7802                "               IndentForLevel[TheLine.Level] :\n"
7803                "               TheLine * 2,\n"
7804                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7805                Style);
7806   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7807                "                  aaaaaaaaaaaaaaa :\n"
7808                "                  bbbbbbbbbbbbbbb ? //\n"
7809                "                      ccccccccccccccc :\n"
7810                "                      ddddddddddddddd;",
7811                Style);
7812   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7813                "                  aaaaaaaaaaaaaaa :\n"
7814                "                  (bbbbbbbbbbbbbbb ? //\n"
7815                "                       ccccccccccccccc :\n"
7816                "                       ddddddddddddddd);",
7817                Style);
7818   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7819                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7820                "            ccccccccccccccccccccccccccc;",
7821                Style);
7822   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7823                "           aaaaa :\n"
7824                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7825                Style);
7826 
7827   // Chained conditionals
7828   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7829                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7830                "                          3333333333333333;",
7831                Style);
7832   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7833                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7834                "                          3333333333333333;",
7835                Style);
7836   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7837                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7838                "                          3333333333333333;",
7839                Style);
7840   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7841                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7842                "                          333333;",
7843                Style);
7844   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7845                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7846                "       cccccccccccccccc ? 3333333333333333 :\n"
7847                "                          4444444444444444;",
7848                Style);
7849   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7850                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7851                "                          3333333333333333;",
7852                Style);
7853   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7854                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7855                "                          (aaa ? bbb : ccc);",
7856                Style);
7857   verifyFormat(
7858       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7859       "                                               cccccccccccccccccc) :\n"
7860       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7861       "                          3333333333333333;",
7862       Style);
7863   verifyFormat(
7864       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7865       "                                               cccccccccccccccccc) :\n"
7866       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7867       "                          3333333333333333;",
7868       Style);
7869   verifyFormat(
7870       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7871       "                                               dddddddddddddddddd) :\n"
7872       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7873       "                          3333333333333333;",
7874       Style);
7875   verifyFormat(
7876       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7877       "                                               dddddddddddddddddd) :\n"
7878       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7879       "                          3333333333333333;",
7880       Style);
7881   verifyFormat(
7882       "return aaaaaaaaa        ? 1111111111111111 :\n"
7883       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7884       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7885       "                                               dddddddddddddddddd)\n",
7886       Style);
7887   verifyFormat(
7888       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7889       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7890       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7891       "                                               cccccccccccccccccc);",
7892       Style);
7893   verifyFormat(
7894       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7895       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7896       "                                               eeeeeeeeeeeeeeeeee) :\n"
7897       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7898       "                          3333333333333333;",
7899       Style);
7900   verifyFormat(
7901       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7902       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7903       "                                               eeeeeeeeeeeeeeeeee) :\n"
7904       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7905       "                          3333333333333333;",
7906       Style);
7907   verifyFormat(
7908       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7909       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7910       "                                               eeeeeeeeeeeeeeeeee) :\n"
7911       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7912       "                          3333333333333333;",
7913       Style);
7914   verifyFormat(
7915       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7916       "                                               cccccccccccccccccc :\n"
7917       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7918       "                          3333333333333333;",
7919       Style);
7920   verifyFormat(
7921       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7922       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7923       "                                               eeeeeeeeeeeeeeeeee :\n"
7924       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7925       "                          3333333333333333;",
7926       Style);
7927   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7928                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7929                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7930                "                                 eeeeeeeeeeeeeeeeee) :\n"
7931                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7932                "                               3333333333333333;",
7933                Style);
7934   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7935                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7936                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7937                "                                  eeeeeeeeeeeeeeeeee :\n"
7938                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7939                "                               3333333333333333;",
7940                Style);
7941 }
7942 
7943 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7944   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7945                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7946   verifyFormat("bool a = true, b = false;");
7947 
7948   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7949                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7950                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7951                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7952   verifyFormat(
7953       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7954       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7955       "     d = e && f;");
7956   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7957                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7958   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7959                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7960   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7961                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7962 
7963   FormatStyle Style = getGoogleStyle();
7964   Style.PointerAlignment = FormatStyle::PAS_Left;
7965   Style.DerivePointerAlignment = false;
7966   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7967                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7968                "    *b = bbbbbbbbbbbbbbbbbbb;",
7969                Style);
7970   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7971                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7972                Style);
7973   verifyFormat("vector<int*> a, b;", Style);
7974   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7975 }
7976 
7977 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7978   verifyFormat("arr[foo ? bar : baz];");
7979   verifyFormat("f()[foo ? bar : baz];");
7980   verifyFormat("(a + b)[foo ? bar : baz];");
7981   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7982 }
7983 
7984 TEST_F(FormatTest, AlignsStringLiterals) {
7985   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7986                "                                      \"short literal\");");
7987   verifyFormat(
7988       "looooooooooooooooooooooooongFunction(\n"
7989       "    \"short literal\"\n"
7990       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7991   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7992                "             \" string literals\",\n"
7993                "             and, other, parameters);");
7994   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7995             "      \"5678\";",
7996             format("fun + \"1243\" /* comment */\n"
7997                    "    \"5678\";",
7998                    getLLVMStyleWithColumns(28)));
7999   EXPECT_EQ(
8000       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8001       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8002       "         \"aaaaaaaaaaaaaaaa\";",
8003       format("aaaaaa ="
8004              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8005              "aaaaaaaaaaaaaaaaaaaaa\" "
8006              "\"aaaaaaaaaaaaaaaa\";"));
8007   verifyFormat("a = a + \"a\"\n"
8008                "        \"a\"\n"
8009                "        \"a\";");
8010   verifyFormat("f(\"a\", \"b\"\n"
8011                "       \"c\");");
8012 
8013   verifyFormat(
8014       "#define LL_FORMAT \"ll\"\n"
8015       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8016       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8017 
8018   verifyFormat("#define A(X)          \\\n"
8019                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8020                "  \"ccccc\"",
8021                getLLVMStyleWithColumns(23));
8022   verifyFormat("#define A \"def\"\n"
8023                "f(\"abc\" A \"ghi\"\n"
8024                "  \"jkl\");");
8025 
8026   verifyFormat("f(L\"a\"\n"
8027                "  L\"b\");");
8028   verifyFormat("#define A(X)            \\\n"
8029                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8030                "  L\"ccccc\"",
8031                getLLVMStyleWithColumns(25));
8032 
8033   verifyFormat("f(@\"a\"\n"
8034                "  @\"b\");");
8035   verifyFormat("NSString s = @\"a\"\n"
8036                "             @\"b\"\n"
8037                "             @\"c\";");
8038   verifyFormat("NSString s = @\"a\"\n"
8039                "              \"b\"\n"
8040                "              \"c\";");
8041 }
8042 
8043 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8044   FormatStyle Style = getLLVMStyle();
8045   // No declarations or definitions should be moved to own line.
8046   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8047   verifyFormat("class A {\n"
8048                "  int f() { return 1; }\n"
8049                "  int g();\n"
8050                "};\n"
8051                "int f() { return 1; }\n"
8052                "int g();\n",
8053                Style);
8054 
8055   // All declarations and definitions should have the return type moved to its
8056   // own line.
8057   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8058   Style.TypenameMacros = {"LIST"};
8059   verifyFormat("SomeType\n"
8060                "funcdecl(LIST(uint64_t));",
8061                Style);
8062   verifyFormat("class E {\n"
8063                "  int\n"
8064                "  f() {\n"
8065                "    return 1;\n"
8066                "  }\n"
8067                "  int\n"
8068                "  g();\n"
8069                "};\n"
8070                "int\n"
8071                "f() {\n"
8072                "  return 1;\n"
8073                "}\n"
8074                "int\n"
8075                "g();\n",
8076                Style);
8077 
8078   // Top-level definitions, and no kinds of declarations should have the
8079   // return type moved to its own line.
8080   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8081   verifyFormat("class B {\n"
8082                "  int f() { return 1; }\n"
8083                "  int g();\n"
8084                "};\n"
8085                "int\n"
8086                "f() {\n"
8087                "  return 1;\n"
8088                "}\n"
8089                "int g();\n",
8090                Style);
8091 
8092   // Top-level definitions and declarations should have the return type moved
8093   // to its own line.
8094   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8095   verifyFormat("class C {\n"
8096                "  int f() { return 1; }\n"
8097                "  int g();\n"
8098                "};\n"
8099                "int\n"
8100                "f() {\n"
8101                "  return 1;\n"
8102                "}\n"
8103                "int\n"
8104                "g();\n",
8105                Style);
8106 
8107   // All definitions should have the return type moved to its own line, but no
8108   // kinds of declarations.
8109   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8110   verifyFormat("class D {\n"
8111                "  int\n"
8112                "  f() {\n"
8113                "    return 1;\n"
8114                "  }\n"
8115                "  int g();\n"
8116                "};\n"
8117                "int\n"
8118                "f() {\n"
8119                "  return 1;\n"
8120                "}\n"
8121                "int g();\n",
8122                Style);
8123   verifyFormat("const char *\n"
8124                "f(void) {\n" // Break here.
8125                "  return \"\";\n"
8126                "}\n"
8127                "const char *bar(void);\n", // No break here.
8128                Style);
8129   verifyFormat("template <class T>\n"
8130                "T *\n"
8131                "f(T &c) {\n" // Break here.
8132                "  return NULL;\n"
8133                "}\n"
8134                "template <class T> T *f(T &c);\n", // No break here.
8135                Style);
8136   verifyFormat("class C {\n"
8137                "  int\n"
8138                "  operator+() {\n"
8139                "    return 1;\n"
8140                "  }\n"
8141                "  int\n"
8142                "  operator()() {\n"
8143                "    return 1;\n"
8144                "  }\n"
8145                "};\n",
8146                Style);
8147   verifyFormat("void\n"
8148                "A::operator()() {}\n"
8149                "void\n"
8150                "A::operator>>() {}\n"
8151                "void\n"
8152                "A::operator+() {}\n"
8153                "void\n"
8154                "A::operator*() {}\n"
8155                "void\n"
8156                "A::operator->() {}\n"
8157                "void\n"
8158                "A::operator void *() {}\n"
8159                "void\n"
8160                "A::operator void &() {}\n"
8161                "void\n"
8162                "A::operator void &&() {}\n"
8163                "void\n"
8164                "A::operator char *() {}\n"
8165                "void\n"
8166                "A::operator[]() {}\n"
8167                "void\n"
8168                "A::operator!() {}\n"
8169                "void\n"
8170                "A::operator**() {}\n"
8171                "void\n"
8172                "A::operator<Foo> *() {}\n"
8173                "void\n"
8174                "A::operator<Foo> **() {}\n"
8175                "void\n"
8176                "A::operator<Foo> &() {}\n"
8177                "void\n"
8178                "A::operator void **() {}\n",
8179                Style);
8180   verifyFormat("constexpr auto\n"
8181                "operator()() const -> reference {}\n"
8182                "constexpr auto\n"
8183                "operator>>() const -> reference {}\n"
8184                "constexpr auto\n"
8185                "operator+() const -> reference {}\n"
8186                "constexpr auto\n"
8187                "operator*() const -> reference {}\n"
8188                "constexpr auto\n"
8189                "operator->() const -> reference {}\n"
8190                "constexpr auto\n"
8191                "operator++() const -> reference {}\n"
8192                "constexpr auto\n"
8193                "operator void *() const -> reference {}\n"
8194                "constexpr auto\n"
8195                "operator void **() const -> reference {}\n"
8196                "constexpr auto\n"
8197                "operator void *() const -> reference {}\n"
8198                "constexpr auto\n"
8199                "operator void &() const -> reference {}\n"
8200                "constexpr auto\n"
8201                "operator void &&() const -> reference {}\n"
8202                "constexpr auto\n"
8203                "operator char *() const -> reference {}\n"
8204                "constexpr auto\n"
8205                "operator!() const -> reference {}\n"
8206                "constexpr auto\n"
8207                "operator[]() const -> reference {}\n",
8208                Style);
8209   verifyFormat("void *operator new(std::size_t s);", // No break here.
8210                Style);
8211   verifyFormat("void *\n"
8212                "operator new(std::size_t s) {}",
8213                Style);
8214   verifyFormat("void *\n"
8215                "operator delete[](void *ptr) {}",
8216                Style);
8217   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8218   verifyFormat("const char *\n"
8219                "f(void)\n" // Break here.
8220                "{\n"
8221                "  return \"\";\n"
8222                "}\n"
8223                "const char *bar(void);\n", // No break here.
8224                Style);
8225   verifyFormat("template <class T>\n"
8226                "T *\n"     // Problem here: no line break
8227                "f(T &c)\n" // Break here.
8228                "{\n"
8229                "  return NULL;\n"
8230                "}\n"
8231                "template <class T> T *f(T &c);\n", // No break here.
8232                Style);
8233   verifyFormat("int\n"
8234                "foo(A<bool> a)\n"
8235                "{\n"
8236                "  return a;\n"
8237                "}\n",
8238                Style);
8239   verifyFormat("int\n"
8240                "foo(A<8> a)\n"
8241                "{\n"
8242                "  return a;\n"
8243                "}\n",
8244                Style);
8245   verifyFormat("int\n"
8246                "foo(A<B<bool>, 8> a)\n"
8247                "{\n"
8248                "  return a;\n"
8249                "}\n",
8250                Style);
8251   verifyFormat("int\n"
8252                "foo(A<B<8>, bool> a)\n"
8253                "{\n"
8254                "  return a;\n"
8255                "}\n",
8256                Style);
8257   verifyFormat("int\n"
8258                "foo(A<B<bool>, bool> a)\n"
8259                "{\n"
8260                "  return a;\n"
8261                "}\n",
8262                Style);
8263   verifyFormat("int\n"
8264                "foo(A<B<8>, 8> a)\n"
8265                "{\n"
8266                "  return a;\n"
8267                "}\n",
8268                Style);
8269 
8270   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8271   Style.BraceWrapping.AfterFunction = true;
8272   verifyFormat("int f(i);\n" // No break here.
8273                "int\n"       // Break here.
8274                "f(i)\n"
8275                "{\n"
8276                "  return i + 1;\n"
8277                "}\n"
8278                "int\n" // Break here.
8279                "f(i)\n"
8280                "{\n"
8281                "  return i + 1;\n"
8282                "};",
8283                Style);
8284   verifyFormat("int f(a, b, c);\n" // No break here.
8285                "int\n"             // Break here.
8286                "f(a, b, c)\n"      // Break here.
8287                "short a, b;\n"
8288                "float c;\n"
8289                "{\n"
8290                "  return a + b < c;\n"
8291                "}\n"
8292                "int\n"        // Break here.
8293                "f(a, b, c)\n" // Break here.
8294                "short a, b;\n"
8295                "float c;\n"
8296                "{\n"
8297                "  return a + b < c;\n"
8298                "};",
8299                Style);
8300   verifyFormat("byte *\n" // Break here.
8301                "f(a)\n"   // Break here.
8302                "byte a[];\n"
8303                "{\n"
8304                "  return a;\n"
8305                "}",
8306                Style);
8307   verifyFormat("bool f(int a, int) override;\n"
8308                "Bar g(int a, Bar) final;\n"
8309                "Bar h(a, Bar) final;",
8310                Style);
8311   verifyFormat("int\n"
8312                "f(a)",
8313                Style);
8314   verifyFormat("bool\n"
8315                "f(size_t = 0, bool b = false)\n"
8316                "{\n"
8317                "  return !b;\n"
8318                "}",
8319                Style);
8320 
8321   // The return breaking style doesn't affect:
8322   // * function and object definitions with attribute-like macros
8323   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8324                "    ABSL_GUARDED_BY(mutex) = {};",
8325                getGoogleStyleWithColumns(40));
8326   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8327                "    ABSL_GUARDED_BY(mutex);  // comment",
8328                getGoogleStyleWithColumns(40));
8329   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8330                "    ABSL_GUARDED_BY(mutex1)\n"
8331                "        ABSL_GUARDED_BY(mutex2);",
8332                getGoogleStyleWithColumns(40));
8333   verifyFormat("Tttttt f(int a, int b)\n"
8334                "    ABSL_GUARDED_BY(mutex1)\n"
8335                "        ABSL_GUARDED_BY(mutex2);",
8336                getGoogleStyleWithColumns(40));
8337   // * typedefs
8338   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8339 
8340   Style = getGNUStyle();
8341 
8342   // Test for comments at the end of function declarations.
8343   verifyFormat("void\n"
8344                "foo (int a, /*abc*/ int b) // def\n"
8345                "{\n"
8346                "}\n",
8347                Style);
8348 
8349   verifyFormat("void\n"
8350                "foo (int a, /* abc */ int b) /* def */\n"
8351                "{\n"
8352                "}\n",
8353                Style);
8354 
8355   // Definitions that should not break after return type
8356   verifyFormat("void foo (int a, int b); // def\n", Style);
8357   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8358   verifyFormat("void foo (int a, int b);\n", Style);
8359 }
8360 
8361 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8362   FormatStyle NoBreak = getLLVMStyle();
8363   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8364   FormatStyle Break = getLLVMStyle();
8365   Break.AlwaysBreakBeforeMultilineStrings = true;
8366   verifyFormat("aaaa = \"bbbb\"\n"
8367                "       \"cccc\";",
8368                NoBreak);
8369   verifyFormat("aaaa =\n"
8370                "    \"bbbb\"\n"
8371                "    \"cccc\";",
8372                Break);
8373   verifyFormat("aaaa(\"bbbb\"\n"
8374                "     \"cccc\");",
8375                NoBreak);
8376   verifyFormat("aaaa(\n"
8377                "    \"bbbb\"\n"
8378                "    \"cccc\");",
8379                Break);
8380   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8381                "          \"cccc\");",
8382                NoBreak);
8383   verifyFormat("aaaa(qqq,\n"
8384                "     \"bbbb\"\n"
8385                "     \"cccc\");",
8386                Break);
8387   verifyFormat("aaaa(qqq,\n"
8388                "     L\"bbbb\"\n"
8389                "     L\"cccc\");",
8390                Break);
8391   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8392                "                      \"bbbb\"));",
8393                Break);
8394   verifyFormat("string s = someFunction(\n"
8395                "    \"abc\"\n"
8396                "    \"abc\");",
8397                Break);
8398 
8399   // As we break before unary operators, breaking right after them is bad.
8400   verifyFormat("string foo = abc ? \"x\"\n"
8401                "                   \"blah blah blah blah blah blah\"\n"
8402                "                 : \"y\";",
8403                Break);
8404 
8405   // Don't break if there is no column gain.
8406   verifyFormat("f(\"aaaa\"\n"
8407                "  \"bbbb\");",
8408                Break);
8409 
8410   // Treat literals with escaped newlines like multi-line string literals.
8411   EXPECT_EQ("x = \"a\\\n"
8412             "b\\\n"
8413             "c\";",
8414             format("x = \"a\\\n"
8415                    "b\\\n"
8416                    "c\";",
8417                    NoBreak));
8418   EXPECT_EQ("xxxx =\n"
8419             "    \"a\\\n"
8420             "b\\\n"
8421             "c\";",
8422             format("xxxx = \"a\\\n"
8423                    "b\\\n"
8424                    "c\";",
8425                    Break));
8426 
8427   EXPECT_EQ("NSString *const kString =\n"
8428             "    @\"aaaa\"\n"
8429             "    @\"bbbb\";",
8430             format("NSString *const kString = @\"aaaa\"\n"
8431                    "@\"bbbb\";",
8432                    Break));
8433 
8434   Break.ColumnLimit = 0;
8435   verifyFormat("const char *hello = \"hello llvm\";", Break);
8436 }
8437 
8438 TEST_F(FormatTest, AlignsPipes) {
8439   verifyFormat(
8440       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8441       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8442       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8443   verifyFormat(
8444       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8445       "                     << aaaaaaaaaaaaaaaaaaaa;");
8446   verifyFormat(
8447       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8448       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8449   verifyFormat(
8450       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8451       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8452   verifyFormat(
8453       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8454       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8455       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8456   verifyFormat(
8457       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8458       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8459       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8460   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8461                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8462                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8463                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8464   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8465                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8466   verifyFormat(
8467       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8468       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8469   verifyFormat(
8470       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8471       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8472 
8473   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8474                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8475   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8476                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8477                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8478                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8479   verifyFormat("LOG_IF(aaa == //\n"
8480                "       bbb)\n"
8481                "    << a << b;");
8482 
8483   // But sometimes, breaking before the first "<<" is desirable.
8484   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8485                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8486   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8487                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8488                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8489   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8490                "    << BEF << IsTemplate << Description << E->getType();");
8491   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8492                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8493                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8494   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8495                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8496                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8497                "    << aaa;");
8498 
8499   verifyFormat(
8500       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8501       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8502 
8503   // Incomplete string literal.
8504   EXPECT_EQ("llvm::errs() << \"\n"
8505             "             << a;",
8506             format("llvm::errs() << \"\n<<a;"));
8507 
8508   verifyFormat("void f() {\n"
8509                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8510                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8511                "}");
8512 
8513   // Handle 'endl'.
8514   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8515                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8516   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8517 
8518   // Handle '\n'.
8519   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8520                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8521   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8522                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8523   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8524                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8525   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8526 }
8527 
8528 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8529   verifyFormat("return out << \"somepacket = {\\n\"\n"
8530                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8531                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8532                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8533                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8534                "           << \"}\";");
8535 
8536   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8537                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8538                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8539   verifyFormat(
8540       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8541       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8542       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8543       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8544       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8545   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8546                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8547   verifyFormat(
8548       "void f() {\n"
8549       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8550       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8551       "}");
8552 
8553   // Breaking before the first "<<" is generally not desirable.
8554   verifyFormat(
8555       "llvm::errs()\n"
8556       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8557       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8558       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8559       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8560       getLLVMStyleWithColumns(70));
8561   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8562                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8563                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8564                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8565                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8566                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8567                getLLVMStyleWithColumns(70));
8568 
8569   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8570                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8571                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8572   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8573                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8574                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8575   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8576                "           (aaaa + aaaa);",
8577                getLLVMStyleWithColumns(40));
8578   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8579                "                  (aaaaaaa + aaaaa));",
8580                getLLVMStyleWithColumns(40));
8581   verifyFormat(
8582       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8583       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8584       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8585 }
8586 
8587 TEST_F(FormatTest, UnderstandsEquals) {
8588   verifyFormat(
8589       "aaaaaaaaaaaaaaaaa =\n"
8590       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8591   verifyFormat(
8592       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8593       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8594   verifyFormat(
8595       "if (a) {\n"
8596       "  f();\n"
8597       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8598       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8599       "}");
8600 
8601   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8602                "        100000000 + 10000000) {\n}");
8603 }
8604 
8605 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8606   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8607                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8608 
8609   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8610                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8611 
8612   verifyFormat(
8613       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8614       "                                                          Parameter2);");
8615 
8616   verifyFormat(
8617       "ShortObject->shortFunction(\n"
8618       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8619       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8620 
8621   verifyFormat("loooooooooooooongFunction(\n"
8622                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8623 
8624   verifyFormat(
8625       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8626       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8627 
8628   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8629                "    .WillRepeatedly(Return(SomeValue));");
8630   verifyFormat("void f() {\n"
8631                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8632                "      .Times(2)\n"
8633                "      .WillRepeatedly(Return(SomeValue));\n"
8634                "}");
8635   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8636                "    ccccccccccccccccccccccc);");
8637   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8638                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8639                "          .aaaaa(aaaaa),\n"
8640                "      aaaaaaaaaaaaaaaaaaaaa);");
8641   verifyFormat("void f() {\n"
8642                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8643                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8644                "}");
8645   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8646                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8647                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8648                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8649                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8650   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8651                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8652                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8653                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8654                "}");
8655 
8656   // Here, it is not necessary to wrap at "." or "->".
8657   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8658                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8659   verifyFormat(
8660       "aaaaaaaaaaa->aaaaaaaaa(\n"
8661       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8662       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8663 
8664   verifyFormat(
8665       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8666       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8667   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8668                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8669   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8670                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8671 
8672   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8673                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8674                "    .a();");
8675 
8676   FormatStyle NoBinPacking = getLLVMStyle();
8677   NoBinPacking.BinPackParameters = false;
8678   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8679                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8680                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8681                "                         aaaaaaaaaaaaaaaaaaa,\n"
8682                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8683                NoBinPacking);
8684 
8685   // If there is a subsequent call, change to hanging indentation.
8686   verifyFormat(
8687       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8688       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8689       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8690   verifyFormat(
8691       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8692       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8693   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8694                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8695                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8696   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8697                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8698                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8699 }
8700 
8701 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8702   verifyFormat("template <typename T>\n"
8703                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8704   verifyFormat("template <typename T>\n"
8705                "// T should be one of {A, B}.\n"
8706                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8707   verifyFormat(
8708       "template <typename T>\n"
8709       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8710   verifyFormat("template <typename T>\n"
8711                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8712                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8713   verifyFormat(
8714       "template <typename T>\n"
8715       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8716       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8717   verifyFormat(
8718       "template <typename T>\n"
8719       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8720       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8721       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8722   verifyFormat("template <typename T>\n"
8723                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8724                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8725   verifyFormat(
8726       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8727       "          typename T4 = char>\n"
8728       "void f();");
8729   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8730                "          template <typename> class cccccccccccccccccccccc,\n"
8731                "          typename ddddddddddddd>\n"
8732                "class C {};");
8733   verifyFormat(
8734       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8735       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8736 
8737   verifyFormat("void f() {\n"
8738                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8739                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8740                "}");
8741 
8742   verifyFormat("template <typename T> class C {};");
8743   verifyFormat("template <typename T> void f();");
8744   verifyFormat("template <typename T> void f() {}");
8745   verifyFormat(
8746       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8747       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8748       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8749       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8750       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8751       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8752       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8753       getLLVMStyleWithColumns(72));
8754   EXPECT_EQ("static_cast<A< //\n"
8755             "    B> *>(\n"
8756             "\n"
8757             ");",
8758             format("static_cast<A<//\n"
8759                    "    B>*>(\n"
8760                    "\n"
8761                    "    );"));
8762   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8763                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8764 
8765   FormatStyle AlwaysBreak = getLLVMStyle();
8766   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8767   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8768   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8769   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8770   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8771                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8772                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8773   verifyFormat("template <template <typename> class Fooooooo,\n"
8774                "          template <typename> class Baaaaaaar>\n"
8775                "struct C {};",
8776                AlwaysBreak);
8777   verifyFormat("template <typename T> // T can be A, B or C.\n"
8778                "struct C {};",
8779                AlwaysBreak);
8780   verifyFormat("template <enum E> class A {\n"
8781                "public:\n"
8782                "  E *f();\n"
8783                "};");
8784 
8785   FormatStyle NeverBreak = getLLVMStyle();
8786   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8787   verifyFormat("template <typename T> class C {};", NeverBreak);
8788   verifyFormat("template <typename T> void f();", NeverBreak);
8789   verifyFormat("template <typename T> void f() {}", NeverBreak);
8790   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8791                "bbbbbbbbbbbbbbbbbbbb) {}",
8792                NeverBreak);
8793   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8794                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8795                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8796                NeverBreak);
8797   verifyFormat("template <template <typename> class Fooooooo,\n"
8798                "          template <typename> class Baaaaaaar>\n"
8799                "struct C {};",
8800                NeverBreak);
8801   verifyFormat("template <typename T> // T can be A, B or C.\n"
8802                "struct C {};",
8803                NeverBreak);
8804   verifyFormat("template <enum E> class A {\n"
8805                "public:\n"
8806                "  E *f();\n"
8807                "};",
8808                NeverBreak);
8809   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8810   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8811                "bbbbbbbbbbbbbbbbbbbb) {}",
8812                NeverBreak);
8813 }
8814 
8815 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8816   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8817   Style.ColumnLimit = 60;
8818   EXPECT_EQ("// Baseline - no comments.\n"
8819             "template <\n"
8820             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8821             "void f() {}",
8822             format("// Baseline - no comments.\n"
8823                    "template <\n"
8824                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8825                    "void f() {}",
8826                    Style));
8827 
8828   EXPECT_EQ("template <\n"
8829             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8830             "void f() {}",
8831             format("template <\n"
8832                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8833                    "void f() {}",
8834                    Style));
8835 
8836   EXPECT_EQ(
8837       "template <\n"
8838       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8839       "void f() {}",
8840       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8841              "void f() {}",
8842              Style));
8843 
8844   EXPECT_EQ(
8845       "template <\n"
8846       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8847       "                                               // multiline\n"
8848       "void f() {}",
8849       format("template <\n"
8850              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8851              "                                              // multiline\n"
8852              "void f() {}",
8853              Style));
8854 
8855   EXPECT_EQ(
8856       "template <typename aaaaaaaaaa<\n"
8857       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8858       "void f() {}",
8859       format(
8860           "template <\n"
8861           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8862           "void f() {}",
8863           Style));
8864 }
8865 
8866 TEST_F(FormatTest, WrapsTemplateParameters) {
8867   FormatStyle Style = getLLVMStyle();
8868   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8869   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8870   verifyFormat(
8871       "template <typename... a> struct q {};\n"
8872       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8873       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8874       "    y;",
8875       Style);
8876   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8877   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8878   verifyFormat(
8879       "template <typename... a> struct r {};\n"
8880       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8881       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8882       "    y;",
8883       Style);
8884   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8885   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8886   verifyFormat("template <typename... a> struct s {};\n"
8887                "extern s<\n"
8888                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8889                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8890                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8891                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8892                "    y;",
8893                Style);
8894   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8895   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8896   verifyFormat("template <typename... a> struct t {};\n"
8897                "extern t<\n"
8898                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8899                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8900                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8901                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8902                "    y;",
8903                Style);
8904 }
8905 
8906 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8907   verifyFormat(
8908       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8909       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8910   verifyFormat(
8911       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8912       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8913       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8914 
8915   // FIXME: Should we have the extra indent after the second break?
8916   verifyFormat(
8917       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8918       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8919       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8920 
8921   verifyFormat(
8922       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8923       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8924 
8925   // Breaking at nested name specifiers is generally not desirable.
8926   verifyFormat(
8927       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8928       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8929 
8930   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8931                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8932                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8933                "                   aaaaaaaaaaaaaaaaaaaaa);",
8934                getLLVMStyleWithColumns(74));
8935 
8936   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8937                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8938                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8939 }
8940 
8941 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8942   verifyFormat("A<int> a;");
8943   verifyFormat("A<A<A<int>>> a;");
8944   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8945   verifyFormat("bool x = a < 1 || 2 > a;");
8946   verifyFormat("bool x = 5 < f<int>();");
8947   verifyFormat("bool x = f<int>() > 5;");
8948   verifyFormat("bool x = 5 < a<int>::x;");
8949   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8950   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8951 
8952   verifyGoogleFormat("A<A<int>> a;");
8953   verifyGoogleFormat("A<A<A<int>>> a;");
8954   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8955   verifyGoogleFormat("A<A<int> > a;");
8956   verifyGoogleFormat("A<A<A<int> > > a;");
8957   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8958   verifyGoogleFormat("A<::A<int>> a;");
8959   verifyGoogleFormat("A<::A> a;");
8960   verifyGoogleFormat("A< ::A> a;");
8961   verifyGoogleFormat("A< ::A<int> > a;");
8962   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8963   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8964   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8965   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8966   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8967             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8968 
8969   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8970 
8971   // template closer followed by a token that starts with > or =
8972   verifyFormat("bool b = a<1> > 1;");
8973   verifyFormat("bool b = a<1> >= 1;");
8974   verifyFormat("int i = a<1> >> 1;");
8975   FormatStyle Style = getLLVMStyle();
8976   Style.SpaceBeforeAssignmentOperators = false;
8977   verifyFormat("bool b= a<1> == 1;", Style);
8978   verifyFormat("a<int> = 1;", Style);
8979   verifyFormat("a<int> >>= 1;", Style);
8980 
8981   verifyFormat("test < a | b >> c;");
8982   verifyFormat("test<test<a | b>> c;");
8983   verifyFormat("test >> a >> b;");
8984   verifyFormat("test << a >> b;");
8985 
8986   verifyFormat("f<int>();");
8987   verifyFormat("template <typename T> void f() {}");
8988   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8989   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8990                "sizeof(char)>::type>;");
8991   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8992   verifyFormat("f(a.operator()<A>());");
8993   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8994                "      .template operator()<A>());",
8995                getLLVMStyleWithColumns(35));
8996 
8997   // Not template parameters.
8998   verifyFormat("return a < b && c > d;");
8999   verifyFormat("void f() {\n"
9000                "  while (a < b && c > d) {\n"
9001                "  }\n"
9002                "}");
9003   verifyFormat("template <typename... Types>\n"
9004                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9005 
9006   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9007                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9008                getLLVMStyleWithColumns(60));
9009   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9010   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9011   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9012   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9013 }
9014 
9015 TEST_F(FormatTest, UnderstandsShiftOperators) {
9016   verifyFormat("if (i < x >> 1)");
9017   verifyFormat("while (i < x >> 1)");
9018   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9019   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9020   verifyFormat(
9021       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9022   verifyFormat("Foo.call<Bar<Function>>()");
9023   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9024   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9025                "++i, v = v >> 1)");
9026   verifyFormat("if (w<u<v<x>>, 1>::t)");
9027 }
9028 
9029 TEST_F(FormatTest, BitshiftOperatorWidth) {
9030   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9031             "                   bar */",
9032             format("int    a=1<<2;  /* foo\n"
9033                    "                   bar */"));
9034 
9035   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9036             "                     bar */",
9037             format("int  b  =256>>1 ;  /* foo\n"
9038                    "                      bar */"));
9039 }
9040 
9041 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9042   verifyFormat("COMPARE(a, ==, b);");
9043   verifyFormat("auto s = sizeof...(Ts) - 1;");
9044 }
9045 
9046 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9047   verifyFormat("int A::*x;");
9048   verifyFormat("int (S::*func)(void *);");
9049   verifyFormat("void f() { int (S::*func)(void *); }");
9050   verifyFormat("typedef bool *(Class::*Member)() const;");
9051   verifyFormat("void f() {\n"
9052                "  (a->*f)();\n"
9053                "  a->*x;\n"
9054                "  (a.*f)();\n"
9055                "  ((*a).*f)();\n"
9056                "  a.*x;\n"
9057                "}");
9058   verifyFormat("void f() {\n"
9059                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9060                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9061                "}");
9062   verifyFormat(
9063       "(aaaaaaaaaa->*bbbbbbb)(\n"
9064       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9065   FormatStyle Style = getLLVMStyle();
9066   Style.PointerAlignment = FormatStyle::PAS_Left;
9067   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9068 }
9069 
9070 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9071   verifyFormat("int a = -2;");
9072   verifyFormat("f(-1, -2, -3);");
9073   verifyFormat("a[-1] = 5;");
9074   verifyFormat("int a = 5 + -2;");
9075   verifyFormat("if (i == -1) {\n}");
9076   verifyFormat("if (i != -1) {\n}");
9077   verifyFormat("if (i > -1) {\n}");
9078   verifyFormat("if (i < -1) {\n}");
9079   verifyFormat("++(a->f());");
9080   verifyFormat("--(a->f());");
9081   verifyFormat("(a->f())++;");
9082   verifyFormat("a[42]++;");
9083   verifyFormat("if (!(a->f())) {\n}");
9084   verifyFormat("if (!+i) {\n}");
9085   verifyFormat("~&a;");
9086 
9087   verifyFormat("a-- > b;");
9088   verifyFormat("b ? -a : c;");
9089   verifyFormat("n * sizeof char16;");
9090   verifyFormat("n * alignof char16;", getGoogleStyle());
9091   verifyFormat("sizeof(char);");
9092   verifyFormat("alignof(char);", getGoogleStyle());
9093 
9094   verifyFormat("return -1;");
9095   verifyFormat("throw -1;");
9096   verifyFormat("switch (a) {\n"
9097                "case -1:\n"
9098                "  break;\n"
9099                "}");
9100   verifyFormat("#define X -1");
9101   verifyFormat("#define X -kConstant");
9102 
9103   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9104   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9105 
9106   verifyFormat("int a = /* confusing comment */ -1;");
9107   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9108   verifyFormat("int a = i /* confusing comment */++;");
9109 
9110   verifyFormat("co_yield -1;");
9111   verifyFormat("co_return -1;");
9112 
9113   // Check that * is not treated as a binary operator when we set
9114   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9115   FormatStyle PASLeftStyle = getLLVMStyle();
9116   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9117   verifyFormat("co_return *a;", PASLeftStyle);
9118   verifyFormat("co_await *a;", PASLeftStyle);
9119   verifyFormat("co_yield *a", PASLeftStyle);
9120   verifyFormat("return *a;", PASLeftStyle);
9121 }
9122 
9123 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9124   verifyFormat("if (!aaaaaaaaaa( // break\n"
9125                "        aaaaa)) {\n"
9126                "}");
9127   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9128                "    aaaaa));");
9129   verifyFormat("*aaa = aaaaaaa( // break\n"
9130                "    bbbbbb);");
9131 }
9132 
9133 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9134   verifyFormat("bool operator<();");
9135   verifyFormat("bool operator>();");
9136   verifyFormat("bool operator=();");
9137   verifyFormat("bool operator==();");
9138   verifyFormat("bool operator!=();");
9139   verifyFormat("int operator+();");
9140   verifyFormat("int operator++();");
9141   verifyFormat("int operator++(int) volatile noexcept;");
9142   verifyFormat("bool operator,();");
9143   verifyFormat("bool operator();");
9144   verifyFormat("bool operator()();");
9145   verifyFormat("bool operator[]();");
9146   verifyFormat("operator bool();");
9147   verifyFormat("operator int();");
9148   verifyFormat("operator void *();");
9149   verifyFormat("operator SomeType<int>();");
9150   verifyFormat("operator SomeType<int, int>();");
9151   verifyFormat("operator SomeType<SomeType<int>>();");
9152   verifyFormat("void *operator new(std::size_t size);");
9153   verifyFormat("void *operator new[](std::size_t size);");
9154   verifyFormat("void operator delete(void *ptr);");
9155   verifyFormat("void operator delete[](void *ptr);");
9156   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9157                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9158   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9159                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9160 
9161   verifyFormat(
9162       "ostream &operator<<(ostream &OutputStream,\n"
9163       "                    SomeReallyLongType WithSomeReallyLongValue);");
9164   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9165                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9166                "  return left.group < right.group;\n"
9167                "}");
9168   verifyFormat("SomeType &operator=(const SomeType &S);");
9169   verifyFormat("f.template operator()<int>();");
9170 
9171   verifyGoogleFormat("operator void*();");
9172   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9173   verifyGoogleFormat("operator ::A();");
9174 
9175   verifyFormat("using A::operator+;");
9176   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9177                "int i;");
9178 
9179   // Calling an operator as a member function.
9180   verifyFormat("void f() { a.operator*(); }");
9181   verifyFormat("void f() { a.operator*(b & b); }");
9182   verifyFormat("void f() { a->operator&(a * b); }");
9183   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9184   // TODO: Calling an operator as a non-member function is hard to distinguish.
9185   // https://llvm.org/PR50629
9186   // verifyFormat("void f() { operator*(a & a); }");
9187   // verifyFormat("void f() { operator&(a, b * b); }");
9188 }
9189 
9190 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9191   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9192   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9193   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9194   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9195   verifyFormat("Deleted &operator=(const Deleted &) &;");
9196   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9197   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9198   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9199   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9200   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9201   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9202   verifyFormat("void Fn(T const &) const &;");
9203   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9204   verifyFormat("template <typename T>\n"
9205                "void F(T) && = delete;",
9206                getGoogleStyle());
9207 
9208   FormatStyle AlignLeft = getLLVMStyle();
9209   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9210   verifyFormat("void A::b() && {}", AlignLeft);
9211   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9212   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9213                AlignLeft);
9214   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9215   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9216   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9217   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9218   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9219   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9220   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9221   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9222 
9223   FormatStyle Spaces = getLLVMStyle();
9224   Spaces.SpacesInCStyleCastParentheses = true;
9225   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9226   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9227   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9228   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9229 
9230   Spaces.SpacesInCStyleCastParentheses = false;
9231   Spaces.SpacesInParentheses = true;
9232   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9233   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9234                Spaces);
9235   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9236   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9237 
9238   FormatStyle BreakTemplate = getLLVMStyle();
9239   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9240 
9241   verifyFormat("struct f {\n"
9242                "  template <class T>\n"
9243                "  int &foo(const std::string &str) &noexcept {}\n"
9244                "};",
9245                BreakTemplate);
9246 
9247   verifyFormat("struct f {\n"
9248                "  template <class T>\n"
9249                "  int &foo(const std::string &str) &&noexcept {}\n"
9250                "};",
9251                BreakTemplate);
9252 
9253   verifyFormat("struct f {\n"
9254                "  template <class T>\n"
9255                "  int &foo(const std::string &str) const &noexcept {}\n"
9256                "};",
9257                BreakTemplate);
9258 
9259   verifyFormat("struct f {\n"
9260                "  template <class T>\n"
9261                "  int &foo(const std::string &str) const &noexcept {}\n"
9262                "};",
9263                BreakTemplate);
9264 
9265   verifyFormat("struct f {\n"
9266                "  template <class T>\n"
9267                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9268                "};",
9269                BreakTemplate);
9270 
9271   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9272   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9273       FormatStyle::BTDS_Yes;
9274   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9275 
9276   verifyFormat("struct f {\n"
9277                "  template <class T>\n"
9278                "  int& foo(const std::string& str) & noexcept {}\n"
9279                "};",
9280                AlignLeftBreakTemplate);
9281 
9282   verifyFormat("struct f {\n"
9283                "  template <class T>\n"
9284                "  int& foo(const std::string& str) && noexcept {}\n"
9285                "};",
9286                AlignLeftBreakTemplate);
9287 
9288   verifyFormat("struct f {\n"
9289                "  template <class T>\n"
9290                "  int& foo(const std::string& str) const& noexcept {}\n"
9291                "};",
9292                AlignLeftBreakTemplate);
9293 
9294   verifyFormat("struct f {\n"
9295                "  template <class T>\n"
9296                "  int& foo(const std::string& str) const&& noexcept {}\n"
9297                "};",
9298                AlignLeftBreakTemplate);
9299 
9300   verifyFormat("struct f {\n"
9301                "  template <class T>\n"
9302                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9303                "};",
9304                AlignLeftBreakTemplate);
9305 
9306   // The `&` in `Type&` should not be confused with a trailing `&` of
9307   // DEPRECATED(reason) member function.
9308   verifyFormat("struct f {\n"
9309                "  template <class T>\n"
9310                "  DEPRECATED(reason)\n"
9311                "  Type &foo(arguments) {}\n"
9312                "};",
9313                BreakTemplate);
9314 
9315   verifyFormat("struct f {\n"
9316                "  template <class T>\n"
9317                "  DEPRECATED(reason)\n"
9318                "  Type& foo(arguments) {}\n"
9319                "};",
9320                AlignLeftBreakTemplate);
9321 
9322   verifyFormat("void (*foopt)(int) = &func;");
9323 }
9324 
9325 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9326   verifyFormat("void f() {\n"
9327                "  A *a = new A;\n"
9328                "  A *a = new (placement) A;\n"
9329                "  delete a;\n"
9330                "  delete (A *)a;\n"
9331                "}");
9332   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9333                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9334   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9335                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9336                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9337   verifyFormat("delete[] h->p;");
9338 }
9339 
9340 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9341   verifyFormat("int *f(int *a) {}");
9342   verifyFormat("int main(int argc, char **argv) {}");
9343   verifyFormat("Test::Test(int b) : a(b * b) {}");
9344   verifyIndependentOfContext("f(a, *a);");
9345   verifyFormat("void g() { f(*a); }");
9346   verifyIndependentOfContext("int a = b * 10;");
9347   verifyIndependentOfContext("int a = 10 * b;");
9348   verifyIndependentOfContext("int a = b * c;");
9349   verifyIndependentOfContext("int a += b * c;");
9350   verifyIndependentOfContext("int a -= b * c;");
9351   verifyIndependentOfContext("int a *= b * c;");
9352   verifyIndependentOfContext("int a /= b * c;");
9353   verifyIndependentOfContext("int a = *b;");
9354   verifyIndependentOfContext("int a = *b * c;");
9355   verifyIndependentOfContext("int a = b * *c;");
9356   verifyIndependentOfContext("int a = b * (10);");
9357   verifyIndependentOfContext("S << b * (10);");
9358   verifyIndependentOfContext("return 10 * b;");
9359   verifyIndependentOfContext("return *b * *c;");
9360   verifyIndependentOfContext("return a & ~b;");
9361   verifyIndependentOfContext("f(b ? *c : *d);");
9362   verifyIndependentOfContext("int a = b ? *c : *d;");
9363   verifyIndependentOfContext("*b = a;");
9364   verifyIndependentOfContext("a * ~b;");
9365   verifyIndependentOfContext("a * !b;");
9366   verifyIndependentOfContext("a * +b;");
9367   verifyIndependentOfContext("a * -b;");
9368   verifyIndependentOfContext("a * ++b;");
9369   verifyIndependentOfContext("a * --b;");
9370   verifyIndependentOfContext("a[4] * b;");
9371   verifyIndependentOfContext("a[a * a] = 1;");
9372   verifyIndependentOfContext("f() * b;");
9373   verifyIndependentOfContext("a * [self dostuff];");
9374   verifyIndependentOfContext("int x = a * (a + b);");
9375   verifyIndependentOfContext("(a *)(a + b);");
9376   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9377   verifyIndependentOfContext("int *pa = (int *)&a;");
9378   verifyIndependentOfContext("return sizeof(int **);");
9379   verifyIndependentOfContext("return sizeof(int ******);");
9380   verifyIndependentOfContext("return (int **&)a;");
9381   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9382   verifyFormat("void f(Type (*parameter)[10]) {}");
9383   verifyFormat("void f(Type (&parameter)[10]) {}");
9384   verifyGoogleFormat("return sizeof(int**);");
9385   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9386   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9387   verifyFormat("auto a = [](int **&, int ***) {};");
9388   verifyFormat("auto PointerBinding = [](const char *S) {};");
9389   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9390   verifyFormat("[](const decltype(*a) &value) {}");
9391   verifyFormat("[](const typeof(*a) &value) {}");
9392   verifyFormat("[](const _Atomic(a *) &value) {}");
9393   verifyFormat("[](const __underlying_type(a) &value) {}");
9394   verifyFormat("decltype(a * b) F();");
9395   verifyFormat("typeof(a * b) F();");
9396   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9397   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9398   verifyIndependentOfContext("typedef void (*f)(int *a);");
9399   verifyIndependentOfContext("int i{a * b};");
9400   verifyIndependentOfContext("aaa && aaa->f();");
9401   verifyIndependentOfContext("int x = ~*p;");
9402   verifyFormat("Constructor() : a(a), area(width * height) {}");
9403   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9404   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9405   verifyFormat("void f() { f(a, c * d); }");
9406   verifyFormat("void f() { f(new a(), c * d); }");
9407   verifyFormat("void f(const MyOverride &override);");
9408   verifyFormat("void f(const MyFinal &final);");
9409   verifyIndependentOfContext("bool a = f() && override.f();");
9410   verifyIndependentOfContext("bool a = f() && final.f();");
9411 
9412   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9413 
9414   verifyIndependentOfContext("A<int *> a;");
9415   verifyIndependentOfContext("A<int **> a;");
9416   verifyIndependentOfContext("A<int *, int *> a;");
9417   verifyIndependentOfContext("A<int *[]> a;");
9418   verifyIndependentOfContext(
9419       "const char *const p = reinterpret_cast<const char *const>(q);");
9420   verifyIndependentOfContext("A<int **, int **> a;");
9421   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9422   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9423   verifyFormat("for (; a && b;) {\n}");
9424   verifyFormat("bool foo = true && [] { return false; }();");
9425 
9426   verifyFormat(
9427       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9428       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9429 
9430   verifyGoogleFormat("int const* a = &b;");
9431   verifyGoogleFormat("**outparam = 1;");
9432   verifyGoogleFormat("*outparam = a * b;");
9433   verifyGoogleFormat("int main(int argc, char** argv) {}");
9434   verifyGoogleFormat("A<int*> a;");
9435   verifyGoogleFormat("A<int**> a;");
9436   verifyGoogleFormat("A<int*, int*> a;");
9437   verifyGoogleFormat("A<int**, int**> a;");
9438   verifyGoogleFormat("f(b ? *c : *d);");
9439   verifyGoogleFormat("int a = b ? *c : *d;");
9440   verifyGoogleFormat("Type* t = **x;");
9441   verifyGoogleFormat("Type* t = *++*x;");
9442   verifyGoogleFormat("*++*x;");
9443   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9444   verifyGoogleFormat("Type* t = x++ * y;");
9445   verifyGoogleFormat(
9446       "const char* const p = reinterpret_cast<const char* const>(q);");
9447   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9448   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9449   verifyGoogleFormat("template <typename T>\n"
9450                      "void f(int i = 0, SomeType** temps = NULL);");
9451 
9452   FormatStyle Left = getLLVMStyle();
9453   Left.PointerAlignment = FormatStyle::PAS_Left;
9454   verifyFormat("x = *a(x) = *a(y);", Left);
9455   verifyFormat("for (;; *a = b) {\n}", Left);
9456   verifyFormat("return *this += 1;", Left);
9457   verifyFormat("throw *x;", Left);
9458   verifyFormat("delete *x;", Left);
9459   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9460   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9461   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9462   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9463   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9464   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9465   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9466   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9467   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9468 
9469   verifyIndependentOfContext("a = *(x + y);");
9470   verifyIndependentOfContext("a = &(x + y);");
9471   verifyIndependentOfContext("*(x + y).call();");
9472   verifyIndependentOfContext("&(x + y)->call();");
9473   verifyFormat("void f() { &(*I).first; }");
9474 
9475   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9476   verifyFormat("f(* /* confusing comment */ foo);");
9477   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9478   verifyFormat("void foo(int * // this is the first paramters\n"
9479                "         ,\n"
9480                "         int second);");
9481   verifyFormat("double term = a * // first\n"
9482                "              b;");
9483   verifyFormat(
9484       "int *MyValues = {\n"
9485       "    *A, // Operator detection might be confused by the '{'\n"
9486       "    *BB // Operator detection might be confused by previous comment\n"
9487       "};");
9488 
9489   verifyIndependentOfContext("if (int *a = &b)");
9490   verifyIndependentOfContext("if (int &a = *b)");
9491   verifyIndependentOfContext("if (a & b[i])");
9492   verifyIndependentOfContext("if constexpr (a & b[i])");
9493   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9494   verifyIndependentOfContext("if (a * (b * c))");
9495   verifyIndependentOfContext("if constexpr (a * (b * c))");
9496   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9497   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9498   verifyIndependentOfContext("if (*b[i])");
9499   verifyIndependentOfContext("if (int *a = (&b))");
9500   verifyIndependentOfContext("while (int *a = &b)");
9501   verifyIndependentOfContext("while (a * (b * c))");
9502   verifyIndependentOfContext("size = sizeof *a;");
9503   verifyIndependentOfContext("if (a && (b = c))");
9504   verifyFormat("void f() {\n"
9505                "  for (const int &v : Values) {\n"
9506                "  }\n"
9507                "}");
9508   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9509   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9510   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9511 
9512   verifyFormat("#define A (!a * b)");
9513   verifyFormat("#define MACRO     \\\n"
9514                "  int *i = a * b; \\\n"
9515                "  void f(a *b);",
9516                getLLVMStyleWithColumns(19));
9517 
9518   verifyIndependentOfContext("A = new SomeType *[Length];");
9519   verifyIndependentOfContext("A = new SomeType *[Length]();");
9520   verifyIndependentOfContext("T **t = new T *;");
9521   verifyIndependentOfContext("T **t = new T *();");
9522   verifyGoogleFormat("A = new SomeType*[Length]();");
9523   verifyGoogleFormat("A = new SomeType*[Length];");
9524   verifyGoogleFormat("T** t = new T*;");
9525   verifyGoogleFormat("T** t = new T*();");
9526 
9527   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9528   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9529   verifyFormat("template <bool a, bool b> "
9530                "typename t::if<x && y>::type f() {}");
9531   verifyFormat("template <int *y> f() {}");
9532   verifyFormat("vector<int *> v;");
9533   verifyFormat("vector<int *const> v;");
9534   verifyFormat("vector<int *const **const *> v;");
9535   verifyFormat("vector<int *volatile> v;");
9536   verifyFormat("vector<a *_Nonnull> v;");
9537   verifyFormat("vector<a *_Nullable> v;");
9538   verifyFormat("vector<a *_Null_unspecified> v;");
9539   verifyFormat("vector<a *__ptr32> v;");
9540   verifyFormat("vector<a *__ptr64> v;");
9541   verifyFormat("vector<a *__capability> v;");
9542   FormatStyle TypeMacros = getLLVMStyle();
9543   TypeMacros.TypenameMacros = {"LIST"};
9544   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9545   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9546   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9547   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9548   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9549 
9550   FormatStyle CustomQualifier = getLLVMStyle();
9551   // Add identifiers that should not be parsed as a qualifier by default.
9552   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9553   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9554   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9555   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9556   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9557   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9558   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9559   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9560   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9561   verifyFormat("vector<a * _NotAQualifier> v;");
9562   verifyFormat("vector<a * __not_a_qualifier> v;");
9563   verifyFormat("vector<a * b> v;");
9564   verifyFormat("foo<b && false>();");
9565   verifyFormat("foo<b & 1>();");
9566   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9567   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9568   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9569   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9570   verifyFormat(
9571       "template <class T, class = typename std::enable_if<\n"
9572       "                       std::is_integral<T>::value &&\n"
9573       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9574       "void F();",
9575       getLLVMStyleWithColumns(70));
9576   verifyFormat("template <class T,\n"
9577                "          class = typename std::enable_if<\n"
9578                "              std::is_integral<T>::value &&\n"
9579                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9580                "          class U>\n"
9581                "void F();",
9582                getLLVMStyleWithColumns(70));
9583   verifyFormat(
9584       "template <class T,\n"
9585       "          class = typename ::std::enable_if<\n"
9586       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9587       "void F();",
9588       getGoogleStyleWithColumns(68));
9589 
9590   verifyIndependentOfContext("MACRO(int *i);");
9591   verifyIndependentOfContext("MACRO(auto *a);");
9592   verifyIndependentOfContext("MACRO(const A *a);");
9593   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9594   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9595   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9596   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9597   verifyIndependentOfContext("MACRO(A *const a);");
9598   verifyIndependentOfContext("MACRO(A *restrict a);");
9599   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9600   verifyIndependentOfContext("MACRO(A *__restrict a);");
9601   verifyIndependentOfContext("MACRO(A *volatile a);");
9602   verifyIndependentOfContext("MACRO(A *__volatile a);");
9603   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9604   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9605   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9606   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9607   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9608   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9609   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9610   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9611   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9612   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9613   verifyIndependentOfContext("MACRO(A *__capability);");
9614   verifyIndependentOfContext("MACRO(A &__capability);");
9615   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9616   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9617   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9618   // a type declaration:
9619   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9620   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9621   // Also check that TypenameMacros prevents parsing it as multiplication:
9622   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9623   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9624 
9625   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9626   verifyFormat("void f() { f(float{1}, a * a); }");
9627   verifyFormat("void f() { f(float(1), a * a); }");
9628 
9629   verifyFormat("f((void (*)(int))g);");
9630   verifyFormat("f((void (&)(int))g);");
9631   verifyFormat("f((void (^)(int))g);");
9632 
9633   // FIXME: Is there a way to make this work?
9634   // verifyIndependentOfContext("MACRO(A *a);");
9635   verifyFormat("MACRO(A &B);");
9636   verifyFormat("MACRO(A *B);");
9637   verifyFormat("void f() { MACRO(A * B); }");
9638   verifyFormat("void f() { MACRO(A & B); }");
9639 
9640   // This lambda was mis-formatted after D88956 (treating it as a binop):
9641   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9642   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9643   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9644   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9645 
9646   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9647   verifyFormat("return options != nullptr && operator==(*options);");
9648 
9649   EXPECT_EQ("#define OP(x)                                    \\\n"
9650             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9651             "    return s << a.DebugString();                 \\\n"
9652             "  }",
9653             format("#define OP(x) \\\n"
9654                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9655                    "    return s << a.DebugString(); \\\n"
9656                    "  }",
9657                    getLLVMStyleWithColumns(50)));
9658 
9659   // FIXME: We cannot handle this case yet; we might be able to figure out that
9660   // foo<x> d > v; doesn't make sense.
9661   verifyFormat("foo<a<b && c> d> v;");
9662 
9663   FormatStyle PointerMiddle = getLLVMStyle();
9664   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9665   verifyFormat("delete *x;", PointerMiddle);
9666   verifyFormat("int * x;", PointerMiddle);
9667   verifyFormat("int *[] x;", PointerMiddle);
9668   verifyFormat("template <int * y> f() {}", PointerMiddle);
9669   verifyFormat("int * f(int * a) {}", PointerMiddle);
9670   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9671   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9672   verifyFormat("A<int *> a;", PointerMiddle);
9673   verifyFormat("A<int **> a;", PointerMiddle);
9674   verifyFormat("A<int *, int *> a;", PointerMiddle);
9675   verifyFormat("A<int *[]> a;", PointerMiddle);
9676   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9677   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9678   verifyFormat("T ** t = new T *;", PointerMiddle);
9679 
9680   // Member function reference qualifiers aren't binary operators.
9681   verifyFormat("string // break\n"
9682                "operator()() & {}");
9683   verifyFormat("string // break\n"
9684                "operator()() && {}");
9685   verifyGoogleFormat("template <typename T>\n"
9686                      "auto x() & -> int {}");
9687 
9688   // Should be binary operators when used as an argument expression (overloaded
9689   // operator invoked as a member function).
9690   verifyFormat("void f() { a.operator()(a * a); }");
9691   verifyFormat("void f() { a->operator()(a & a); }");
9692   verifyFormat("void f() { a.operator()(*a & *a); }");
9693   verifyFormat("void f() { a->operator()(*a * *a); }");
9694 
9695   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
9696   verifyFormat("int operator()(T (&)[N]) { return 0; }");
9697 }
9698 
9699 TEST_F(FormatTest, UnderstandsAttributes) {
9700   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9701   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9702                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9703   FormatStyle AfterType = getLLVMStyle();
9704   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9705   verifyFormat("__attribute__((nodebug)) void\n"
9706                "foo() {}\n",
9707                AfterType);
9708   verifyFormat("__unused void\n"
9709                "foo() {}",
9710                AfterType);
9711 
9712   FormatStyle CustomAttrs = getLLVMStyle();
9713   CustomAttrs.AttributeMacros.push_back("__unused");
9714   CustomAttrs.AttributeMacros.push_back("__attr1");
9715   CustomAttrs.AttributeMacros.push_back("__attr2");
9716   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9717   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9718   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9719   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9720   // Check that it is parsed as a multiplication without AttributeMacros and
9721   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9722   verifyFormat("vector<SomeType * __attr1> v;");
9723   verifyFormat("vector<SomeType __attr1 *> v;");
9724   verifyFormat("vector<SomeType __attr1 *const> v;");
9725   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9726   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9727   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9728   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9729   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9730   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9731   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9732   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9733 
9734   // Check that these are not parsed as function declarations:
9735   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9736   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9737   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9738   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9739   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9740   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9741   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9742   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9743   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9744   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9745 }
9746 
9747 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9748   // Check that qualifiers on pointers don't break parsing of casts.
9749   verifyFormat("x = (foo *const)*v;");
9750   verifyFormat("x = (foo *volatile)*v;");
9751   verifyFormat("x = (foo *restrict)*v;");
9752   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9753   verifyFormat("x = (foo *_Nonnull)*v;");
9754   verifyFormat("x = (foo *_Nullable)*v;");
9755   verifyFormat("x = (foo *_Null_unspecified)*v;");
9756   verifyFormat("x = (foo *_Nonnull)*v;");
9757   verifyFormat("x = (foo *[[clang::attr]])*v;");
9758   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9759   verifyFormat("x = (foo *__ptr32)*v;");
9760   verifyFormat("x = (foo *__ptr64)*v;");
9761   verifyFormat("x = (foo *__capability)*v;");
9762 
9763   // Check that we handle multiple trailing qualifiers and skip them all to
9764   // determine that the expression is a cast to a pointer type.
9765   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9766   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9767   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9768   StringRef AllQualifiers =
9769       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9770       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9771   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9772   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9773 
9774   // Also check that address-of is not parsed as a binary bitwise-and:
9775   verifyFormat("x = (foo *const)&v;");
9776   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9777   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9778 
9779   // Check custom qualifiers:
9780   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9781   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9782   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9783   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9784   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9785                CustomQualifier);
9786   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9787                CustomQualifier);
9788 
9789   // Check that unknown identifiers result in binary operator parsing:
9790   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9791   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9792 }
9793 
9794 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9795   verifyFormat("SomeType s [[unused]] (InitValue);");
9796   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9797   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9798   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9799   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9800   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9801                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9802   verifyFormat("[[nodiscard]] bool f() { return false; }");
9803   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9804   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9805   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9806 
9807   // Make sure we do not mistake attributes for array subscripts.
9808   verifyFormat("int a() {}\n"
9809                "[[unused]] int b() {}\n");
9810   verifyFormat("NSArray *arr;\n"
9811                "arr[[Foo() bar]];");
9812 
9813   // On the other hand, we still need to correctly find array subscripts.
9814   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9815 
9816   // Make sure that we do not mistake Objective-C method inside array literals
9817   // as attributes, even if those method names are also keywords.
9818   verifyFormat("@[ [foo bar] ];");
9819   verifyFormat("@[ [NSArray class] ];");
9820   verifyFormat("@[ [foo enum] ];");
9821 
9822   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9823 
9824   // Make sure we do not parse attributes as lambda introducers.
9825   FormatStyle MultiLineFunctions = getLLVMStyle();
9826   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9827   verifyFormat("[[unused]] int b() {\n"
9828                "  return 42;\n"
9829                "}\n",
9830                MultiLineFunctions);
9831 }
9832 
9833 TEST_F(FormatTest, AttributeClass) {
9834   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9835   verifyFormat("class S {\n"
9836                "  S(S&&) = default;\n"
9837                "};",
9838                Style);
9839   verifyFormat("class [[nodiscard]] S {\n"
9840                "  S(S&&) = default;\n"
9841                "};",
9842                Style);
9843   verifyFormat("class __attribute((maybeunused)) S {\n"
9844                "  S(S&&) = default;\n"
9845                "};",
9846                Style);
9847   verifyFormat("struct S {\n"
9848                "  S(S&&) = default;\n"
9849                "};",
9850                Style);
9851   verifyFormat("struct [[nodiscard]] S {\n"
9852                "  S(S&&) = default;\n"
9853                "};",
9854                Style);
9855 }
9856 
9857 TEST_F(FormatTest, AttributesAfterMacro) {
9858   FormatStyle Style = getLLVMStyle();
9859   verifyFormat("MACRO;\n"
9860                "__attribute__((maybe_unused)) int foo() {\n"
9861                "  //...\n"
9862                "}");
9863 
9864   verifyFormat("MACRO;\n"
9865                "[[nodiscard]] int foo() {\n"
9866                "  //...\n"
9867                "}");
9868 
9869   EXPECT_EQ("MACRO\n\n"
9870             "__attribute__((maybe_unused)) int foo() {\n"
9871             "  //...\n"
9872             "}",
9873             format("MACRO\n\n"
9874                    "__attribute__((maybe_unused)) int foo() {\n"
9875                    "  //...\n"
9876                    "}"));
9877 
9878   EXPECT_EQ("MACRO\n\n"
9879             "[[nodiscard]] int foo() {\n"
9880             "  //...\n"
9881             "}",
9882             format("MACRO\n\n"
9883                    "[[nodiscard]] int foo() {\n"
9884                    "  //...\n"
9885                    "}"));
9886 }
9887 
9888 TEST_F(FormatTest, AttributePenaltyBreaking) {
9889   FormatStyle Style = getLLVMStyle();
9890   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9891                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9892                Style);
9893   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9894                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9895                Style);
9896   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9897                "shared_ptr<ALongTypeName> &C d) {\n}",
9898                Style);
9899 }
9900 
9901 TEST_F(FormatTest, UnderstandsEllipsis) {
9902   FormatStyle Style = getLLVMStyle();
9903   verifyFormat("int printf(const char *fmt, ...);");
9904   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9905   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9906 
9907   verifyFormat("template <int *...PP> a;", Style);
9908 
9909   Style.PointerAlignment = FormatStyle::PAS_Left;
9910   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9911 
9912   verifyFormat("template <int*... PP> a;", Style);
9913 
9914   Style.PointerAlignment = FormatStyle::PAS_Middle;
9915   verifyFormat("template <int *... PP> a;", Style);
9916 }
9917 
9918 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9919   EXPECT_EQ("int *a;\n"
9920             "int *a;\n"
9921             "int *a;",
9922             format("int *a;\n"
9923                    "int* a;\n"
9924                    "int *a;",
9925                    getGoogleStyle()));
9926   EXPECT_EQ("int* a;\n"
9927             "int* a;\n"
9928             "int* a;",
9929             format("int* a;\n"
9930                    "int* a;\n"
9931                    "int *a;",
9932                    getGoogleStyle()));
9933   EXPECT_EQ("int *a;\n"
9934             "int *a;\n"
9935             "int *a;",
9936             format("int *a;\n"
9937                    "int * a;\n"
9938                    "int *  a;",
9939                    getGoogleStyle()));
9940   EXPECT_EQ("auto x = [] {\n"
9941             "  int *a;\n"
9942             "  int *a;\n"
9943             "  int *a;\n"
9944             "};",
9945             format("auto x=[]{int *a;\n"
9946                    "int * a;\n"
9947                    "int *  a;};",
9948                    getGoogleStyle()));
9949 }
9950 
9951 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9952   verifyFormat("int f(int &&a) {}");
9953   verifyFormat("int f(int a, char &&b) {}");
9954   verifyFormat("void f() { int &&a = b; }");
9955   verifyGoogleFormat("int f(int a, char&& b) {}");
9956   verifyGoogleFormat("void f() { int&& a = b; }");
9957 
9958   verifyIndependentOfContext("A<int &&> a;");
9959   verifyIndependentOfContext("A<int &&, int &&> a;");
9960   verifyGoogleFormat("A<int&&> a;");
9961   verifyGoogleFormat("A<int&&, int&&> a;");
9962 
9963   // Not rvalue references:
9964   verifyFormat("template <bool B, bool C> class A {\n"
9965                "  static_assert(B && C, \"Something is wrong\");\n"
9966                "};");
9967   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9968   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9969   verifyFormat("#define A(a, b) (a && b)");
9970 }
9971 
9972 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9973   verifyFormat("void f() {\n"
9974                "  x[aaaaaaaaa -\n"
9975                "    b] = 23;\n"
9976                "}",
9977                getLLVMStyleWithColumns(15));
9978 }
9979 
9980 TEST_F(FormatTest, FormatsCasts) {
9981   verifyFormat("Type *A = static_cast<Type *>(P);");
9982   verifyFormat("Type *A = (Type *)P;");
9983   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9984   verifyFormat("int a = (int)(2.0f);");
9985   verifyFormat("int a = (int)2.0f;");
9986   verifyFormat("x[(int32)y];");
9987   verifyFormat("x = (int32)y;");
9988   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9989   verifyFormat("int a = (int)*b;");
9990   verifyFormat("int a = (int)2.0f;");
9991   verifyFormat("int a = (int)~0;");
9992   verifyFormat("int a = (int)++a;");
9993   verifyFormat("int a = (int)sizeof(int);");
9994   verifyFormat("int a = (int)+2;");
9995   verifyFormat("my_int a = (my_int)2.0f;");
9996   verifyFormat("my_int a = (my_int)sizeof(int);");
9997   verifyFormat("return (my_int)aaa;");
9998   verifyFormat("#define x ((int)-1)");
9999   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10000   verifyFormat("#define p(q) ((int *)&q)");
10001   verifyFormat("fn(a)(b) + 1;");
10002 
10003   verifyFormat("void f() { my_int a = (my_int)*b; }");
10004   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10005   verifyFormat("my_int a = (my_int)~0;");
10006   verifyFormat("my_int a = (my_int)++a;");
10007   verifyFormat("my_int a = (my_int)-2;");
10008   verifyFormat("my_int a = (my_int)1;");
10009   verifyFormat("my_int a = (my_int *)1;");
10010   verifyFormat("my_int a = (const my_int)-1;");
10011   verifyFormat("my_int a = (const my_int *)-1;");
10012   verifyFormat("my_int a = (my_int)(my_int)-1;");
10013   verifyFormat("my_int a = (ns::my_int)-2;");
10014   verifyFormat("case (my_int)ONE:");
10015   verifyFormat("auto x = (X)this;");
10016   // Casts in Obj-C style calls used to not be recognized as such.
10017   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10018 
10019   // FIXME: single value wrapped with paren will be treated as cast.
10020   verifyFormat("void f(int i = (kValue)*kMask) {}");
10021 
10022   verifyFormat("{ (void)F; }");
10023 
10024   // Don't break after a cast's
10025   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10026                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10027                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10028 
10029   // These are not casts.
10030   verifyFormat("void f(int *) {}");
10031   verifyFormat("f(foo)->b;");
10032   verifyFormat("f(foo).b;");
10033   verifyFormat("f(foo)(b);");
10034   verifyFormat("f(foo)[b];");
10035   verifyFormat("[](foo) { return 4; }(bar);");
10036   verifyFormat("(*funptr)(foo)[4];");
10037   verifyFormat("funptrs[4](foo)[4];");
10038   verifyFormat("void f(int *);");
10039   verifyFormat("void f(int *) = 0;");
10040   verifyFormat("void f(SmallVector<int>) {}");
10041   verifyFormat("void f(SmallVector<int>);");
10042   verifyFormat("void f(SmallVector<int>) = 0;");
10043   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10044   verifyFormat("int a = sizeof(int) * b;");
10045   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10046   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10047   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10048   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10049 
10050   // These are not casts, but at some point were confused with casts.
10051   verifyFormat("virtual void foo(int *) override;");
10052   verifyFormat("virtual void foo(char &) const;");
10053   verifyFormat("virtual void foo(int *a, char *) const;");
10054   verifyFormat("int a = sizeof(int *) + b;");
10055   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10056   verifyFormat("bool b = f(g<int>) && c;");
10057   verifyFormat("typedef void (*f)(int i) func;");
10058   verifyFormat("void operator++(int) noexcept;");
10059   verifyFormat("void operator++(int &) noexcept;");
10060   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10061                "&) noexcept;");
10062   verifyFormat(
10063       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10064   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10065   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10066   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10067   verifyFormat("void operator delete(foo &) noexcept;");
10068   verifyFormat("void operator delete(foo) noexcept;");
10069   verifyFormat("void operator delete(int) noexcept;");
10070   verifyFormat("void operator delete(int &) noexcept;");
10071   verifyFormat("void operator delete(int &) volatile noexcept;");
10072   verifyFormat("void operator delete(int &) const");
10073   verifyFormat("void operator delete(int &) = default");
10074   verifyFormat("void operator delete(int &) = delete");
10075   verifyFormat("void operator delete(int &) [[noreturn]]");
10076   verifyFormat("void operator delete(int &) throw();");
10077   verifyFormat("void operator delete(int &) throw(int);");
10078   verifyFormat("auto operator delete(int &) -> int;");
10079   verifyFormat("auto operator delete(int &) override");
10080   verifyFormat("auto operator delete(int &) final");
10081 
10082   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10083                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10084   // FIXME: The indentation here is not ideal.
10085   verifyFormat(
10086       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10087       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10088       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10089 }
10090 
10091 TEST_F(FormatTest, FormatsFunctionTypes) {
10092   verifyFormat("A<bool()> a;");
10093   verifyFormat("A<SomeType()> a;");
10094   verifyFormat("A<void (*)(int, std::string)> a;");
10095   verifyFormat("A<void *(int)>;");
10096   verifyFormat("void *(*a)(int *, SomeType *);");
10097   verifyFormat("int (*func)(void *);");
10098   verifyFormat("void f() { int (*func)(void *); }");
10099   verifyFormat("template <class CallbackClass>\n"
10100                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10101 
10102   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10103   verifyGoogleFormat("void* (*a)(int);");
10104   verifyGoogleFormat(
10105       "template <class CallbackClass>\n"
10106       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10107 
10108   // Other constructs can look somewhat like function types:
10109   verifyFormat("A<sizeof(*x)> a;");
10110   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10111   verifyFormat("some_var = function(*some_pointer_var)[0];");
10112   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10113   verifyFormat("int x = f(&h)();");
10114   verifyFormat("returnsFunction(&param1, &param2)(param);");
10115   verifyFormat("std::function<\n"
10116                "    LooooooooooongTemplatedType<\n"
10117                "        SomeType>*(\n"
10118                "        LooooooooooooooooongType type)>\n"
10119                "    function;",
10120                getGoogleStyleWithColumns(40));
10121 }
10122 
10123 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10124   verifyFormat("A (*foo_)[6];");
10125   verifyFormat("vector<int> (*foo_)[6];");
10126 }
10127 
10128 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10129   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10130                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10131   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10132                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10133   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10134                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10135 
10136   // Different ways of ()-initializiation.
10137   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10138                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10139   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10140                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10141   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10142                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10143   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10144                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10145 
10146   // Lambdas should not confuse the variable declaration heuristic.
10147   verifyFormat("LooooooooooooooooongType\n"
10148                "    variable(nullptr, [](A *a) {});",
10149                getLLVMStyleWithColumns(40));
10150 }
10151 
10152 TEST_F(FormatTest, BreaksLongDeclarations) {
10153   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10154                "    AnotherNameForTheLongType;");
10155   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10156                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10157   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10158                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10159   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10160                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10161   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10162                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10163   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10164                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10165   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10166                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10167   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10168                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10169   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10170                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10171   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10172                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10173   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10174                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10175   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10176                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10177   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10178                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10179   FormatStyle Indented = getLLVMStyle();
10180   Indented.IndentWrappedFunctionNames = true;
10181   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10182                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10183                Indented);
10184   verifyFormat(
10185       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10186       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10187       Indented);
10188   verifyFormat(
10189       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10190       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10191       Indented);
10192   verifyFormat(
10193       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10194       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10195       Indented);
10196 
10197   // FIXME: Without the comment, this breaks after "(".
10198   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10199                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10200                getGoogleStyle());
10201 
10202   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10203                "                  int LoooooooooooooooooooongParam2) {}");
10204   verifyFormat(
10205       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10206       "                                   SourceLocation L, IdentifierIn *II,\n"
10207       "                                   Type *T) {}");
10208   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10209                "ReallyReaaallyLongFunctionName(\n"
10210                "    const std::string &SomeParameter,\n"
10211                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10212                "        &ReallyReallyLongParameterName,\n"
10213                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10214                "        &AnotherLongParameterName) {}");
10215   verifyFormat("template <typename A>\n"
10216                "SomeLoooooooooooooooooooooongType<\n"
10217                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10218                "Function() {}");
10219 
10220   verifyGoogleFormat(
10221       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10222       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10223   verifyGoogleFormat(
10224       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10225       "                                   SourceLocation L) {}");
10226   verifyGoogleFormat(
10227       "some_namespace::LongReturnType\n"
10228       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10229       "    int first_long_parameter, int second_parameter) {}");
10230 
10231   verifyGoogleFormat("template <typename T>\n"
10232                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10233                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10234   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10235                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10236 
10237   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10238                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10239                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10240   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10241                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10242                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10243   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10244                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10245                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10246                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10247 
10248   verifyFormat("template <typename T> // Templates on own line.\n"
10249                "static int            // Some comment.\n"
10250                "MyFunction(int a);",
10251                getLLVMStyle());
10252 }
10253 
10254 TEST_F(FormatTest, FormatsAccessModifiers) {
10255   FormatStyle Style = getLLVMStyle();
10256   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10257             FormatStyle::ELBAMS_LogicalBlock);
10258   verifyFormat("struct foo {\n"
10259                "private:\n"
10260                "  void f() {}\n"
10261                "\n"
10262                "private:\n"
10263                "  int i;\n"
10264                "\n"
10265                "protected:\n"
10266                "  int j;\n"
10267                "};\n",
10268                Style);
10269   verifyFormat("struct foo {\n"
10270                "private:\n"
10271                "  void f() {}\n"
10272                "\n"
10273                "private:\n"
10274                "  int i;\n"
10275                "\n"
10276                "protected:\n"
10277                "  int j;\n"
10278                "};\n",
10279                "struct foo {\n"
10280                "private:\n"
10281                "  void f() {}\n"
10282                "private:\n"
10283                "  int i;\n"
10284                "protected:\n"
10285                "  int j;\n"
10286                "};\n",
10287                Style);
10288   verifyFormat("struct foo { /* comment */\n"
10289                "private:\n"
10290                "  int i;\n"
10291                "  // comment\n"
10292                "private:\n"
10293                "  int j;\n"
10294                "};\n",
10295                Style);
10296   verifyFormat("struct foo {\n"
10297                "#ifdef FOO\n"
10298                "#endif\n"
10299                "private:\n"
10300                "  int i;\n"
10301                "#ifdef FOO\n"
10302                "private:\n"
10303                "#endif\n"
10304                "  int j;\n"
10305                "};\n",
10306                Style);
10307   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10308   verifyFormat("struct foo {\n"
10309                "private:\n"
10310                "  void f() {}\n"
10311                "private:\n"
10312                "  int i;\n"
10313                "protected:\n"
10314                "  int j;\n"
10315                "};\n",
10316                Style);
10317   verifyFormat("struct foo {\n"
10318                "private:\n"
10319                "  void f() {}\n"
10320                "private:\n"
10321                "  int i;\n"
10322                "protected:\n"
10323                "  int j;\n"
10324                "};\n",
10325                "struct foo {\n"
10326                "\n"
10327                "private:\n"
10328                "  void f() {}\n"
10329                "\n"
10330                "private:\n"
10331                "  int i;\n"
10332                "\n"
10333                "protected:\n"
10334                "  int j;\n"
10335                "};\n",
10336                Style);
10337   verifyFormat("struct foo { /* comment */\n"
10338                "private:\n"
10339                "  int i;\n"
10340                "  // comment\n"
10341                "private:\n"
10342                "  int j;\n"
10343                "};\n",
10344                "struct foo { /* comment */\n"
10345                "\n"
10346                "private:\n"
10347                "  int i;\n"
10348                "  // comment\n"
10349                "\n"
10350                "private:\n"
10351                "  int j;\n"
10352                "};\n",
10353                Style);
10354   verifyFormat("struct foo {\n"
10355                "#ifdef FOO\n"
10356                "#endif\n"
10357                "private:\n"
10358                "  int i;\n"
10359                "#ifdef FOO\n"
10360                "private:\n"
10361                "#endif\n"
10362                "  int j;\n"
10363                "};\n",
10364                "struct foo {\n"
10365                "#ifdef FOO\n"
10366                "#endif\n"
10367                "\n"
10368                "private:\n"
10369                "  int i;\n"
10370                "#ifdef FOO\n"
10371                "\n"
10372                "private:\n"
10373                "#endif\n"
10374                "  int j;\n"
10375                "};\n",
10376                Style);
10377   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10378   verifyFormat("struct foo {\n"
10379                "private:\n"
10380                "  void f() {}\n"
10381                "\n"
10382                "private:\n"
10383                "  int i;\n"
10384                "\n"
10385                "protected:\n"
10386                "  int j;\n"
10387                "};\n",
10388                Style);
10389   verifyFormat("struct foo {\n"
10390                "private:\n"
10391                "  void f() {}\n"
10392                "\n"
10393                "private:\n"
10394                "  int i;\n"
10395                "\n"
10396                "protected:\n"
10397                "  int j;\n"
10398                "};\n",
10399                "struct foo {\n"
10400                "private:\n"
10401                "  void f() {}\n"
10402                "private:\n"
10403                "  int i;\n"
10404                "protected:\n"
10405                "  int j;\n"
10406                "};\n",
10407                Style);
10408   verifyFormat("struct foo { /* comment */\n"
10409                "private:\n"
10410                "  int i;\n"
10411                "  // comment\n"
10412                "\n"
10413                "private:\n"
10414                "  int j;\n"
10415                "};\n",
10416                "struct foo { /* comment */\n"
10417                "private:\n"
10418                "  int i;\n"
10419                "  // comment\n"
10420                "\n"
10421                "private:\n"
10422                "  int j;\n"
10423                "};\n",
10424                Style);
10425   verifyFormat("struct foo {\n"
10426                "#ifdef FOO\n"
10427                "#endif\n"
10428                "\n"
10429                "private:\n"
10430                "  int i;\n"
10431                "#ifdef FOO\n"
10432                "\n"
10433                "private:\n"
10434                "#endif\n"
10435                "  int j;\n"
10436                "};\n",
10437                "struct foo {\n"
10438                "#ifdef FOO\n"
10439                "#endif\n"
10440                "private:\n"
10441                "  int i;\n"
10442                "#ifdef FOO\n"
10443                "private:\n"
10444                "#endif\n"
10445                "  int j;\n"
10446                "};\n",
10447                Style);
10448   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10449   EXPECT_EQ("struct foo {\n"
10450             "\n"
10451             "private:\n"
10452             "  void f() {}\n"
10453             "\n"
10454             "private:\n"
10455             "  int i;\n"
10456             "\n"
10457             "protected:\n"
10458             "  int j;\n"
10459             "};\n",
10460             format("struct foo {\n"
10461                    "\n"
10462                    "private:\n"
10463                    "  void f() {}\n"
10464                    "\n"
10465                    "private:\n"
10466                    "  int i;\n"
10467                    "\n"
10468                    "protected:\n"
10469                    "  int j;\n"
10470                    "};\n",
10471                    Style));
10472   verifyFormat("struct foo {\n"
10473                "private:\n"
10474                "  void f() {}\n"
10475                "private:\n"
10476                "  int i;\n"
10477                "protected:\n"
10478                "  int j;\n"
10479                "};\n",
10480                Style);
10481   EXPECT_EQ("struct foo { /* comment */\n"
10482             "\n"
10483             "private:\n"
10484             "  int i;\n"
10485             "  // comment\n"
10486             "\n"
10487             "private:\n"
10488             "  int j;\n"
10489             "};\n",
10490             format("struct foo { /* comment */\n"
10491                    "\n"
10492                    "private:\n"
10493                    "  int i;\n"
10494                    "  // comment\n"
10495                    "\n"
10496                    "private:\n"
10497                    "  int j;\n"
10498                    "};\n",
10499                    Style));
10500   verifyFormat("struct foo { /* comment */\n"
10501                "private:\n"
10502                "  int i;\n"
10503                "  // comment\n"
10504                "private:\n"
10505                "  int j;\n"
10506                "};\n",
10507                Style);
10508   EXPECT_EQ("struct foo {\n"
10509             "#ifdef FOO\n"
10510             "#endif\n"
10511             "\n"
10512             "private:\n"
10513             "  int i;\n"
10514             "#ifdef FOO\n"
10515             "\n"
10516             "private:\n"
10517             "#endif\n"
10518             "  int j;\n"
10519             "};\n",
10520             format("struct foo {\n"
10521                    "#ifdef FOO\n"
10522                    "#endif\n"
10523                    "\n"
10524                    "private:\n"
10525                    "  int i;\n"
10526                    "#ifdef FOO\n"
10527                    "\n"
10528                    "private:\n"
10529                    "#endif\n"
10530                    "  int j;\n"
10531                    "};\n",
10532                    Style));
10533   verifyFormat("struct foo {\n"
10534                "#ifdef FOO\n"
10535                "#endif\n"
10536                "private:\n"
10537                "  int i;\n"
10538                "#ifdef FOO\n"
10539                "private:\n"
10540                "#endif\n"
10541                "  int j;\n"
10542                "};\n",
10543                Style);
10544 
10545   FormatStyle NoEmptyLines = getLLVMStyle();
10546   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10547   verifyFormat("struct foo {\n"
10548                "private:\n"
10549                "  void f() {}\n"
10550                "\n"
10551                "private:\n"
10552                "  int i;\n"
10553                "\n"
10554                "public:\n"
10555                "protected:\n"
10556                "  int j;\n"
10557                "};\n",
10558                NoEmptyLines);
10559 
10560   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10561   verifyFormat("struct foo {\n"
10562                "private:\n"
10563                "  void f() {}\n"
10564                "private:\n"
10565                "  int i;\n"
10566                "public:\n"
10567                "protected:\n"
10568                "  int j;\n"
10569                "};\n",
10570                NoEmptyLines);
10571 
10572   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10573   verifyFormat("struct foo {\n"
10574                "private:\n"
10575                "  void f() {}\n"
10576                "\n"
10577                "private:\n"
10578                "  int i;\n"
10579                "\n"
10580                "public:\n"
10581                "\n"
10582                "protected:\n"
10583                "  int j;\n"
10584                "};\n",
10585                NoEmptyLines);
10586 }
10587 
10588 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10589 
10590   FormatStyle Style = getLLVMStyle();
10591   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10592   verifyFormat("struct foo {\n"
10593                "private:\n"
10594                "  void f() {}\n"
10595                "\n"
10596                "private:\n"
10597                "  int i;\n"
10598                "\n"
10599                "protected:\n"
10600                "  int j;\n"
10601                "};\n",
10602                Style);
10603 
10604   // Check if lines are removed.
10605   verifyFormat("struct foo {\n"
10606                "private:\n"
10607                "  void f() {}\n"
10608                "\n"
10609                "private:\n"
10610                "  int i;\n"
10611                "\n"
10612                "protected:\n"
10613                "  int j;\n"
10614                "};\n",
10615                "struct foo {\n"
10616                "private:\n"
10617                "\n"
10618                "  void f() {}\n"
10619                "\n"
10620                "private:\n"
10621                "\n"
10622                "  int i;\n"
10623                "\n"
10624                "protected:\n"
10625                "\n"
10626                "  int j;\n"
10627                "};\n",
10628                Style);
10629 
10630   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10631   verifyFormat("struct foo {\n"
10632                "private:\n"
10633                "\n"
10634                "  void f() {}\n"
10635                "\n"
10636                "private:\n"
10637                "\n"
10638                "  int i;\n"
10639                "\n"
10640                "protected:\n"
10641                "\n"
10642                "  int j;\n"
10643                "};\n",
10644                Style);
10645 
10646   // Check if lines are added.
10647   verifyFormat("struct foo {\n"
10648                "private:\n"
10649                "\n"
10650                "  void f() {}\n"
10651                "\n"
10652                "private:\n"
10653                "\n"
10654                "  int i;\n"
10655                "\n"
10656                "protected:\n"
10657                "\n"
10658                "  int j;\n"
10659                "};\n",
10660                "struct foo {\n"
10661                "private:\n"
10662                "  void f() {}\n"
10663                "\n"
10664                "private:\n"
10665                "  int i;\n"
10666                "\n"
10667                "protected:\n"
10668                "  int j;\n"
10669                "};\n",
10670                Style);
10671 
10672   // Leave tests rely on the code layout, test::messUp can not be used.
10673   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10674   Style.MaxEmptyLinesToKeep = 0u;
10675   verifyFormat("struct foo {\n"
10676                "private:\n"
10677                "  void f() {}\n"
10678                "\n"
10679                "private:\n"
10680                "  int i;\n"
10681                "\n"
10682                "protected:\n"
10683                "  int j;\n"
10684                "};\n",
10685                Style);
10686 
10687   // Check if MaxEmptyLinesToKeep is respected.
10688   EXPECT_EQ("struct foo {\n"
10689             "private:\n"
10690             "  void f() {}\n"
10691             "\n"
10692             "private:\n"
10693             "  int i;\n"
10694             "\n"
10695             "protected:\n"
10696             "  int j;\n"
10697             "};\n",
10698             format("struct foo {\n"
10699                    "private:\n"
10700                    "\n\n\n"
10701                    "  void f() {}\n"
10702                    "\n"
10703                    "private:\n"
10704                    "\n\n\n"
10705                    "  int i;\n"
10706                    "\n"
10707                    "protected:\n"
10708                    "\n\n\n"
10709                    "  int j;\n"
10710                    "};\n",
10711                    Style));
10712 
10713   Style.MaxEmptyLinesToKeep = 1u;
10714   EXPECT_EQ("struct foo {\n"
10715             "private:\n"
10716             "\n"
10717             "  void f() {}\n"
10718             "\n"
10719             "private:\n"
10720             "\n"
10721             "  int i;\n"
10722             "\n"
10723             "protected:\n"
10724             "\n"
10725             "  int j;\n"
10726             "};\n",
10727             format("struct foo {\n"
10728                    "private:\n"
10729                    "\n"
10730                    "  void f() {}\n"
10731                    "\n"
10732                    "private:\n"
10733                    "\n"
10734                    "  int i;\n"
10735                    "\n"
10736                    "protected:\n"
10737                    "\n"
10738                    "  int j;\n"
10739                    "};\n",
10740                    Style));
10741   // Check if no lines are kept.
10742   EXPECT_EQ("struct foo {\n"
10743             "private:\n"
10744             "  void f() {}\n"
10745             "\n"
10746             "private:\n"
10747             "  int i;\n"
10748             "\n"
10749             "protected:\n"
10750             "  int j;\n"
10751             "};\n",
10752             format("struct foo {\n"
10753                    "private:\n"
10754                    "  void f() {}\n"
10755                    "\n"
10756                    "private:\n"
10757                    "  int i;\n"
10758                    "\n"
10759                    "protected:\n"
10760                    "  int j;\n"
10761                    "};\n",
10762                    Style));
10763   // Check if MaxEmptyLinesToKeep is respected.
10764   EXPECT_EQ("struct foo {\n"
10765             "private:\n"
10766             "\n"
10767             "  void f() {}\n"
10768             "\n"
10769             "private:\n"
10770             "\n"
10771             "  int i;\n"
10772             "\n"
10773             "protected:\n"
10774             "\n"
10775             "  int j;\n"
10776             "};\n",
10777             format("struct foo {\n"
10778                    "private:\n"
10779                    "\n\n\n"
10780                    "  void f() {}\n"
10781                    "\n"
10782                    "private:\n"
10783                    "\n\n\n"
10784                    "  int i;\n"
10785                    "\n"
10786                    "protected:\n"
10787                    "\n\n\n"
10788                    "  int j;\n"
10789                    "};\n",
10790                    Style));
10791 
10792   Style.MaxEmptyLinesToKeep = 10u;
10793   EXPECT_EQ("struct foo {\n"
10794             "private:\n"
10795             "\n\n\n"
10796             "  void f() {}\n"
10797             "\n"
10798             "private:\n"
10799             "\n\n\n"
10800             "  int i;\n"
10801             "\n"
10802             "protected:\n"
10803             "\n\n\n"
10804             "  int j;\n"
10805             "};\n",
10806             format("struct foo {\n"
10807                    "private:\n"
10808                    "\n\n\n"
10809                    "  void f() {}\n"
10810                    "\n"
10811                    "private:\n"
10812                    "\n\n\n"
10813                    "  int i;\n"
10814                    "\n"
10815                    "protected:\n"
10816                    "\n\n\n"
10817                    "  int j;\n"
10818                    "};\n",
10819                    Style));
10820 
10821   // Test with comments.
10822   Style = getLLVMStyle();
10823   verifyFormat("struct foo {\n"
10824                "private:\n"
10825                "  // comment\n"
10826                "  void f() {}\n"
10827                "\n"
10828                "private: /* comment */\n"
10829                "  int i;\n"
10830                "};\n",
10831                Style);
10832   verifyFormat("struct foo {\n"
10833                "private:\n"
10834                "  // comment\n"
10835                "  void f() {}\n"
10836                "\n"
10837                "private: /* comment */\n"
10838                "  int i;\n"
10839                "};\n",
10840                "struct foo {\n"
10841                "private:\n"
10842                "\n"
10843                "  // comment\n"
10844                "  void f() {}\n"
10845                "\n"
10846                "private: /* comment */\n"
10847                "\n"
10848                "  int i;\n"
10849                "};\n",
10850                Style);
10851 
10852   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10853   verifyFormat("struct foo {\n"
10854                "private:\n"
10855                "\n"
10856                "  // comment\n"
10857                "  void f() {}\n"
10858                "\n"
10859                "private: /* comment */\n"
10860                "\n"
10861                "  int i;\n"
10862                "};\n",
10863                "struct foo {\n"
10864                "private:\n"
10865                "  // comment\n"
10866                "  void f() {}\n"
10867                "\n"
10868                "private: /* comment */\n"
10869                "  int i;\n"
10870                "};\n",
10871                Style);
10872   verifyFormat("struct foo {\n"
10873                "private:\n"
10874                "\n"
10875                "  // comment\n"
10876                "  void f() {}\n"
10877                "\n"
10878                "private: /* comment */\n"
10879                "\n"
10880                "  int i;\n"
10881                "};\n",
10882                Style);
10883 
10884   // Test with preprocessor defines.
10885   Style = getLLVMStyle();
10886   verifyFormat("struct foo {\n"
10887                "private:\n"
10888                "#ifdef FOO\n"
10889                "#endif\n"
10890                "  void f() {}\n"
10891                "};\n",
10892                Style);
10893   verifyFormat("struct foo {\n"
10894                "private:\n"
10895                "#ifdef FOO\n"
10896                "#endif\n"
10897                "  void f() {}\n"
10898                "};\n",
10899                "struct foo {\n"
10900                "private:\n"
10901                "\n"
10902                "#ifdef FOO\n"
10903                "#endif\n"
10904                "  void f() {}\n"
10905                "};\n",
10906                Style);
10907 
10908   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10909   verifyFormat("struct foo {\n"
10910                "private:\n"
10911                "\n"
10912                "#ifdef FOO\n"
10913                "#endif\n"
10914                "  void f() {}\n"
10915                "};\n",
10916                "struct foo {\n"
10917                "private:\n"
10918                "#ifdef FOO\n"
10919                "#endif\n"
10920                "  void f() {}\n"
10921                "};\n",
10922                Style);
10923   verifyFormat("struct foo {\n"
10924                "private:\n"
10925                "\n"
10926                "#ifdef FOO\n"
10927                "#endif\n"
10928                "  void f() {}\n"
10929                "};\n",
10930                Style);
10931 }
10932 
10933 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10934   // Combined tests of EmptyLineAfterAccessModifier and
10935   // EmptyLineBeforeAccessModifier.
10936   FormatStyle Style = getLLVMStyle();
10937   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10938   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10939   verifyFormat("struct foo {\n"
10940                "private:\n"
10941                "\n"
10942                "protected:\n"
10943                "};\n",
10944                Style);
10945 
10946   Style.MaxEmptyLinesToKeep = 10u;
10947   // Both remove all new lines.
10948   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10949   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10950   verifyFormat("struct foo {\n"
10951                "private:\n"
10952                "protected:\n"
10953                "};\n",
10954                "struct foo {\n"
10955                "private:\n"
10956                "\n\n\n"
10957                "protected:\n"
10958                "};\n",
10959                Style);
10960 
10961   // Leave tests rely on the code layout, test::messUp can not be used.
10962   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10963   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10964   Style.MaxEmptyLinesToKeep = 10u;
10965   EXPECT_EQ("struct foo {\n"
10966             "private:\n"
10967             "\n\n\n"
10968             "protected:\n"
10969             "};\n",
10970             format("struct foo {\n"
10971                    "private:\n"
10972                    "\n\n\n"
10973                    "protected:\n"
10974                    "};\n",
10975                    Style));
10976   Style.MaxEmptyLinesToKeep = 3u;
10977   EXPECT_EQ("struct foo {\n"
10978             "private:\n"
10979             "\n\n\n"
10980             "protected:\n"
10981             "};\n",
10982             format("struct foo {\n"
10983                    "private:\n"
10984                    "\n\n\n"
10985                    "protected:\n"
10986                    "};\n",
10987                    Style));
10988   Style.MaxEmptyLinesToKeep = 1u;
10989   EXPECT_EQ("struct foo {\n"
10990             "private:\n"
10991             "\n\n\n"
10992             "protected:\n"
10993             "};\n",
10994             format("struct foo {\n"
10995                    "private:\n"
10996                    "\n\n\n"
10997                    "protected:\n"
10998                    "};\n",
10999                    Style)); // Based on new lines in original document and not
11000                             // on the setting.
11001 
11002   Style.MaxEmptyLinesToKeep = 10u;
11003   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11004   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11005   // Newlines are kept if they are greater than zero,
11006   // test::messUp removes all new lines which changes the logic
11007   EXPECT_EQ("struct foo {\n"
11008             "private:\n"
11009             "\n\n\n"
11010             "protected:\n"
11011             "};\n",
11012             format("struct foo {\n"
11013                    "private:\n"
11014                    "\n\n\n"
11015                    "protected:\n"
11016                    "};\n",
11017                    Style));
11018 
11019   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11020   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11021   // test::messUp removes all new lines which changes the logic
11022   EXPECT_EQ("struct foo {\n"
11023             "private:\n"
11024             "\n\n\n"
11025             "protected:\n"
11026             "};\n",
11027             format("struct foo {\n"
11028                    "private:\n"
11029                    "\n\n\n"
11030                    "protected:\n"
11031                    "};\n",
11032                    Style));
11033 
11034   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11035   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11036   EXPECT_EQ("struct foo {\n"
11037             "private:\n"
11038             "\n\n\n"
11039             "protected:\n"
11040             "};\n",
11041             format("struct foo {\n"
11042                    "private:\n"
11043                    "\n\n\n"
11044                    "protected:\n"
11045                    "};\n",
11046                    Style)); // test::messUp removes all new lines which changes
11047                             // the logic.
11048 
11049   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11050   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11051   verifyFormat("struct foo {\n"
11052                "private:\n"
11053                "protected:\n"
11054                "};\n",
11055                "struct foo {\n"
11056                "private:\n"
11057                "\n\n\n"
11058                "protected:\n"
11059                "};\n",
11060                Style);
11061 
11062   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11063   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11064   EXPECT_EQ("struct foo {\n"
11065             "private:\n"
11066             "\n\n\n"
11067             "protected:\n"
11068             "};\n",
11069             format("struct foo {\n"
11070                    "private:\n"
11071                    "\n\n\n"
11072                    "protected:\n"
11073                    "};\n",
11074                    Style)); // test::messUp removes all new lines which changes
11075                             // the logic.
11076 
11077   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11078   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11079   verifyFormat("struct foo {\n"
11080                "private:\n"
11081                "protected:\n"
11082                "};\n",
11083                "struct foo {\n"
11084                "private:\n"
11085                "\n\n\n"
11086                "protected:\n"
11087                "};\n",
11088                Style);
11089 
11090   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11091   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11092   verifyFormat("struct foo {\n"
11093                "private:\n"
11094                "protected:\n"
11095                "};\n",
11096                "struct foo {\n"
11097                "private:\n"
11098                "\n\n\n"
11099                "protected:\n"
11100                "};\n",
11101                Style);
11102 
11103   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11104   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11105   verifyFormat("struct foo {\n"
11106                "private:\n"
11107                "protected:\n"
11108                "};\n",
11109                "struct foo {\n"
11110                "private:\n"
11111                "\n\n\n"
11112                "protected:\n"
11113                "};\n",
11114                Style);
11115 
11116   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11117   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11118   verifyFormat("struct foo {\n"
11119                "private:\n"
11120                "protected:\n"
11121                "};\n",
11122                "struct foo {\n"
11123                "private:\n"
11124                "\n\n\n"
11125                "protected:\n"
11126                "};\n",
11127                Style);
11128 }
11129 
11130 TEST_F(FormatTest, FormatsArrays) {
11131   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11132                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11133   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11134                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11135   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11136                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11137   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11138                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11139   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11140                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11141   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11142                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11143                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11144   verifyFormat(
11145       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11146       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11147       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11148   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11149                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11150 
11151   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11152                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11153   verifyFormat(
11154       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11155       "                                  .aaaaaaa[0]\n"
11156       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11157   verifyFormat("a[::b::c];");
11158 
11159   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11160 
11161   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11162   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11163 }
11164 
11165 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11166   verifyFormat("(a)->b();");
11167   verifyFormat("--a;");
11168 }
11169 
11170 TEST_F(FormatTest, HandlesIncludeDirectives) {
11171   verifyFormat("#include <string>\n"
11172                "#include <a/b/c.h>\n"
11173                "#include \"a/b/string\"\n"
11174                "#include \"string.h\"\n"
11175                "#include \"string.h\"\n"
11176                "#include <a-a>\n"
11177                "#include < path with space >\n"
11178                "#include_next <test.h>"
11179                "#include \"abc.h\" // this is included for ABC\n"
11180                "#include \"some long include\" // with a comment\n"
11181                "#include \"some very long include path\"\n"
11182                "#include <some/very/long/include/path>\n",
11183                getLLVMStyleWithColumns(35));
11184   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11185   EXPECT_EQ("#include <a>", format("#include<a>"));
11186 
11187   verifyFormat("#import <string>");
11188   verifyFormat("#import <a/b/c.h>");
11189   verifyFormat("#import \"a/b/string\"");
11190   verifyFormat("#import \"string.h\"");
11191   verifyFormat("#import \"string.h\"");
11192   verifyFormat("#if __has_include(<strstream>)\n"
11193                "#include <strstream>\n"
11194                "#endif");
11195 
11196   verifyFormat("#define MY_IMPORT <a/b>");
11197 
11198   verifyFormat("#if __has_include(<a/b>)");
11199   verifyFormat("#if __has_include_next(<a/b>)");
11200   verifyFormat("#define F __has_include(<a/b>)");
11201   verifyFormat("#define F __has_include_next(<a/b>)");
11202 
11203   // Protocol buffer definition or missing "#".
11204   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11205                getLLVMStyleWithColumns(30));
11206 
11207   FormatStyle Style = getLLVMStyle();
11208   Style.AlwaysBreakBeforeMultilineStrings = true;
11209   Style.ColumnLimit = 0;
11210   verifyFormat("#import \"abc.h\"", Style);
11211 
11212   // But 'import' might also be a regular C++ namespace.
11213   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11214                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11215 }
11216 
11217 //===----------------------------------------------------------------------===//
11218 // Error recovery tests.
11219 //===----------------------------------------------------------------------===//
11220 
11221 TEST_F(FormatTest, IncompleteParameterLists) {
11222   FormatStyle NoBinPacking = getLLVMStyle();
11223   NoBinPacking.BinPackParameters = false;
11224   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11225                "                        double *min_x,\n"
11226                "                        double *max_x,\n"
11227                "                        double *min_y,\n"
11228                "                        double *max_y,\n"
11229                "                        double *min_z,\n"
11230                "                        double *max_z, ) {}",
11231                NoBinPacking);
11232 }
11233 
11234 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11235   verifyFormat("void f() { return; }\n42");
11236   verifyFormat("void f() {\n"
11237                "  if (0)\n"
11238                "    return;\n"
11239                "}\n"
11240                "42");
11241   verifyFormat("void f() { return }\n42");
11242   verifyFormat("void f() {\n"
11243                "  if (0)\n"
11244                "    return\n"
11245                "}\n"
11246                "42");
11247 }
11248 
11249 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11250   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11251   EXPECT_EQ("void f() {\n"
11252             "  if (a)\n"
11253             "    return\n"
11254             "}",
11255             format("void  f  (  )  {  if  ( a )  return  }"));
11256   EXPECT_EQ("namespace N {\n"
11257             "void f()\n"
11258             "}",
11259             format("namespace  N  {  void f()  }"));
11260   EXPECT_EQ("namespace N {\n"
11261             "void f() {}\n"
11262             "void g()\n"
11263             "} // namespace N",
11264             format("namespace N  { void f( ) { } void g( ) }"));
11265 }
11266 
11267 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11268   verifyFormat("int aaaaaaaa =\n"
11269                "    // Overlylongcomment\n"
11270                "    b;",
11271                getLLVMStyleWithColumns(20));
11272   verifyFormat("function(\n"
11273                "    ShortArgument,\n"
11274                "    LoooooooooooongArgument);\n",
11275                getLLVMStyleWithColumns(20));
11276 }
11277 
11278 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11279   verifyFormat("public:");
11280   verifyFormat("class A {\n"
11281                "public\n"
11282                "  void f() {}\n"
11283                "};");
11284   verifyFormat("public\n"
11285                "int qwerty;");
11286   verifyFormat("public\n"
11287                "B {}");
11288   verifyFormat("public\n"
11289                "{}");
11290   verifyFormat("public\n"
11291                "B { int x; }");
11292 }
11293 
11294 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11295   verifyFormat("{");
11296   verifyFormat("#})");
11297   verifyNoCrash("(/**/[:!] ?[).");
11298 }
11299 
11300 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11301   // Found by oss-fuzz:
11302   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11303   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11304   Style.ColumnLimit = 60;
11305   verifyNoCrash(
11306       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11307       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11308       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11309       Style);
11310 }
11311 
11312 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11313   verifyFormat("do {\n}");
11314   verifyFormat("do {\n}\n"
11315                "f();");
11316   verifyFormat("do {\n}\n"
11317                "wheeee(fun);");
11318   verifyFormat("do {\n"
11319                "  f();\n"
11320                "}");
11321 }
11322 
11323 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11324   verifyFormat("if {\n  foo;\n  foo();\n}");
11325   verifyFormat("switch {\n  foo;\n  foo();\n}");
11326   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11327   verifyFormat("while {\n  foo;\n  foo();\n}");
11328   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11329 }
11330 
11331 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11332   verifyIncompleteFormat("namespace {\n"
11333                          "class Foo { Foo (\n"
11334                          "};\n"
11335                          "} // namespace");
11336 }
11337 
11338 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11339   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11340   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11341   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11342   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11343 
11344   EXPECT_EQ("{\n"
11345             "  {\n"
11346             "    breakme(\n"
11347             "        qwe);\n"
11348             "  }\n",
11349             format("{\n"
11350                    "    {\n"
11351                    " breakme(qwe);\n"
11352                    "}\n",
11353                    getLLVMStyleWithColumns(10)));
11354 }
11355 
11356 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11357   verifyFormat("int x = {\n"
11358                "    avariable,\n"
11359                "    b(alongervariable)};",
11360                getLLVMStyleWithColumns(25));
11361 }
11362 
11363 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11364   verifyFormat("return (a)(b){1, 2, 3};");
11365 }
11366 
11367 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11368   verifyFormat("vector<int> x{1, 2, 3, 4};");
11369   verifyFormat("vector<int> x{\n"
11370                "    1,\n"
11371                "    2,\n"
11372                "    3,\n"
11373                "    4,\n"
11374                "};");
11375   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11376   verifyFormat("f({1, 2});");
11377   verifyFormat("auto v = Foo{-1};");
11378   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11379   verifyFormat("Class::Class : member{1, 2, 3} {}");
11380   verifyFormat("new vector<int>{1, 2, 3};");
11381   verifyFormat("new int[3]{1, 2, 3};");
11382   verifyFormat("new int{1};");
11383   verifyFormat("return {arg1, arg2};");
11384   verifyFormat("return {arg1, SomeType{parameter}};");
11385   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11386   verifyFormat("new T{arg1, arg2};");
11387   verifyFormat("f(MyMap[{composite, key}]);");
11388   verifyFormat("class Class {\n"
11389                "  T member = {arg1, arg2};\n"
11390                "};");
11391   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11392   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11393   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11394   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11395   verifyFormat("int a = std::is_integral<int>{} + 0;");
11396 
11397   verifyFormat("int foo(int i) { return fo1{}(i); }");
11398   verifyFormat("int foo(int i) { return fo1{}(i); }");
11399   verifyFormat("auto i = decltype(x){};");
11400   verifyFormat("auto i = typeof(x){};");
11401   verifyFormat("auto i = _Atomic(x){};");
11402   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11403   verifyFormat("Node n{1, Node{1000}, //\n"
11404                "       2};");
11405   verifyFormat("Aaaa aaaaaaa{\n"
11406                "    {\n"
11407                "        aaaa,\n"
11408                "    },\n"
11409                "};");
11410   verifyFormat("class C : public D {\n"
11411                "  SomeClass SC{2};\n"
11412                "};");
11413   verifyFormat("class C : public A {\n"
11414                "  class D : public B {\n"
11415                "    void f() { int i{2}; }\n"
11416                "  };\n"
11417                "};");
11418   verifyFormat("#define A {a, a},");
11419 
11420   // Avoid breaking between equal sign and opening brace
11421   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11422   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11423   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11424                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11425                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11426                "     {\"ccccccccccccccccccccc\", 2}};",
11427                AvoidBreakingFirstArgument);
11428 
11429   // Binpacking only if there is no trailing comma
11430   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11431                "                      cccccccccc, dddddddddd};",
11432                getLLVMStyleWithColumns(50));
11433   verifyFormat("const Aaaaaa aaaaa = {\n"
11434                "    aaaaaaaaaaa,\n"
11435                "    bbbbbbbbbbb,\n"
11436                "    ccccccccccc,\n"
11437                "    ddddddddddd,\n"
11438                "};",
11439                getLLVMStyleWithColumns(50));
11440 
11441   // Cases where distinguising braced lists and blocks is hard.
11442   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11443   verifyFormat("void f() {\n"
11444                "  return; // comment\n"
11445                "}\n"
11446                "SomeType t;");
11447   verifyFormat("void f() {\n"
11448                "  if (a) {\n"
11449                "    f();\n"
11450                "  }\n"
11451                "}\n"
11452                "SomeType t;");
11453 
11454   // In combination with BinPackArguments = false.
11455   FormatStyle NoBinPacking = getLLVMStyle();
11456   NoBinPacking.BinPackArguments = false;
11457   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11458                "                      bbbbb,\n"
11459                "                      ccccc,\n"
11460                "                      ddddd,\n"
11461                "                      eeeee,\n"
11462                "                      ffffff,\n"
11463                "                      ggggg,\n"
11464                "                      hhhhhh,\n"
11465                "                      iiiiii,\n"
11466                "                      jjjjjj,\n"
11467                "                      kkkkkk};",
11468                NoBinPacking);
11469   verifyFormat("const Aaaaaa aaaaa = {\n"
11470                "    aaaaa,\n"
11471                "    bbbbb,\n"
11472                "    ccccc,\n"
11473                "    ddddd,\n"
11474                "    eeeee,\n"
11475                "    ffffff,\n"
11476                "    ggggg,\n"
11477                "    hhhhhh,\n"
11478                "    iiiiii,\n"
11479                "    jjjjjj,\n"
11480                "    kkkkkk,\n"
11481                "};",
11482                NoBinPacking);
11483   verifyFormat(
11484       "const Aaaaaa aaaaa = {\n"
11485       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11486       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11487       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11488       "};",
11489       NoBinPacking);
11490 
11491   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11492   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11493             "    CDDDP83848_BMCR_REGISTER,\n"
11494             "    CDDDP83848_BMSR_REGISTER,\n"
11495             "    CDDDP83848_RBR_REGISTER};",
11496             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11497                    "                                CDDDP83848_BMSR_REGISTER,\n"
11498                    "                                CDDDP83848_RBR_REGISTER};",
11499                    NoBinPacking));
11500 
11501   // FIXME: The alignment of these trailing comments might be bad. Then again,
11502   // this might be utterly useless in real code.
11503   verifyFormat("Constructor::Constructor()\n"
11504                "    : some_value{         //\n"
11505                "                 aaaaaaa, //\n"
11506                "                 bbbbbbb} {}");
11507 
11508   // In braced lists, the first comment is always assumed to belong to the
11509   // first element. Thus, it can be moved to the next or previous line as
11510   // appropriate.
11511   EXPECT_EQ("function({// First element:\n"
11512             "          1,\n"
11513             "          // Second element:\n"
11514             "          2});",
11515             format("function({\n"
11516                    "    // First element:\n"
11517                    "    1,\n"
11518                    "    // Second element:\n"
11519                    "    2});"));
11520   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11521             "    // First element:\n"
11522             "    1,\n"
11523             "    // Second element:\n"
11524             "    2};",
11525             format("std::vector<int> MyNumbers{// First element:\n"
11526                    "                           1,\n"
11527                    "                           // Second element:\n"
11528                    "                           2};",
11529                    getLLVMStyleWithColumns(30)));
11530   // A trailing comma should still lead to an enforced line break and no
11531   // binpacking.
11532   EXPECT_EQ("vector<int> SomeVector = {\n"
11533             "    // aaa\n"
11534             "    1,\n"
11535             "    2,\n"
11536             "};",
11537             format("vector<int> SomeVector = { // aaa\n"
11538                    "    1, 2, };"));
11539 
11540   // C++11 brace initializer list l-braces should not be treated any differently
11541   // when breaking before lambda bodies is enabled
11542   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11543   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11544   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11545   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11546   verifyFormat(
11547       "std::runtime_error{\n"
11548       "    \"Long string which will force a break onto the next line...\"};",
11549       BreakBeforeLambdaBody);
11550 
11551   FormatStyle ExtraSpaces = getLLVMStyle();
11552   ExtraSpaces.Cpp11BracedListStyle = false;
11553   ExtraSpaces.ColumnLimit = 75;
11554   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11555   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11556   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11557   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11558   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11559   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11560   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11561   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11562   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11563   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11564   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11565   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11566   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11567   verifyFormat("class Class {\n"
11568                "  T member = { arg1, arg2 };\n"
11569                "};",
11570                ExtraSpaces);
11571   verifyFormat(
11572       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11573       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11574       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11575       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11576       ExtraSpaces);
11577   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11578   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11579                ExtraSpaces);
11580   verifyFormat(
11581       "someFunction(OtherParam,\n"
11582       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11583       "                         param1, param2,\n"
11584       "                         // comment 2\n"
11585       "                         param3, param4 });",
11586       ExtraSpaces);
11587   verifyFormat(
11588       "std::this_thread::sleep_for(\n"
11589       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11590       ExtraSpaces);
11591   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11592                "    aaaaaaa,\n"
11593                "    aaaaaaaaaa,\n"
11594                "    aaaaa,\n"
11595                "    aaaaaaaaaaaaaaa,\n"
11596                "    aaa,\n"
11597                "    aaaaaaaaaa,\n"
11598                "    a,\n"
11599                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11600                "    aaaaaaaaaaaa,\n"
11601                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11602                "    aaaaaaa,\n"
11603                "    a};");
11604   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11605   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11606   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11607 
11608   // Avoid breaking between initializer/equal sign and opening brace
11609   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11610   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11611                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11612                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11613                "  { \"ccccccccccccccccccccc\", 2 }\n"
11614                "};",
11615                ExtraSpaces);
11616   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11617                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11618                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11619                "  { \"ccccccccccccccccccccc\", 2 }\n"
11620                "};",
11621                ExtraSpaces);
11622 
11623   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11624   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11625   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11626   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11627 
11628   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11629   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11630   SpaceBetweenBraces.SpacesInParentheses = true;
11631   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11632   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11633   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11634   verifyFormat("vector< int > x{ // comment 1\n"
11635                "                 1, 2, 3, 4 };",
11636                SpaceBetweenBraces);
11637   SpaceBetweenBraces.ColumnLimit = 20;
11638   EXPECT_EQ("vector< int > x{\n"
11639             "    1, 2, 3, 4 };",
11640             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11641   SpaceBetweenBraces.ColumnLimit = 24;
11642   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11643             "                 3, 4 };",
11644             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11645   EXPECT_EQ("vector< int > x{\n"
11646             "    1,\n"
11647             "    2,\n"
11648             "    3,\n"
11649             "    4,\n"
11650             "};",
11651             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11652   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11653   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11654   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11655 }
11656 
11657 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11658   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11659                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11660                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11661                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11662                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11663                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11664   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11665                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11666                "                 1, 22, 333, 4444, 55555, //\n"
11667                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11668                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11669   verifyFormat(
11670       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11671       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11672       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11673       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11674       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11675       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11676       "                 7777777};");
11677   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11678                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11679                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11680   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11681                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11682                "    // Separating comment.\n"
11683                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11684   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11685                "    // Leading comment\n"
11686                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11687                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11688   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11689                "                 1, 1, 1, 1};",
11690                getLLVMStyleWithColumns(39));
11691   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11692                "                 1, 1, 1, 1};",
11693                getLLVMStyleWithColumns(38));
11694   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11695                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11696                getLLVMStyleWithColumns(43));
11697   verifyFormat(
11698       "static unsigned SomeValues[10][3] = {\n"
11699       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11700       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11701   verifyFormat("static auto fields = new vector<string>{\n"
11702                "    \"aaaaaaaaaaaaa\",\n"
11703                "    \"aaaaaaaaaaaaa\",\n"
11704                "    \"aaaaaaaaaaaa\",\n"
11705                "    \"aaaaaaaaaaaaaa\",\n"
11706                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11707                "    \"aaaaaaaaaaaa\",\n"
11708                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11709                "};");
11710   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11711   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11712                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11713                "                 3, cccccccccccccccccccccc};",
11714                getLLVMStyleWithColumns(60));
11715 
11716   // Trailing commas.
11717   verifyFormat("vector<int> x = {\n"
11718                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11719                "};",
11720                getLLVMStyleWithColumns(39));
11721   verifyFormat("vector<int> x = {\n"
11722                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11723                "};",
11724                getLLVMStyleWithColumns(39));
11725   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11726                "                 1, 1, 1, 1,\n"
11727                "                 /**/ /**/};",
11728                getLLVMStyleWithColumns(39));
11729 
11730   // Trailing comment in the first line.
11731   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11732                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11733                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11734                "    11111111,   22222222,   333333333,   44444444};");
11735   // Trailing comment in the last line.
11736   verifyFormat("int aaaaa[] = {\n"
11737                "    1, 2, 3, // comment\n"
11738                "    4, 5, 6  // comment\n"
11739                "};");
11740 
11741   // With nested lists, we should either format one item per line or all nested
11742   // lists one on line.
11743   // FIXME: For some nested lists, we can do better.
11744   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11745                "        {aaaaaaaaaaaaaaaaaaa},\n"
11746                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11747                "        {aaaaaaaaaaaaaaaaa}};",
11748                getLLVMStyleWithColumns(60));
11749   verifyFormat(
11750       "SomeStruct my_struct_array = {\n"
11751       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11752       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11753       "    {aaa, aaa},\n"
11754       "    {aaa, aaa},\n"
11755       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11756       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11757       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11758 
11759   // No column layout should be used here.
11760   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11761                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11762 
11763   verifyNoCrash("a<,");
11764 
11765   // No braced initializer here.
11766   verifyFormat("void f() {\n"
11767                "  struct Dummy {};\n"
11768                "  f(v);\n"
11769                "}");
11770 
11771   // Long lists should be formatted in columns even if they are nested.
11772   verifyFormat(
11773       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11774       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11775       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11776       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11777       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11778       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11779 
11780   // Allow "single-column" layout even if that violates the column limit. There
11781   // isn't going to be a better way.
11782   verifyFormat("std::vector<int> a = {\n"
11783                "    aaaaaaaa,\n"
11784                "    aaaaaaaa,\n"
11785                "    aaaaaaaa,\n"
11786                "    aaaaaaaa,\n"
11787                "    aaaaaaaaaa,\n"
11788                "    aaaaaaaa,\n"
11789                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11790                getLLVMStyleWithColumns(30));
11791   verifyFormat("vector<int> aaaa = {\n"
11792                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11793                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11794                "    aaaaaa.aaaaaaa,\n"
11795                "    aaaaaa.aaaaaaa,\n"
11796                "    aaaaaa.aaaaaaa,\n"
11797                "    aaaaaa.aaaaaaa,\n"
11798                "};");
11799 
11800   // Don't create hanging lists.
11801   verifyFormat("someFunction(Param, {List1, List2,\n"
11802                "                     List3});",
11803                getLLVMStyleWithColumns(35));
11804   verifyFormat("someFunction(Param, Param,\n"
11805                "             {List1, List2,\n"
11806                "              List3});",
11807                getLLVMStyleWithColumns(35));
11808   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11809                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11810 }
11811 
11812 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11813   FormatStyle DoNotMerge = getLLVMStyle();
11814   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11815 
11816   verifyFormat("void f() { return 42; }");
11817   verifyFormat("void f() {\n"
11818                "  return 42;\n"
11819                "}",
11820                DoNotMerge);
11821   verifyFormat("void f() {\n"
11822                "  // Comment\n"
11823                "}");
11824   verifyFormat("{\n"
11825                "#error {\n"
11826                "  int a;\n"
11827                "}");
11828   verifyFormat("{\n"
11829                "  int a;\n"
11830                "#error {\n"
11831                "}");
11832   verifyFormat("void f() {} // comment");
11833   verifyFormat("void f() { int a; } // comment");
11834   verifyFormat("void f() {\n"
11835                "} // comment",
11836                DoNotMerge);
11837   verifyFormat("void f() {\n"
11838                "  int a;\n"
11839                "} // comment",
11840                DoNotMerge);
11841   verifyFormat("void f() {\n"
11842                "} // comment",
11843                getLLVMStyleWithColumns(15));
11844 
11845   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11846   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11847 
11848   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11849   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11850   verifyFormat("class C {\n"
11851                "  C()\n"
11852                "      : iiiiiiii(nullptr),\n"
11853                "        kkkkkkk(nullptr),\n"
11854                "        mmmmmmm(nullptr),\n"
11855                "        nnnnnnn(nullptr) {}\n"
11856                "};",
11857                getGoogleStyle());
11858 
11859   FormatStyle NoColumnLimit = getLLVMStyle();
11860   NoColumnLimit.ColumnLimit = 0;
11861   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11862   EXPECT_EQ("class C {\n"
11863             "  A() : b(0) {}\n"
11864             "};",
11865             format("class C{A():b(0){}};", NoColumnLimit));
11866   EXPECT_EQ("A()\n"
11867             "    : b(0) {\n"
11868             "}",
11869             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11870 
11871   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11872   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11873       FormatStyle::SFS_None;
11874   EXPECT_EQ("A()\n"
11875             "    : b(0) {\n"
11876             "}",
11877             format("A():b(0){}", DoNotMergeNoColumnLimit));
11878   EXPECT_EQ("A()\n"
11879             "    : b(0) {\n"
11880             "}",
11881             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11882 
11883   verifyFormat("#define A          \\\n"
11884                "  void f() {       \\\n"
11885                "    int i;         \\\n"
11886                "  }",
11887                getLLVMStyleWithColumns(20));
11888   verifyFormat("#define A           \\\n"
11889                "  void f() { int i; }",
11890                getLLVMStyleWithColumns(21));
11891   verifyFormat("#define A            \\\n"
11892                "  void f() {         \\\n"
11893                "    int i;           \\\n"
11894                "  }                  \\\n"
11895                "  int j;",
11896                getLLVMStyleWithColumns(22));
11897   verifyFormat("#define A             \\\n"
11898                "  void f() { int i; } \\\n"
11899                "  int j;",
11900                getLLVMStyleWithColumns(23));
11901 }
11902 
11903 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11904   FormatStyle MergeEmptyOnly = getLLVMStyle();
11905   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11906   verifyFormat("class C {\n"
11907                "  int f() {}\n"
11908                "};",
11909                MergeEmptyOnly);
11910   verifyFormat("class C {\n"
11911                "  int f() {\n"
11912                "    return 42;\n"
11913                "  }\n"
11914                "};",
11915                MergeEmptyOnly);
11916   verifyFormat("int f() {}", MergeEmptyOnly);
11917   verifyFormat("int f() {\n"
11918                "  return 42;\n"
11919                "}",
11920                MergeEmptyOnly);
11921 
11922   // Also verify behavior when BraceWrapping.AfterFunction = true
11923   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11924   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11925   verifyFormat("int f() {}", MergeEmptyOnly);
11926   verifyFormat("class C {\n"
11927                "  int f() {}\n"
11928                "};",
11929                MergeEmptyOnly);
11930 }
11931 
11932 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11933   FormatStyle MergeInlineOnly = getLLVMStyle();
11934   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11935   verifyFormat("class C {\n"
11936                "  int f() { return 42; }\n"
11937                "};",
11938                MergeInlineOnly);
11939   verifyFormat("int f() {\n"
11940                "  return 42;\n"
11941                "}",
11942                MergeInlineOnly);
11943 
11944   // SFS_Inline implies SFS_Empty
11945   verifyFormat("class C {\n"
11946                "  int f() {}\n"
11947                "};",
11948                MergeInlineOnly);
11949   verifyFormat("int f() {}", MergeInlineOnly);
11950 
11951   // Also verify behavior when BraceWrapping.AfterFunction = true
11952   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11953   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11954   verifyFormat("class C {\n"
11955                "  int f() { return 42; }\n"
11956                "};",
11957                MergeInlineOnly);
11958   verifyFormat("int f()\n"
11959                "{\n"
11960                "  return 42;\n"
11961                "}",
11962                MergeInlineOnly);
11963 
11964   // SFS_Inline implies SFS_Empty
11965   verifyFormat("int f() {}", MergeInlineOnly);
11966   verifyFormat("class C {\n"
11967                "  int f() {}\n"
11968                "};",
11969                MergeInlineOnly);
11970 }
11971 
11972 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11973   FormatStyle MergeInlineOnly = getLLVMStyle();
11974   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11975       FormatStyle::SFS_InlineOnly;
11976   verifyFormat("class C {\n"
11977                "  int f() { return 42; }\n"
11978                "};",
11979                MergeInlineOnly);
11980   verifyFormat("int f() {\n"
11981                "  return 42;\n"
11982                "}",
11983                MergeInlineOnly);
11984 
11985   // SFS_InlineOnly does not imply SFS_Empty
11986   verifyFormat("class C {\n"
11987                "  int f() {}\n"
11988                "};",
11989                MergeInlineOnly);
11990   verifyFormat("int f() {\n"
11991                "}",
11992                MergeInlineOnly);
11993 
11994   // Also verify behavior when BraceWrapping.AfterFunction = true
11995   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11996   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11997   verifyFormat("class C {\n"
11998                "  int f() { return 42; }\n"
11999                "};",
12000                MergeInlineOnly);
12001   verifyFormat("int f()\n"
12002                "{\n"
12003                "  return 42;\n"
12004                "}",
12005                MergeInlineOnly);
12006 
12007   // SFS_InlineOnly does not imply SFS_Empty
12008   verifyFormat("int f()\n"
12009                "{\n"
12010                "}",
12011                MergeInlineOnly);
12012   verifyFormat("class C {\n"
12013                "  int f() {}\n"
12014                "};",
12015                MergeInlineOnly);
12016 }
12017 
12018 TEST_F(FormatTest, SplitEmptyFunction) {
12019   FormatStyle Style = getLLVMStyle();
12020   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12021   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12022   Style.BraceWrapping.AfterFunction = true;
12023   Style.BraceWrapping.SplitEmptyFunction = false;
12024   Style.ColumnLimit = 40;
12025 
12026   verifyFormat("int f()\n"
12027                "{}",
12028                Style);
12029   verifyFormat("int f()\n"
12030                "{\n"
12031                "  return 42;\n"
12032                "}",
12033                Style);
12034   verifyFormat("int f()\n"
12035                "{\n"
12036                "  // some comment\n"
12037                "}",
12038                Style);
12039 
12040   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12041   verifyFormat("int f() {}", Style);
12042   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12043                "{}",
12044                Style);
12045   verifyFormat("int f()\n"
12046                "{\n"
12047                "  return 0;\n"
12048                "}",
12049                Style);
12050 
12051   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12052   verifyFormat("class Foo {\n"
12053                "  int f() {}\n"
12054                "};\n",
12055                Style);
12056   verifyFormat("class Foo {\n"
12057                "  int f() { return 0; }\n"
12058                "};\n",
12059                Style);
12060   verifyFormat("class Foo {\n"
12061                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12062                "  {}\n"
12063                "};\n",
12064                Style);
12065   verifyFormat("class Foo {\n"
12066                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12067                "  {\n"
12068                "    return 0;\n"
12069                "  }\n"
12070                "};\n",
12071                Style);
12072 
12073   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12074   verifyFormat("int f() {}", Style);
12075   verifyFormat("int f() { return 0; }", Style);
12076   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12077                "{}",
12078                Style);
12079   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12080                "{\n"
12081                "  return 0;\n"
12082                "}",
12083                Style);
12084 }
12085 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12086   FormatStyle Style = getLLVMStyle();
12087   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12088   verifyFormat("#ifdef A\n"
12089                "int f() {}\n"
12090                "#else\n"
12091                "int g() {}\n"
12092                "#endif",
12093                Style);
12094 }
12095 
12096 TEST_F(FormatTest, SplitEmptyClass) {
12097   FormatStyle Style = getLLVMStyle();
12098   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12099   Style.BraceWrapping.AfterClass = true;
12100   Style.BraceWrapping.SplitEmptyRecord = false;
12101 
12102   verifyFormat("class Foo\n"
12103                "{};",
12104                Style);
12105   verifyFormat("/* something */ class Foo\n"
12106                "{};",
12107                Style);
12108   verifyFormat("template <typename X> class Foo\n"
12109                "{};",
12110                Style);
12111   verifyFormat("class Foo\n"
12112                "{\n"
12113                "  Foo();\n"
12114                "};",
12115                Style);
12116   verifyFormat("typedef class Foo\n"
12117                "{\n"
12118                "} Foo_t;",
12119                Style);
12120 
12121   Style.BraceWrapping.SplitEmptyRecord = true;
12122   Style.BraceWrapping.AfterStruct = true;
12123   verifyFormat("class rep\n"
12124                "{\n"
12125                "};",
12126                Style);
12127   verifyFormat("struct rep\n"
12128                "{\n"
12129                "};",
12130                Style);
12131   verifyFormat("template <typename T> class rep\n"
12132                "{\n"
12133                "};",
12134                Style);
12135   verifyFormat("template <typename T> struct rep\n"
12136                "{\n"
12137                "};",
12138                Style);
12139   verifyFormat("class rep\n"
12140                "{\n"
12141                "  int x;\n"
12142                "};",
12143                Style);
12144   verifyFormat("struct rep\n"
12145                "{\n"
12146                "  int x;\n"
12147                "};",
12148                Style);
12149   verifyFormat("template <typename T> class rep\n"
12150                "{\n"
12151                "  int x;\n"
12152                "};",
12153                Style);
12154   verifyFormat("template <typename T> struct rep\n"
12155                "{\n"
12156                "  int x;\n"
12157                "};",
12158                Style);
12159   verifyFormat("template <typename T> class rep // Foo\n"
12160                "{\n"
12161                "  int x;\n"
12162                "};",
12163                Style);
12164   verifyFormat("template <typename T> struct rep // Bar\n"
12165                "{\n"
12166                "  int x;\n"
12167                "};",
12168                Style);
12169 
12170   verifyFormat("template <typename T> class rep<T>\n"
12171                "{\n"
12172                "  int x;\n"
12173                "};",
12174                Style);
12175 
12176   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12177                "{\n"
12178                "  int x;\n"
12179                "};",
12180                Style);
12181   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12182                "{\n"
12183                "};",
12184                Style);
12185 
12186   verifyFormat("#include \"stdint.h\"\n"
12187                "namespace rep {}",
12188                Style);
12189   verifyFormat("#include <stdint.h>\n"
12190                "namespace rep {}",
12191                Style);
12192   verifyFormat("#include <stdint.h>\n"
12193                "namespace rep {}",
12194                "#include <stdint.h>\n"
12195                "namespace rep {\n"
12196                "\n"
12197                "\n"
12198                "}",
12199                Style);
12200 }
12201 
12202 TEST_F(FormatTest, SplitEmptyStruct) {
12203   FormatStyle Style = getLLVMStyle();
12204   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12205   Style.BraceWrapping.AfterStruct = true;
12206   Style.BraceWrapping.SplitEmptyRecord = false;
12207 
12208   verifyFormat("struct Foo\n"
12209                "{};",
12210                Style);
12211   verifyFormat("/* something */ struct Foo\n"
12212                "{};",
12213                Style);
12214   verifyFormat("template <typename X> struct Foo\n"
12215                "{};",
12216                Style);
12217   verifyFormat("struct Foo\n"
12218                "{\n"
12219                "  Foo();\n"
12220                "};",
12221                Style);
12222   verifyFormat("typedef struct Foo\n"
12223                "{\n"
12224                "} Foo_t;",
12225                Style);
12226   // typedef struct Bar {} Bar_t;
12227 }
12228 
12229 TEST_F(FormatTest, SplitEmptyUnion) {
12230   FormatStyle Style = getLLVMStyle();
12231   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12232   Style.BraceWrapping.AfterUnion = true;
12233   Style.BraceWrapping.SplitEmptyRecord = false;
12234 
12235   verifyFormat("union Foo\n"
12236                "{};",
12237                Style);
12238   verifyFormat("/* something */ union Foo\n"
12239                "{};",
12240                Style);
12241   verifyFormat("union Foo\n"
12242                "{\n"
12243                "  A,\n"
12244                "};",
12245                Style);
12246   verifyFormat("typedef union Foo\n"
12247                "{\n"
12248                "} Foo_t;",
12249                Style);
12250 }
12251 
12252 TEST_F(FormatTest, SplitEmptyNamespace) {
12253   FormatStyle Style = getLLVMStyle();
12254   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12255   Style.BraceWrapping.AfterNamespace = true;
12256   Style.BraceWrapping.SplitEmptyNamespace = false;
12257 
12258   verifyFormat("namespace Foo\n"
12259                "{};",
12260                Style);
12261   verifyFormat("/* something */ namespace Foo\n"
12262                "{};",
12263                Style);
12264   verifyFormat("inline namespace Foo\n"
12265                "{};",
12266                Style);
12267   verifyFormat("/* something */ inline namespace Foo\n"
12268                "{};",
12269                Style);
12270   verifyFormat("export namespace Foo\n"
12271                "{};",
12272                Style);
12273   verifyFormat("namespace Foo\n"
12274                "{\n"
12275                "void Bar();\n"
12276                "};",
12277                Style);
12278 }
12279 
12280 TEST_F(FormatTest, NeverMergeShortRecords) {
12281   FormatStyle Style = getLLVMStyle();
12282 
12283   verifyFormat("class Foo {\n"
12284                "  Foo();\n"
12285                "};",
12286                Style);
12287   verifyFormat("typedef class Foo {\n"
12288                "  Foo();\n"
12289                "} Foo_t;",
12290                Style);
12291   verifyFormat("struct Foo {\n"
12292                "  Foo();\n"
12293                "};",
12294                Style);
12295   verifyFormat("typedef struct Foo {\n"
12296                "  Foo();\n"
12297                "} Foo_t;",
12298                Style);
12299   verifyFormat("union Foo {\n"
12300                "  A,\n"
12301                "};",
12302                Style);
12303   verifyFormat("typedef union Foo {\n"
12304                "  A,\n"
12305                "} Foo_t;",
12306                Style);
12307   verifyFormat("namespace Foo {\n"
12308                "void Bar();\n"
12309                "};",
12310                Style);
12311 
12312   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12313   Style.BraceWrapping.AfterClass = true;
12314   Style.BraceWrapping.AfterStruct = true;
12315   Style.BraceWrapping.AfterUnion = true;
12316   Style.BraceWrapping.AfterNamespace = true;
12317   verifyFormat("class Foo\n"
12318                "{\n"
12319                "  Foo();\n"
12320                "};",
12321                Style);
12322   verifyFormat("typedef class Foo\n"
12323                "{\n"
12324                "  Foo();\n"
12325                "} Foo_t;",
12326                Style);
12327   verifyFormat("struct Foo\n"
12328                "{\n"
12329                "  Foo();\n"
12330                "};",
12331                Style);
12332   verifyFormat("typedef struct Foo\n"
12333                "{\n"
12334                "  Foo();\n"
12335                "} Foo_t;",
12336                Style);
12337   verifyFormat("union Foo\n"
12338                "{\n"
12339                "  A,\n"
12340                "};",
12341                Style);
12342   verifyFormat("typedef union Foo\n"
12343                "{\n"
12344                "  A,\n"
12345                "} Foo_t;",
12346                Style);
12347   verifyFormat("namespace Foo\n"
12348                "{\n"
12349                "void Bar();\n"
12350                "};",
12351                Style);
12352 }
12353 
12354 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12355   // Elaborate type variable declarations.
12356   verifyFormat("struct foo a = {bar};\nint n;");
12357   verifyFormat("class foo a = {bar};\nint n;");
12358   verifyFormat("union foo a = {bar};\nint n;");
12359 
12360   // Elaborate types inside function definitions.
12361   verifyFormat("struct foo f() {}\nint n;");
12362   verifyFormat("class foo f() {}\nint n;");
12363   verifyFormat("union foo f() {}\nint n;");
12364 
12365   // Templates.
12366   verifyFormat("template <class X> void f() {}\nint n;");
12367   verifyFormat("template <struct X> void f() {}\nint n;");
12368   verifyFormat("template <union X> void f() {}\nint n;");
12369 
12370   // Actual definitions...
12371   verifyFormat("struct {\n} n;");
12372   verifyFormat(
12373       "template <template <class T, class Y>, class Z> class X {\n} n;");
12374   verifyFormat("union Z {\n  int n;\n} x;");
12375   verifyFormat("class MACRO Z {\n} n;");
12376   verifyFormat("class MACRO(X) Z {\n} n;");
12377   verifyFormat("class __attribute__(X) Z {\n} n;");
12378   verifyFormat("class __declspec(X) Z {\n} n;");
12379   verifyFormat("class A##B##C {\n} n;");
12380   verifyFormat("class alignas(16) Z {\n} n;");
12381   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12382   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12383 
12384   // Redefinition from nested context:
12385   verifyFormat("class A::B::C {\n} n;");
12386 
12387   // Template definitions.
12388   verifyFormat(
12389       "template <typename F>\n"
12390       "Matcher(const Matcher<F> &Other,\n"
12391       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12392       "                             !is_same<F, T>::value>::type * = 0)\n"
12393       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12394 
12395   // FIXME: This is still incorrectly handled at the formatter side.
12396   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12397   verifyFormat("int i = SomeFunction(a<b, a> b);");
12398 
12399   // FIXME:
12400   // This now gets parsed incorrectly as class definition.
12401   // verifyFormat("class A<int> f() {\n}\nint n;");
12402 
12403   // Elaborate types where incorrectly parsing the structural element would
12404   // break the indent.
12405   verifyFormat("if (true)\n"
12406                "  class X x;\n"
12407                "else\n"
12408                "  f();\n");
12409 
12410   // This is simply incomplete. Formatting is not important, but must not crash.
12411   verifyFormat("class A:");
12412 }
12413 
12414 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12415   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12416             format("#error Leave     all         white!!!!! space* alone!\n"));
12417   EXPECT_EQ(
12418       "#warning Leave     all         white!!!!! space* alone!\n",
12419       format("#warning Leave     all         white!!!!! space* alone!\n"));
12420   EXPECT_EQ("#error 1", format("  #  error   1"));
12421   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12422 }
12423 
12424 TEST_F(FormatTest, FormatHashIfExpressions) {
12425   verifyFormat("#if AAAA && BBBB");
12426   verifyFormat("#if (AAAA && BBBB)");
12427   verifyFormat("#elif (AAAA && BBBB)");
12428   // FIXME: Come up with a better indentation for #elif.
12429   verifyFormat(
12430       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12431       "    defined(BBBBBBBB)\n"
12432       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12433       "    defined(BBBBBBBB)\n"
12434       "#endif",
12435       getLLVMStyleWithColumns(65));
12436 }
12437 
12438 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12439   FormatStyle AllowsMergedIf = getGoogleStyle();
12440   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12441       FormatStyle::SIS_WithoutElse;
12442   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12443   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12444   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12445   EXPECT_EQ("if (true) return 42;",
12446             format("if (true)\nreturn 42;", AllowsMergedIf));
12447   FormatStyle ShortMergedIf = AllowsMergedIf;
12448   ShortMergedIf.ColumnLimit = 25;
12449   verifyFormat("#define A \\\n"
12450                "  if (true) return 42;",
12451                ShortMergedIf);
12452   verifyFormat("#define A \\\n"
12453                "  f();    \\\n"
12454                "  if (true)\n"
12455                "#define B",
12456                ShortMergedIf);
12457   verifyFormat("#define A \\\n"
12458                "  f();    \\\n"
12459                "  if (true)\n"
12460                "g();",
12461                ShortMergedIf);
12462   verifyFormat("{\n"
12463                "#ifdef A\n"
12464                "  // Comment\n"
12465                "  if (true) continue;\n"
12466                "#endif\n"
12467                "  // Comment\n"
12468                "  if (true) continue;\n"
12469                "}",
12470                ShortMergedIf);
12471   ShortMergedIf.ColumnLimit = 33;
12472   verifyFormat("#define A \\\n"
12473                "  if constexpr (true) return 42;",
12474                ShortMergedIf);
12475   verifyFormat("#define A \\\n"
12476                "  if CONSTEXPR (true) return 42;",
12477                ShortMergedIf);
12478   ShortMergedIf.ColumnLimit = 29;
12479   verifyFormat("#define A                   \\\n"
12480                "  if (aaaaaaaaaa) return 1; \\\n"
12481                "  return 2;",
12482                ShortMergedIf);
12483   ShortMergedIf.ColumnLimit = 28;
12484   verifyFormat("#define A         \\\n"
12485                "  if (aaaaaaaaaa) \\\n"
12486                "    return 1;     \\\n"
12487                "  return 2;",
12488                ShortMergedIf);
12489   verifyFormat("#define A                \\\n"
12490                "  if constexpr (aaaaaaa) \\\n"
12491                "    return 1;            \\\n"
12492                "  return 2;",
12493                ShortMergedIf);
12494   verifyFormat("#define A                \\\n"
12495                "  if CONSTEXPR (aaaaaaa) \\\n"
12496                "    return 1;            \\\n"
12497                "  return 2;",
12498                ShortMergedIf);
12499 }
12500 
12501 TEST_F(FormatTest, FormatStarDependingOnContext) {
12502   verifyFormat("void f(int *a);");
12503   verifyFormat("void f() { f(fint * b); }");
12504   verifyFormat("class A {\n  void f(int *a);\n};");
12505   verifyFormat("class A {\n  int *a;\n};");
12506   verifyFormat("namespace a {\n"
12507                "namespace b {\n"
12508                "class A {\n"
12509                "  void f() {}\n"
12510                "  int *a;\n"
12511                "};\n"
12512                "} // namespace b\n"
12513                "} // namespace a");
12514 }
12515 
12516 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12517   verifyFormat("while");
12518   verifyFormat("operator");
12519 }
12520 
12521 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12522   // This code would be painfully slow to format if we didn't skip it.
12523   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
12524                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12525                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12526                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12527                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12528                    "A(1, 1)\n"
12529                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12530                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12531                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12532                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12533                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12534                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12535                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12536                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12537                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12538                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12539   // Deeply nested part is untouched, rest is formatted.
12540   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12541             format(std::string("int    i;\n") + Code + "int    j;\n",
12542                    getLLVMStyle(), SC_ExpectIncomplete));
12543 }
12544 
12545 //===----------------------------------------------------------------------===//
12546 // Objective-C tests.
12547 //===----------------------------------------------------------------------===//
12548 
12549 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12550   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12551   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12552             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12553   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12554   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12555   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12556             format("-(NSInteger)Method3:(id)anObject;"));
12557   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12558             format("-(NSInteger)Method4:(id)anObject;"));
12559   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12560             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12561   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12562             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12563   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12564             "forAllCells:(BOOL)flag;",
12565             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12566                    "forAllCells:(BOOL)flag;"));
12567 
12568   // Very long objectiveC method declaration.
12569   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12570                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12571   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12572                "                    inRange:(NSRange)range\n"
12573                "                   outRange:(NSRange)out_range\n"
12574                "                  outRange1:(NSRange)out_range1\n"
12575                "                  outRange2:(NSRange)out_range2\n"
12576                "                  outRange3:(NSRange)out_range3\n"
12577                "                  outRange4:(NSRange)out_range4\n"
12578                "                  outRange5:(NSRange)out_range5\n"
12579                "                  outRange6:(NSRange)out_range6\n"
12580                "                  outRange7:(NSRange)out_range7\n"
12581                "                  outRange8:(NSRange)out_range8\n"
12582                "                  outRange9:(NSRange)out_range9;");
12583 
12584   // When the function name has to be wrapped.
12585   FormatStyle Style = getLLVMStyle();
12586   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12587   // and always indents instead.
12588   Style.IndentWrappedFunctionNames = false;
12589   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12590                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12591                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12592                "}",
12593                Style);
12594   Style.IndentWrappedFunctionNames = true;
12595   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12596                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12597                "               anotherName:(NSString)dddddddddddddd {\n"
12598                "}",
12599                Style);
12600 
12601   verifyFormat("- (int)sum:(vector<int>)numbers;");
12602   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12603   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12604   // protocol lists (but not for template classes):
12605   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12606 
12607   verifyFormat("- (int (*)())foo:(int (*)())f;");
12608   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12609 
12610   // If there's no return type (very rare in practice!), LLVM and Google style
12611   // agree.
12612   verifyFormat("- foo;");
12613   verifyFormat("- foo:(int)f;");
12614   verifyGoogleFormat("- foo:(int)foo;");
12615 }
12616 
12617 TEST_F(FormatTest, BreaksStringLiterals) {
12618   EXPECT_EQ("\"some text \"\n"
12619             "\"other\";",
12620             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12621   EXPECT_EQ("\"some text \"\n"
12622             "\"other\";",
12623             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12624   EXPECT_EQ(
12625       "#define A  \\\n"
12626       "  \"some \"  \\\n"
12627       "  \"text \"  \\\n"
12628       "  \"other\";",
12629       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12630   EXPECT_EQ(
12631       "#define A  \\\n"
12632       "  \"so \"    \\\n"
12633       "  \"text \"  \\\n"
12634       "  \"other\";",
12635       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12636 
12637   EXPECT_EQ("\"some text\"",
12638             format("\"some text\"", getLLVMStyleWithColumns(1)));
12639   EXPECT_EQ("\"some text\"",
12640             format("\"some text\"", getLLVMStyleWithColumns(11)));
12641   EXPECT_EQ("\"some \"\n"
12642             "\"text\"",
12643             format("\"some text\"", getLLVMStyleWithColumns(10)));
12644   EXPECT_EQ("\"some \"\n"
12645             "\"text\"",
12646             format("\"some text\"", getLLVMStyleWithColumns(7)));
12647   EXPECT_EQ("\"some\"\n"
12648             "\" tex\"\n"
12649             "\"t\"",
12650             format("\"some text\"", getLLVMStyleWithColumns(6)));
12651   EXPECT_EQ("\"some\"\n"
12652             "\" tex\"\n"
12653             "\" and\"",
12654             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12655   EXPECT_EQ("\"some\"\n"
12656             "\"/tex\"\n"
12657             "\"/and\"",
12658             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12659 
12660   EXPECT_EQ("variable =\n"
12661             "    \"long string \"\n"
12662             "    \"literal\";",
12663             format("variable = \"long string literal\";",
12664                    getLLVMStyleWithColumns(20)));
12665 
12666   EXPECT_EQ("variable = f(\n"
12667             "    \"long string \"\n"
12668             "    \"literal\",\n"
12669             "    short,\n"
12670             "    loooooooooooooooooooong);",
12671             format("variable = f(\"long string literal\", short, "
12672                    "loooooooooooooooooooong);",
12673                    getLLVMStyleWithColumns(20)));
12674 
12675   EXPECT_EQ(
12676       "f(g(\"long string \"\n"
12677       "    \"literal\"),\n"
12678       "  b);",
12679       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12680   EXPECT_EQ("f(g(\"long string \"\n"
12681             "    \"literal\",\n"
12682             "    a),\n"
12683             "  b);",
12684             format("f(g(\"long string literal\", a), b);",
12685                    getLLVMStyleWithColumns(20)));
12686   EXPECT_EQ(
12687       "f(\"one two\".split(\n"
12688       "    variable));",
12689       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12690   EXPECT_EQ("f(\"one two three four five six \"\n"
12691             "  \"seven\".split(\n"
12692             "      really_looooong_variable));",
12693             format("f(\"one two three four five six seven\"."
12694                    "split(really_looooong_variable));",
12695                    getLLVMStyleWithColumns(33)));
12696 
12697   EXPECT_EQ("f(\"some \"\n"
12698             "  \"text\",\n"
12699             "  other);",
12700             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12701 
12702   // Only break as a last resort.
12703   verifyFormat(
12704       "aaaaaaaaaaaaaaaaaaaa(\n"
12705       "    aaaaaaaaaaaaaaaaaaaa,\n"
12706       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12707 
12708   EXPECT_EQ("\"splitmea\"\n"
12709             "\"trandomp\"\n"
12710             "\"oint\"",
12711             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12712 
12713   EXPECT_EQ("\"split/\"\n"
12714             "\"pathat/\"\n"
12715             "\"slashes\"",
12716             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12717 
12718   EXPECT_EQ("\"split/\"\n"
12719             "\"pathat/\"\n"
12720             "\"slashes\"",
12721             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12722   EXPECT_EQ("\"split at \"\n"
12723             "\"spaces/at/\"\n"
12724             "\"slashes.at.any$\"\n"
12725             "\"non-alphanumeric%\"\n"
12726             "\"1111111111characte\"\n"
12727             "\"rs\"",
12728             format("\"split at "
12729                    "spaces/at/"
12730                    "slashes.at."
12731                    "any$non-"
12732                    "alphanumeric%"
12733                    "1111111111characte"
12734                    "rs\"",
12735                    getLLVMStyleWithColumns(20)));
12736 
12737   // Verify that splitting the strings understands
12738   // Style::AlwaysBreakBeforeMultilineStrings.
12739   EXPECT_EQ("aaaaaaaaaaaa(\n"
12740             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12741             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12742             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12743                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12744                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12745                    getGoogleStyle()));
12746   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12747             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12748             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12749                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12750                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12751                    getGoogleStyle()));
12752   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12753             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12754             format("llvm::outs() << "
12755                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12756                    "aaaaaaaaaaaaaaaaaaa\";"));
12757   EXPECT_EQ("ffff(\n"
12758             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12759             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12760             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12761                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12762                    getGoogleStyle()));
12763 
12764   FormatStyle Style = getLLVMStyleWithColumns(12);
12765   Style.BreakStringLiterals = false;
12766   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12767 
12768   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12769   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12770   EXPECT_EQ("#define A \\\n"
12771             "  \"some \" \\\n"
12772             "  \"text \" \\\n"
12773             "  \"other\";",
12774             format("#define A \"some text other\";", AlignLeft));
12775 }
12776 
12777 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12778   EXPECT_EQ("C a = \"some more \"\n"
12779             "      \"text\";",
12780             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12781 }
12782 
12783 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12784   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12785   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12786   EXPECT_EQ("int i = a(b());",
12787             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12788 }
12789 
12790 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12791   EXPECT_EQ(
12792       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12793       "(\n"
12794       "    \"x\t\");",
12795       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12796              "aaaaaaa("
12797              "\"x\t\");"));
12798 }
12799 
12800 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12801   EXPECT_EQ(
12802       "u8\"utf8 string \"\n"
12803       "u8\"literal\";",
12804       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12805   EXPECT_EQ(
12806       "u\"utf16 string \"\n"
12807       "u\"literal\";",
12808       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12809   EXPECT_EQ(
12810       "U\"utf32 string \"\n"
12811       "U\"literal\";",
12812       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12813   EXPECT_EQ("L\"wide string \"\n"
12814             "L\"literal\";",
12815             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12816   EXPECT_EQ("@\"NSString \"\n"
12817             "@\"literal\";",
12818             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12819   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12820 
12821   // This input makes clang-format try to split the incomplete unicode escape
12822   // sequence, which used to lead to a crasher.
12823   verifyNoCrash(
12824       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12825       getLLVMStyleWithColumns(60));
12826 }
12827 
12828 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12829   FormatStyle Style = getGoogleStyleWithColumns(15);
12830   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12831   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12832   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12833   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12834   EXPECT_EQ("u8R\"x(raw literal)x\";",
12835             format("u8R\"x(raw literal)x\";", Style));
12836 }
12837 
12838 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12839   FormatStyle Style = getLLVMStyleWithColumns(20);
12840   EXPECT_EQ(
12841       "_T(\"aaaaaaaaaaaaaa\")\n"
12842       "_T(\"aaaaaaaaaaaaaa\")\n"
12843       "_T(\"aaaaaaaaaaaa\")",
12844       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12845   EXPECT_EQ("f(x,\n"
12846             "  _T(\"aaaaaaaaaaaa\")\n"
12847             "  _T(\"aaa\"),\n"
12848             "  z);",
12849             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12850 
12851   // FIXME: Handle embedded spaces in one iteration.
12852   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12853   //            "_T(\"aaaaaaaaaaaaa\")\n"
12854   //            "_T(\"aaaaaaaaaaaaa\")\n"
12855   //            "_T(\"a\")",
12856   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12857   //                   getLLVMStyleWithColumns(20)));
12858   EXPECT_EQ(
12859       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12860       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12861   EXPECT_EQ("f(\n"
12862             "#if !TEST\n"
12863             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12864             "#endif\n"
12865             ");",
12866             format("f(\n"
12867                    "#if !TEST\n"
12868                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12869                    "#endif\n"
12870                    ");"));
12871   EXPECT_EQ("f(\n"
12872             "\n"
12873             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12874             format("f(\n"
12875                    "\n"
12876                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12877 }
12878 
12879 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12880   // In a function call with two operands, the second can be broken with no line
12881   // break before it.
12882   EXPECT_EQ(
12883       "func(a, \"long long \"\n"
12884       "        \"long long\");",
12885       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12886   // In a function call with three operands, the second must be broken with a
12887   // line break before it.
12888   EXPECT_EQ("func(a,\n"
12889             "     \"long long long \"\n"
12890             "     \"long\",\n"
12891             "     c);",
12892             format("func(a, \"long long long long\", c);",
12893                    getLLVMStyleWithColumns(24)));
12894   // In a function call with three operands, the third must be broken with a
12895   // line break before it.
12896   EXPECT_EQ("func(a, b,\n"
12897             "     \"long long long \"\n"
12898             "     \"long\");",
12899             format("func(a, b, \"long long long long\");",
12900                    getLLVMStyleWithColumns(24)));
12901   // In a function call with three operands, both the second and the third must
12902   // be broken with a line break before them.
12903   EXPECT_EQ("func(a,\n"
12904             "     \"long long long \"\n"
12905             "     \"long\",\n"
12906             "     \"long long long \"\n"
12907             "     \"long\");",
12908             format("func(a, \"long long long long\", \"long long long long\");",
12909                    getLLVMStyleWithColumns(24)));
12910   // In a chain of << with two operands, the second can be broken with no line
12911   // break before it.
12912   EXPECT_EQ("a << \"line line \"\n"
12913             "     \"line\";",
12914             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12915   // In a chain of << with three operands, the second can be broken with no line
12916   // break before it.
12917   EXPECT_EQ(
12918       "abcde << \"line \"\n"
12919       "         \"line line\"\n"
12920       "      << c;",
12921       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12922   // In a chain of << with three operands, the third must be broken with a line
12923   // break before it.
12924   EXPECT_EQ(
12925       "a << b\n"
12926       "  << \"line line \"\n"
12927       "     \"line\";",
12928       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12929   // In a chain of << with three operands, the second can be broken with no line
12930   // break before it and the third must be broken with a line break before it.
12931   EXPECT_EQ("abcd << \"line line \"\n"
12932             "        \"line\"\n"
12933             "     << \"line line \"\n"
12934             "        \"line\";",
12935             format("abcd << \"line line line\" << \"line line line\";",
12936                    getLLVMStyleWithColumns(20)));
12937   // In a chain of binary operators with two operands, the second can be broken
12938   // with no line break before it.
12939   EXPECT_EQ(
12940       "abcd + \"line line \"\n"
12941       "       \"line line\";",
12942       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12943   // In a chain of binary operators with three operands, the second must be
12944   // broken with a line break before it.
12945   EXPECT_EQ("abcd +\n"
12946             "    \"line line \"\n"
12947             "    \"line line\" +\n"
12948             "    e;",
12949             format("abcd + \"line line line line\" + e;",
12950                    getLLVMStyleWithColumns(20)));
12951   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12952   // the first must be broken with a line break before it.
12953   FormatStyle Style = getLLVMStyleWithColumns(25);
12954   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12955   EXPECT_EQ("someFunction(\n"
12956             "    \"long long long \"\n"
12957             "    \"long\",\n"
12958             "    a);",
12959             format("someFunction(\"long long long long\", a);", Style));
12960 }
12961 
12962 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12963   EXPECT_EQ(
12964       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12965       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12966       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12967       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12968              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12969              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12970 }
12971 
12972 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12973   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12974             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12975   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12976             "multiline raw string literal xxxxxxxxxxxxxx\n"
12977             ")x\",\n"
12978             "              a),\n"
12979             "            b);",
12980             format("fffffffffff(g(R\"x(\n"
12981                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12982                    ")x\", a), b);",
12983                    getGoogleStyleWithColumns(20)));
12984   EXPECT_EQ("fffffffffff(\n"
12985             "    g(R\"x(qqq\n"
12986             "multiline raw string literal xxxxxxxxxxxxxx\n"
12987             ")x\",\n"
12988             "      a),\n"
12989             "    b);",
12990             format("fffffffffff(g(R\"x(qqq\n"
12991                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12992                    ")x\", a), b);",
12993                    getGoogleStyleWithColumns(20)));
12994 
12995   EXPECT_EQ("fffffffffff(R\"x(\n"
12996             "multiline raw string literal xxxxxxxxxxxxxx\n"
12997             ")x\");",
12998             format("fffffffffff(R\"x(\n"
12999                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13000                    ")x\");",
13001                    getGoogleStyleWithColumns(20)));
13002   EXPECT_EQ("fffffffffff(R\"x(\n"
13003             "multiline raw string literal xxxxxxxxxxxxxx\n"
13004             ")x\" + bbbbbb);",
13005             format("fffffffffff(R\"x(\n"
13006                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13007                    ")x\" +   bbbbbb);",
13008                    getGoogleStyleWithColumns(20)));
13009   EXPECT_EQ("fffffffffff(\n"
13010             "    R\"x(\n"
13011             "multiline raw string literal xxxxxxxxxxxxxx\n"
13012             ")x\" +\n"
13013             "    bbbbbb);",
13014             format("fffffffffff(\n"
13015                    " R\"x(\n"
13016                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13017                    ")x\" + bbbbbb);",
13018                    getGoogleStyleWithColumns(20)));
13019   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13020             format("fffffffffff(\n"
13021                    " R\"(single line raw string)\" + bbbbbb);"));
13022 }
13023 
13024 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13025   verifyFormat("string a = \"unterminated;");
13026   EXPECT_EQ("function(\"unterminated,\n"
13027             "         OtherParameter);",
13028             format("function(  \"unterminated,\n"
13029                    "    OtherParameter);"));
13030 }
13031 
13032 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13033   FormatStyle Style = getLLVMStyle();
13034   Style.Standard = FormatStyle::LS_Cpp03;
13035   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13036             format("#define x(_a) printf(\"foo\"_a);", Style));
13037 }
13038 
13039 TEST_F(FormatTest, CppLexVersion) {
13040   FormatStyle Style = getLLVMStyle();
13041   // Formatting of x * y differs if x is a type.
13042   verifyFormat("void foo() { MACRO(a * b); }", Style);
13043   verifyFormat("void foo() { MACRO(int *b); }", Style);
13044 
13045   // LLVM style uses latest lexer.
13046   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13047   Style.Standard = FormatStyle::LS_Cpp17;
13048   // But in c++17, char8_t isn't a keyword.
13049   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13050 }
13051 
13052 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13053 
13054 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13055   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13056             "             \"ddeeefff\");",
13057             format("someFunction(\"aaabbbcccdddeeefff\");",
13058                    getLLVMStyleWithColumns(25)));
13059   EXPECT_EQ("someFunction1234567890(\n"
13060             "    \"aaabbbcccdddeeefff\");",
13061             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13062                    getLLVMStyleWithColumns(26)));
13063   EXPECT_EQ("someFunction1234567890(\n"
13064             "    \"aaabbbcccdddeeeff\"\n"
13065             "    \"f\");",
13066             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13067                    getLLVMStyleWithColumns(25)));
13068   EXPECT_EQ("someFunction1234567890(\n"
13069             "    \"aaabbbcccdddeeeff\"\n"
13070             "    \"f\");",
13071             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13072                    getLLVMStyleWithColumns(24)));
13073   EXPECT_EQ("someFunction(\n"
13074             "    \"aaabbbcc ddde \"\n"
13075             "    \"efff\");",
13076             format("someFunction(\"aaabbbcc ddde efff\");",
13077                    getLLVMStyleWithColumns(25)));
13078   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13079             "             \"ddeeefff\");",
13080             format("someFunction(\"aaabbbccc ddeeefff\");",
13081                    getLLVMStyleWithColumns(25)));
13082   EXPECT_EQ("someFunction1234567890(\n"
13083             "    \"aaabb \"\n"
13084             "    \"cccdddeeefff\");",
13085             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13086                    getLLVMStyleWithColumns(25)));
13087   EXPECT_EQ("#define A          \\\n"
13088             "  string s =       \\\n"
13089             "      \"123456789\"  \\\n"
13090             "      \"0\";         \\\n"
13091             "  int i;",
13092             format("#define A string s = \"1234567890\"; int i;",
13093                    getLLVMStyleWithColumns(20)));
13094   EXPECT_EQ("someFunction(\n"
13095             "    \"aaabbbcc \"\n"
13096             "    \"dddeeefff\");",
13097             format("someFunction(\"aaabbbcc dddeeefff\");",
13098                    getLLVMStyleWithColumns(25)));
13099 }
13100 
13101 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13102   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13103   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13104   EXPECT_EQ("\"test\"\n"
13105             "\"\\n\"",
13106             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13107   EXPECT_EQ("\"tes\\\\\"\n"
13108             "\"n\"",
13109             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13110   EXPECT_EQ("\"\\\\\\\\\"\n"
13111             "\"\\n\"",
13112             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13113   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13114   EXPECT_EQ("\"\\uff01\"\n"
13115             "\"test\"",
13116             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13117   EXPECT_EQ("\"\\Uff01ff02\"",
13118             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13119   EXPECT_EQ("\"\\x000000000001\"\n"
13120             "\"next\"",
13121             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13122   EXPECT_EQ("\"\\x000000000001next\"",
13123             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13124   EXPECT_EQ("\"\\x000000000001\"",
13125             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13126   EXPECT_EQ("\"test\"\n"
13127             "\"\\000000\"\n"
13128             "\"000001\"",
13129             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13130   EXPECT_EQ("\"test\\000\"\n"
13131             "\"00000000\"\n"
13132             "\"1\"",
13133             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13134 }
13135 
13136 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13137   verifyFormat("void f() {\n"
13138                "  return g() {}\n"
13139                "  void h() {}");
13140   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13141                "g();\n"
13142                "}");
13143 }
13144 
13145 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13146   verifyFormat(
13147       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13148 }
13149 
13150 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13151   verifyFormat("class X {\n"
13152                "  void f() {\n"
13153                "  }\n"
13154                "};",
13155                getLLVMStyleWithColumns(12));
13156 }
13157 
13158 TEST_F(FormatTest, ConfigurableIndentWidth) {
13159   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13160   EightIndent.IndentWidth = 8;
13161   EightIndent.ContinuationIndentWidth = 8;
13162   verifyFormat("void f() {\n"
13163                "        someFunction();\n"
13164                "        if (true) {\n"
13165                "                f();\n"
13166                "        }\n"
13167                "}",
13168                EightIndent);
13169   verifyFormat("class X {\n"
13170                "        void f() {\n"
13171                "        }\n"
13172                "};",
13173                EightIndent);
13174   verifyFormat("int x[] = {\n"
13175                "        call(),\n"
13176                "        call()};",
13177                EightIndent);
13178 }
13179 
13180 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13181   verifyFormat("double\n"
13182                "f();",
13183                getLLVMStyleWithColumns(8));
13184 }
13185 
13186 TEST_F(FormatTest, ConfigurableUseOfTab) {
13187   FormatStyle Tab = getLLVMStyleWithColumns(42);
13188   Tab.IndentWidth = 8;
13189   Tab.UseTab = FormatStyle::UT_Always;
13190   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13191 
13192   EXPECT_EQ("if (aaaaaaaa && // q\n"
13193             "    bb)\t\t// w\n"
13194             "\t;",
13195             format("if (aaaaaaaa &&// q\n"
13196                    "bb)// w\n"
13197                    ";",
13198                    Tab));
13199   EXPECT_EQ("if (aaa && bbb) // w\n"
13200             "\t;",
13201             format("if(aaa&&bbb)// w\n"
13202                    ";",
13203                    Tab));
13204 
13205   verifyFormat("class X {\n"
13206                "\tvoid f() {\n"
13207                "\t\tsomeFunction(parameter1,\n"
13208                "\t\t\t     parameter2);\n"
13209                "\t}\n"
13210                "};",
13211                Tab);
13212   verifyFormat("#define A                        \\\n"
13213                "\tvoid f() {               \\\n"
13214                "\t\tsomeFunction(    \\\n"
13215                "\t\t    parameter1,  \\\n"
13216                "\t\t    parameter2); \\\n"
13217                "\t}",
13218                Tab);
13219   verifyFormat("int a;\t      // x\n"
13220                "int bbbbbbbb; // x\n",
13221                Tab);
13222 
13223   Tab.TabWidth = 4;
13224   Tab.IndentWidth = 8;
13225   verifyFormat("class TabWidth4Indent8 {\n"
13226                "\t\tvoid f() {\n"
13227                "\t\t\t\tsomeFunction(parameter1,\n"
13228                "\t\t\t\t\t\t\t parameter2);\n"
13229                "\t\t}\n"
13230                "};",
13231                Tab);
13232 
13233   Tab.TabWidth = 4;
13234   Tab.IndentWidth = 4;
13235   verifyFormat("class TabWidth4Indent4 {\n"
13236                "\tvoid f() {\n"
13237                "\t\tsomeFunction(parameter1,\n"
13238                "\t\t\t\t\t parameter2);\n"
13239                "\t}\n"
13240                "};",
13241                Tab);
13242 
13243   Tab.TabWidth = 8;
13244   Tab.IndentWidth = 4;
13245   verifyFormat("class TabWidth8Indent4 {\n"
13246                "    void f() {\n"
13247                "\tsomeFunction(parameter1,\n"
13248                "\t\t     parameter2);\n"
13249                "    }\n"
13250                "};",
13251                Tab);
13252 
13253   Tab.TabWidth = 8;
13254   Tab.IndentWidth = 8;
13255   EXPECT_EQ("/*\n"
13256             "\t      a\t\tcomment\n"
13257             "\t      in multiple lines\n"
13258             "       */",
13259             format("   /*\t \t \n"
13260                    " \t \t a\t\tcomment\t \t\n"
13261                    " \t \t in multiple lines\t\n"
13262                    " \t  */",
13263                    Tab));
13264 
13265   Tab.UseTab = FormatStyle::UT_ForIndentation;
13266   verifyFormat("{\n"
13267                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13268                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13269                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13270                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13271                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13272                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13273                "};",
13274                Tab);
13275   verifyFormat("enum AA {\n"
13276                "\ta1, // Force multiple lines\n"
13277                "\ta2,\n"
13278                "\ta3\n"
13279                "};",
13280                Tab);
13281   EXPECT_EQ("if (aaaaaaaa && // q\n"
13282             "    bb)         // w\n"
13283             "\t;",
13284             format("if (aaaaaaaa &&// q\n"
13285                    "bb)// w\n"
13286                    ";",
13287                    Tab));
13288   verifyFormat("class X {\n"
13289                "\tvoid f() {\n"
13290                "\t\tsomeFunction(parameter1,\n"
13291                "\t\t             parameter2);\n"
13292                "\t}\n"
13293                "};",
13294                Tab);
13295   verifyFormat("{\n"
13296                "\tQ(\n"
13297                "\t    {\n"
13298                "\t\t    int a;\n"
13299                "\t\t    someFunction(aaaaaaaa,\n"
13300                "\t\t                 bbbbbbb);\n"
13301                "\t    },\n"
13302                "\t    p);\n"
13303                "}",
13304                Tab);
13305   EXPECT_EQ("{\n"
13306             "\t/* aaaa\n"
13307             "\t   bbbb */\n"
13308             "}",
13309             format("{\n"
13310                    "/* aaaa\n"
13311                    "   bbbb */\n"
13312                    "}",
13313                    Tab));
13314   EXPECT_EQ("{\n"
13315             "\t/*\n"
13316             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13317             "\t  bbbbbbbbbbbbb\n"
13318             "\t*/\n"
13319             "}",
13320             format("{\n"
13321                    "/*\n"
13322                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13323                    "*/\n"
13324                    "}",
13325                    Tab));
13326   EXPECT_EQ("{\n"
13327             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13328             "\t// bbbbbbbbbbbbb\n"
13329             "}",
13330             format("{\n"
13331                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13332                    "}",
13333                    Tab));
13334   EXPECT_EQ("{\n"
13335             "\t/*\n"
13336             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13337             "\t  bbbbbbbbbbbbb\n"
13338             "\t*/\n"
13339             "}",
13340             format("{\n"
13341                    "\t/*\n"
13342                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13343                    "\t*/\n"
13344                    "}",
13345                    Tab));
13346   EXPECT_EQ("{\n"
13347             "\t/*\n"
13348             "\n"
13349             "\t*/\n"
13350             "}",
13351             format("{\n"
13352                    "\t/*\n"
13353                    "\n"
13354                    "\t*/\n"
13355                    "}",
13356                    Tab));
13357   EXPECT_EQ("{\n"
13358             "\t/*\n"
13359             " asdf\n"
13360             "\t*/\n"
13361             "}",
13362             format("{\n"
13363                    "\t/*\n"
13364                    " asdf\n"
13365                    "\t*/\n"
13366                    "}",
13367                    Tab));
13368 
13369   Tab.UseTab = FormatStyle::UT_Never;
13370   EXPECT_EQ("/*\n"
13371             "              a\t\tcomment\n"
13372             "              in multiple lines\n"
13373             "       */",
13374             format("   /*\t \t \n"
13375                    " \t \t a\t\tcomment\t \t\n"
13376                    " \t \t in multiple lines\t\n"
13377                    " \t  */",
13378                    Tab));
13379   EXPECT_EQ("/* some\n"
13380             "   comment */",
13381             format(" \t \t /* some\n"
13382                    " \t \t    comment */",
13383                    Tab));
13384   EXPECT_EQ("int a; /* some\n"
13385             "   comment */",
13386             format(" \t \t int a; /* some\n"
13387                    " \t \t    comment */",
13388                    Tab));
13389 
13390   EXPECT_EQ("int a; /* some\n"
13391             "comment */",
13392             format(" \t \t int\ta; /* some\n"
13393                    " \t \t    comment */",
13394                    Tab));
13395   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13396             "    comment */",
13397             format(" \t \t f(\"\t\t\"); /* some\n"
13398                    " \t \t    comment */",
13399                    Tab));
13400   EXPECT_EQ("{\n"
13401             "        /*\n"
13402             "         * Comment\n"
13403             "         */\n"
13404             "        int i;\n"
13405             "}",
13406             format("{\n"
13407                    "\t/*\n"
13408                    "\t * Comment\n"
13409                    "\t */\n"
13410                    "\t int i;\n"
13411                    "}",
13412                    Tab));
13413 
13414   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13415   Tab.TabWidth = 8;
13416   Tab.IndentWidth = 8;
13417   EXPECT_EQ("if (aaaaaaaa && // q\n"
13418             "    bb)         // w\n"
13419             "\t;",
13420             format("if (aaaaaaaa &&// q\n"
13421                    "bb)// w\n"
13422                    ";",
13423                    Tab));
13424   EXPECT_EQ("if (aaa && bbb) // w\n"
13425             "\t;",
13426             format("if(aaa&&bbb)// w\n"
13427                    ";",
13428                    Tab));
13429   verifyFormat("class X {\n"
13430                "\tvoid f() {\n"
13431                "\t\tsomeFunction(parameter1,\n"
13432                "\t\t\t     parameter2);\n"
13433                "\t}\n"
13434                "};",
13435                Tab);
13436   verifyFormat("#define A                        \\\n"
13437                "\tvoid f() {               \\\n"
13438                "\t\tsomeFunction(    \\\n"
13439                "\t\t    parameter1,  \\\n"
13440                "\t\t    parameter2); \\\n"
13441                "\t}",
13442                Tab);
13443   Tab.TabWidth = 4;
13444   Tab.IndentWidth = 8;
13445   verifyFormat("class TabWidth4Indent8 {\n"
13446                "\t\tvoid f() {\n"
13447                "\t\t\t\tsomeFunction(parameter1,\n"
13448                "\t\t\t\t\t\t\t parameter2);\n"
13449                "\t\t}\n"
13450                "};",
13451                Tab);
13452   Tab.TabWidth = 4;
13453   Tab.IndentWidth = 4;
13454   verifyFormat("class TabWidth4Indent4 {\n"
13455                "\tvoid f() {\n"
13456                "\t\tsomeFunction(parameter1,\n"
13457                "\t\t\t\t\t parameter2);\n"
13458                "\t}\n"
13459                "};",
13460                Tab);
13461   Tab.TabWidth = 8;
13462   Tab.IndentWidth = 4;
13463   verifyFormat("class TabWidth8Indent4 {\n"
13464                "    void f() {\n"
13465                "\tsomeFunction(parameter1,\n"
13466                "\t\t     parameter2);\n"
13467                "    }\n"
13468                "};",
13469                Tab);
13470   Tab.TabWidth = 8;
13471   Tab.IndentWidth = 8;
13472   EXPECT_EQ("/*\n"
13473             "\t      a\t\tcomment\n"
13474             "\t      in multiple lines\n"
13475             "       */",
13476             format("   /*\t \t \n"
13477                    " \t \t a\t\tcomment\t \t\n"
13478                    " \t \t in multiple lines\t\n"
13479                    " \t  */",
13480                    Tab));
13481   verifyFormat("{\n"
13482                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13483                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13484                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13485                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13486                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13487                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13488                "};",
13489                Tab);
13490   verifyFormat("enum AA {\n"
13491                "\ta1, // Force multiple lines\n"
13492                "\ta2,\n"
13493                "\ta3\n"
13494                "};",
13495                Tab);
13496   EXPECT_EQ("if (aaaaaaaa && // q\n"
13497             "    bb)         // w\n"
13498             "\t;",
13499             format("if (aaaaaaaa &&// q\n"
13500                    "bb)// w\n"
13501                    ";",
13502                    Tab));
13503   verifyFormat("class X {\n"
13504                "\tvoid f() {\n"
13505                "\t\tsomeFunction(parameter1,\n"
13506                "\t\t\t     parameter2);\n"
13507                "\t}\n"
13508                "};",
13509                Tab);
13510   verifyFormat("{\n"
13511                "\tQ(\n"
13512                "\t    {\n"
13513                "\t\t    int a;\n"
13514                "\t\t    someFunction(aaaaaaaa,\n"
13515                "\t\t\t\t bbbbbbb);\n"
13516                "\t    },\n"
13517                "\t    p);\n"
13518                "}",
13519                Tab);
13520   EXPECT_EQ("{\n"
13521             "\t/* aaaa\n"
13522             "\t   bbbb */\n"
13523             "}",
13524             format("{\n"
13525                    "/* aaaa\n"
13526                    "   bbbb */\n"
13527                    "}",
13528                    Tab));
13529   EXPECT_EQ("{\n"
13530             "\t/*\n"
13531             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13532             "\t  bbbbbbbbbbbbb\n"
13533             "\t*/\n"
13534             "}",
13535             format("{\n"
13536                    "/*\n"
13537                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13538                    "*/\n"
13539                    "}",
13540                    Tab));
13541   EXPECT_EQ("{\n"
13542             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13543             "\t// bbbbbbbbbbbbb\n"
13544             "}",
13545             format("{\n"
13546                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13547                    "}",
13548                    Tab));
13549   EXPECT_EQ("{\n"
13550             "\t/*\n"
13551             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13552             "\t  bbbbbbbbbbbbb\n"
13553             "\t*/\n"
13554             "}",
13555             format("{\n"
13556                    "\t/*\n"
13557                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13558                    "\t*/\n"
13559                    "}",
13560                    Tab));
13561   EXPECT_EQ("{\n"
13562             "\t/*\n"
13563             "\n"
13564             "\t*/\n"
13565             "}",
13566             format("{\n"
13567                    "\t/*\n"
13568                    "\n"
13569                    "\t*/\n"
13570                    "}",
13571                    Tab));
13572   EXPECT_EQ("{\n"
13573             "\t/*\n"
13574             " asdf\n"
13575             "\t*/\n"
13576             "}",
13577             format("{\n"
13578                    "\t/*\n"
13579                    " asdf\n"
13580                    "\t*/\n"
13581                    "}",
13582                    Tab));
13583   EXPECT_EQ("/* some\n"
13584             "   comment */",
13585             format(" \t \t /* some\n"
13586                    " \t \t    comment */",
13587                    Tab));
13588   EXPECT_EQ("int a; /* some\n"
13589             "   comment */",
13590             format(" \t \t int a; /* some\n"
13591                    " \t \t    comment */",
13592                    Tab));
13593   EXPECT_EQ("int a; /* some\n"
13594             "comment */",
13595             format(" \t \t int\ta; /* some\n"
13596                    " \t \t    comment */",
13597                    Tab));
13598   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13599             "    comment */",
13600             format(" \t \t f(\"\t\t\"); /* some\n"
13601                    " \t \t    comment */",
13602                    Tab));
13603   EXPECT_EQ("{\n"
13604             "\t/*\n"
13605             "\t * Comment\n"
13606             "\t */\n"
13607             "\tint i;\n"
13608             "}",
13609             format("{\n"
13610                    "\t/*\n"
13611                    "\t * Comment\n"
13612                    "\t */\n"
13613                    "\t int i;\n"
13614                    "}",
13615                    Tab));
13616   Tab.TabWidth = 2;
13617   Tab.IndentWidth = 2;
13618   EXPECT_EQ("{\n"
13619             "\t/* aaaa\n"
13620             "\t\t bbbb */\n"
13621             "}",
13622             format("{\n"
13623                    "/* aaaa\n"
13624                    "\t bbbb */\n"
13625                    "}",
13626                    Tab));
13627   EXPECT_EQ("{\n"
13628             "\t/*\n"
13629             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13630             "\t\tbbbbbbbbbbbbb\n"
13631             "\t*/\n"
13632             "}",
13633             format("{\n"
13634                    "/*\n"
13635                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13636                    "*/\n"
13637                    "}",
13638                    Tab));
13639   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13640   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13641   Tab.TabWidth = 4;
13642   Tab.IndentWidth = 4;
13643   verifyFormat("class Assign {\n"
13644                "\tvoid f() {\n"
13645                "\t\tint         x      = 123;\n"
13646                "\t\tint         random = 4;\n"
13647                "\t\tstd::string alphabet =\n"
13648                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13649                "\t}\n"
13650                "};",
13651                Tab);
13652 
13653   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13654   Tab.TabWidth = 8;
13655   Tab.IndentWidth = 8;
13656   EXPECT_EQ("if (aaaaaaaa && // q\n"
13657             "    bb)         // w\n"
13658             "\t;",
13659             format("if (aaaaaaaa &&// q\n"
13660                    "bb)// w\n"
13661                    ";",
13662                    Tab));
13663   EXPECT_EQ("if (aaa && bbb) // w\n"
13664             "\t;",
13665             format("if(aaa&&bbb)// w\n"
13666                    ";",
13667                    Tab));
13668   verifyFormat("class X {\n"
13669                "\tvoid f() {\n"
13670                "\t\tsomeFunction(parameter1,\n"
13671                "\t\t             parameter2);\n"
13672                "\t}\n"
13673                "};",
13674                Tab);
13675   verifyFormat("#define A                        \\\n"
13676                "\tvoid f() {               \\\n"
13677                "\t\tsomeFunction(    \\\n"
13678                "\t\t    parameter1,  \\\n"
13679                "\t\t    parameter2); \\\n"
13680                "\t}",
13681                Tab);
13682   Tab.TabWidth = 4;
13683   Tab.IndentWidth = 8;
13684   verifyFormat("class TabWidth4Indent8 {\n"
13685                "\t\tvoid f() {\n"
13686                "\t\t\t\tsomeFunction(parameter1,\n"
13687                "\t\t\t\t             parameter2);\n"
13688                "\t\t}\n"
13689                "};",
13690                Tab);
13691   Tab.TabWidth = 4;
13692   Tab.IndentWidth = 4;
13693   verifyFormat("class TabWidth4Indent4 {\n"
13694                "\tvoid f() {\n"
13695                "\t\tsomeFunction(parameter1,\n"
13696                "\t\t             parameter2);\n"
13697                "\t}\n"
13698                "};",
13699                Tab);
13700   Tab.TabWidth = 8;
13701   Tab.IndentWidth = 4;
13702   verifyFormat("class TabWidth8Indent4 {\n"
13703                "    void f() {\n"
13704                "\tsomeFunction(parameter1,\n"
13705                "\t             parameter2);\n"
13706                "    }\n"
13707                "};",
13708                Tab);
13709   Tab.TabWidth = 8;
13710   Tab.IndentWidth = 8;
13711   EXPECT_EQ("/*\n"
13712             "              a\t\tcomment\n"
13713             "              in multiple lines\n"
13714             "       */",
13715             format("   /*\t \t \n"
13716                    " \t \t a\t\tcomment\t \t\n"
13717                    " \t \t in multiple lines\t\n"
13718                    " \t  */",
13719                    Tab));
13720   verifyFormat("{\n"
13721                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13722                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13723                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13724                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13725                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13726                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13727                "};",
13728                Tab);
13729   verifyFormat("enum AA {\n"
13730                "\ta1, // Force multiple lines\n"
13731                "\ta2,\n"
13732                "\ta3\n"
13733                "};",
13734                Tab);
13735   EXPECT_EQ("if (aaaaaaaa && // q\n"
13736             "    bb)         // w\n"
13737             "\t;",
13738             format("if (aaaaaaaa &&// q\n"
13739                    "bb)// w\n"
13740                    ";",
13741                    Tab));
13742   verifyFormat("class X {\n"
13743                "\tvoid f() {\n"
13744                "\t\tsomeFunction(parameter1,\n"
13745                "\t\t             parameter2);\n"
13746                "\t}\n"
13747                "};",
13748                Tab);
13749   verifyFormat("{\n"
13750                "\tQ(\n"
13751                "\t    {\n"
13752                "\t\t    int a;\n"
13753                "\t\t    someFunction(aaaaaaaa,\n"
13754                "\t\t                 bbbbbbb);\n"
13755                "\t    },\n"
13756                "\t    p);\n"
13757                "}",
13758                Tab);
13759   EXPECT_EQ("{\n"
13760             "\t/* aaaa\n"
13761             "\t   bbbb */\n"
13762             "}",
13763             format("{\n"
13764                    "/* aaaa\n"
13765                    "   bbbb */\n"
13766                    "}",
13767                    Tab));
13768   EXPECT_EQ("{\n"
13769             "\t/*\n"
13770             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13771             "\t  bbbbbbbbbbbbb\n"
13772             "\t*/\n"
13773             "}",
13774             format("{\n"
13775                    "/*\n"
13776                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13777                    "*/\n"
13778                    "}",
13779                    Tab));
13780   EXPECT_EQ("{\n"
13781             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13782             "\t// bbbbbbbbbbbbb\n"
13783             "}",
13784             format("{\n"
13785                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13786                    "}",
13787                    Tab));
13788   EXPECT_EQ("{\n"
13789             "\t/*\n"
13790             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13791             "\t  bbbbbbbbbbbbb\n"
13792             "\t*/\n"
13793             "}",
13794             format("{\n"
13795                    "\t/*\n"
13796                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13797                    "\t*/\n"
13798                    "}",
13799                    Tab));
13800   EXPECT_EQ("{\n"
13801             "\t/*\n"
13802             "\n"
13803             "\t*/\n"
13804             "}",
13805             format("{\n"
13806                    "\t/*\n"
13807                    "\n"
13808                    "\t*/\n"
13809                    "}",
13810                    Tab));
13811   EXPECT_EQ("{\n"
13812             "\t/*\n"
13813             " asdf\n"
13814             "\t*/\n"
13815             "}",
13816             format("{\n"
13817                    "\t/*\n"
13818                    " asdf\n"
13819                    "\t*/\n"
13820                    "}",
13821                    Tab));
13822   EXPECT_EQ("/* some\n"
13823             "   comment */",
13824             format(" \t \t /* some\n"
13825                    " \t \t    comment */",
13826                    Tab));
13827   EXPECT_EQ("int a; /* some\n"
13828             "   comment */",
13829             format(" \t \t int a; /* some\n"
13830                    " \t \t    comment */",
13831                    Tab));
13832   EXPECT_EQ("int a; /* some\n"
13833             "comment */",
13834             format(" \t \t int\ta; /* some\n"
13835                    " \t \t    comment */",
13836                    Tab));
13837   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13838             "    comment */",
13839             format(" \t \t f(\"\t\t\"); /* some\n"
13840                    " \t \t    comment */",
13841                    Tab));
13842   EXPECT_EQ("{\n"
13843             "\t/*\n"
13844             "\t * Comment\n"
13845             "\t */\n"
13846             "\tint i;\n"
13847             "}",
13848             format("{\n"
13849                    "\t/*\n"
13850                    "\t * Comment\n"
13851                    "\t */\n"
13852                    "\t int i;\n"
13853                    "}",
13854                    Tab));
13855   Tab.TabWidth = 2;
13856   Tab.IndentWidth = 2;
13857   EXPECT_EQ("{\n"
13858             "\t/* aaaa\n"
13859             "\t   bbbb */\n"
13860             "}",
13861             format("{\n"
13862                    "/* aaaa\n"
13863                    "   bbbb */\n"
13864                    "}",
13865                    Tab));
13866   EXPECT_EQ("{\n"
13867             "\t/*\n"
13868             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13869             "\t  bbbbbbbbbbbbb\n"
13870             "\t*/\n"
13871             "}",
13872             format("{\n"
13873                    "/*\n"
13874                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13875                    "*/\n"
13876                    "}",
13877                    Tab));
13878   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13879   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13880   Tab.TabWidth = 4;
13881   Tab.IndentWidth = 4;
13882   verifyFormat("class Assign {\n"
13883                "\tvoid f() {\n"
13884                "\t\tint         x      = 123;\n"
13885                "\t\tint         random = 4;\n"
13886                "\t\tstd::string alphabet =\n"
13887                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13888                "\t}\n"
13889                "};",
13890                Tab);
13891   Tab.AlignOperands = FormatStyle::OAS_Align;
13892   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13893                "                 cccccccccccccccccccc;",
13894                Tab);
13895   // no alignment
13896   verifyFormat("int aaaaaaaaaa =\n"
13897                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13898                Tab);
13899   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13900                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13901                "                        : 333333333333333;",
13902                Tab);
13903   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13904   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13905   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13906                "               + cccccccccccccccccccc;",
13907                Tab);
13908 }
13909 
13910 TEST_F(FormatTest, ZeroTabWidth) {
13911   FormatStyle Tab = getLLVMStyleWithColumns(42);
13912   Tab.IndentWidth = 8;
13913   Tab.UseTab = FormatStyle::UT_Never;
13914   Tab.TabWidth = 0;
13915   EXPECT_EQ("void a(){\n"
13916             "    // line starts with '\t'\n"
13917             "};",
13918             format("void a(){\n"
13919                    "\t// line starts with '\t'\n"
13920                    "};",
13921                    Tab));
13922 
13923   EXPECT_EQ("void a(){\n"
13924             "    // line starts with '\t'\n"
13925             "};",
13926             format("void a(){\n"
13927                    "\t\t// line starts with '\t'\n"
13928                    "};",
13929                    Tab));
13930 
13931   Tab.UseTab = FormatStyle::UT_ForIndentation;
13932   EXPECT_EQ("void a(){\n"
13933             "    // line starts with '\t'\n"
13934             "};",
13935             format("void a(){\n"
13936                    "\t// line starts with '\t'\n"
13937                    "};",
13938                    Tab));
13939 
13940   EXPECT_EQ("void a(){\n"
13941             "    // line starts with '\t'\n"
13942             "};",
13943             format("void a(){\n"
13944                    "\t\t// line starts with '\t'\n"
13945                    "};",
13946                    Tab));
13947 
13948   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13949   EXPECT_EQ("void a(){\n"
13950             "    // line starts with '\t'\n"
13951             "};",
13952             format("void a(){\n"
13953                    "\t// line starts with '\t'\n"
13954                    "};",
13955                    Tab));
13956 
13957   EXPECT_EQ("void a(){\n"
13958             "    // line starts with '\t'\n"
13959             "};",
13960             format("void a(){\n"
13961                    "\t\t// line starts with '\t'\n"
13962                    "};",
13963                    Tab));
13964 
13965   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13966   EXPECT_EQ("void a(){\n"
13967             "    // line starts with '\t'\n"
13968             "};",
13969             format("void a(){\n"
13970                    "\t// line starts with '\t'\n"
13971                    "};",
13972                    Tab));
13973 
13974   EXPECT_EQ("void a(){\n"
13975             "    // line starts with '\t'\n"
13976             "};",
13977             format("void a(){\n"
13978                    "\t\t// line starts with '\t'\n"
13979                    "};",
13980                    Tab));
13981 
13982   Tab.UseTab = FormatStyle::UT_Always;
13983   EXPECT_EQ("void a(){\n"
13984             "// line starts with '\t'\n"
13985             "};",
13986             format("void a(){\n"
13987                    "\t// line starts with '\t'\n"
13988                    "};",
13989                    Tab));
13990 
13991   EXPECT_EQ("void a(){\n"
13992             "// line starts with '\t'\n"
13993             "};",
13994             format("void a(){\n"
13995                    "\t\t// line starts with '\t'\n"
13996                    "};",
13997                    Tab));
13998 }
13999 
14000 TEST_F(FormatTest, CalculatesOriginalColumn) {
14001   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14002             "q\"; /* some\n"
14003             "       comment */",
14004             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14005                    "q\"; /* some\n"
14006                    "       comment */",
14007                    getLLVMStyle()));
14008   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14009             "/* some\n"
14010             "   comment */",
14011             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14012                    " /* some\n"
14013                    "    comment */",
14014                    getLLVMStyle()));
14015   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14016             "qqq\n"
14017             "/* some\n"
14018             "   comment */",
14019             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14020                    "qqq\n"
14021                    " /* some\n"
14022                    "    comment */",
14023                    getLLVMStyle()));
14024   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14025             "wwww; /* some\n"
14026             "         comment */",
14027             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14028                    "wwww; /* some\n"
14029                    "         comment */",
14030                    getLLVMStyle()));
14031 }
14032 
14033 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14034   FormatStyle NoSpace = getLLVMStyle();
14035   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14036 
14037   verifyFormat("while(true)\n"
14038                "  continue;",
14039                NoSpace);
14040   verifyFormat("for(;;)\n"
14041                "  continue;",
14042                NoSpace);
14043   verifyFormat("if(true)\n"
14044                "  f();\n"
14045                "else if(true)\n"
14046                "  f();",
14047                NoSpace);
14048   verifyFormat("do {\n"
14049                "  do_something();\n"
14050                "} while(something());",
14051                NoSpace);
14052   verifyFormat("switch(x) {\n"
14053                "default:\n"
14054                "  break;\n"
14055                "}",
14056                NoSpace);
14057   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14058   verifyFormat("size_t x = sizeof(x);", NoSpace);
14059   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14060   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14061   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14062   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14063   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14064   verifyFormat("alignas(128) char a[128];", NoSpace);
14065   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14066   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14067   verifyFormat("int f() throw(Deprecated);", NoSpace);
14068   verifyFormat("typedef void (*cb)(int);", NoSpace);
14069   verifyFormat("T A::operator()();", NoSpace);
14070   verifyFormat("X A::operator++(T);", NoSpace);
14071   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14072 
14073   FormatStyle Space = getLLVMStyle();
14074   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14075 
14076   verifyFormat("int f ();", Space);
14077   verifyFormat("void f (int a, T b) {\n"
14078                "  while (true)\n"
14079                "    continue;\n"
14080                "}",
14081                Space);
14082   verifyFormat("if (true)\n"
14083                "  f ();\n"
14084                "else if (true)\n"
14085                "  f ();",
14086                Space);
14087   verifyFormat("do {\n"
14088                "  do_something ();\n"
14089                "} while (something ());",
14090                Space);
14091   verifyFormat("switch (x) {\n"
14092                "default:\n"
14093                "  break;\n"
14094                "}",
14095                Space);
14096   verifyFormat("A::A () : a (1) {}", Space);
14097   verifyFormat("void f () __attribute__ ((asdf));", Space);
14098   verifyFormat("*(&a + 1);\n"
14099                "&((&a)[1]);\n"
14100                "a[(b + c) * d];\n"
14101                "(((a + 1) * 2) + 3) * 4;",
14102                Space);
14103   verifyFormat("#define A(x) x", Space);
14104   verifyFormat("#define A (x) x", Space);
14105   verifyFormat("#if defined(x)\n"
14106                "#endif",
14107                Space);
14108   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14109   verifyFormat("size_t x = sizeof (x);", Space);
14110   verifyFormat("auto f (int x) -> decltype (x);", Space);
14111   verifyFormat("auto f (int x) -> typeof (x);", Space);
14112   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14113   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14114   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14115   verifyFormat("alignas (128) char a[128];", Space);
14116   verifyFormat("size_t x = alignof (MyType);", Space);
14117   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14118   verifyFormat("int f () throw (Deprecated);", Space);
14119   verifyFormat("typedef void (*cb) (int);", Space);
14120   verifyFormat("T A::operator() ();", Space);
14121   verifyFormat("X A::operator++ (T);", Space);
14122   verifyFormat("auto lambda = [] () { return 0; };", Space);
14123   verifyFormat("int x = int (y);", Space);
14124 
14125   FormatStyle SomeSpace = getLLVMStyle();
14126   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14127 
14128   verifyFormat("[]() -> float {}", SomeSpace);
14129   verifyFormat("[] (auto foo) {}", SomeSpace);
14130   verifyFormat("[foo]() -> int {}", SomeSpace);
14131   verifyFormat("int f();", SomeSpace);
14132   verifyFormat("void f (int a, T b) {\n"
14133                "  while (true)\n"
14134                "    continue;\n"
14135                "}",
14136                SomeSpace);
14137   verifyFormat("if (true)\n"
14138                "  f();\n"
14139                "else if (true)\n"
14140                "  f();",
14141                SomeSpace);
14142   verifyFormat("do {\n"
14143                "  do_something();\n"
14144                "} while (something());",
14145                SomeSpace);
14146   verifyFormat("switch (x) {\n"
14147                "default:\n"
14148                "  break;\n"
14149                "}",
14150                SomeSpace);
14151   verifyFormat("A::A() : a (1) {}", SomeSpace);
14152   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14153   verifyFormat("*(&a + 1);\n"
14154                "&((&a)[1]);\n"
14155                "a[(b + c) * d];\n"
14156                "(((a + 1) * 2) + 3) * 4;",
14157                SomeSpace);
14158   verifyFormat("#define A(x) x", SomeSpace);
14159   verifyFormat("#define A (x) x", SomeSpace);
14160   verifyFormat("#if defined(x)\n"
14161                "#endif",
14162                SomeSpace);
14163   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14164   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14165   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14166   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14167   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14168   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14169   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14170   verifyFormat("alignas (128) char a[128];", SomeSpace);
14171   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14172   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14173                SomeSpace);
14174   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14175   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14176   verifyFormat("T A::operator()();", SomeSpace);
14177   verifyFormat("X A::operator++ (T);", SomeSpace);
14178   verifyFormat("int x = int (y);", SomeSpace);
14179   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14180 
14181   FormatStyle SpaceControlStatements = getLLVMStyle();
14182   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14183   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14184 
14185   verifyFormat("while (true)\n"
14186                "  continue;",
14187                SpaceControlStatements);
14188   verifyFormat("if (true)\n"
14189                "  f();\n"
14190                "else if (true)\n"
14191                "  f();",
14192                SpaceControlStatements);
14193   verifyFormat("for (;;) {\n"
14194                "  do_something();\n"
14195                "}",
14196                SpaceControlStatements);
14197   verifyFormat("do {\n"
14198                "  do_something();\n"
14199                "} while (something());",
14200                SpaceControlStatements);
14201   verifyFormat("switch (x) {\n"
14202                "default:\n"
14203                "  break;\n"
14204                "}",
14205                SpaceControlStatements);
14206 
14207   FormatStyle SpaceFuncDecl = getLLVMStyle();
14208   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14209   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14210 
14211   verifyFormat("int f ();", SpaceFuncDecl);
14212   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14213   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14214   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14215   verifyFormat("#define A(x) x", SpaceFuncDecl);
14216   verifyFormat("#define A (x) x", SpaceFuncDecl);
14217   verifyFormat("#if defined(x)\n"
14218                "#endif",
14219                SpaceFuncDecl);
14220   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14221   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14222   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14223   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14224   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14225   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14226   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14227   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14228   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14229   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14230                SpaceFuncDecl);
14231   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14232   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14233   verifyFormat("T A::operator() ();", SpaceFuncDecl);
14234   verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14235   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14236   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14237   verifyFormat("int x = int(y);", SpaceFuncDecl);
14238   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14239                SpaceFuncDecl);
14240 
14241   FormatStyle SpaceFuncDef = getLLVMStyle();
14242   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14243   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14244 
14245   verifyFormat("int f();", SpaceFuncDef);
14246   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14247   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14248   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14249   verifyFormat("#define A(x) x", SpaceFuncDef);
14250   verifyFormat("#define A (x) x", SpaceFuncDef);
14251   verifyFormat("#if defined(x)\n"
14252                "#endif",
14253                SpaceFuncDef);
14254   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14255   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14256   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14257   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14258   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14259   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14260   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14261   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14262   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14263   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14264                SpaceFuncDef);
14265   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14266   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14267   verifyFormat("T A::operator()();", SpaceFuncDef);
14268   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14269   verifyFormat("T A::operator() () {}", SpaceFuncDef);
14270   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14271   verifyFormat("int x = int(y);", SpaceFuncDef);
14272   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14273                SpaceFuncDef);
14274 
14275   FormatStyle SpaceIfMacros = getLLVMStyle();
14276   SpaceIfMacros.IfMacros.clear();
14277   SpaceIfMacros.IfMacros.push_back("MYIF");
14278   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14279   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14280   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14281   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14282   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14283 
14284   FormatStyle SpaceForeachMacros = getLLVMStyle();
14285   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14286   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14287   verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros);
14288   verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14289   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14290   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14291 
14292   FormatStyle SomeSpace2 = getLLVMStyle();
14293   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14294   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14295   verifyFormat("[]() -> float {}", SomeSpace2);
14296   verifyFormat("[] (auto foo) {}", SomeSpace2);
14297   verifyFormat("[foo]() -> int {}", SomeSpace2);
14298   verifyFormat("int f();", SomeSpace2);
14299   verifyFormat("void f (int a, T b) {\n"
14300                "  while (true)\n"
14301                "    continue;\n"
14302                "}",
14303                SomeSpace2);
14304   verifyFormat("if (true)\n"
14305                "  f();\n"
14306                "else if (true)\n"
14307                "  f();",
14308                SomeSpace2);
14309   verifyFormat("do {\n"
14310                "  do_something();\n"
14311                "} while (something());",
14312                SomeSpace2);
14313   verifyFormat("switch (x) {\n"
14314                "default:\n"
14315                "  break;\n"
14316                "}",
14317                SomeSpace2);
14318   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14319   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14320   verifyFormat("*(&a + 1);\n"
14321                "&((&a)[1]);\n"
14322                "a[(b + c) * d];\n"
14323                "(((a + 1) * 2) + 3) * 4;",
14324                SomeSpace2);
14325   verifyFormat("#define A(x) x", SomeSpace2);
14326   verifyFormat("#define A (x) x", SomeSpace2);
14327   verifyFormat("#if defined(x)\n"
14328                "#endif",
14329                SomeSpace2);
14330   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14331   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14332   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14333   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14334   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14335   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14336   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14337   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14338   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14339   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14340                SomeSpace2);
14341   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14342   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14343   verifyFormat("T A::operator()();", SomeSpace2);
14344   verifyFormat("X A::operator++ (T);", SomeSpace2);
14345   verifyFormat("int x = int (y);", SomeSpace2);
14346   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14347 }
14348 
14349 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14350   FormatStyle Spaces = getLLVMStyle();
14351   Spaces.SpaceAfterLogicalNot = true;
14352 
14353   verifyFormat("bool x = ! y", Spaces);
14354   verifyFormat("if (! isFailure())", Spaces);
14355   verifyFormat("if (! (a && b))", Spaces);
14356   verifyFormat("\"Error!\"", Spaces);
14357   verifyFormat("! ! x", Spaces);
14358 }
14359 
14360 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14361   FormatStyle Spaces = getLLVMStyle();
14362 
14363   Spaces.SpacesInParentheses = true;
14364   verifyFormat("do_something( ::globalVar );", Spaces);
14365   verifyFormat("call( x, y, z );", Spaces);
14366   verifyFormat("call();", Spaces);
14367   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14368   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14369                Spaces);
14370   verifyFormat("while ( (bool)1 )\n"
14371                "  continue;",
14372                Spaces);
14373   verifyFormat("for ( ;; )\n"
14374                "  continue;",
14375                Spaces);
14376   verifyFormat("if ( true )\n"
14377                "  f();\n"
14378                "else if ( true )\n"
14379                "  f();",
14380                Spaces);
14381   verifyFormat("do {\n"
14382                "  do_something( (int)i );\n"
14383                "} while ( something() );",
14384                Spaces);
14385   verifyFormat("switch ( x ) {\n"
14386                "default:\n"
14387                "  break;\n"
14388                "}",
14389                Spaces);
14390 
14391   Spaces.SpacesInParentheses = false;
14392   Spaces.SpacesInCStyleCastParentheses = true;
14393   verifyFormat("Type *A = ( Type * )P;", Spaces);
14394   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14395   verifyFormat("x = ( int32 )y;", Spaces);
14396   verifyFormat("int a = ( int )(2.0f);", Spaces);
14397   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14398   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14399   verifyFormat("#define x (( int )-1)", Spaces);
14400 
14401   // Run the first set of tests again with:
14402   Spaces.SpacesInParentheses = false;
14403   Spaces.SpaceInEmptyParentheses = true;
14404   Spaces.SpacesInCStyleCastParentheses = true;
14405   verifyFormat("call(x, y, z);", Spaces);
14406   verifyFormat("call( );", Spaces);
14407   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14408   verifyFormat("while (( bool )1)\n"
14409                "  continue;",
14410                Spaces);
14411   verifyFormat("for (;;)\n"
14412                "  continue;",
14413                Spaces);
14414   verifyFormat("if (true)\n"
14415                "  f( );\n"
14416                "else if (true)\n"
14417                "  f( );",
14418                Spaces);
14419   verifyFormat("do {\n"
14420                "  do_something(( int )i);\n"
14421                "} while (something( ));",
14422                Spaces);
14423   verifyFormat("switch (x) {\n"
14424                "default:\n"
14425                "  break;\n"
14426                "}",
14427                Spaces);
14428 
14429   // Run the first set of tests again with:
14430   Spaces.SpaceAfterCStyleCast = true;
14431   verifyFormat("call(x, y, z);", Spaces);
14432   verifyFormat("call( );", Spaces);
14433   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14434   verifyFormat("while (( bool ) 1)\n"
14435                "  continue;",
14436                Spaces);
14437   verifyFormat("for (;;)\n"
14438                "  continue;",
14439                Spaces);
14440   verifyFormat("if (true)\n"
14441                "  f( );\n"
14442                "else if (true)\n"
14443                "  f( );",
14444                Spaces);
14445   verifyFormat("do {\n"
14446                "  do_something(( int ) i);\n"
14447                "} while (something( ));",
14448                Spaces);
14449   verifyFormat("switch (x) {\n"
14450                "default:\n"
14451                "  break;\n"
14452                "}",
14453                Spaces);
14454 
14455   // Run subset of tests again with:
14456   Spaces.SpacesInCStyleCastParentheses = false;
14457   Spaces.SpaceAfterCStyleCast = true;
14458   verifyFormat("while ((bool) 1)\n"
14459                "  continue;",
14460                Spaces);
14461   verifyFormat("do {\n"
14462                "  do_something((int) i);\n"
14463                "} while (something( ));",
14464                Spaces);
14465 
14466   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14467   verifyFormat("size_t idx = (size_t) a;", Spaces);
14468   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14469   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14470   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14471   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14472   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14473   Spaces.ColumnLimit = 80;
14474   Spaces.IndentWidth = 4;
14475   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14476   verifyFormat("void foo( ) {\n"
14477                "    size_t foo = (*(function))(\n"
14478                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14479                "BarrrrrrrrrrrrLong,\n"
14480                "        FoooooooooLooooong);\n"
14481                "}",
14482                Spaces);
14483   Spaces.SpaceAfterCStyleCast = false;
14484   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14485   verifyFormat("size_t idx = (size_t)a;", Spaces);
14486   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14487   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14488   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14489   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14490   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14491 
14492   verifyFormat("void foo( ) {\n"
14493                "    size_t foo = (*(function))(\n"
14494                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14495                "BarrrrrrrrrrrrLong,\n"
14496                "        FoooooooooLooooong);\n"
14497                "}",
14498                Spaces);
14499 }
14500 
14501 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14502   verifyFormat("int a[5];");
14503   verifyFormat("a[3] += 42;");
14504 
14505   FormatStyle Spaces = getLLVMStyle();
14506   Spaces.SpacesInSquareBrackets = true;
14507   // Not lambdas.
14508   verifyFormat("int a[ 5 ];", Spaces);
14509   verifyFormat("a[ 3 ] += 42;", Spaces);
14510   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14511   verifyFormat("double &operator[](int i) { return 0; }\n"
14512                "int i;",
14513                Spaces);
14514   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14515   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14516   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14517   // Lambdas.
14518   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14519   verifyFormat("return [ i, args... ] {};", Spaces);
14520   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14521   verifyFormat("int foo = [ = ]() {};", Spaces);
14522   verifyFormat("int foo = [ & ]() {};", Spaces);
14523   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14524   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14525 }
14526 
14527 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14528   FormatStyle NoSpaceStyle = getLLVMStyle();
14529   verifyFormat("int a[5];", NoSpaceStyle);
14530   verifyFormat("a[3] += 42;", NoSpaceStyle);
14531 
14532   verifyFormat("int a[1];", NoSpaceStyle);
14533   verifyFormat("int 1 [a];", NoSpaceStyle);
14534   verifyFormat("int a[1][2];", NoSpaceStyle);
14535   verifyFormat("a[7] = 5;", NoSpaceStyle);
14536   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14537   verifyFormat("f([] {})", NoSpaceStyle);
14538 
14539   FormatStyle Space = getLLVMStyle();
14540   Space.SpaceBeforeSquareBrackets = true;
14541   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14542   verifyFormat("return [i, args...] {};", Space);
14543 
14544   verifyFormat("int a [5];", Space);
14545   verifyFormat("a [3] += 42;", Space);
14546   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14547   verifyFormat("double &operator[](int i) { return 0; }\n"
14548                "int i;",
14549                Space);
14550   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14551   verifyFormat("int i = a [a][a]->f();", Space);
14552   verifyFormat("int i = (*b) [a]->f();", Space);
14553 
14554   verifyFormat("int a [1];", Space);
14555   verifyFormat("int 1 [a];", Space);
14556   verifyFormat("int a [1][2];", Space);
14557   verifyFormat("a [7] = 5;", Space);
14558   verifyFormat("int a = (f()) [23];", Space);
14559   verifyFormat("f([] {})", Space);
14560 }
14561 
14562 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14563   verifyFormat("int a = 5;");
14564   verifyFormat("a += 42;");
14565   verifyFormat("a or_eq 8;");
14566 
14567   FormatStyle Spaces = getLLVMStyle();
14568   Spaces.SpaceBeforeAssignmentOperators = false;
14569   verifyFormat("int a= 5;", Spaces);
14570   verifyFormat("a+= 42;", Spaces);
14571   verifyFormat("a or_eq 8;", Spaces);
14572 }
14573 
14574 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14575   verifyFormat("class Foo : public Bar {};");
14576   verifyFormat("Foo::Foo() : foo(1) {}");
14577   verifyFormat("for (auto a : b) {\n}");
14578   verifyFormat("int x = a ? b : c;");
14579   verifyFormat("{\n"
14580                "label0:\n"
14581                "  int x = 0;\n"
14582                "}");
14583   verifyFormat("switch (x) {\n"
14584                "case 1:\n"
14585                "default:\n"
14586                "}");
14587   verifyFormat("switch (allBraces) {\n"
14588                "case 1: {\n"
14589                "  break;\n"
14590                "}\n"
14591                "case 2: {\n"
14592                "  [[fallthrough]];\n"
14593                "}\n"
14594                "default: {\n"
14595                "  break;\n"
14596                "}\n"
14597                "}");
14598 
14599   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14600   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14601   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14602   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14603   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14604   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14605   verifyFormat("{\n"
14606                "label1:\n"
14607                "  int x = 0;\n"
14608                "}",
14609                CtorInitializerStyle);
14610   verifyFormat("switch (x) {\n"
14611                "case 1:\n"
14612                "default:\n"
14613                "}",
14614                CtorInitializerStyle);
14615   verifyFormat("switch (allBraces) {\n"
14616                "case 1: {\n"
14617                "  break;\n"
14618                "}\n"
14619                "case 2: {\n"
14620                "  [[fallthrough]];\n"
14621                "}\n"
14622                "default: {\n"
14623                "  break;\n"
14624                "}\n"
14625                "}",
14626                CtorInitializerStyle);
14627   CtorInitializerStyle.BreakConstructorInitializers =
14628       FormatStyle::BCIS_AfterColon;
14629   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14630                "    aaaaaaaaaaaaaaaa(1),\n"
14631                "    bbbbbbbbbbbbbbbb(2) {}",
14632                CtorInitializerStyle);
14633   CtorInitializerStyle.BreakConstructorInitializers =
14634       FormatStyle::BCIS_BeforeComma;
14635   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14636                "    : aaaaaaaaaaaaaaaa(1)\n"
14637                "    , bbbbbbbbbbbbbbbb(2) {}",
14638                CtorInitializerStyle);
14639   CtorInitializerStyle.BreakConstructorInitializers =
14640       FormatStyle::BCIS_BeforeColon;
14641   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14642                "    : aaaaaaaaaaaaaaaa(1),\n"
14643                "      bbbbbbbbbbbbbbbb(2) {}",
14644                CtorInitializerStyle);
14645   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14646   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14647                ": aaaaaaaaaaaaaaaa(1),\n"
14648                "  bbbbbbbbbbbbbbbb(2) {}",
14649                CtorInitializerStyle);
14650 
14651   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14652   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14653   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14654   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14655   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14656   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14657   verifyFormat("{\n"
14658                "label2:\n"
14659                "  int x = 0;\n"
14660                "}",
14661                InheritanceStyle);
14662   verifyFormat("switch (x) {\n"
14663                "case 1:\n"
14664                "default:\n"
14665                "}",
14666                InheritanceStyle);
14667   verifyFormat("switch (allBraces) {\n"
14668                "case 1: {\n"
14669                "  break;\n"
14670                "}\n"
14671                "case 2: {\n"
14672                "  [[fallthrough]];\n"
14673                "}\n"
14674                "default: {\n"
14675                "  break;\n"
14676                "}\n"
14677                "}",
14678                InheritanceStyle);
14679   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14680   verifyFormat("class Foooooooooooooooooooooo\n"
14681                "    : public aaaaaaaaaaaaaaaaaa,\n"
14682                "      public bbbbbbbbbbbbbbbbbb {\n"
14683                "}",
14684                InheritanceStyle);
14685   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14686   verifyFormat("class Foooooooooooooooooooooo:\n"
14687                "    public aaaaaaaaaaaaaaaaaa,\n"
14688                "    public bbbbbbbbbbbbbbbbbb {\n"
14689                "}",
14690                InheritanceStyle);
14691   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14692   verifyFormat("class Foooooooooooooooooooooo\n"
14693                "    : public aaaaaaaaaaaaaaaaaa\n"
14694                "    , public bbbbbbbbbbbbbbbbbb {\n"
14695                "}",
14696                InheritanceStyle);
14697   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14698   verifyFormat("class Foooooooooooooooooooooo\n"
14699                "    : public aaaaaaaaaaaaaaaaaa,\n"
14700                "      public bbbbbbbbbbbbbbbbbb {\n"
14701                "}",
14702                InheritanceStyle);
14703   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14704   verifyFormat("class Foooooooooooooooooooooo\n"
14705                ": public aaaaaaaaaaaaaaaaaa,\n"
14706                "  public bbbbbbbbbbbbbbbbbb {}",
14707                InheritanceStyle);
14708 
14709   FormatStyle ForLoopStyle = getLLVMStyle();
14710   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14711   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14712   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14713   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14714   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14715   verifyFormat("{\n"
14716                "label2:\n"
14717                "  int x = 0;\n"
14718                "}",
14719                ForLoopStyle);
14720   verifyFormat("switch (x) {\n"
14721                "case 1:\n"
14722                "default:\n"
14723                "}",
14724                ForLoopStyle);
14725   verifyFormat("switch (allBraces) {\n"
14726                "case 1: {\n"
14727                "  break;\n"
14728                "}\n"
14729                "case 2: {\n"
14730                "  [[fallthrough]];\n"
14731                "}\n"
14732                "default: {\n"
14733                "  break;\n"
14734                "}\n"
14735                "}",
14736                ForLoopStyle);
14737 
14738   FormatStyle CaseStyle = getLLVMStyle();
14739   CaseStyle.SpaceBeforeCaseColon = true;
14740   verifyFormat("class Foo : public Bar {};", CaseStyle);
14741   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14742   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14743   verifyFormat("int x = a ? b : c;", CaseStyle);
14744   verifyFormat("switch (x) {\n"
14745                "case 1 :\n"
14746                "default :\n"
14747                "}",
14748                CaseStyle);
14749   verifyFormat("switch (allBraces) {\n"
14750                "case 1 : {\n"
14751                "  break;\n"
14752                "}\n"
14753                "case 2 : {\n"
14754                "  [[fallthrough]];\n"
14755                "}\n"
14756                "default : {\n"
14757                "  break;\n"
14758                "}\n"
14759                "}",
14760                CaseStyle);
14761 
14762   FormatStyle NoSpaceStyle = getLLVMStyle();
14763   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14764   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14765   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14766   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14767   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14768   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14769   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14770   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14771   verifyFormat("{\n"
14772                "label3:\n"
14773                "  int x = 0;\n"
14774                "}",
14775                NoSpaceStyle);
14776   verifyFormat("switch (x) {\n"
14777                "case 1:\n"
14778                "default:\n"
14779                "}",
14780                NoSpaceStyle);
14781   verifyFormat("switch (allBraces) {\n"
14782                "case 1: {\n"
14783                "  break;\n"
14784                "}\n"
14785                "case 2: {\n"
14786                "  [[fallthrough]];\n"
14787                "}\n"
14788                "default: {\n"
14789                "  break;\n"
14790                "}\n"
14791                "}",
14792                NoSpaceStyle);
14793 
14794   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14795   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14796   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14797   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14798   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14799   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14800   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14801   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14802   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14803   verifyFormat("{\n"
14804                "label3:\n"
14805                "  int x = 0;\n"
14806                "}",
14807                InvertedSpaceStyle);
14808   verifyFormat("switch (x) {\n"
14809                "case 1 :\n"
14810                "case 2 : {\n"
14811                "  break;\n"
14812                "}\n"
14813                "default :\n"
14814                "  break;\n"
14815                "}",
14816                InvertedSpaceStyle);
14817   verifyFormat("switch (allBraces) {\n"
14818                "case 1 : {\n"
14819                "  break;\n"
14820                "}\n"
14821                "case 2 : {\n"
14822                "  [[fallthrough]];\n"
14823                "}\n"
14824                "default : {\n"
14825                "  break;\n"
14826                "}\n"
14827                "}",
14828                InvertedSpaceStyle);
14829 }
14830 
14831 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14832   FormatStyle Style = getLLVMStyle();
14833 
14834   Style.PointerAlignment = FormatStyle::PAS_Left;
14835   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14836   verifyFormat("void* const* x = NULL;", Style);
14837 
14838 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14839   do {                                                                         \
14840     Style.PointerAlignment = FormatStyle::Pointers;                            \
14841     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14842     verifyFormat(Code, Style);                                                 \
14843   } while (false)
14844 
14845   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14846   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14847   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14848 
14849   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14850   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14851   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14852 
14853   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14854   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14855   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14856 
14857   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14858   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14859   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14860 
14861   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14862   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14863                         SAPQ_Default);
14864   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14865                         SAPQ_Default);
14866 
14867   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14868   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14869                         SAPQ_Before);
14870   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14871                         SAPQ_Before);
14872 
14873   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14874   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14875   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14876                         SAPQ_After);
14877 
14878   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14879   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14880   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14881 
14882 #undef verifyQualifierSpaces
14883 
14884   FormatStyle Spaces = getLLVMStyle();
14885   Spaces.AttributeMacros.push_back("qualified");
14886   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14887   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14888   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14889   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14890   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14891   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14892   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14893   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14894   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14895   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14896   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14897   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14898   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14899 
14900   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14901   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14902   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14903   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14904   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14905   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14906   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14907   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14908   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14909   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14910   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14911   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14912   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14913   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14914   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14915 
14916   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14917   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14918   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14919   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14920   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14921   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14922   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14923   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14924 }
14925 
14926 TEST_F(FormatTest, AlignConsecutiveMacros) {
14927   FormatStyle Style = getLLVMStyle();
14928   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14929   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14930   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14931 
14932   verifyFormat("#define a 3\n"
14933                "#define bbbb 4\n"
14934                "#define ccc (5)",
14935                Style);
14936 
14937   verifyFormat("#define f(x) (x * x)\n"
14938                "#define fff(x, y, z) (x * y + z)\n"
14939                "#define ffff(x, y) (x - y)",
14940                Style);
14941 
14942   verifyFormat("#define foo(x, y) (x + y)\n"
14943                "#define bar (5, 6)(2 + 2)",
14944                Style);
14945 
14946   verifyFormat("#define a 3\n"
14947                "#define bbbb 4\n"
14948                "#define ccc (5)\n"
14949                "#define f(x) (x * x)\n"
14950                "#define fff(x, y, z) (x * y + z)\n"
14951                "#define ffff(x, y) (x - y)",
14952                Style);
14953 
14954   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14955   verifyFormat("#define a    3\n"
14956                "#define bbbb 4\n"
14957                "#define ccc  (5)",
14958                Style);
14959 
14960   verifyFormat("#define f(x)         (x * x)\n"
14961                "#define fff(x, y, z) (x * y + z)\n"
14962                "#define ffff(x, y)   (x - y)",
14963                Style);
14964 
14965   verifyFormat("#define foo(x, y) (x + y)\n"
14966                "#define bar       (5, 6)(2 + 2)",
14967                Style);
14968 
14969   verifyFormat("#define a            3\n"
14970                "#define bbbb         4\n"
14971                "#define ccc          (5)\n"
14972                "#define f(x)         (x * x)\n"
14973                "#define fff(x, y, z) (x * y + z)\n"
14974                "#define ffff(x, y)   (x - y)",
14975                Style);
14976 
14977   verifyFormat("#define a         5\n"
14978                "#define foo(x, y) (x + y)\n"
14979                "#define CCC       (6)\n"
14980                "auto lambda = []() {\n"
14981                "  auto  ii = 0;\n"
14982                "  float j  = 0;\n"
14983                "  return 0;\n"
14984                "};\n"
14985                "int   i  = 0;\n"
14986                "float i2 = 0;\n"
14987                "auto  v  = type{\n"
14988                "    i = 1,   //\n"
14989                "    (i = 2), //\n"
14990                "    i = 3    //\n"
14991                "};",
14992                Style);
14993 
14994   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14995   Style.ColumnLimit = 20;
14996 
14997   verifyFormat("#define a          \\\n"
14998                "  \"aabbbbbbbbbbbb\"\n"
14999                "#define D          \\\n"
15000                "  \"aabbbbbbbbbbbb\" \\\n"
15001                "  \"ccddeeeeeeeee\"\n"
15002                "#define B          \\\n"
15003                "  \"QQQQQQQQQQQQQ\"  \\\n"
15004                "  \"FFFFFFFFFFFFF\"  \\\n"
15005                "  \"LLLLLLLL\"\n",
15006                Style);
15007 
15008   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15009   verifyFormat("#define a          \\\n"
15010                "  \"aabbbbbbbbbbbb\"\n"
15011                "#define D          \\\n"
15012                "  \"aabbbbbbbbbbbb\" \\\n"
15013                "  \"ccddeeeeeeeee\"\n"
15014                "#define B          \\\n"
15015                "  \"QQQQQQQQQQQQQ\"  \\\n"
15016                "  \"FFFFFFFFFFFFF\"  \\\n"
15017                "  \"LLLLLLLL\"\n",
15018                Style);
15019 
15020   // Test across comments
15021   Style.MaxEmptyLinesToKeep = 10;
15022   Style.ReflowComments = false;
15023   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15024   EXPECT_EQ("#define a    3\n"
15025             "// line comment\n"
15026             "#define bbbb 4\n"
15027             "#define ccc  (5)",
15028             format("#define a 3\n"
15029                    "// line comment\n"
15030                    "#define bbbb 4\n"
15031                    "#define ccc (5)",
15032                    Style));
15033 
15034   EXPECT_EQ("#define a    3\n"
15035             "/* block comment */\n"
15036             "#define bbbb 4\n"
15037             "#define ccc  (5)",
15038             format("#define a  3\n"
15039                    "/* block comment */\n"
15040                    "#define bbbb 4\n"
15041                    "#define ccc (5)",
15042                    Style));
15043 
15044   EXPECT_EQ("#define a    3\n"
15045             "/* multi-line *\n"
15046             " * block comment */\n"
15047             "#define bbbb 4\n"
15048             "#define ccc  (5)",
15049             format("#define a 3\n"
15050                    "/* multi-line *\n"
15051                    " * block comment */\n"
15052                    "#define bbbb 4\n"
15053                    "#define ccc (5)",
15054                    Style));
15055 
15056   EXPECT_EQ("#define a    3\n"
15057             "// multi-line line comment\n"
15058             "//\n"
15059             "#define bbbb 4\n"
15060             "#define ccc  (5)",
15061             format("#define a  3\n"
15062                    "// multi-line line comment\n"
15063                    "//\n"
15064                    "#define bbbb 4\n"
15065                    "#define ccc (5)",
15066                    Style));
15067 
15068   EXPECT_EQ("#define a 3\n"
15069             "// empty lines still break.\n"
15070             "\n"
15071             "#define bbbb 4\n"
15072             "#define ccc  (5)",
15073             format("#define a     3\n"
15074                    "// empty lines still break.\n"
15075                    "\n"
15076                    "#define bbbb     4\n"
15077                    "#define ccc  (5)",
15078                    Style));
15079 
15080   // Test across empty lines
15081   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15082   EXPECT_EQ("#define a    3\n"
15083             "\n"
15084             "#define bbbb 4\n"
15085             "#define ccc  (5)",
15086             format("#define a 3\n"
15087                    "\n"
15088                    "#define bbbb 4\n"
15089                    "#define ccc (5)",
15090                    Style));
15091 
15092   EXPECT_EQ("#define a    3\n"
15093             "\n"
15094             "\n"
15095             "\n"
15096             "#define bbbb 4\n"
15097             "#define ccc  (5)",
15098             format("#define a        3\n"
15099                    "\n"
15100                    "\n"
15101                    "\n"
15102                    "#define bbbb 4\n"
15103                    "#define ccc (5)",
15104                    Style));
15105 
15106   EXPECT_EQ("#define a 3\n"
15107             "// comments should break alignment\n"
15108             "//\n"
15109             "#define bbbb 4\n"
15110             "#define ccc  (5)",
15111             format("#define a        3\n"
15112                    "// comments should break alignment\n"
15113                    "//\n"
15114                    "#define bbbb 4\n"
15115                    "#define ccc (5)",
15116                    Style));
15117 
15118   // Test across empty lines and comments
15119   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15120   verifyFormat("#define a    3\n"
15121                "\n"
15122                "// line comment\n"
15123                "#define bbbb 4\n"
15124                "#define ccc  (5)",
15125                Style);
15126 
15127   EXPECT_EQ("#define a    3\n"
15128             "\n"
15129             "\n"
15130             "/* multi-line *\n"
15131             " * block comment */\n"
15132             "\n"
15133             "\n"
15134             "#define bbbb 4\n"
15135             "#define ccc  (5)",
15136             format("#define a 3\n"
15137                    "\n"
15138                    "\n"
15139                    "/* multi-line *\n"
15140                    " * block comment */\n"
15141                    "\n"
15142                    "\n"
15143                    "#define bbbb 4\n"
15144                    "#define ccc (5)",
15145                    Style));
15146 
15147   EXPECT_EQ("#define a    3\n"
15148             "\n"
15149             "\n"
15150             "/* multi-line *\n"
15151             " * block comment */\n"
15152             "\n"
15153             "\n"
15154             "#define bbbb 4\n"
15155             "#define ccc  (5)",
15156             format("#define a 3\n"
15157                    "\n"
15158                    "\n"
15159                    "/* multi-line *\n"
15160                    " * block comment */\n"
15161                    "\n"
15162                    "\n"
15163                    "#define bbbb 4\n"
15164                    "#define ccc       (5)",
15165                    Style));
15166 }
15167 
15168 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15169   FormatStyle Alignment = getLLVMStyle();
15170   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15171   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15172 
15173   Alignment.MaxEmptyLinesToKeep = 10;
15174   /* Test alignment across empty lines */
15175   EXPECT_EQ("int a           = 5;\n"
15176             "\n"
15177             "int oneTwoThree = 123;",
15178             format("int a       = 5;\n"
15179                    "\n"
15180                    "int oneTwoThree= 123;",
15181                    Alignment));
15182   EXPECT_EQ("int a           = 5;\n"
15183             "int one         = 1;\n"
15184             "\n"
15185             "int oneTwoThree = 123;",
15186             format("int a = 5;\n"
15187                    "int one = 1;\n"
15188                    "\n"
15189                    "int oneTwoThree = 123;",
15190                    Alignment));
15191   EXPECT_EQ("int a           = 5;\n"
15192             "int one         = 1;\n"
15193             "\n"
15194             "int oneTwoThree = 123;\n"
15195             "int oneTwo      = 12;",
15196             format("int a = 5;\n"
15197                    "int one = 1;\n"
15198                    "\n"
15199                    "int oneTwoThree = 123;\n"
15200                    "int oneTwo = 12;",
15201                    Alignment));
15202 
15203   /* Test across comments */
15204   EXPECT_EQ("int a = 5;\n"
15205             "/* block comment */\n"
15206             "int oneTwoThree = 123;",
15207             format("int a = 5;\n"
15208                    "/* block comment */\n"
15209                    "int oneTwoThree=123;",
15210                    Alignment));
15211 
15212   EXPECT_EQ("int a = 5;\n"
15213             "// line comment\n"
15214             "int oneTwoThree = 123;",
15215             format("int a = 5;\n"
15216                    "// line comment\n"
15217                    "int oneTwoThree=123;",
15218                    Alignment));
15219 
15220   /* Test across comments and newlines */
15221   EXPECT_EQ("int a = 5;\n"
15222             "\n"
15223             "/* block comment */\n"
15224             "int oneTwoThree = 123;",
15225             format("int a = 5;\n"
15226                    "\n"
15227                    "/* block comment */\n"
15228                    "int oneTwoThree=123;",
15229                    Alignment));
15230 
15231   EXPECT_EQ("int a = 5;\n"
15232             "\n"
15233             "// line comment\n"
15234             "int oneTwoThree = 123;",
15235             format("int a = 5;\n"
15236                    "\n"
15237                    "// line comment\n"
15238                    "int oneTwoThree=123;",
15239                    Alignment));
15240 }
15241 
15242 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15243   FormatStyle Alignment = getLLVMStyle();
15244   Alignment.AlignConsecutiveDeclarations =
15245       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15246   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15247 
15248   Alignment.MaxEmptyLinesToKeep = 10;
15249   /* Test alignment across empty lines */
15250   EXPECT_EQ("int         a = 5;\n"
15251             "\n"
15252             "float const oneTwoThree = 123;",
15253             format("int a = 5;\n"
15254                    "\n"
15255                    "float const oneTwoThree = 123;",
15256                    Alignment));
15257   EXPECT_EQ("int         a = 5;\n"
15258             "float const one = 1;\n"
15259             "\n"
15260             "int         oneTwoThree = 123;",
15261             format("int a = 5;\n"
15262                    "float const one = 1;\n"
15263                    "\n"
15264                    "int oneTwoThree = 123;",
15265                    Alignment));
15266 
15267   /* Test across comments */
15268   EXPECT_EQ("float const a = 5;\n"
15269             "/* block comment */\n"
15270             "int         oneTwoThree = 123;",
15271             format("float const a = 5;\n"
15272                    "/* block comment */\n"
15273                    "int oneTwoThree=123;",
15274                    Alignment));
15275 
15276   EXPECT_EQ("float const a = 5;\n"
15277             "// line comment\n"
15278             "int         oneTwoThree = 123;",
15279             format("float const a = 5;\n"
15280                    "// line comment\n"
15281                    "int oneTwoThree=123;",
15282                    Alignment));
15283 
15284   /* Test across comments and newlines */
15285   EXPECT_EQ("float const a = 5;\n"
15286             "\n"
15287             "/* block comment */\n"
15288             "int         oneTwoThree = 123;",
15289             format("float const a = 5;\n"
15290                    "\n"
15291                    "/* block comment */\n"
15292                    "int         oneTwoThree=123;",
15293                    Alignment));
15294 
15295   EXPECT_EQ("float const a = 5;\n"
15296             "\n"
15297             "// line comment\n"
15298             "int         oneTwoThree = 123;",
15299             format("float const a = 5;\n"
15300                    "\n"
15301                    "// line comment\n"
15302                    "int oneTwoThree=123;",
15303                    Alignment));
15304 }
15305 
15306 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15307   FormatStyle Alignment = getLLVMStyle();
15308   Alignment.AlignConsecutiveBitFields =
15309       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15310 
15311   Alignment.MaxEmptyLinesToKeep = 10;
15312   /* Test alignment across empty lines */
15313   EXPECT_EQ("int a            : 5;\n"
15314             "\n"
15315             "int longbitfield : 6;",
15316             format("int a : 5;\n"
15317                    "\n"
15318                    "int longbitfield : 6;",
15319                    Alignment));
15320   EXPECT_EQ("int a            : 5;\n"
15321             "int one          : 1;\n"
15322             "\n"
15323             "int longbitfield : 6;",
15324             format("int a : 5;\n"
15325                    "int one : 1;\n"
15326                    "\n"
15327                    "int longbitfield : 6;",
15328                    Alignment));
15329 
15330   /* Test across comments */
15331   EXPECT_EQ("int a            : 5;\n"
15332             "/* block comment */\n"
15333             "int longbitfield : 6;",
15334             format("int a : 5;\n"
15335                    "/* block comment */\n"
15336                    "int longbitfield : 6;",
15337                    Alignment));
15338   EXPECT_EQ("int a            : 5;\n"
15339             "int one          : 1;\n"
15340             "// line comment\n"
15341             "int longbitfield : 6;",
15342             format("int a : 5;\n"
15343                    "int one : 1;\n"
15344                    "// line comment\n"
15345                    "int longbitfield : 6;",
15346                    Alignment));
15347 
15348   /* Test across comments and newlines */
15349   EXPECT_EQ("int a            : 5;\n"
15350             "/* block comment */\n"
15351             "\n"
15352             "int longbitfield : 6;",
15353             format("int a : 5;\n"
15354                    "/* block comment */\n"
15355                    "\n"
15356                    "int longbitfield : 6;",
15357                    Alignment));
15358   EXPECT_EQ("int a            : 5;\n"
15359             "int one          : 1;\n"
15360             "\n"
15361             "// line comment\n"
15362             "\n"
15363             "int longbitfield : 6;",
15364             format("int a : 5;\n"
15365                    "int one : 1;\n"
15366                    "\n"
15367                    "// line comment \n"
15368                    "\n"
15369                    "int longbitfield : 6;",
15370                    Alignment));
15371 }
15372 
15373 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15374   FormatStyle Alignment = getLLVMStyle();
15375   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15376   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15377 
15378   Alignment.MaxEmptyLinesToKeep = 10;
15379   /* Test alignment across empty lines */
15380   EXPECT_EQ("int a = 5;\n"
15381             "\n"
15382             "int oneTwoThree = 123;",
15383             format("int a       = 5;\n"
15384                    "\n"
15385                    "int oneTwoThree= 123;",
15386                    Alignment));
15387   EXPECT_EQ("int a   = 5;\n"
15388             "int one = 1;\n"
15389             "\n"
15390             "int oneTwoThree = 123;",
15391             format("int a = 5;\n"
15392                    "int one = 1;\n"
15393                    "\n"
15394                    "int oneTwoThree = 123;",
15395                    Alignment));
15396 
15397   /* Test across comments */
15398   EXPECT_EQ("int a           = 5;\n"
15399             "/* block comment */\n"
15400             "int oneTwoThree = 123;",
15401             format("int a = 5;\n"
15402                    "/* block comment */\n"
15403                    "int oneTwoThree=123;",
15404                    Alignment));
15405 
15406   EXPECT_EQ("int a           = 5;\n"
15407             "// line comment\n"
15408             "int oneTwoThree = 123;",
15409             format("int a = 5;\n"
15410                    "// line comment\n"
15411                    "int oneTwoThree=123;",
15412                    Alignment));
15413 
15414   EXPECT_EQ("int a           = 5;\n"
15415             "/*\n"
15416             " * multi-line block comment\n"
15417             " */\n"
15418             "int oneTwoThree = 123;",
15419             format("int a = 5;\n"
15420                    "/*\n"
15421                    " * multi-line block comment\n"
15422                    " */\n"
15423                    "int oneTwoThree=123;",
15424                    Alignment));
15425 
15426   EXPECT_EQ("int a           = 5;\n"
15427             "//\n"
15428             "// multi-line line comment\n"
15429             "//\n"
15430             "int oneTwoThree = 123;",
15431             format("int a = 5;\n"
15432                    "//\n"
15433                    "// multi-line line comment\n"
15434                    "//\n"
15435                    "int oneTwoThree=123;",
15436                    Alignment));
15437 
15438   /* Test across comments and newlines */
15439   EXPECT_EQ("int a = 5;\n"
15440             "\n"
15441             "/* block comment */\n"
15442             "int oneTwoThree = 123;",
15443             format("int a = 5;\n"
15444                    "\n"
15445                    "/* block comment */\n"
15446                    "int oneTwoThree=123;",
15447                    Alignment));
15448 
15449   EXPECT_EQ("int a = 5;\n"
15450             "\n"
15451             "// line comment\n"
15452             "int oneTwoThree = 123;",
15453             format("int a = 5;\n"
15454                    "\n"
15455                    "// line comment\n"
15456                    "int oneTwoThree=123;",
15457                    Alignment));
15458 }
15459 
15460 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15461   FormatStyle Alignment = getLLVMStyle();
15462   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15463   Alignment.AlignConsecutiveAssignments =
15464       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15465   verifyFormat("int a           = 5;\n"
15466                "int oneTwoThree = 123;",
15467                Alignment);
15468   verifyFormat("int a           = method();\n"
15469                "int oneTwoThree = 133;",
15470                Alignment);
15471   verifyFormat("a &= 5;\n"
15472                "bcd *= 5;\n"
15473                "ghtyf += 5;\n"
15474                "dvfvdb -= 5;\n"
15475                "a /= 5;\n"
15476                "vdsvsv %= 5;\n"
15477                "sfdbddfbdfbb ^= 5;\n"
15478                "dvsdsv |= 5;\n"
15479                "int dsvvdvsdvvv = 123;",
15480                Alignment);
15481   verifyFormat("int i = 1, j = 10;\n"
15482                "something = 2000;",
15483                Alignment);
15484   verifyFormat("something = 2000;\n"
15485                "int i = 1, j = 10;\n",
15486                Alignment);
15487   verifyFormat("something = 2000;\n"
15488                "another   = 911;\n"
15489                "int i = 1, j = 10;\n"
15490                "oneMore = 1;\n"
15491                "i       = 2;",
15492                Alignment);
15493   verifyFormat("int a   = 5;\n"
15494                "int one = 1;\n"
15495                "method();\n"
15496                "int oneTwoThree = 123;\n"
15497                "int oneTwo      = 12;",
15498                Alignment);
15499   verifyFormat("int oneTwoThree = 123;\n"
15500                "int oneTwo      = 12;\n"
15501                "method();\n",
15502                Alignment);
15503   verifyFormat("int oneTwoThree = 123; // comment\n"
15504                "int oneTwo      = 12;  // comment",
15505                Alignment);
15506 
15507   // Bug 25167
15508   /* Uncomment when fixed
15509     verifyFormat("#if A\n"
15510                  "#else\n"
15511                  "int aaaaaaaa = 12;\n"
15512                  "#endif\n"
15513                  "#if B\n"
15514                  "#else\n"
15515                  "int a = 12;\n"
15516                  "#endif\n",
15517                  Alignment);
15518     verifyFormat("enum foo {\n"
15519                  "#if A\n"
15520                  "#else\n"
15521                  "  aaaaaaaa = 12;\n"
15522                  "#endif\n"
15523                  "#if B\n"
15524                  "#else\n"
15525                  "  a = 12;\n"
15526                  "#endif\n"
15527                  "};\n",
15528                  Alignment);
15529   */
15530 
15531   Alignment.MaxEmptyLinesToKeep = 10;
15532   /* Test alignment across empty lines */
15533   EXPECT_EQ("int a           = 5;\n"
15534             "\n"
15535             "int oneTwoThree = 123;",
15536             format("int a       = 5;\n"
15537                    "\n"
15538                    "int oneTwoThree= 123;",
15539                    Alignment));
15540   EXPECT_EQ("int a           = 5;\n"
15541             "int one         = 1;\n"
15542             "\n"
15543             "int oneTwoThree = 123;",
15544             format("int a = 5;\n"
15545                    "int one = 1;\n"
15546                    "\n"
15547                    "int oneTwoThree = 123;",
15548                    Alignment));
15549   EXPECT_EQ("int a           = 5;\n"
15550             "int one         = 1;\n"
15551             "\n"
15552             "int oneTwoThree = 123;\n"
15553             "int oneTwo      = 12;",
15554             format("int a = 5;\n"
15555                    "int one = 1;\n"
15556                    "\n"
15557                    "int oneTwoThree = 123;\n"
15558                    "int oneTwo = 12;",
15559                    Alignment));
15560 
15561   /* Test across comments */
15562   EXPECT_EQ("int a           = 5;\n"
15563             "/* block comment */\n"
15564             "int oneTwoThree = 123;",
15565             format("int a = 5;\n"
15566                    "/* block comment */\n"
15567                    "int oneTwoThree=123;",
15568                    Alignment));
15569 
15570   EXPECT_EQ("int a           = 5;\n"
15571             "// line comment\n"
15572             "int oneTwoThree = 123;",
15573             format("int a = 5;\n"
15574                    "// line comment\n"
15575                    "int oneTwoThree=123;",
15576                    Alignment));
15577 
15578   /* Test across comments and newlines */
15579   EXPECT_EQ("int a           = 5;\n"
15580             "\n"
15581             "/* block comment */\n"
15582             "int oneTwoThree = 123;",
15583             format("int a = 5;\n"
15584                    "\n"
15585                    "/* block comment */\n"
15586                    "int oneTwoThree=123;",
15587                    Alignment));
15588 
15589   EXPECT_EQ("int a           = 5;\n"
15590             "\n"
15591             "// line comment\n"
15592             "int oneTwoThree = 123;",
15593             format("int a = 5;\n"
15594                    "\n"
15595                    "// line comment\n"
15596                    "int oneTwoThree=123;",
15597                    Alignment));
15598 
15599   EXPECT_EQ("int a           = 5;\n"
15600             "//\n"
15601             "// multi-line line comment\n"
15602             "//\n"
15603             "int oneTwoThree = 123;",
15604             format("int a = 5;\n"
15605                    "//\n"
15606                    "// multi-line line comment\n"
15607                    "//\n"
15608                    "int oneTwoThree=123;",
15609                    Alignment));
15610 
15611   EXPECT_EQ("int a           = 5;\n"
15612             "/*\n"
15613             " *  multi-line block comment\n"
15614             " */\n"
15615             "int oneTwoThree = 123;",
15616             format("int a = 5;\n"
15617                    "/*\n"
15618                    " *  multi-line block comment\n"
15619                    " */\n"
15620                    "int oneTwoThree=123;",
15621                    Alignment));
15622 
15623   EXPECT_EQ("int a           = 5;\n"
15624             "\n"
15625             "/* block comment */\n"
15626             "\n"
15627             "\n"
15628             "\n"
15629             "int oneTwoThree = 123;",
15630             format("int a = 5;\n"
15631                    "\n"
15632                    "/* block comment */\n"
15633                    "\n"
15634                    "\n"
15635                    "\n"
15636                    "int oneTwoThree=123;",
15637                    Alignment));
15638 
15639   EXPECT_EQ("int a           = 5;\n"
15640             "\n"
15641             "// line comment\n"
15642             "\n"
15643             "\n"
15644             "\n"
15645             "int oneTwoThree = 123;",
15646             format("int a = 5;\n"
15647                    "\n"
15648                    "// line comment\n"
15649                    "\n"
15650                    "\n"
15651                    "\n"
15652                    "int oneTwoThree=123;",
15653                    Alignment));
15654 
15655   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15656   verifyFormat("#define A \\\n"
15657                "  int aaaa       = 12; \\\n"
15658                "  int b          = 23; \\\n"
15659                "  int ccc        = 234; \\\n"
15660                "  int dddddddddd = 2345;",
15661                Alignment);
15662   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15663   verifyFormat("#define A               \\\n"
15664                "  int aaaa       = 12;  \\\n"
15665                "  int b          = 23;  \\\n"
15666                "  int ccc        = 234; \\\n"
15667                "  int dddddddddd = 2345;",
15668                Alignment);
15669   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15670   verifyFormat("#define A                                                      "
15671                "                \\\n"
15672                "  int aaaa       = 12;                                         "
15673                "                \\\n"
15674                "  int b          = 23;                                         "
15675                "                \\\n"
15676                "  int ccc        = 234;                                        "
15677                "                \\\n"
15678                "  int dddddddddd = 2345;",
15679                Alignment);
15680   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15681                "k = 4, int l = 5,\n"
15682                "                  int m = 6) {\n"
15683                "  int j      = 10;\n"
15684                "  otherThing = 1;\n"
15685                "}",
15686                Alignment);
15687   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15688                "  int i   = 1;\n"
15689                "  int j   = 2;\n"
15690                "  int big = 10000;\n"
15691                "}",
15692                Alignment);
15693   verifyFormat("class C {\n"
15694                "public:\n"
15695                "  int i            = 1;\n"
15696                "  virtual void f() = 0;\n"
15697                "};",
15698                Alignment);
15699   verifyFormat("int i = 1;\n"
15700                "if (SomeType t = getSomething()) {\n"
15701                "}\n"
15702                "int j   = 2;\n"
15703                "int big = 10000;",
15704                Alignment);
15705   verifyFormat("int j = 7;\n"
15706                "for (int k = 0; k < N; ++k) {\n"
15707                "}\n"
15708                "int j   = 2;\n"
15709                "int big = 10000;\n"
15710                "}",
15711                Alignment);
15712   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15713   verifyFormat("int i = 1;\n"
15714                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15715                "    = someLooooooooooooooooongFunction();\n"
15716                "int j = 2;",
15717                Alignment);
15718   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15719   verifyFormat("int i = 1;\n"
15720                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15721                "    someLooooooooooooooooongFunction();\n"
15722                "int j = 2;",
15723                Alignment);
15724 
15725   verifyFormat("auto lambda = []() {\n"
15726                "  auto i = 0;\n"
15727                "  return 0;\n"
15728                "};\n"
15729                "int i  = 0;\n"
15730                "auto v = type{\n"
15731                "    i = 1,   //\n"
15732                "    (i = 2), //\n"
15733                "    i = 3    //\n"
15734                "};",
15735                Alignment);
15736 
15737   verifyFormat(
15738       "int i      = 1;\n"
15739       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15740       "                          loooooooooooooooooooooongParameterB);\n"
15741       "int j      = 2;",
15742       Alignment);
15743 
15744   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15745                "          typename B   = very_long_type_name_1,\n"
15746                "          typename T_2 = very_long_type_name_2>\n"
15747                "auto foo() {}\n",
15748                Alignment);
15749   verifyFormat("int a, b = 1;\n"
15750                "int c  = 2;\n"
15751                "int dd = 3;\n",
15752                Alignment);
15753   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15754                "float b[1][] = {{3.f}};\n",
15755                Alignment);
15756   verifyFormat("for (int i = 0; i < 1; i++)\n"
15757                "  int x = 1;\n",
15758                Alignment);
15759   verifyFormat("for (i = 0; i < 1; i++)\n"
15760                "  x = 1;\n"
15761                "y = 1;\n",
15762                Alignment);
15763 
15764   Alignment.ReflowComments = true;
15765   Alignment.ColumnLimit = 50;
15766   EXPECT_EQ("int x   = 0;\n"
15767             "int yy  = 1; /// specificlennospace\n"
15768             "int zzz = 2;\n",
15769             format("int x   = 0;\n"
15770                    "int yy  = 1; ///specificlennospace\n"
15771                    "int zzz = 2;\n",
15772                    Alignment));
15773 }
15774 
15775 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15776   FormatStyle Alignment = getLLVMStyle();
15777   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15778   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15779   verifyFormat("int a = 5;\n"
15780                "int oneTwoThree = 123;",
15781                Alignment);
15782   verifyFormat("int a = 5;\n"
15783                "int oneTwoThree = 123;",
15784                Alignment);
15785 
15786   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15787   verifyFormat("int a           = 5;\n"
15788                "int oneTwoThree = 123;",
15789                Alignment);
15790   verifyFormat("int a           = method();\n"
15791                "int oneTwoThree = 133;",
15792                Alignment);
15793   verifyFormat("a &= 5;\n"
15794                "bcd *= 5;\n"
15795                "ghtyf += 5;\n"
15796                "dvfvdb -= 5;\n"
15797                "a /= 5;\n"
15798                "vdsvsv %= 5;\n"
15799                "sfdbddfbdfbb ^= 5;\n"
15800                "dvsdsv |= 5;\n"
15801                "int dsvvdvsdvvv = 123;",
15802                Alignment);
15803   verifyFormat("int i = 1, j = 10;\n"
15804                "something = 2000;",
15805                Alignment);
15806   verifyFormat("something = 2000;\n"
15807                "int i = 1, j = 10;\n",
15808                Alignment);
15809   verifyFormat("something = 2000;\n"
15810                "another   = 911;\n"
15811                "int i = 1, j = 10;\n"
15812                "oneMore = 1;\n"
15813                "i       = 2;",
15814                Alignment);
15815   verifyFormat("int a   = 5;\n"
15816                "int one = 1;\n"
15817                "method();\n"
15818                "int oneTwoThree = 123;\n"
15819                "int oneTwo      = 12;",
15820                Alignment);
15821   verifyFormat("int oneTwoThree = 123;\n"
15822                "int oneTwo      = 12;\n"
15823                "method();\n",
15824                Alignment);
15825   verifyFormat("int oneTwoThree = 123; // comment\n"
15826                "int oneTwo      = 12;  // comment",
15827                Alignment);
15828 
15829   // Bug 25167
15830   /* Uncomment when fixed
15831     verifyFormat("#if A\n"
15832                  "#else\n"
15833                  "int aaaaaaaa = 12;\n"
15834                  "#endif\n"
15835                  "#if B\n"
15836                  "#else\n"
15837                  "int a = 12;\n"
15838                  "#endif\n",
15839                  Alignment);
15840     verifyFormat("enum foo {\n"
15841                  "#if A\n"
15842                  "#else\n"
15843                  "  aaaaaaaa = 12;\n"
15844                  "#endif\n"
15845                  "#if B\n"
15846                  "#else\n"
15847                  "  a = 12;\n"
15848                  "#endif\n"
15849                  "};\n",
15850                  Alignment);
15851   */
15852 
15853   EXPECT_EQ("int a = 5;\n"
15854             "\n"
15855             "int oneTwoThree = 123;",
15856             format("int a       = 5;\n"
15857                    "\n"
15858                    "int oneTwoThree= 123;",
15859                    Alignment));
15860   EXPECT_EQ("int a   = 5;\n"
15861             "int one = 1;\n"
15862             "\n"
15863             "int oneTwoThree = 123;",
15864             format("int a = 5;\n"
15865                    "int one = 1;\n"
15866                    "\n"
15867                    "int oneTwoThree = 123;",
15868                    Alignment));
15869   EXPECT_EQ("int a   = 5;\n"
15870             "int one = 1;\n"
15871             "\n"
15872             "int oneTwoThree = 123;\n"
15873             "int oneTwo      = 12;",
15874             format("int a = 5;\n"
15875                    "int one = 1;\n"
15876                    "\n"
15877                    "int oneTwoThree = 123;\n"
15878                    "int oneTwo = 12;",
15879                    Alignment));
15880   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15881   verifyFormat("#define A \\\n"
15882                "  int aaaa       = 12; \\\n"
15883                "  int b          = 23; \\\n"
15884                "  int ccc        = 234; \\\n"
15885                "  int dddddddddd = 2345;",
15886                Alignment);
15887   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15888   verifyFormat("#define A               \\\n"
15889                "  int aaaa       = 12;  \\\n"
15890                "  int b          = 23;  \\\n"
15891                "  int ccc        = 234; \\\n"
15892                "  int dddddddddd = 2345;",
15893                Alignment);
15894   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15895   verifyFormat("#define A                                                      "
15896                "                \\\n"
15897                "  int aaaa       = 12;                                         "
15898                "                \\\n"
15899                "  int b          = 23;                                         "
15900                "                \\\n"
15901                "  int ccc        = 234;                                        "
15902                "                \\\n"
15903                "  int dddddddddd = 2345;",
15904                Alignment);
15905   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15906                "k = 4, int l = 5,\n"
15907                "                  int m = 6) {\n"
15908                "  int j      = 10;\n"
15909                "  otherThing = 1;\n"
15910                "}",
15911                Alignment);
15912   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15913                "  int i   = 1;\n"
15914                "  int j   = 2;\n"
15915                "  int big = 10000;\n"
15916                "}",
15917                Alignment);
15918   verifyFormat("class C {\n"
15919                "public:\n"
15920                "  int i            = 1;\n"
15921                "  virtual void f() = 0;\n"
15922                "};",
15923                Alignment);
15924   verifyFormat("int i = 1;\n"
15925                "if (SomeType t = getSomething()) {\n"
15926                "}\n"
15927                "int j   = 2;\n"
15928                "int big = 10000;",
15929                Alignment);
15930   verifyFormat("int j = 7;\n"
15931                "for (int k = 0; k < N; ++k) {\n"
15932                "}\n"
15933                "int j   = 2;\n"
15934                "int big = 10000;\n"
15935                "}",
15936                Alignment);
15937   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15938   verifyFormat("int i = 1;\n"
15939                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15940                "    = someLooooooooooooooooongFunction();\n"
15941                "int j = 2;",
15942                Alignment);
15943   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15944   verifyFormat("int i = 1;\n"
15945                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15946                "    someLooooooooooooooooongFunction();\n"
15947                "int j = 2;",
15948                Alignment);
15949 
15950   verifyFormat("auto lambda = []() {\n"
15951                "  auto i = 0;\n"
15952                "  return 0;\n"
15953                "};\n"
15954                "int i  = 0;\n"
15955                "auto v = type{\n"
15956                "    i = 1,   //\n"
15957                "    (i = 2), //\n"
15958                "    i = 3    //\n"
15959                "};",
15960                Alignment);
15961 
15962   verifyFormat(
15963       "int i      = 1;\n"
15964       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15965       "                          loooooooooooooooooooooongParameterB);\n"
15966       "int j      = 2;",
15967       Alignment);
15968 
15969   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15970                "          typename B   = very_long_type_name_1,\n"
15971                "          typename T_2 = very_long_type_name_2>\n"
15972                "auto foo() {}\n",
15973                Alignment);
15974   verifyFormat("int a, b = 1;\n"
15975                "int c  = 2;\n"
15976                "int dd = 3;\n",
15977                Alignment);
15978   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15979                "float b[1][] = {{3.f}};\n",
15980                Alignment);
15981   verifyFormat("for (int i = 0; i < 1; i++)\n"
15982                "  int x = 1;\n",
15983                Alignment);
15984   verifyFormat("for (i = 0; i < 1; i++)\n"
15985                "  x = 1;\n"
15986                "y = 1;\n",
15987                Alignment);
15988 
15989   Alignment.ReflowComments = true;
15990   Alignment.ColumnLimit = 50;
15991   EXPECT_EQ("int x   = 0;\n"
15992             "int yy  = 1; /// specificlennospace\n"
15993             "int zzz = 2;\n",
15994             format("int x   = 0;\n"
15995                    "int yy  = 1; ///specificlennospace\n"
15996                    "int zzz = 2;\n",
15997                    Alignment));
15998 }
15999 
16000 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16001   FormatStyle Alignment = getLLVMStyle();
16002   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16003   verifyFormat("int const a     : 5;\n"
16004                "int oneTwoThree : 23;",
16005                Alignment);
16006 
16007   // Initializers are allowed starting with c++2a
16008   verifyFormat("int const a     : 5 = 1;\n"
16009                "int oneTwoThree : 23 = 0;",
16010                Alignment);
16011 
16012   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16013   verifyFormat("int const a           : 5;\n"
16014                "int       oneTwoThree : 23;",
16015                Alignment);
16016 
16017   verifyFormat("int const a           : 5;  // comment\n"
16018                "int       oneTwoThree : 23; // comment",
16019                Alignment);
16020 
16021   verifyFormat("int const a           : 5 = 1;\n"
16022                "int       oneTwoThree : 23 = 0;",
16023                Alignment);
16024 
16025   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16026   verifyFormat("int const a           : 5  = 1;\n"
16027                "int       oneTwoThree : 23 = 0;",
16028                Alignment);
16029   verifyFormat("int const a           : 5  = {1};\n"
16030                "int       oneTwoThree : 23 = 0;",
16031                Alignment);
16032 
16033   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16034   verifyFormat("int const a          :5;\n"
16035                "int       oneTwoThree:23;",
16036                Alignment);
16037 
16038   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16039   verifyFormat("int const a           :5;\n"
16040                "int       oneTwoThree :23;",
16041                Alignment);
16042 
16043   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16044   verifyFormat("int const a          : 5;\n"
16045                "int       oneTwoThree: 23;",
16046                Alignment);
16047 
16048   // Known limitations: ':' is only recognized as a bitfield colon when
16049   // followed by a number.
16050   /*
16051   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16052                "int a           : 5;",
16053                Alignment);
16054   */
16055 }
16056 
16057 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16058   FormatStyle Alignment = getLLVMStyle();
16059   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16060   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16061   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16062   verifyFormat("float const a = 5;\n"
16063                "int oneTwoThree = 123;",
16064                Alignment);
16065   verifyFormat("int a = 5;\n"
16066                "float const oneTwoThree = 123;",
16067                Alignment);
16068 
16069   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16070   verifyFormat("float const a = 5;\n"
16071                "int         oneTwoThree = 123;",
16072                Alignment);
16073   verifyFormat("int         a = method();\n"
16074                "float const oneTwoThree = 133;",
16075                Alignment);
16076   verifyFormat("int i = 1, j = 10;\n"
16077                "something = 2000;",
16078                Alignment);
16079   verifyFormat("something = 2000;\n"
16080                "int i = 1, j = 10;\n",
16081                Alignment);
16082   verifyFormat("float      something = 2000;\n"
16083                "double     another = 911;\n"
16084                "int        i = 1, j = 10;\n"
16085                "const int *oneMore = 1;\n"
16086                "unsigned   i = 2;",
16087                Alignment);
16088   verifyFormat("float a = 5;\n"
16089                "int   one = 1;\n"
16090                "method();\n"
16091                "const double       oneTwoThree = 123;\n"
16092                "const unsigned int oneTwo = 12;",
16093                Alignment);
16094   verifyFormat("int      oneTwoThree{0}; // comment\n"
16095                "unsigned oneTwo;         // comment",
16096                Alignment);
16097   verifyFormat("unsigned int       *a;\n"
16098                "int                *b;\n"
16099                "unsigned int Const *c;\n"
16100                "unsigned int const *d;\n"
16101                "unsigned int Const &e;\n"
16102                "unsigned int const &f;",
16103                Alignment);
16104   verifyFormat("Const unsigned int *c;\n"
16105                "const unsigned int *d;\n"
16106                "Const unsigned int &e;\n"
16107                "const unsigned int &f;\n"
16108                "const unsigned      g;\n"
16109                "Const unsigned      h;",
16110                Alignment);
16111   EXPECT_EQ("float const a = 5;\n"
16112             "\n"
16113             "int oneTwoThree = 123;",
16114             format("float const   a = 5;\n"
16115                    "\n"
16116                    "int           oneTwoThree= 123;",
16117                    Alignment));
16118   EXPECT_EQ("float a = 5;\n"
16119             "int   one = 1;\n"
16120             "\n"
16121             "unsigned oneTwoThree = 123;",
16122             format("float    a = 5;\n"
16123                    "int      one = 1;\n"
16124                    "\n"
16125                    "unsigned oneTwoThree = 123;",
16126                    Alignment));
16127   EXPECT_EQ("float a = 5;\n"
16128             "int   one = 1;\n"
16129             "\n"
16130             "unsigned oneTwoThree = 123;\n"
16131             "int      oneTwo = 12;",
16132             format("float    a = 5;\n"
16133                    "int one = 1;\n"
16134                    "\n"
16135                    "unsigned oneTwoThree = 123;\n"
16136                    "int oneTwo = 12;",
16137                    Alignment));
16138   // Function prototype alignment
16139   verifyFormat("int    a();\n"
16140                "double b();",
16141                Alignment);
16142   verifyFormat("int    a(int x);\n"
16143                "double b();",
16144                Alignment);
16145   unsigned OldColumnLimit = Alignment.ColumnLimit;
16146   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16147   // otherwise the function parameters will be re-flowed onto a single line.
16148   Alignment.ColumnLimit = 0;
16149   EXPECT_EQ("int    a(int   x,\n"
16150             "         float y);\n"
16151             "double b(int    x,\n"
16152             "         double y);",
16153             format("int a(int x,\n"
16154                    " float y);\n"
16155                    "double b(int x,\n"
16156                    " double y);",
16157                    Alignment));
16158   // This ensures that function parameters of function declarations are
16159   // correctly indented when their owning functions are indented.
16160   // The failure case here is for 'double y' to not be indented enough.
16161   EXPECT_EQ("double a(int x);\n"
16162             "int    b(int    y,\n"
16163             "         double z);",
16164             format("double a(int x);\n"
16165                    "int b(int y,\n"
16166                    " double z);",
16167                    Alignment));
16168   // Set ColumnLimit low so that we induce wrapping immediately after
16169   // the function name and opening paren.
16170   Alignment.ColumnLimit = 13;
16171   verifyFormat("int function(\n"
16172                "    int  x,\n"
16173                "    bool y);",
16174                Alignment);
16175   Alignment.ColumnLimit = OldColumnLimit;
16176   // Ensure function pointers don't screw up recursive alignment
16177   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16178                "double b();",
16179                Alignment);
16180   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16181   // Ensure recursive alignment is broken by function braces, so that the
16182   // "a = 1" does not align with subsequent assignments inside the function
16183   // body.
16184   verifyFormat("int func(int a = 1) {\n"
16185                "  int b  = 2;\n"
16186                "  int cc = 3;\n"
16187                "}",
16188                Alignment);
16189   verifyFormat("float      something = 2000;\n"
16190                "double     another   = 911;\n"
16191                "int        i = 1, j = 10;\n"
16192                "const int *oneMore = 1;\n"
16193                "unsigned   i       = 2;",
16194                Alignment);
16195   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16196                "unsigned oneTwo      = 0;   // comment",
16197                Alignment);
16198   // Make sure that scope is correctly tracked, in the absence of braces
16199   verifyFormat("for (int i = 0; i < n; i++)\n"
16200                "  j = i;\n"
16201                "double x = 1;\n",
16202                Alignment);
16203   verifyFormat("if (int i = 0)\n"
16204                "  j = i;\n"
16205                "double x = 1;\n",
16206                Alignment);
16207   // Ensure operator[] and operator() are comprehended
16208   verifyFormat("struct test {\n"
16209                "  long long int foo();\n"
16210                "  int           operator[](int a);\n"
16211                "  double        bar();\n"
16212                "};\n",
16213                Alignment);
16214   verifyFormat("struct test {\n"
16215                "  long long int foo();\n"
16216                "  int           operator()(int a);\n"
16217                "  double        bar();\n"
16218                "};\n",
16219                Alignment);
16220 
16221   // PAS_Right
16222   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16223             "  int const i   = 1;\n"
16224             "  int      *j   = 2;\n"
16225             "  int       big = 10000;\n"
16226             "\n"
16227             "  unsigned oneTwoThree = 123;\n"
16228             "  int      oneTwo      = 12;\n"
16229             "  method();\n"
16230             "  float k  = 2;\n"
16231             "  int   ll = 10000;\n"
16232             "}",
16233             format("void SomeFunction(int parameter= 0) {\n"
16234                    " int const  i= 1;\n"
16235                    "  int *j=2;\n"
16236                    " int big  =  10000;\n"
16237                    "\n"
16238                    "unsigned oneTwoThree  =123;\n"
16239                    "int oneTwo = 12;\n"
16240                    "  method();\n"
16241                    "float k= 2;\n"
16242                    "int ll=10000;\n"
16243                    "}",
16244                    Alignment));
16245   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16246             "  int const i   = 1;\n"
16247             "  int     **j   = 2, ***k;\n"
16248             "  int      &k   = i;\n"
16249             "  int     &&l   = i + j;\n"
16250             "  int       big = 10000;\n"
16251             "\n"
16252             "  unsigned oneTwoThree = 123;\n"
16253             "  int      oneTwo      = 12;\n"
16254             "  method();\n"
16255             "  float k  = 2;\n"
16256             "  int   ll = 10000;\n"
16257             "}",
16258             format("void SomeFunction(int parameter= 0) {\n"
16259                    " int const  i= 1;\n"
16260                    "  int **j=2,***k;\n"
16261                    "int &k=i;\n"
16262                    "int &&l=i+j;\n"
16263                    " int big  =  10000;\n"
16264                    "\n"
16265                    "unsigned oneTwoThree  =123;\n"
16266                    "int oneTwo = 12;\n"
16267                    "  method();\n"
16268                    "float k= 2;\n"
16269                    "int ll=10000;\n"
16270                    "}",
16271                    Alignment));
16272   // variables are aligned at their name, pointers are at the right most
16273   // position
16274   verifyFormat("int   *a;\n"
16275                "int  **b;\n"
16276                "int ***c;\n"
16277                "int    foobar;\n",
16278                Alignment);
16279 
16280   // PAS_Left
16281   FormatStyle AlignmentLeft = Alignment;
16282   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16283   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16284             "  int const i   = 1;\n"
16285             "  int*      j   = 2;\n"
16286             "  int       big = 10000;\n"
16287             "\n"
16288             "  unsigned oneTwoThree = 123;\n"
16289             "  int      oneTwo      = 12;\n"
16290             "  method();\n"
16291             "  float k  = 2;\n"
16292             "  int   ll = 10000;\n"
16293             "}",
16294             format("void SomeFunction(int parameter= 0) {\n"
16295                    " int const  i= 1;\n"
16296                    "  int *j=2;\n"
16297                    " int big  =  10000;\n"
16298                    "\n"
16299                    "unsigned oneTwoThree  =123;\n"
16300                    "int oneTwo = 12;\n"
16301                    "  method();\n"
16302                    "float k= 2;\n"
16303                    "int ll=10000;\n"
16304                    "}",
16305                    AlignmentLeft));
16306   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16307             "  int const i   = 1;\n"
16308             "  int**     j   = 2;\n"
16309             "  int&      k   = i;\n"
16310             "  int&&     l   = i + j;\n"
16311             "  int       big = 10000;\n"
16312             "\n"
16313             "  unsigned oneTwoThree = 123;\n"
16314             "  int      oneTwo      = 12;\n"
16315             "  method();\n"
16316             "  float k  = 2;\n"
16317             "  int   ll = 10000;\n"
16318             "}",
16319             format("void SomeFunction(int parameter= 0) {\n"
16320                    " int const  i= 1;\n"
16321                    "  int **j=2;\n"
16322                    "int &k=i;\n"
16323                    "int &&l=i+j;\n"
16324                    " int big  =  10000;\n"
16325                    "\n"
16326                    "unsigned oneTwoThree  =123;\n"
16327                    "int oneTwo = 12;\n"
16328                    "  method();\n"
16329                    "float k= 2;\n"
16330                    "int ll=10000;\n"
16331                    "}",
16332                    AlignmentLeft));
16333   // variables are aligned at their name, pointers are at the left most position
16334   verifyFormat("int*   a;\n"
16335                "int**  b;\n"
16336                "int*** c;\n"
16337                "int    foobar;\n",
16338                AlignmentLeft);
16339 
16340   // PAS_Middle
16341   FormatStyle AlignmentMiddle = Alignment;
16342   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16343   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16344             "  int const i   = 1;\n"
16345             "  int *     j   = 2;\n"
16346             "  int       big = 10000;\n"
16347             "\n"
16348             "  unsigned oneTwoThree = 123;\n"
16349             "  int      oneTwo      = 12;\n"
16350             "  method();\n"
16351             "  float k  = 2;\n"
16352             "  int   ll = 10000;\n"
16353             "}",
16354             format("void SomeFunction(int parameter= 0) {\n"
16355                    " int const  i= 1;\n"
16356                    "  int *j=2;\n"
16357                    " int big  =  10000;\n"
16358                    "\n"
16359                    "unsigned oneTwoThree  =123;\n"
16360                    "int oneTwo = 12;\n"
16361                    "  method();\n"
16362                    "float k= 2;\n"
16363                    "int ll=10000;\n"
16364                    "}",
16365                    AlignmentMiddle));
16366   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16367             "  int const i   = 1;\n"
16368             "  int **    j   = 2, ***k;\n"
16369             "  int &     k   = i;\n"
16370             "  int &&    l   = i + j;\n"
16371             "  int       big = 10000;\n"
16372             "\n"
16373             "  unsigned oneTwoThree = 123;\n"
16374             "  int      oneTwo      = 12;\n"
16375             "  method();\n"
16376             "  float k  = 2;\n"
16377             "  int   ll = 10000;\n"
16378             "}",
16379             format("void SomeFunction(int parameter= 0) {\n"
16380                    " int const  i= 1;\n"
16381                    "  int **j=2,***k;\n"
16382                    "int &k=i;\n"
16383                    "int &&l=i+j;\n"
16384                    " int big  =  10000;\n"
16385                    "\n"
16386                    "unsigned oneTwoThree  =123;\n"
16387                    "int oneTwo = 12;\n"
16388                    "  method();\n"
16389                    "float k= 2;\n"
16390                    "int ll=10000;\n"
16391                    "}",
16392                    AlignmentMiddle));
16393   // variables are aligned at their name, pointers are in the middle
16394   verifyFormat("int *   a;\n"
16395                "int *   b;\n"
16396                "int *** c;\n"
16397                "int     foobar;\n",
16398                AlignmentMiddle);
16399 
16400   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16401   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16402   verifyFormat("#define A \\\n"
16403                "  int       aaaa = 12; \\\n"
16404                "  float     b = 23; \\\n"
16405                "  const int ccc = 234; \\\n"
16406                "  unsigned  dddddddddd = 2345;",
16407                Alignment);
16408   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16409   verifyFormat("#define A              \\\n"
16410                "  int       aaaa = 12; \\\n"
16411                "  float     b = 23;    \\\n"
16412                "  const int ccc = 234; \\\n"
16413                "  unsigned  dddddddddd = 2345;",
16414                Alignment);
16415   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16416   Alignment.ColumnLimit = 30;
16417   verifyFormat("#define A                    \\\n"
16418                "  int       aaaa = 12;       \\\n"
16419                "  float     b = 23;          \\\n"
16420                "  const int ccc = 234;       \\\n"
16421                "  int       dddddddddd = 2345;",
16422                Alignment);
16423   Alignment.ColumnLimit = 80;
16424   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16425                "k = 4, int l = 5,\n"
16426                "                  int m = 6) {\n"
16427                "  const int j = 10;\n"
16428                "  otherThing = 1;\n"
16429                "}",
16430                Alignment);
16431   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16432                "  int const i = 1;\n"
16433                "  int      *j = 2;\n"
16434                "  int       big = 10000;\n"
16435                "}",
16436                Alignment);
16437   verifyFormat("class C {\n"
16438                "public:\n"
16439                "  int          i = 1;\n"
16440                "  virtual void f() = 0;\n"
16441                "};",
16442                Alignment);
16443   verifyFormat("float i = 1;\n"
16444                "if (SomeType t = getSomething()) {\n"
16445                "}\n"
16446                "const unsigned j = 2;\n"
16447                "int            big = 10000;",
16448                Alignment);
16449   verifyFormat("float j = 7;\n"
16450                "for (int k = 0; k < N; ++k) {\n"
16451                "}\n"
16452                "unsigned j = 2;\n"
16453                "int      big = 10000;\n"
16454                "}",
16455                Alignment);
16456   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16457   verifyFormat("float              i = 1;\n"
16458                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16459                "    = someLooooooooooooooooongFunction();\n"
16460                "int j = 2;",
16461                Alignment);
16462   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16463   verifyFormat("int                i = 1;\n"
16464                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16465                "    someLooooooooooooooooongFunction();\n"
16466                "int j = 2;",
16467                Alignment);
16468 
16469   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16470   verifyFormat("auto lambda = []() {\n"
16471                "  auto  ii = 0;\n"
16472                "  float j  = 0;\n"
16473                "  return 0;\n"
16474                "};\n"
16475                "int   i  = 0;\n"
16476                "float i2 = 0;\n"
16477                "auto  v  = type{\n"
16478                "    i = 1,   //\n"
16479                "    (i = 2), //\n"
16480                "    i = 3    //\n"
16481                "};",
16482                Alignment);
16483   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16484 
16485   verifyFormat(
16486       "int      i = 1;\n"
16487       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16488       "                          loooooooooooooooooooooongParameterB);\n"
16489       "int      j = 2;",
16490       Alignment);
16491 
16492   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16493   // We expect declarations and assignments to align, as long as it doesn't
16494   // exceed the column limit, starting a new alignment sequence whenever it
16495   // happens.
16496   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16497   Alignment.ColumnLimit = 30;
16498   verifyFormat("float    ii              = 1;\n"
16499                "unsigned j               = 2;\n"
16500                "int someVerylongVariable = 1;\n"
16501                "AnotherLongType  ll = 123456;\n"
16502                "VeryVeryLongType k  = 2;\n"
16503                "int              myvar = 1;",
16504                Alignment);
16505   Alignment.ColumnLimit = 80;
16506   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16507 
16508   verifyFormat(
16509       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16510       "          typename LongType, typename B>\n"
16511       "auto foo() {}\n",
16512       Alignment);
16513   verifyFormat("float a, b = 1;\n"
16514                "int   c = 2;\n"
16515                "int   dd = 3;\n",
16516                Alignment);
16517   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16518                "float b[1][] = {{3.f}};\n",
16519                Alignment);
16520   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16521   verifyFormat("float a, b = 1;\n"
16522                "int   c  = 2;\n"
16523                "int   dd = 3;\n",
16524                Alignment);
16525   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16526                "float b[1][] = {{3.f}};\n",
16527                Alignment);
16528   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16529 
16530   Alignment.ColumnLimit = 30;
16531   Alignment.BinPackParameters = false;
16532   verifyFormat("void foo(float     a,\n"
16533                "         float     b,\n"
16534                "         int       c,\n"
16535                "         uint32_t *d) {\n"
16536                "  int   *e = 0;\n"
16537                "  float  f = 0;\n"
16538                "  double g = 0;\n"
16539                "}\n"
16540                "void bar(ino_t     a,\n"
16541                "         int       b,\n"
16542                "         uint32_t *c,\n"
16543                "         bool      d) {}\n",
16544                Alignment);
16545   Alignment.BinPackParameters = true;
16546   Alignment.ColumnLimit = 80;
16547 
16548   // Bug 33507
16549   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16550   verifyFormat(
16551       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16552       "  static const Version verVs2017;\n"
16553       "  return true;\n"
16554       "});\n",
16555       Alignment);
16556   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16557 
16558   // See llvm.org/PR35641
16559   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16560   verifyFormat("int func() { //\n"
16561                "  int      b;\n"
16562                "  unsigned c;\n"
16563                "}",
16564                Alignment);
16565 
16566   // See PR37175
16567   FormatStyle Style = getMozillaStyle();
16568   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16569   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16570             "foo(int a);",
16571             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16572 
16573   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16574   verifyFormat("unsigned int*       a;\n"
16575                "int*                b;\n"
16576                "unsigned int Const* c;\n"
16577                "unsigned int const* d;\n"
16578                "unsigned int Const& e;\n"
16579                "unsigned int const& f;",
16580                Alignment);
16581   verifyFormat("Const unsigned int* c;\n"
16582                "const unsigned int* d;\n"
16583                "Const unsigned int& e;\n"
16584                "const unsigned int& f;\n"
16585                "const unsigned      g;\n"
16586                "Const unsigned      h;",
16587                Alignment);
16588 
16589   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16590   verifyFormat("unsigned int *       a;\n"
16591                "int *                b;\n"
16592                "unsigned int Const * c;\n"
16593                "unsigned int const * d;\n"
16594                "unsigned int Const & e;\n"
16595                "unsigned int const & f;",
16596                Alignment);
16597   verifyFormat("Const unsigned int * c;\n"
16598                "const unsigned int * d;\n"
16599                "Const unsigned int & e;\n"
16600                "const unsigned int & f;\n"
16601                "const unsigned       g;\n"
16602                "Const unsigned       h;",
16603                Alignment);
16604 }
16605 
16606 TEST_F(FormatTest, AlignWithLineBreaks) {
16607   auto Style = getLLVMStyleWithColumns(120);
16608 
16609   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16610   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16611   verifyFormat("void foo() {\n"
16612                "  int myVar = 5;\n"
16613                "  double x = 3.14;\n"
16614                "  auto str = \"Hello \"\n"
16615                "             \"World\";\n"
16616                "  auto s = \"Hello \"\n"
16617                "           \"Again\";\n"
16618                "}",
16619                Style);
16620 
16621   // clang-format off
16622   verifyFormat("void foo() {\n"
16623                "  const int capacityBefore = Entries.capacity();\n"
16624                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16625                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16626                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16627                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16628                "}",
16629                Style);
16630   // clang-format on
16631 
16632   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16633   verifyFormat("void foo() {\n"
16634                "  int myVar = 5;\n"
16635                "  double x  = 3.14;\n"
16636                "  auto str  = \"Hello \"\n"
16637                "              \"World\";\n"
16638                "  auto s    = \"Hello \"\n"
16639                "              \"Again\";\n"
16640                "}",
16641                Style);
16642 
16643   // clang-format off
16644   verifyFormat("void foo() {\n"
16645                "  const int capacityBefore = Entries.capacity();\n"
16646                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16647                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16648                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16649                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16650                "}",
16651                Style);
16652   // clang-format on
16653 
16654   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16655   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16656   verifyFormat("void foo() {\n"
16657                "  int    myVar = 5;\n"
16658                "  double x = 3.14;\n"
16659                "  auto   str = \"Hello \"\n"
16660                "               \"World\";\n"
16661                "  auto   s = \"Hello \"\n"
16662                "             \"Again\";\n"
16663                "}",
16664                Style);
16665 
16666   // clang-format off
16667   verifyFormat("void foo() {\n"
16668                "  const int  capacityBefore = Entries.capacity();\n"
16669                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16670                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16671                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16672                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16673                "}",
16674                Style);
16675   // clang-format on
16676 
16677   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16678   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16679 
16680   verifyFormat("void foo() {\n"
16681                "  int    myVar = 5;\n"
16682                "  double x     = 3.14;\n"
16683                "  auto   str   = \"Hello \"\n"
16684                "                 \"World\";\n"
16685                "  auto   s     = \"Hello \"\n"
16686                "                 \"Again\";\n"
16687                "}",
16688                Style);
16689 
16690   // clang-format off
16691   verifyFormat("void foo() {\n"
16692                "  const int  capacityBefore = Entries.capacity();\n"
16693                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16694                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16695                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16696                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16697                "}",
16698                Style);
16699   // clang-format on
16700 
16701   Style = getLLVMStyleWithColumns(120);
16702   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16703   Style.ContinuationIndentWidth = 4;
16704   Style.IndentWidth = 4;
16705 
16706   // clang-format off
16707   verifyFormat("void SomeFunc() {\n"
16708                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16709                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16710                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16711                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16712                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16713                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16714                "}",
16715                Style);
16716   // clang-format on
16717 
16718   Style.BinPackArguments = false;
16719 
16720   // clang-format off
16721   verifyFormat("void SomeFunc() {\n"
16722                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16723                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16724                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16725                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16726                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16727                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16728                "}",
16729                Style);
16730   // clang-format on
16731 }
16732 
16733 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16734   auto Style = getLLVMStyleWithColumns(60);
16735 
16736   verifyFormat("void foo1(void) {\n"
16737                "  BYTE p[1] = 1;\n"
16738                "  A B = {.one_foooooooooooooooo = 2,\n"
16739                "         .two_fooooooooooooo = 3,\n"
16740                "         .three_fooooooooooooo = 4};\n"
16741                "  BYTE payload = 2;\n"
16742                "}",
16743                Style);
16744 
16745   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16746   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16747   verifyFormat("void foo2(void) {\n"
16748                "  BYTE p[1]    = 1;\n"
16749                "  A B          = {.one_foooooooooooooooo = 2,\n"
16750                "                  .two_fooooooooooooo    = 3,\n"
16751                "                  .three_fooooooooooooo  = 4};\n"
16752                "  BYTE payload = 2;\n"
16753                "}",
16754                Style);
16755 
16756   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16757   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16758   verifyFormat("void foo3(void) {\n"
16759                "  BYTE p[1] = 1;\n"
16760                "  A    B = {.one_foooooooooooooooo = 2,\n"
16761                "            .two_fooooooooooooo = 3,\n"
16762                "            .three_fooooooooooooo = 4};\n"
16763                "  BYTE payload = 2;\n"
16764                "}",
16765                Style);
16766 
16767   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16768   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16769   verifyFormat("void foo4(void) {\n"
16770                "  BYTE p[1]    = 1;\n"
16771                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16772                "                  .two_fooooooooooooo    = 3,\n"
16773                "                  .three_fooooooooooooo  = 4};\n"
16774                "  BYTE payload = 2;\n"
16775                "}",
16776                Style);
16777 }
16778 
16779 TEST_F(FormatTest, LinuxBraceBreaking) {
16780   FormatStyle LinuxBraceStyle = getLLVMStyle();
16781   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16782   verifyFormat("namespace a\n"
16783                "{\n"
16784                "class A\n"
16785                "{\n"
16786                "  void f()\n"
16787                "  {\n"
16788                "    if (true) {\n"
16789                "      a();\n"
16790                "      b();\n"
16791                "    } else {\n"
16792                "      a();\n"
16793                "    }\n"
16794                "  }\n"
16795                "  void g() { return; }\n"
16796                "};\n"
16797                "struct B {\n"
16798                "  int x;\n"
16799                "};\n"
16800                "} // namespace a\n",
16801                LinuxBraceStyle);
16802   verifyFormat("enum X {\n"
16803                "  Y = 0,\n"
16804                "}\n",
16805                LinuxBraceStyle);
16806   verifyFormat("struct S {\n"
16807                "  int Type;\n"
16808                "  union {\n"
16809                "    int x;\n"
16810                "    double y;\n"
16811                "  } Value;\n"
16812                "  class C\n"
16813                "  {\n"
16814                "    MyFavoriteType Value;\n"
16815                "  } Class;\n"
16816                "}\n",
16817                LinuxBraceStyle);
16818 }
16819 
16820 TEST_F(FormatTest, MozillaBraceBreaking) {
16821   FormatStyle MozillaBraceStyle = getLLVMStyle();
16822   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16823   MozillaBraceStyle.FixNamespaceComments = false;
16824   verifyFormat("namespace a {\n"
16825                "class A\n"
16826                "{\n"
16827                "  void f()\n"
16828                "  {\n"
16829                "    if (true) {\n"
16830                "      a();\n"
16831                "      b();\n"
16832                "    }\n"
16833                "  }\n"
16834                "  void g() { return; }\n"
16835                "};\n"
16836                "enum E\n"
16837                "{\n"
16838                "  A,\n"
16839                "  // foo\n"
16840                "  B,\n"
16841                "  C\n"
16842                "};\n"
16843                "struct B\n"
16844                "{\n"
16845                "  int x;\n"
16846                "};\n"
16847                "}\n",
16848                MozillaBraceStyle);
16849   verifyFormat("struct S\n"
16850                "{\n"
16851                "  int Type;\n"
16852                "  union\n"
16853                "  {\n"
16854                "    int x;\n"
16855                "    double y;\n"
16856                "  } Value;\n"
16857                "  class C\n"
16858                "  {\n"
16859                "    MyFavoriteType Value;\n"
16860                "  } Class;\n"
16861                "}\n",
16862                MozillaBraceStyle);
16863 }
16864 
16865 TEST_F(FormatTest, StroustrupBraceBreaking) {
16866   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16867   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16868   verifyFormat("namespace a {\n"
16869                "class A {\n"
16870                "  void f()\n"
16871                "  {\n"
16872                "    if (true) {\n"
16873                "      a();\n"
16874                "      b();\n"
16875                "    }\n"
16876                "  }\n"
16877                "  void g() { return; }\n"
16878                "};\n"
16879                "struct B {\n"
16880                "  int x;\n"
16881                "};\n"
16882                "} // namespace a\n",
16883                StroustrupBraceStyle);
16884 
16885   verifyFormat("void foo()\n"
16886                "{\n"
16887                "  if (a) {\n"
16888                "    a();\n"
16889                "  }\n"
16890                "  else {\n"
16891                "    b();\n"
16892                "  }\n"
16893                "}\n",
16894                StroustrupBraceStyle);
16895 
16896   verifyFormat("#ifdef _DEBUG\n"
16897                "int foo(int i = 0)\n"
16898                "#else\n"
16899                "int foo(int i = 5)\n"
16900                "#endif\n"
16901                "{\n"
16902                "  return i;\n"
16903                "}",
16904                StroustrupBraceStyle);
16905 
16906   verifyFormat("void foo() {}\n"
16907                "void bar()\n"
16908                "#ifdef _DEBUG\n"
16909                "{\n"
16910                "  foo();\n"
16911                "}\n"
16912                "#else\n"
16913                "{\n"
16914                "}\n"
16915                "#endif",
16916                StroustrupBraceStyle);
16917 
16918   verifyFormat("void foobar() { int i = 5; }\n"
16919                "#ifdef _DEBUG\n"
16920                "void bar() {}\n"
16921                "#else\n"
16922                "void bar() { foobar(); }\n"
16923                "#endif",
16924                StroustrupBraceStyle);
16925 }
16926 
16927 TEST_F(FormatTest, AllmanBraceBreaking) {
16928   FormatStyle AllmanBraceStyle = getLLVMStyle();
16929   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16930 
16931   EXPECT_EQ("namespace a\n"
16932             "{\n"
16933             "void f();\n"
16934             "void g();\n"
16935             "} // namespace a\n",
16936             format("namespace a\n"
16937                    "{\n"
16938                    "void f();\n"
16939                    "void g();\n"
16940                    "}\n",
16941                    AllmanBraceStyle));
16942 
16943   verifyFormat("namespace a\n"
16944                "{\n"
16945                "class A\n"
16946                "{\n"
16947                "  void f()\n"
16948                "  {\n"
16949                "    if (true)\n"
16950                "    {\n"
16951                "      a();\n"
16952                "      b();\n"
16953                "    }\n"
16954                "  }\n"
16955                "  void g() { return; }\n"
16956                "};\n"
16957                "struct B\n"
16958                "{\n"
16959                "  int x;\n"
16960                "};\n"
16961                "union C\n"
16962                "{\n"
16963                "};\n"
16964                "} // namespace a",
16965                AllmanBraceStyle);
16966 
16967   verifyFormat("void f()\n"
16968                "{\n"
16969                "  if (true)\n"
16970                "  {\n"
16971                "    a();\n"
16972                "  }\n"
16973                "  else if (false)\n"
16974                "  {\n"
16975                "    b();\n"
16976                "  }\n"
16977                "  else\n"
16978                "  {\n"
16979                "    c();\n"
16980                "  }\n"
16981                "}\n",
16982                AllmanBraceStyle);
16983 
16984   verifyFormat("void f()\n"
16985                "{\n"
16986                "  for (int i = 0; i < 10; ++i)\n"
16987                "  {\n"
16988                "    a();\n"
16989                "  }\n"
16990                "  while (false)\n"
16991                "  {\n"
16992                "    b();\n"
16993                "  }\n"
16994                "  do\n"
16995                "  {\n"
16996                "    c();\n"
16997                "  } while (false)\n"
16998                "}\n",
16999                AllmanBraceStyle);
17000 
17001   verifyFormat("void f(int a)\n"
17002                "{\n"
17003                "  switch (a)\n"
17004                "  {\n"
17005                "  case 0:\n"
17006                "    break;\n"
17007                "  case 1:\n"
17008                "  {\n"
17009                "    break;\n"
17010                "  }\n"
17011                "  case 2:\n"
17012                "  {\n"
17013                "  }\n"
17014                "  break;\n"
17015                "  default:\n"
17016                "    break;\n"
17017                "  }\n"
17018                "}\n",
17019                AllmanBraceStyle);
17020 
17021   verifyFormat("enum X\n"
17022                "{\n"
17023                "  Y = 0,\n"
17024                "}\n",
17025                AllmanBraceStyle);
17026   verifyFormat("enum X\n"
17027                "{\n"
17028                "  Y = 0\n"
17029                "}\n",
17030                AllmanBraceStyle);
17031 
17032   verifyFormat("@interface BSApplicationController ()\n"
17033                "{\n"
17034                "@private\n"
17035                "  id _extraIvar;\n"
17036                "}\n"
17037                "@end\n",
17038                AllmanBraceStyle);
17039 
17040   verifyFormat("#ifdef _DEBUG\n"
17041                "int foo(int i = 0)\n"
17042                "#else\n"
17043                "int foo(int i = 5)\n"
17044                "#endif\n"
17045                "{\n"
17046                "  return i;\n"
17047                "}",
17048                AllmanBraceStyle);
17049 
17050   verifyFormat("void foo() {}\n"
17051                "void bar()\n"
17052                "#ifdef _DEBUG\n"
17053                "{\n"
17054                "  foo();\n"
17055                "}\n"
17056                "#else\n"
17057                "{\n"
17058                "}\n"
17059                "#endif",
17060                AllmanBraceStyle);
17061 
17062   verifyFormat("void foobar() { int i = 5; }\n"
17063                "#ifdef _DEBUG\n"
17064                "void bar() {}\n"
17065                "#else\n"
17066                "void bar() { foobar(); }\n"
17067                "#endif",
17068                AllmanBraceStyle);
17069 
17070   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17071             FormatStyle::SLS_All);
17072 
17073   verifyFormat("[](int i) { return i + 2; };\n"
17074                "[](int i, int j)\n"
17075                "{\n"
17076                "  auto x = i + j;\n"
17077                "  auto y = i * j;\n"
17078                "  return x ^ y;\n"
17079                "};\n"
17080                "void foo()\n"
17081                "{\n"
17082                "  auto shortLambda = [](int i) { return i + 2; };\n"
17083                "  auto longLambda = [](int i, int j)\n"
17084                "  {\n"
17085                "    auto x = i + j;\n"
17086                "    auto y = i * j;\n"
17087                "    return x ^ y;\n"
17088                "  };\n"
17089                "}",
17090                AllmanBraceStyle);
17091 
17092   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17093 
17094   verifyFormat("[](int i)\n"
17095                "{\n"
17096                "  return i + 2;\n"
17097                "};\n"
17098                "[](int i, int j)\n"
17099                "{\n"
17100                "  auto x = i + j;\n"
17101                "  auto y = i * j;\n"
17102                "  return x ^ y;\n"
17103                "};\n"
17104                "void foo()\n"
17105                "{\n"
17106                "  auto shortLambda = [](int i)\n"
17107                "  {\n"
17108                "    return i + 2;\n"
17109                "  };\n"
17110                "  auto longLambda = [](int i, int j)\n"
17111                "  {\n"
17112                "    auto x = i + j;\n"
17113                "    auto y = i * j;\n"
17114                "    return x ^ y;\n"
17115                "  };\n"
17116                "}",
17117                AllmanBraceStyle);
17118 
17119   // Reset
17120   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17121 
17122   // This shouldn't affect ObjC blocks..
17123   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17124                "  // ...\n"
17125                "  int i;\n"
17126                "}];",
17127                AllmanBraceStyle);
17128   verifyFormat("void (^block)(void) = ^{\n"
17129                "  // ...\n"
17130                "  int i;\n"
17131                "};",
17132                AllmanBraceStyle);
17133   // .. or dict literals.
17134   verifyFormat("void f()\n"
17135                "{\n"
17136                "  // ...\n"
17137                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17138                "}",
17139                AllmanBraceStyle);
17140   verifyFormat("void f()\n"
17141                "{\n"
17142                "  // ...\n"
17143                "  [object someMethod:@{a : @\"b\"}];\n"
17144                "}",
17145                AllmanBraceStyle);
17146   verifyFormat("int f()\n"
17147                "{ // comment\n"
17148                "  return 42;\n"
17149                "}",
17150                AllmanBraceStyle);
17151 
17152   AllmanBraceStyle.ColumnLimit = 19;
17153   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17154   AllmanBraceStyle.ColumnLimit = 18;
17155   verifyFormat("void f()\n"
17156                "{\n"
17157                "  int i;\n"
17158                "}",
17159                AllmanBraceStyle);
17160   AllmanBraceStyle.ColumnLimit = 80;
17161 
17162   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17163   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17164       FormatStyle::SIS_WithoutElse;
17165   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17166   verifyFormat("void f(bool b)\n"
17167                "{\n"
17168                "  if (b)\n"
17169                "  {\n"
17170                "    return;\n"
17171                "  }\n"
17172                "}\n",
17173                BreakBeforeBraceShortIfs);
17174   verifyFormat("void f(bool b)\n"
17175                "{\n"
17176                "  if constexpr (b)\n"
17177                "  {\n"
17178                "    return;\n"
17179                "  }\n"
17180                "}\n",
17181                BreakBeforeBraceShortIfs);
17182   verifyFormat("void f(bool b)\n"
17183                "{\n"
17184                "  if CONSTEXPR (b)\n"
17185                "  {\n"
17186                "    return;\n"
17187                "  }\n"
17188                "}\n",
17189                BreakBeforeBraceShortIfs);
17190   verifyFormat("void f(bool b)\n"
17191                "{\n"
17192                "  if (b) return;\n"
17193                "}\n",
17194                BreakBeforeBraceShortIfs);
17195   verifyFormat("void f(bool b)\n"
17196                "{\n"
17197                "  if constexpr (b) return;\n"
17198                "}\n",
17199                BreakBeforeBraceShortIfs);
17200   verifyFormat("void f(bool b)\n"
17201                "{\n"
17202                "  if CONSTEXPR (b) return;\n"
17203                "}\n",
17204                BreakBeforeBraceShortIfs);
17205   verifyFormat("void f(bool b)\n"
17206                "{\n"
17207                "  while (b)\n"
17208                "  {\n"
17209                "    return;\n"
17210                "  }\n"
17211                "}\n",
17212                BreakBeforeBraceShortIfs);
17213 }
17214 
17215 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17216   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
17217   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17218 
17219   // Make a few changes to the style for testing purposes
17220   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17221       FormatStyle::SFS_Empty;
17222   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17223   WhitesmithsBraceStyle.ColumnLimit = 0;
17224 
17225   // FIXME: this test case can't decide whether there should be a blank line
17226   // after the ~D() line or not. It adds one if one doesn't exist in the test
17227   // and it removes the line if one exists.
17228   /*
17229   verifyFormat("class A;\n"
17230                "namespace B\n"
17231                "  {\n"
17232                "class C;\n"
17233                "// Comment\n"
17234                "class D\n"
17235                "  {\n"
17236                "public:\n"
17237                "  D();\n"
17238                "  ~D() {}\n"
17239                "private:\n"
17240                "  enum E\n"
17241                "    {\n"
17242                "    F\n"
17243                "    }\n"
17244                "  };\n"
17245                "  } // namespace B\n",
17246                WhitesmithsBraceStyle);
17247   */
17248 
17249   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17250   verifyFormat("namespace a\n"
17251                "  {\n"
17252                "class A\n"
17253                "  {\n"
17254                "  void f()\n"
17255                "    {\n"
17256                "    if (true)\n"
17257                "      {\n"
17258                "      a();\n"
17259                "      b();\n"
17260                "      }\n"
17261                "    }\n"
17262                "  void g()\n"
17263                "    {\n"
17264                "    return;\n"
17265                "    }\n"
17266                "  };\n"
17267                "struct B\n"
17268                "  {\n"
17269                "  int x;\n"
17270                "  };\n"
17271                "  } // namespace a",
17272                WhitesmithsBraceStyle);
17273 
17274   verifyFormat("namespace a\n"
17275                "  {\n"
17276                "namespace b\n"
17277                "  {\n"
17278                "class A\n"
17279                "  {\n"
17280                "  void f()\n"
17281                "    {\n"
17282                "    if (true)\n"
17283                "      {\n"
17284                "      a();\n"
17285                "      b();\n"
17286                "      }\n"
17287                "    }\n"
17288                "  void g()\n"
17289                "    {\n"
17290                "    return;\n"
17291                "    }\n"
17292                "  };\n"
17293                "struct B\n"
17294                "  {\n"
17295                "  int x;\n"
17296                "  };\n"
17297                "  } // namespace b\n"
17298                "  } // namespace a",
17299                WhitesmithsBraceStyle);
17300 
17301   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17302   verifyFormat("namespace a\n"
17303                "  {\n"
17304                "namespace b\n"
17305                "  {\n"
17306                "  class A\n"
17307                "    {\n"
17308                "    void f()\n"
17309                "      {\n"
17310                "      if (true)\n"
17311                "        {\n"
17312                "        a();\n"
17313                "        b();\n"
17314                "        }\n"
17315                "      }\n"
17316                "    void g()\n"
17317                "      {\n"
17318                "      return;\n"
17319                "      }\n"
17320                "    };\n"
17321                "  struct B\n"
17322                "    {\n"
17323                "    int x;\n"
17324                "    };\n"
17325                "  } // namespace b\n"
17326                "  } // namespace a",
17327                WhitesmithsBraceStyle);
17328 
17329   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17330   verifyFormat("namespace a\n"
17331                "  {\n"
17332                "  namespace b\n"
17333                "    {\n"
17334                "    class A\n"
17335                "      {\n"
17336                "      void f()\n"
17337                "        {\n"
17338                "        if (true)\n"
17339                "          {\n"
17340                "          a();\n"
17341                "          b();\n"
17342                "          }\n"
17343                "        }\n"
17344                "      void g()\n"
17345                "        {\n"
17346                "        return;\n"
17347                "        }\n"
17348                "      };\n"
17349                "    struct B\n"
17350                "      {\n"
17351                "      int x;\n"
17352                "      };\n"
17353                "    } // namespace b\n"
17354                "  }   // namespace a",
17355                WhitesmithsBraceStyle);
17356 
17357   verifyFormat("void f()\n"
17358                "  {\n"
17359                "  if (true)\n"
17360                "    {\n"
17361                "    a();\n"
17362                "    }\n"
17363                "  else if (false)\n"
17364                "    {\n"
17365                "    b();\n"
17366                "    }\n"
17367                "  else\n"
17368                "    {\n"
17369                "    c();\n"
17370                "    }\n"
17371                "  }\n",
17372                WhitesmithsBraceStyle);
17373 
17374   verifyFormat("void f()\n"
17375                "  {\n"
17376                "  for (int i = 0; i < 10; ++i)\n"
17377                "    {\n"
17378                "    a();\n"
17379                "    }\n"
17380                "  while (false)\n"
17381                "    {\n"
17382                "    b();\n"
17383                "    }\n"
17384                "  do\n"
17385                "    {\n"
17386                "    c();\n"
17387                "    } while (false)\n"
17388                "  }\n",
17389                WhitesmithsBraceStyle);
17390 
17391   WhitesmithsBraceStyle.IndentCaseLabels = true;
17392   verifyFormat("void switchTest1(int a)\n"
17393                "  {\n"
17394                "  switch (a)\n"
17395                "    {\n"
17396                "    case 2:\n"
17397                "      {\n"
17398                "      }\n"
17399                "      break;\n"
17400                "    }\n"
17401                "  }\n",
17402                WhitesmithsBraceStyle);
17403 
17404   verifyFormat("void switchTest2(int a)\n"
17405                "  {\n"
17406                "  switch (a)\n"
17407                "    {\n"
17408                "    case 0:\n"
17409                "      break;\n"
17410                "    case 1:\n"
17411                "      {\n"
17412                "      break;\n"
17413                "      }\n"
17414                "    case 2:\n"
17415                "      {\n"
17416                "      }\n"
17417                "      break;\n"
17418                "    default:\n"
17419                "      break;\n"
17420                "    }\n"
17421                "  }\n",
17422                WhitesmithsBraceStyle);
17423 
17424   verifyFormat("void switchTest3(int a)\n"
17425                "  {\n"
17426                "  switch (a)\n"
17427                "    {\n"
17428                "    case 0:\n"
17429                "      {\n"
17430                "      foo(x);\n"
17431                "      }\n"
17432                "      break;\n"
17433                "    default:\n"
17434                "      {\n"
17435                "      foo(1);\n"
17436                "      }\n"
17437                "      break;\n"
17438                "    }\n"
17439                "  }\n",
17440                WhitesmithsBraceStyle);
17441 
17442   WhitesmithsBraceStyle.IndentCaseLabels = false;
17443 
17444   verifyFormat("void switchTest4(int a)\n"
17445                "  {\n"
17446                "  switch (a)\n"
17447                "    {\n"
17448                "  case 2:\n"
17449                "    {\n"
17450                "    }\n"
17451                "    break;\n"
17452                "    }\n"
17453                "  }\n",
17454                WhitesmithsBraceStyle);
17455 
17456   verifyFormat("void switchTest5(int a)\n"
17457                "  {\n"
17458                "  switch (a)\n"
17459                "    {\n"
17460                "  case 0:\n"
17461                "    break;\n"
17462                "  case 1:\n"
17463                "    {\n"
17464                "    foo();\n"
17465                "    break;\n"
17466                "    }\n"
17467                "  case 2:\n"
17468                "    {\n"
17469                "    }\n"
17470                "    break;\n"
17471                "  default:\n"
17472                "    break;\n"
17473                "    }\n"
17474                "  }\n",
17475                WhitesmithsBraceStyle);
17476 
17477   verifyFormat("void switchTest6(int a)\n"
17478                "  {\n"
17479                "  switch (a)\n"
17480                "    {\n"
17481                "  case 0:\n"
17482                "    {\n"
17483                "    foo(x);\n"
17484                "    }\n"
17485                "    break;\n"
17486                "  default:\n"
17487                "    {\n"
17488                "    foo(1);\n"
17489                "    }\n"
17490                "    break;\n"
17491                "    }\n"
17492                "  }\n",
17493                WhitesmithsBraceStyle);
17494 
17495   verifyFormat("enum X\n"
17496                "  {\n"
17497                "  Y = 0, // testing\n"
17498                "  }\n",
17499                WhitesmithsBraceStyle);
17500 
17501   verifyFormat("enum X\n"
17502                "  {\n"
17503                "  Y = 0\n"
17504                "  }\n",
17505                WhitesmithsBraceStyle);
17506   verifyFormat("enum X\n"
17507                "  {\n"
17508                "  Y = 0,\n"
17509                "  Z = 1\n"
17510                "  };\n",
17511                WhitesmithsBraceStyle);
17512 
17513   verifyFormat("@interface BSApplicationController ()\n"
17514                "  {\n"
17515                "@private\n"
17516                "  id _extraIvar;\n"
17517                "  }\n"
17518                "@end\n",
17519                WhitesmithsBraceStyle);
17520 
17521   verifyFormat("#ifdef _DEBUG\n"
17522                "int foo(int i = 0)\n"
17523                "#else\n"
17524                "int foo(int i = 5)\n"
17525                "#endif\n"
17526                "  {\n"
17527                "  return i;\n"
17528                "  }",
17529                WhitesmithsBraceStyle);
17530 
17531   verifyFormat("void foo() {}\n"
17532                "void bar()\n"
17533                "#ifdef _DEBUG\n"
17534                "  {\n"
17535                "  foo();\n"
17536                "  }\n"
17537                "#else\n"
17538                "  {\n"
17539                "  }\n"
17540                "#endif",
17541                WhitesmithsBraceStyle);
17542 
17543   verifyFormat("void foobar()\n"
17544                "  {\n"
17545                "  int i = 5;\n"
17546                "  }\n"
17547                "#ifdef _DEBUG\n"
17548                "void bar()\n"
17549                "  {\n"
17550                "  }\n"
17551                "#else\n"
17552                "void bar()\n"
17553                "  {\n"
17554                "  foobar();\n"
17555                "  }\n"
17556                "#endif",
17557                WhitesmithsBraceStyle);
17558 
17559   // This shouldn't affect ObjC blocks..
17560   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17561                "  // ...\n"
17562                "  int i;\n"
17563                "}];",
17564                WhitesmithsBraceStyle);
17565   verifyFormat("void (^block)(void) = ^{\n"
17566                "  // ...\n"
17567                "  int i;\n"
17568                "};",
17569                WhitesmithsBraceStyle);
17570   // .. or dict literals.
17571   verifyFormat("void f()\n"
17572                "  {\n"
17573                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17574                "  }",
17575                WhitesmithsBraceStyle);
17576 
17577   verifyFormat("int f()\n"
17578                "  { // comment\n"
17579                "  return 42;\n"
17580                "  }",
17581                WhitesmithsBraceStyle);
17582 
17583   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17584   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17585       FormatStyle::SIS_OnlyFirstIf;
17586   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17587   verifyFormat("void f(bool b)\n"
17588                "  {\n"
17589                "  if (b)\n"
17590                "    {\n"
17591                "    return;\n"
17592                "    }\n"
17593                "  }\n",
17594                BreakBeforeBraceShortIfs);
17595   verifyFormat("void f(bool b)\n"
17596                "  {\n"
17597                "  if (b) return;\n"
17598                "  }\n",
17599                BreakBeforeBraceShortIfs);
17600   verifyFormat("void f(bool b)\n"
17601                "  {\n"
17602                "  while (b)\n"
17603                "    {\n"
17604                "    return;\n"
17605                "    }\n"
17606                "  }\n",
17607                BreakBeforeBraceShortIfs);
17608 }
17609 
17610 TEST_F(FormatTest, GNUBraceBreaking) {
17611   FormatStyle GNUBraceStyle = getLLVMStyle();
17612   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17613   verifyFormat("namespace a\n"
17614                "{\n"
17615                "class A\n"
17616                "{\n"
17617                "  void f()\n"
17618                "  {\n"
17619                "    int a;\n"
17620                "    {\n"
17621                "      int b;\n"
17622                "    }\n"
17623                "    if (true)\n"
17624                "      {\n"
17625                "        a();\n"
17626                "        b();\n"
17627                "      }\n"
17628                "  }\n"
17629                "  void g() { return; }\n"
17630                "}\n"
17631                "} // namespace a",
17632                GNUBraceStyle);
17633 
17634   verifyFormat("void f()\n"
17635                "{\n"
17636                "  if (true)\n"
17637                "    {\n"
17638                "      a();\n"
17639                "    }\n"
17640                "  else if (false)\n"
17641                "    {\n"
17642                "      b();\n"
17643                "    }\n"
17644                "  else\n"
17645                "    {\n"
17646                "      c();\n"
17647                "    }\n"
17648                "}\n",
17649                GNUBraceStyle);
17650 
17651   verifyFormat("void f()\n"
17652                "{\n"
17653                "  for (int i = 0; i < 10; ++i)\n"
17654                "    {\n"
17655                "      a();\n"
17656                "    }\n"
17657                "  while (false)\n"
17658                "    {\n"
17659                "      b();\n"
17660                "    }\n"
17661                "  do\n"
17662                "    {\n"
17663                "      c();\n"
17664                "    }\n"
17665                "  while (false);\n"
17666                "}\n",
17667                GNUBraceStyle);
17668 
17669   verifyFormat("void f(int a)\n"
17670                "{\n"
17671                "  switch (a)\n"
17672                "    {\n"
17673                "    case 0:\n"
17674                "      break;\n"
17675                "    case 1:\n"
17676                "      {\n"
17677                "        break;\n"
17678                "      }\n"
17679                "    case 2:\n"
17680                "      {\n"
17681                "      }\n"
17682                "      break;\n"
17683                "    default:\n"
17684                "      break;\n"
17685                "    }\n"
17686                "}\n",
17687                GNUBraceStyle);
17688 
17689   verifyFormat("enum X\n"
17690                "{\n"
17691                "  Y = 0,\n"
17692                "}\n",
17693                GNUBraceStyle);
17694 
17695   verifyFormat("@interface BSApplicationController ()\n"
17696                "{\n"
17697                "@private\n"
17698                "  id _extraIvar;\n"
17699                "}\n"
17700                "@end\n",
17701                GNUBraceStyle);
17702 
17703   verifyFormat("#ifdef _DEBUG\n"
17704                "int foo(int i = 0)\n"
17705                "#else\n"
17706                "int foo(int i = 5)\n"
17707                "#endif\n"
17708                "{\n"
17709                "  return i;\n"
17710                "}",
17711                GNUBraceStyle);
17712 
17713   verifyFormat("void foo() {}\n"
17714                "void bar()\n"
17715                "#ifdef _DEBUG\n"
17716                "{\n"
17717                "  foo();\n"
17718                "}\n"
17719                "#else\n"
17720                "{\n"
17721                "}\n"
17722                "#endif",
17723                GNUBraceStyle);
17724 
17725   verifyFormat("void foobar() { int i = 5; }\n"
17726                "#ifdef _DEBUG\n"
17727                "void bar() {}\n"
17728                "#else\n"
17729                "void bar() { foobar(); }\n"
17730                "#endif",
17731                GNUBraceStyle);
17732 }
17733 
17734 TEST_F(FormatTest, WebKitBraceBreaking) {
17735   FormatStyle WebKitBraceStyle = getLLVMStyle();
17736   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17737   WebKitBraceStyle.FixNamespaceComments = false;
17738   verifyFormat("namespace a {\n"
17739                "class A {\n"
17740                "  void f()\n"
17741                "  {\n"
17742                "    if (true) {\n"
17743                "      a();\n"
17744                "      b();\n"
17745                "    }\n"
17746                "  }\n"
17747                "  void g() { return; }\n"
17748                "};\n"
17749                "enum E {\n"
17750                "  A,\n"
17751                "  // foo\n"
17752                "  B,\n"
17753                "  C\n"
17754                "};\n"
17755                "struct B {\n"
17756                "  int x;\n"
17757                "};\n"
17758                "}\n",
17759                WebKitBraceStyle);
17760   verifyFormat("struct S {\n"
17761                "  int Type;\n"
17762                "  union {\n"
17763                "    int x;\n"
17764                "    double y;\n"
17765                "  } Value;\n"
17766                "  class C {\n"
17767                "    MyFavoriteType Value;\n"
17768                "  } Class;\n"
17769                "};\n",
17770                WebKitBraceStyle);
17771 }
17772 
17773 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17774   verifyFormat("void f() {\n"
17775                "  try {\n"
17776                "  } catch (const Exception &e) {\n"
17777                "  }\n"
17778                "}\n",
17779                getLLVMStyle());
17780 }
17781 
17782 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17783   auto Style = getLLVMStyle();
17784   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17785   Style.AlignConsecutiveAssignments =
17786       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17787   Style.AlignConsecutiveDeclarations =
17788       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17789   verifyFormat("struct test demo[] = {\n"
17790                "    {56,    23, \"hello\"},\n"
17791                "    {-1, 93463, \"world\"},\n"
17792                "    { 7,     5,    \"!!\"}\n"
17793                "};\n",
17794                Style);
17795 
17796   verifyFormat("struct test demo[] = {\n"
17797                "    {56,    23, \"hello\"}, // first line\n"
17798                "    {-1, 93463, \"world\"}, // second line\n"
17799                "    { 7,     5,    \"!!\"}  // third line\n"
17800                "};\n",
17801                Style);
17802 
17803   verifyFormat("struct test demo[4] = {\n"
17804                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17805                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17806                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17807                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17808                "};\n",
17809                Style);
17810 
17811   verifyFormat("struct test demo[3] = {\n"
17812                "    {56,    23, \"hello\"},\n"
17813                "    {-1, 93463, \"world\"},\n"
17814                "    { 7,     5,    \"!!\"}\n"
17815                "};\n",
17816                Style);
17817 
17818   verifyFormat("struct test demo[3] = {\n"
17819                "    {int{56},    23, \"hello\"},\n"
17820                "    {int{-1}, 93463, \"world\"},\n"
17821                "    { int{7},     5,    \"!!\"}\n"
17822                "};\n",
17823                Style);
17824 
17825   verifyFormat("struct test demo[] = {\n"
17826                "    {56,    23, \"hello\"},\n"
17827                "    {-1, 93463, \"world\"},\n"
17828                "    { 7,     5,    \"!!\"},\n"
17829                "};\n",
17830                Style);
17831 
17832   verifyFormat("test demo[] = {\n"
17833                "    {56,    23, \"hello\"},\n"
17834                "    {-1, 93463, \"world\"},\n"
17835                "    { 7,     5,    \"!!\"},\n"
17836                "};\n",
17837                Style);
17838 
17839   verifyFormat("demo = std::array<struct test, 3>{\n"
17840                "    test{56,    23, \"hello\"},\n"
17841                "    test{-1, 93463, \"world\"},\n"
17842                "    test{ 7,     5,    \"!!\"},\n"
17843                "};\n",
17844                Style);
17845 
17846   verifyFormat("test demo[] = {\n"
17847                "    {56,    23, \"hello\"},\n"
17848                "#if X\n"
17849                "    {-1, 93463, \"world\"},\n"
17850                "#endif\n"
17851                "    { 7,     5,    \"!!\"}\n"
17852                "};\n",
17853                Style);
17854 
17855   verifyFormat(
17856       "test demo[] = {\n"
17857       "    { 7,    23,\n"
17858       "     \"hello world i am a very long line that really, in any\"\n"
17859       "     \"just world, ought to be split over multiple lines\"},\n"
17860       "    {-1, 93463,                                  \"world\"},\n"
17861       "    {56,     5,                                     \"!!\"}\n"
17862       "};\n",
17863       Style);
17864 
17865   verifyFormat("return GradForUnaryCwise(g, {\n"
17866                "                                {{\"sign\"}, \"Sign\",  "
17867                "  {\"x\", \"dy\"}},\n"
17868                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17869                ", \"sign\"}},\n"
17870                "});\n",
17871                Style);
17872 
17873   Style.ColumnLimit = 0;
17874   EXPECT_EQ(
17875       "test demo[] = {\n"
17876       "    {56,    23, \"hello world i am a very long line that really, "
17877       "in any just world, ought to be split over multiple lines\"},\n"
17878       "    {-1, 93463,                                                  "
17879       "                                                 \"world\"},\n"
17880       "    { 7,     5,                                                  "
17881       "                                                    \"!!\"},\n"
17882       "};",
17883       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17884              "that really, in any just world, ought to be split over multiple "
17885              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17886              Style));
17887 
17888   Style.ColumnLimit = 80;
17889   verifyFormat("test demo[] = {\n"
17890                "    {56,    23, /* a comment */ \"hello\"},\n"
17891                "    {-1, 93463,                 \"world\"},\n"
17892                "    { 7,     5,                    \"!!\"}\n"
17893                "};\n",
17894                Style);
17895 
17896   verifyFormat("test demo[] = {\n"
17897                "    {56,    23,                    \"hello\"},\n"
17898                "    {-1, 93463, \"world\" /* comment here */},\n"
17899                "    { 7,     5,                       \"!!\"}\n"
17900                "};\n",
17901                Style);
17902 
17903   verifyFormat("test demo[] = {\n"
17904                "    {56, /* a comment */ 23, \"hello\"},\n"
17905                "    {-1,              93463, \"world\"},\n"
17906                "    { 7,                  5,    \"!!\"}\n"
17907                "};\n",
17908                Style);
17909 
17910   Style.ColumnLimit = 20;
17911   EXPECT_EQ(
17912       "demo = std::array<\n"
17913       "    struct test, 3>{\n"
17914       "    test{\n"
17915       "         56,    23,\n"
17916       "         \"hello \"\n"
17917       "         \"world i \"\n"
17918       "         \"am a very \"\n"
17919       "         \"long line \"\n"
17920       "         \"that \"\n"
17921       "         \"really, \"\n"
17922       "         \"in any \"\n"
17923       "         \"just \"\n"
17924       "         \"world, \"\n"
17925       "         \"ought to \"\n"
17926       "         \"be split \"\n"
17927       "         \"over \"\n"
17928       "         \"multiple \"\n"
17929       "         \"lines\"},\n"
17930       "    test{-1, 93463,\n"
17931       "         \"world\"},\n"
17932       "    test{ 7,     5,\n"
17933       "         \"!!\"   },\n"
17934       "};",
17935       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17936              "i am a very long line that really, in any just world, ought "
17937              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17938              "test{7, 5, \"!!\"},};",
17939              Style));
17940   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17941   Style = getLLVMStyleWithColumns(50);
17942   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17943   verifyFormat("static A x = {\n"
17944                "    {{init1, init2, init3, init4},\n"
17945                "     {init1, init2, init3, init4}}\n"
17946                "};",
17947                Style);
17948   Style.ColumnLimit = 100;
17949   EXPECT_EQ(
17950       "test demo[] = {\n"
17951       "    {56,    23,\n"
17952       "     \"hello world i am a very long line that really, in any just world"
17953       ", ought to be split over \"\n"
17954       "     \"multiple lines\"  },\n"
17955       "    {-1, 93463, \"world\"},\n"
17956       "    { 7,     5,    \"!!\"},\n"
17957       "};",
17958       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17959              "that really, in any just world, ought to be split over multiple "
17960              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17961              Style));
17962 
17963   Style = getLLVMStyleWithColumns(50);
17964   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17965   Style.AlignConsecutiveAssignments =
17966       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17967   Style.AlignConsecutiveDeclarations =
17968       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17969   verifyFormat("struct test demo[] = {\n"
17970                "    {56,    23, \"hello\"},\n"
17971                "    {-1, 93463, \"world\"},\n"
17972                "    { 7,     5,    \"!!\"}\n"
17973                "};\n"
17974                "static A x = {\n"
17975                "    {{init1, init2, init3, init4},\n"
17976                "     {init1, init2, init3, init4}}\n"
17977                "};",
17978                Style);
17979   Style.ColumnLimit = 100;
17980   Style.AlignConsecutiveAssignments =
17981       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17982   Style.AlignConsecutiveDeclarations =
17983       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17984   verifyFormat("struct test demo[] = {\n"
17985                "    {56,    23, \"hello\"},\n"
17986                "    {-1, 93463, \"world\"},\n"
17987                "    { 7,     5,    \"!!\"}\n"
17988                "};\n"
17989                "struct test demo[4] = {\n"
17990                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17991                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17992                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17993                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17994                "};\n",
17995                Style);
17996   EXPECT_EQ(
17997       "test demo[] = {\n"
17998       "    {56,\n"
17999       "     \"hello world i am a very long line that really, in any just world"
18000       ", ought to be split over \"\n"
18001       "     \"multiple lines\",    23},\n"
18002       "    {-1,      \"world\", 93463},\n"
18003       "    { 7,         \"!!\",     5},\n"
18004       "};",
18005       format("test demo[] = {{56, \"hello world i am a very long line "
18006              "that really, in any just world, ought to be split over multiple "
18007              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18008              Style));
18009 }
18010 
18011 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18012   auto Style = getLLVMStyle();
18013   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18014   /* FIXME: This case gets misformatted.
18015   verifyFormat("auto foo = Items{\n"
18016                "    Section{0, bar(), },\n"
18017                "    Section{1, boo()  }\n"
18018                "};\n",
18019                Style);
18020   */
18021   verifyFormat("auto foo = Items{\n"
18022                "    Section{\n"
18023                "            0, bar(),\n"
18024                "            }\n"
18025                "};\n",
18026                Style);
18027   verifyFormat("struct test demo[] = {\n"
18028                "    {56, 23,    \"hello\"},\n"
18029                "    {-1, 93463, \"world\"},\n"
18030                "    {7,  5,     \"!!\"   }\n"
18031                "};\n",
18032                Style);
18033   verifyFormat("struct test demo[] = {\n"
18034                "    {56, 23,    \"hello\"}, // first line\n"
18035                "    {-1, 93463, \"world\"}, // second line\n"
18036                "    {7,  5,     \"!!\"   }  // third line\n"
18037                "};\n",
18038                Style);
18039   verifyFormat("struct test demo[4] = {\n"
18040                "    {56,  23,    21, \"oh\"      }, // first line\n"
18041                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18042                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18043                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18044                "};\n",
18045                Style);
18046   verifyFormat("struct test demo[3] = {\n"
18047                "    {56, 23,    \"hello\"},\n"
18048                "    {-1, 93463, \"world\"},\n"
18049                "    {7,  5,     \"!!\"   }\n"
18050                "};\n",
18051                Style);
18052 
18053   verifyFormat("struct test demo[3] = {\n"
18054                "    {int{56}, 23,    \"hello\"},\n"
18055                "    {int{-1}, 93463, \"world\"},\n"
18056                "    {int{7},  5,     \"!!\"   }\n"
18057                "};\n",
18058                Style);
18059   verifyFormat("struct test demo[] = {\n"
18060                "    {56, 23,    \"hello\"},\n"
18061                "    {-1, 93463, \"world\"},\n"
18062                "    {7,  5,     \"!!\"   },\n"
18063                "};\n",
18064                Style);
18065   verifyFormat("test demo[] = {\n"
18066                "    {56, 23,    \"hello\"},\n"
18067                "    {-1, 93463, \"world\"},\n"
18068                "    {7,  5,     \"!!\"   },\n"
18069                "};\n",
18070                Style);
18071   verifyFormat("demo = std::array<struct test, 3>{\n"
18072                "    test{56, 23,    \"hello\"},\n"
18073                "    test{-1, 93463, \"world\"},\n"
18074                "    test{7,  5,     \"!!\"   },\n"
18075                "};\n",
18076                Style);
18077   verifyFormat("test demo[] = {\n"
18078                "    {56, 23,    \"hello\"},\n"
18079                "#if X\n"
18080                "    {-1, 93463, \"world\"},\n"
18081                "#endif\n"
18082                "    {7,  5,     \"!!\"   }\n"
18083                "};\n",
18084                Style);
18085   verifyFormat(
18086       "test demo[] = {\n"
18087       "    {7,  23,\n"
18088       "     \"hello world i am a very long line that really, in any\"\n"
18089       "     \"just world, ought to be split over multiple lines\"},\n"
18090       "    {-1, 93463, \"world\"                                 },\n"
18091       "    {56, 5,     \"!!\"                                    }\n"
18092       "};\n",
18093       Style);
18094 
18095   verifyFormat("return GradForUnaryCwise(g, {\n"
18096                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18097                "\"dy\"}   },\n"
18098                "                                {{\"dx\"},   \"Mul\",  "
18099                "{\"dy\", \"sign\"}},\n"
18100                "});\n",
18101                Style);
18102 
18103   Style.ColumnLimit = 0;
18104   EXPECT_EQ(
18105       "test demo[] = {\n"
18106       "    {56, 23,    \"hello world i am a very long line that really, in any "
18107       "just world, ought to be split over multiple lines\"},\n"
18108       "    {-1, 93463, \"world\"                                               "
18109       "                                                   },\n"
18110       "    {7,  5,     \"!!\"                                                  "
18111       "                                                   },\n"
18112       "};",
18113       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18114              "that really, in any just world, ought to be split over multiple "
18115              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18116              Style));
18117 
18118   Style.ColumnLimit = 80;
18119   verifyFormat("test demo[] = {\n"
18120                "    {56, 23,    /* a comment */ \"hello\"},\n"
18121                "    {-1, 93463, \"world\"                },\n"
18122                "    {7,  5,     \"!!\"                   }\n"
18123                "};\n",
18124                Style);
18125 
18126   verifyFormat("test demo[] = {\n"
18127                "    {56, 23,    \"hello\"                   },\n"
18128                "    {-1, 93463, \"world\" /* comment here */},\n"
18129                "    {7,  5,     \"!!\"                      }\n"
18130                "};\n",
18131                Style);
18132 
18133   verifyFormat("test demo[] = {\n"
18134                "    {56, /* a comment */ 23, \"hello\"},\n"
18135                "    {-1, 93463,              \"world\"},\n"
18136                "    {7,  5,                  \"!!\"   }\n"
18137                "};\n",
18138                Style);
18139 
18140   Style.ColumnLimit = 20;
18141   EXPECT_EQ(
18142       "demo = std::array<\n"
18143       "    struct test, 3>{\n"
18144       "    test{\n"
18145       "         56, 23,\n"
18146       "         \"hello \"\n"
18147       "         \"world i \"\n"
18148       "         \"am a very \"\n"
18149       "         \"long line \"\n"
18150       "         \"that \"\n"
18151       "         \"really, \"\n"
18152       "         \"in any \"\n"
18153       "         \"just \"\n"
18154       "         \"world, \"\n"
18155       "         \"ought to \"\n"
18156       "         \"be split \"\n"
18157       "         \"over \"\n"
18158       "         \"multiple \"\n"
18159       "         \"lines\"},\n"
18160       "    test{-1, 93463,\n"
18161       "         \"world\"},\n"
18162       "    test{7,  5,\n"
18163       "         \"!!\"   },\n"
18164       "};",
18165       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18166              "i am a very long line that really, in any just world, ought "
18167              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18168              "test{7, 5, \"!!\"},};",
18169              Style));
18170 
18171   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18172   Style = getLLVMStyleWithColumns(50);
18173   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18174   verifyFormat("static A x = {\n"
18175                "    {{init1, init2, init3, init4},\n"
18176                "     {init1, init2, init3, init4}}\n"
18177                "};",
18178                Style);
18179   Style.ColumnLimit = 100;
18180   EXPECT_EQ(
18181       "test demo[] = {\n"
18182       "    {56, 23,\n"
18183       "     \"hello world i am a very long line that really, in any just world"
18184       ", ought to be split over \"\n"
18185       "     \"multiple lines\"  },\n"
18186       "    {-1, 93463, \"world\"},\n"
18187       "    {7,  5,     \"!!\"   },\n"
18188       "};",
18189       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18190              "that really, in any just world, ought to be split over multiple "
18191              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18192              Style));
18193 }
18194 
18195 TEST_F(FormatTest, UnderstandsPragmas) {
18196   verifyFormat("#pragma omp reduction(| : var)");
18197   verifyFormat("#pragma omp reduction(+ : var)");
18198 
18199   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18200             "(including parentheses).",
18201             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18202                    "(including parentheses)."));
18203 }
18204 
18205 TEST_F(FormatTest, UnderstandPragmaOption) {
18206   verifyFormat("#pragma option -C -A");
18207 
18208   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18209 }
18210 
18211 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18212   FormatStyle Style = getLLVMStyle();
18213   Style.ColumnLimit = 20;
18214 
18215   // See PR41213
18216   EXPECT_EQ("/*\n"
18217             " *\t9012345\n"
18218             " * /8901\n"
18219             " */",
18220             format("/*\n"
18221                    " *\t9012345 /8901\n"
18222                    " */",
18223                    Style));
18224   EXPECT_EQ("/*\n"
18225             " *345678\n"
18226             " *\t/8901\n"
18227             " */",
18228             format("/*\n"
18229                    " *345678\t/8901\n"
18230                    " */",
18231                    Style));
18232 
18233   verifyFormat("int a; // the\n"
18234                "       // comment",
18235                Style);
18236   EXPECT_EQ("int a; /* first line\n"
18237             "        * second\n"
18238             "        * line third\n"
18239             "        * line\n"
18240             "        */",
18241             format("int a; /* first line\n"
18242                    "        * second\n"
18243                    "        * line third\n"
18244                    "        * line\n"
18245                    "        */",
18246                    Style));
18247   EXPECT_EQ("int a; // first line\n"
18248             "       // second\n"
18249             "       // line third\n"
18250             "       // line",
18251             format("int a; // first line\n"
18252                    "       // second line\n"
18253                    "       // third line",
18254                    Style));
18255 
18256   Style.PenaltyExcessCharacter = 90;
18257   verifyFormat("int a; // the comment", Style);
18258   EXPECT_EQ("int a; // the comment\n"
18259             "       // aaa",
18260             format("int a; // the comment aaa", Style));
18261   EXPECT_EQ("int a; /* first line\n"
18262             "        * second line\n"
18263             "        * third line\n"
18264             "        */",
18265             format("int a; /* first line\n"
18266                    "        * second line\n"
18267                    "        * third line\n"
18268                    "        */",
18269                    Style));
18270   EXPECT_EQ("int a; // first line\n"
18271             "       // second line\n"
18272             "       // third line",
18273             format("int a; // first line\n"
18274                    "       // second line\n"
18275                    "       // third line",
18276                    Style));
18277   // FIXME: Investigate why this is not getting the same layout as the test
18278   // above.
18279   EXPECT_EQ("int a; /* first line\n"
18280             "        * second line\n"
18281             "        * third line\n"
18282             "        */",
18283             format("int a; /* first line second line third line"
18284                    "\n*/",
18285                    Style));
18286 
18287   EXPECT_EQ("// foo bar baz bazfoo\n"
18288             "// foo bar foo bar\n",
18289             format("// foo bar baz bazfoo\n"
18290                    "// foo bar foo           bar\n",
18291                    Style));
18292   EXPECT_EQ("// foo bar baz bazfoo\n"
18293             "// foo bar foo bar\n",
18294             format("// foo bar baz      bazfoo\n"
18295                    "// foo            bar foo bar\n",
18296                    Style));
18297 
18298   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18299   // next one.
18300   EXPECT_EQ("// foo bar baz bazfoo\n"
18301             "// bar foo bar\n",
18302             format("// foo bar baz      bazfoo bar\n"
18303                    "// foo            bar\n",
18304                    Style));
18305 
18306   EXPECT_EQ("// foo bar baz bazfoo\n"
18307             "// foo bar baz bazfoo\n"
18308             "// bar foo bar\n",
18309             format("// foo bar baz      bazfoo\n"
18310                    "// foo bar baz      bazfoo bar\n"
18311                    "// foo bar\n",
18312                    Style));
18313 
18314   EXPECT_EQ("// foo bar baz bazfoo\n"
18315             "// foo bar baz bazfoo\n"
18316             "// bar foo bar\n",
18317             format("// foo bar baz      bazfoo\n"
18318                    "// foo bar baz      bazfoo bar\n"
18319                    "// foo           bar\n",
18320                    Style));
18321 
18322   // Make sure we do not keep protruding characters if strict mode reflow is
18323   // cheaper than keeping protruding characters.
18324   Style.ColumnLimit = 21;
18325   EXPECT_EQ(
18326       "// foo foo foo foo\n"
18327       "// foo foo foo foo\n"
18328       "// foo foo foo foo\n",
18329       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18330 
18331   EXPECT_EQ("int a = /* long block\n"
18332             "           comment */\n"
18333             "    42;",
18334             format("int a = /* long block comment */ 42;", Style));
18335 }
18336 
18337 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18338   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18339   EXPECT_EQ(Styles[0], Styles[i])                                              \
18340       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18341 
18342 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18343   SmallVector<FormatStyle, 3> Styles;
18344   Styles.resize(3);
18345 
18346   Styles[0] = getLLVMStyle();
18347   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18348   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18349   EXPECT_ALL_STYLES_EQUAL(Styles);
18350 
18351   Styles[0] = getGoogleStyle();
18352   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18353   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18354   EXPECT_ALL_STYLES_EQUAL(Styles);
18355 
18356   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18357   EXPECT_TRUE(
18358       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18359   EXPECT_TRUE(
18360       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18361   EXPECT_ALL_STYLES_EQUAL(Styles);
18362 
18363   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18364   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18365   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18366   EXPECT_ALL_STYLES_EQUAL(Styles);
18367 
18368   Styles[0] = getMozillaStyle();
18369   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18370   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18371   EXPECT_ALL_STYLES_EQUAL(Styles);
18372 
18373   Styles[0] = getWebKitStyle();
18374   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18375   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18376   EXPECT_ALL_STYLES_EQUAL(Styles);
18377 
18378   Styles[0] = getGNUStyle();
18379   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18380   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18381   EXPECT_ALL_STYLES_EQUAL(Styles);
18382 
18383   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18384 }
18385 
18386 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18387   SmallVector<FormatStyle, 8> Styles;
18388   Styles.resize(2);
18389 
18390   Styles[0] = getGoogleStyle();
18391   Styles[1] = getLLVMStyle();
18392   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18393   EXPECT_ALL_STYLES_EQUAL(Styles);
18394 
18395   Styles.resize(5);
18396   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18397   Styles[1] = getLLVMStyle();
18398   Styles[1].Language = FormatStyle::LK_JavaScript;
18399   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18400 
18401   Styles[2] = getLLVMStyle();
18402   Styles[2].Language = FormatStyle::LK_JavaScript;
18403   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18404                                   "BasedOnStyle: Google",
18405                                   &Styles[2])
18406                    .value());
18407 
18408   Styles[3] = getLLVMStyle();
18409   Styles[3].Language = FormatStyle::LK_JavaScript;
18410   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18411                                   "Language: JavaScript",
18412                                   &Styles[3])
18413                    .value());
18414 
18415   Styles[4] = getLLVMStyle();
18416   Styles[4].Language = FormatStyle::LK_JavaScript;
18417   EXPECT_EQ(0, parseConfiguration("---\n"
18418                                   "BasedOnStyle: LLVM\n"
18419                                   "IndentWidth: 123\n"
18420                                   "---\n"
18421                                   "BasedOnStyle: Google\n"
18422                                   "Language: JavaScript",
18423                                   &Styles[4])
18424                    .value());
18425   EXPECT_ALL_STYLES_EQUAL(Styles);
18426 }
18427 
18428 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18429   Style.FIELD = false;                                                         \
18430   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18431   EXPECT_TRUE(Style.FIELD);                                                    \
18432   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18433   EXPECT_FALSE(Style.FIELD);
18434 
18435 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18436 
18437 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18438   Style.STRUCT.FIELD = false;                                                  \
18439   EXPECT_EQ(0,                                                                 \
18440             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18441                 .value());                                                     \
18442   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18443   EXPECT_EQ(0,                                                                 \
18444             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18445                 .value());                                                     \
18446   EXPECT_FALSE(Style.STRUCT.FIELD);
18447 
18448 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18449   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18450 
18451 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18452   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18453   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18454   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18455 
18456 TEST_F(FormatTest, ParsesConfigurationBools) {
18457   FormatStyle Style = {};
18458   Style.Language = FormatStyle::LK_Cpp;
18459   CHECK_PARSE_BOOL(AlignTrailingComments);
18460   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18461   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18462   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18463   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18464   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18465   CHECK_PARSE_BOOL(BinPackArguments);
18466   CHECK_PARSE_BOOL(BinPackParameters);
18467   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18468   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18469   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18470   CHECK_PARSE_BOOL(BreakStringLiterals);
18471   CHECK_PARSE_BOOL(CompactNamespaces);
18472   CHECK_PARSE_BOOL(DeriveLineEnding);
18473   CHECK_PARSE_BOOL(DerivePointerAlignment);
18474   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18475   CHECK_PARSE_BOOL(DisableFormat);
18476   CHECK_PARSE_BOOL(IndentAccessModifiers);
18477   CHECK_PARSE_BOOL(IndentCaseLabels);
18478   CHECK_PARSE_BOOL(IndentCaseBlocks);
18479   CHECK_PARSE_BOOL(IndentGotoLabels);
18480   CHECK_PARSE_BOOL(IndentRequires);
18481   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18482   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18483   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18484   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18485   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18486   CHECK_PARSE_BOOL(ReflowComments);
18487   CHECK_PARSE_BOOL(SortUsingDeclarations);
18488   CHECK_PARSE_BOOL(SpacesInParentheses);
18489   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18490   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18491   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18492   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18493   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18494   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18495   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18496   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18497   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18498   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18499   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18500   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18501   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18502   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18503   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18504   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18505   CHECK_PARSE_BOOL(UseCRLF);
18506 
18507   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18508   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18509   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18510   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18511   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18512   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18513   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18514   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18515   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18516   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18517   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18518   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18519   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18520   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18521   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18522   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18523   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18524 }
18525 
18526 #undef CHECK_PARSE_BOOL
18527 
18528 TEST_F(FormatTest, ParsesConfiguration) {
18529   FormatStyle Style = {};
18530   Style.Language = FormatStyle::LK_Cpp;
18531   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18532   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18533               ConstructorInitializerIndentWidth, 1234u);
18534   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18535   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18536   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18537   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18538   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18539               PenaltyBreakBeforeFirstCallParameter, 1234u);
18540   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18541               PenaltyBreakTemplateDeclaration, 1234u);
18542   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18543   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18544               PenaltyReturnTypeOnItsOwnLine, 1234u);
18545   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18546               SpacesBeforeTrailingComments, 1234u);
18547   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18548   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18549   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18550 
18551   Style.QualifierAlignment = FormatStyle::QAS_Right;
18552   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
18553               FormatStyle::QAS_Leave);
18554   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
18555               FormatStyle::QAS_Right);
18556   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
18557               FormatStyle::QAS_Left);
18558   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
18559               FormatStyle::QAS_Custom);
18560 
18561   Style.QualifierOrder.clear();
18562   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
18563               std::vector<std::string>({"const", "volatile", "type"}));
18564   Style.QualifierOrder.clear();
18565   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
18566               std::vector<std::string>({"const", "type"}));
18567   Style.QualifierOrder.clear();
18568   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
18569               std::vector<std::string>({"volatile", "type"}));
18570 
18571   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18572   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18573               FormatStyle::ACS_None);
18574   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18575               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18576   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18577               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18578   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18579               AlignConsecutiveAssignments,
18580               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18581   // For backwards compability, false / true should still parse
18582   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18583               FormatStyle::ACS_None);
18584   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18585               FormatStyle::ACS_Consecutive);
18586 
18587   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18588   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18589               FormatStyle::ACS_None);
18590   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18591               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18592   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18593               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18594   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18595               AlignConsecutiveBitFields,
18596               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18597   // For backwards compability, false / true should still parse
18598   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18599               FormatStyle::ACS_None);
18600   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18601               FormatStyle::ACS_Consecutive);
18602 
18603   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18604   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18605               FormatStyle::ACS_None);
18606   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18607               FormatStyle::ACS_Consecutive);
18608   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18609               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18610   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18611               AlignConsecutiveMacros,
18612               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18613   // For backwards compability, false / true should still parse
18614   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18615               FormatStyle::ACS_None);
18616   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18617               FormatStyle::ACS_Consecutive);
18618 
18619   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18620   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18621               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18622   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18623               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18624   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18625               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18626   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18627               AlignConsecutiveDeclarations,
18628               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18629   // For backwards compability, false / true should still parse
18630   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18631               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18632   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18633               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18634 
18635   Style.PointerAlignment = FormatStyle::PAS_Middle;
18636   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18637               FormatStyle::PAS_Left);
18638   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18639               FormatStyle::PAS_Right);
18640   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18641               FormatStyle::PAS_Middle);
18642   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18643   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18644               FormatStyle::RAS_Pointer);
18645   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18646               FormatStyle::RAS_Left);
18647   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18648               FormatStyle::RAS_Right);
18649   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18650               FormatStyle::RAS_Middle);
18651   // For backward compatibility:
18652   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18653               FormatStyle::PAS_Left);
18654   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18655               FormatStyle::PAS_Right);
18656   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18657               FormatStyle::PAS_Middle);
18658 
18659   Style.Standard = FormatStyle::LS_Auto;
18660   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18661   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18662   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18663   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18664   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18665   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18666   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18667   // Legacy aliases:
18668   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18669   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18670   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18671   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18672 
18673   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18674   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18675               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18676   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18677               FormatStyle::BOS_None);
18678   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18679               FormatStyle::BOS_All);
18680   // For backward compatibility:
18681   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18682               FormatStyle::BOS_None);
18683   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18684               FormatStyle::BOS_All);
18685 
18686   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18687   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18688               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18689   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18690               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18691   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18692               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18693   // For backward compatibility:
18694   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18695               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18696 
18697   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18698   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18699               FormatStyle::BILS_AfterComma);
18700   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18701               FormatStyle::BILS_BeforeComma);
18702   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18703               FormatStyle::BILS_AfterColon);
18704   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18705               FormatStyle::BILS_BeforeColon);
18706   // For backward compatibility:
18707   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18708               FormatStyle::BILS_BeforeComma);
18709 
18710   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18711   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
18712               FormatStyle::PCIS_Never);
18713   CHECK_PARSE("PackConstructorInitializers: BinPack",
18714               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18715   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
18716               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18717   CHECK_PARSE("PackConstructorInitializers: NextLine",
18718               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18719   // For backward compatibility:
18720   CHECK_PARSE("BasedOnStyle: Google\n"
18721               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18722               "AllowAllConstructorInitializersOnNextLine: false",
18723               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18724   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
18725   CHECK_PARSE("BasedOnStyle: Google\n"
18726               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
18727               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18728   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18729               "AllowAllConstructorInitializersOnNextLine: true",
18730               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18731   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18732   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18733               "AllowAllConstructorInitializersOnNextLine: false",
18734               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18735 
18736   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18737   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18738               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18739   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18740               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18741   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18742               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18743   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18744               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18745 
18746   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18747   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18748               FormatStyle::BAS_Align);
18749   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18750               FormatStyle::BAS_DontAlign);
18751   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18752               FormatStyle::BAS_AlwaysBreak);
18753   // For backward compatibility:
18754   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18755               FormatStyle::BAS_DontAlign);
18756   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18757               FormatStyle::BAS_Align);
18758 
18759   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18760   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18761               FormatStyle::ENAS_DontAlign);
18762   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18763               FormatStyle::ENAS_Left);
18764   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18765               FormatStyle::ENAS_Right);
18766   // For backward compatibility:
18767   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18768               FormatStyle::ENAS_Left);
18769   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18770               FormatStyle::ENAS_Right);
18771 
18772   Style.AlignOperands = FormatStyle::OAS_Align;
18773   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18774               FormatStyle::OAS_DontAlign);
18775   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18776   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18777               FormatStyle::OAS_AlignAfterOperator);
18778   // For backward compatibility:
18779   CHECK_PARSE("AlignOperands: false", AlignOperands,
18780               FormatStyle::OAS_DontAlign);
18781   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18782 
18783   Style.UseTab = FormatStyle::UT_ForIndentation;
18784   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18785   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18786   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18787   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18788               FormatStyle::UT_ForContinuationAndIndentation);
18789   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18790               FormatStyle::UT_AlignWithSpaces);
18791   // For backward compatibility:
18792   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18793   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18794 
18795   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18796   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18797               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18798   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18799               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18800   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18801               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18802   // For backward compatibility:
18803   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18804               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18805   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18806               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18807 
18808   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18809   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18810               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18811   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18812               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18813   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18814               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18815   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18816               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18817   // For backward compatibility:
18818   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18819               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18820   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18821               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18822 
18823   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18824   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18825               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18826   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18827               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18828   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18829               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18830   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18831               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18832 
18833   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18834   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18835               FormatStyle::SBPO_Never);
18836   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18837               FormatStyle::SBPO_Always);
18838   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18839               FormatStyle::SBPO_ControlStatements);
18840   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18841               SpaceBeforeParens,
18842               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18843   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18844               FormatStyle::SBPO_NonEmptyParentheses);
18845   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
18846               FormatStyle::SBPO_Custom);
18847   // For backward compatibility:
18848   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18849               FormatStyle::SBPO_Never);
18850   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18851               FormatStyle::SBPO_ControlStatements);
18852   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18853               SpaceBeforeParens,
18854               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18855 
18856   Style.ColumnLimit = 123;
18857   FormatStyle BaseStyle = getLLVMStyle();
18858   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18859   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18860 
18861   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18862   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18863               FormatStyle::BS_Attach);
18864   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18865               FormatStyle::BS_Linux);
18866   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18867               FormatStyle::BS_Mozilla);
18868   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18869               FormatStyle::BS_Stroustrup);
18870   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18871               FormatStyle::BS_Allman);
18872   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18873               FormatStyle::BS_Whitesmiths);
18874   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18875   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18876               FormatStyle::BS_WebKit);
18877   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18878               FormatStyle::BS_Custom);
18879 
18880   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18881   CHECK_PARSE("BraceWrapping:\n"
18882               "  AfterControlStatement: MultiLine",
18883               BraceWrapping.AfterControlStatement,
18884               FormatStyle::BWACS_MultiLine);
18885   CHECK_PARSE("BraceWrapping:\n"
18886               "  AfterControlStatement: Always",
18887               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18888   CHECK_PARSE("BraceWrapping:\n"
18889               "  AfterControlStatement: Never",
18890               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18891   // For backward compatibility:
18892   CHECK_PARSE("BraceWrapping:\n"
18893               "  AfterControlStatement: true",
18894               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18895   CHECK_PARSE("BraceWrapping:\n"
18896               "  AfterControlStatement: false",
18897               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18898 
18899   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18900   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18901               FormatStyle::RTBS_None);
18902   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18903               FormatStyle::RTBS_All);
18904   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18905               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18906   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18907               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18908   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18909               AlwaysBreakAfterReturnType,
18910               FormatStyle::RTBS_TopLevelDefinitions);
18911 
18912   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18913   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18914               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18915   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18916               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18917   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18918               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18919   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18920               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18921   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18922               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18923 
18924   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18925   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18926               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18927   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18928               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18929   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18930               AlwaysBreakAfterDefinitionReturnType,
18931               FormatStyle::DRTBS_TopLevel);
18932 
18933   Style.NamespaceIndentation = FormatStyle::NI_All;
18934   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18935               FormatStyle::NI_None);
18936   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18937               FormatStyle::NI_Inner);
18938   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18939               FormatStyle::NI_All);
18940 
18941   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18942   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18943               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18944   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18945               AllowShortIfStatementsOnASingleLine,
18946               FormatStyle::SIS_WithoutElse);
18947   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18948               AllowShortIfStatementsOnASingleLine,
18949               FormatStyle::SIS_OnlyFirstIf);
18950   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18951               AllowShortIfStatementsOnASingleLine,
18952               FormatStyle::SIS_AllIfsAndElse);
18953   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18954               AllowShortIfStatementsOnASingleLine,
18955               FormatStyle::SIS_OnlyFirstIf);
18956   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18957               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18958   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18959               AllowShortIfStatementsOnASingleLine,
18960               FormatStyle::SIS_WithoutElse);
18961 
18962   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18963   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18964               FormatStyle::IEBS_AfterExternBlock);
18965   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18966               FormatStyle::IEBS_Indent);
18967   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18968               FormatStyle::IEBS_NoIndent);
18969   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18970               FormatStyle::IEBS_Indent);
18971   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18972               FormatStyle::IEBS_NoIndent);
18973 
18974   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18975   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18976               FormatStyle::BFCS_Both);
18977   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18978               FormatStyle::BFCS_None);
18979   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18980               FormatStyle::BFCS_Before);
18981   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18982               FormatStyle::BFCS_After);
18983 
18984   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18985   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
18986               FormatStyle::SJSIO_After);
18987   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
18988               FormatStyle::SJSIO_Before);
18989 
18990   // FIXME: This is required because parsing a configuration simply overwrites
18991   // the first N elements of the list instead of resetting it.
18992   Style.ForEachMacros.clear();
18993   std::vector<std::string> BoostForeach;
18994   BoostForeach.push_back("BOOST_FOREACH");
18995   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
18996   std::vector<std::string> BoostAndQForeach;
18997   BoostAndQForeach.push_back("BOOST_FOREACH");
18998   BoostAndQForeach.push_back("Q_FOREACH");
18999   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19000               BoostAndQForeach);
19001 
19002   Style.IfMacros.clear();
19003   std::vector<std::string> CustomIfs;
19004   CustomIfs.push_back("MYIF");
19005   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19006 
19007   Style.AttributeMacros.clear();
19008   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19009               std::vector<std::string>{"__capability"});
19010   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19011               std::vector<std::string>({"attr1", "attr2"}));
19012 
19013   Style.StatementAttributeLikeMacros.clear();
19014   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19015               StatementAttributeLikeMacros,
19016               std::vector<std::string>({"emit", "Q_EMIT"}));
19017 
19018   Style.StatementMacros.clear();
19019   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19020               std::vector<std::string>{"QUNUSED"});
19021   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19022               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19023 
19024   Style.NamespaceMacros.clear();
19025   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19026               std::vector<std::string>{"TESTSUITE"});
19027   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19028               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19029 
19030   Style.WhitespaceSensitiveMacros.clear();
19031   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19032               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19033   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19034               WhitespaceSensitiveMacros,
19035               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19036   Style.WhitespaceSensitiveMacros.clear();
19037   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19038               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19039   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19040               WhitespaceSensitiveMacros,
19041               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19042 
19043   Style.IncludeStyle.IncludeCategories.clear();
19044   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19045       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19046   CHECK_PARSE("IncludeCategories:\n"
19047               "  - Regex: abc/.*\n"
19048               "    Priority: 2\n"
19049               "  - Regex: .*\n"
19050               "    Priority: 1\n"
19051               "    CaseSensitive: true\n",
19052               IncludeStyle.IncludeCategories, ExpectedCategories);
19053   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19054               "abc$");
19055   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19056               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19057 
19058   Style.SortIncludes = FormatStyle::SI_Never;
19059   CHECK_PARSE("SortIncludes: true", SortIncludes,
19060               FormatStyle::SI_CaseSensitive);
19061   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19062   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19063               FormatStyle::SI_CaseInsensitive);
19064   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19065               FormatStyle::SI_CaseSensitive);
19066   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19067 
19068   Style.RawStringFormats.clear();
19069   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19070       {
19071           FormatStyle::LK_TextProto,
19072           {"pb", "proto"},
19073           {"PARSE_TEXT_PROTO"},
19074           /*CanonicalDelimiter=*/"",
19075           "llvm",
19076       },
19077       {
19078           FormatStyle::LK_Cpp,
19079           {"cc", "cpp"},
19080           {"C_CODEBLOCK", "CPPEVAL"},
19081           /*CanonicalDelimiter=*/"cc",
19082           /*BasedOnStyle=*/"",
19083       },
19084   };
19085 
19086   CHECK_PARSE("RawStringFormats:\n"
19087               "  - Language: TextProto\n"
19088               "    Delimiters:\n"
19089               "      - 'pb'\n"
19090               "      - 'proto'\n"
19091               "    EnclosingFunctions:\n"
19092               "      - 'PARSE_TEXT_PROTO'\n"
19093               "    BasedOnStyle: llvm\n"
19094               "  - Language: Cpp\n"
19095               "    Delimiters:\n"
19096               "      - 'cc'\n"
19097               "      - 'cpp'\n"
19098               "    EnclosingFunctions:\n"
19099               "      - 'C_CODEBLOCK'\n"
19100               "      - 'CPPEVAL'\n"
19101               "    CanonicalDelimiter: 'cc'",
19102               RawStringFormats, ExpectedRawStringFormats);
19103 
19104   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19105               "  Minimum: 0\n"
19106               "  Maximum: 0",
19107               SpacesInLineCommentPrefix.Minimum, 0u);
19108   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19109   Style.SpacesInLineCommentPrefix.Minimum = 1;
19110   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19111               "  Minimum: 2",
19112               SpacesInLineCommentPrefix.Minimum, 0u);
19113   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19114               "  Maximum: -1",
19115               SpacesInLineCommentPrefix.Maximum, -1u);
19116   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19117               "  Minimum: 2",
19118               SpacesInLineCommentPrefix.Minimum, 2u);
19119   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19120               "  Maximum: 1",
19121               SpacesInLineCommentPrefix.Maximum, 1u);
19122   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19123 
19124   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19125   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19126   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19127               FormatStyle::SIAS_Always);
19128   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19129   // For backward compatibility:
19130   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19131   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19132 }
19133 
19134 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19135   FormatStyle Style = {};
19136   Style.Language = FormatStyle::LK_Cpp;
19137   CHECK_PARSE("Language: Cpp\n"
19138               "IndentWidth: 12",
19139               IndentWidth, 12u);
19140   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19141                                "IndentWidth: 34",
19142                                &Style),
19143             ParseError::Unsuitable);
19144   FormatStyle BinPackedTCS = {};
19145   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19146   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19147                                "InsertTrailingCommas: Wrapped",
19148                                &BinPackedTCS),
19149             ParseError::BinPackTrailingCommaConflict);
19150   EXPECT_EQ(12u, Style.IndentWidth);
19151   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19152   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19153 
19154   Style.Language = FormatStyle::LK_JavaScript;
19155   CHECK_PARSE("Language: JavaScript\n"
19156               "IndentWidth: 12",
19157               IndentWidth, 12u);
19158   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19159   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19160                                "IndentWidth: 34",
19161                                &Style),
19162             ParseError::Unsuitable);
19163   EXPECT_EQ(23u, Style.IndentWidth);
19164   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19165   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19166 
19167   CHECK_PARSE("BasedOnStyle: LLVM\n"
19168               "IndentWidth: 67",
19169               IndentWidth, 67u);
19170 
19171   CHECK_PARSE("---\n"
19172               "Language: JavaScript\n"
19173               "IndentWidth: 12\n"
19174               "---\n"
19175               "Language: Cpp\n"
19176               "IndentWidth: 34\n"
19177               "...\n",
19178               IndentWidth, 12u);
19179 
19180   Style.Language = FormatStyle::LK_Cpp;
19181   CHECK_PARSE("---\n"
19182               "Language: JavaScript\n"
19183               "IndentWidth: 12\n"
19184               "---\n"
19185               "Language: Cpp\n"
19186               "IndentWidth: 34\n"
19187               "...\n",
19188               IndentWidth, 34u);
19189   CHECK_PARSE("---\n"
19190               "IndentWidth: 78\n"
19191               "---\n"
19192               "Language: JavaScript\n"
19193               "IndentWidth: 56\n"
19194               "...\n",
19195               IndentWidth, 78u);
19196 
19197   Style.ColumnLimit = 123;
19198   Style.IndentWidth = 234;
19199   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19200   Style.TabWidth = 345;
19201   EXPECT_FALSE(parseConfiguration("---\n"
19202                                   "IndentWidth: 456\n"
19203                                   "BreakBeforeBraces: Allman\n"
19204                                   "---\n"
19205                                   "Language: JavaScript\n"
19206                                   "IndentWidth: 111\n"
19207                                   "TabWidth: 111\n"
19208                                   "---\n"
19209                                   "Language: Cpp\n"
19210                                   "BreakBeforeBraces: Stroustrup\n"
19211                                   "TabWidth: 789\n"
19212                                   "...\n",
19213                                   &Style));
19214   EXPECT_EQ(123u, Style.ColumnLimit);
19215   EXPECT_EQ(456u, Style.IndentWidth);
19216   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19217   EXPECT_EQ(789u, Style.TabWidth);
19218 
19219   EXPECT_EQ(parseConfiguration("---\n"
19220                                "Language: JavaScript\n"
19221                                "IndentWidth: 56\n"
19222                                "---\n"
19223                                "IndentWidth: 78\n"
19224                                "...\n",
19225                                &Style),
19226             ParseError::Error);
19227   EXPECT_EQ(parseConfiguration("---\n"
19228                                "Language: JavaScript\n"
19229                                "IndentWidth: 56\n"
19230                                "---\n"
19231                                "Language: JavaScript\n"
19232                                "IndentWidth: 78\n"
19233                                "...\n",
19234                                &Style),
19235             ParseError::Error);
19236 
19237   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19238 }
19239 
19240 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19241   FormatStyle Style = {};
19242   Style.Language = FormatStyle::LK_JavaScript;
19243   Style.BreakBeforeTernaryOperators = true;
19244   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19245   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19246 
19247   Style.BreakBeforeTernaryOperators = true;
19248   EXPECT_EQ(0, parseConfiguration("---\n"
19249                                   "BasedOnStyle: Google\n"
19250                                   "---\n"
19251                                   "Language: JavaScript\n"
19252                                   "IndentWidth: 76\n"
19253                                   "...\n",
19254                                   &Style)
19255                    .value());
19256   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19257   EXPECT_EQ(76u, Style.IndentWidth);
19258   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19259 }
19260 
19261 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19262   FormatStyle Style = getLLVMStyle();
19263   std::string YAML = configurationAsText(Style);
19264   FormatStyle ParsedStyle = {};
19265   ParsedStyle.Language = FormatStyle::LK_Cpp;
19266   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19267   EXPECT_EQ(Style, ParsedStyle);
19268 }
19269 
19270 TEST_F(FormatTest, WorksFor8bitEncodings) {
19271   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19272             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19273             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19274             "\"\xef\xee\xf0\xf3...\"",
19275             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19276                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19277                    "\xef\xee\xf0\xf3...\"",
19278                    getLLVMStyleWithColumns(12)));
19279 }
19280 
19281 TEST_F(FormatTest, HandlesUTF8BOM) {
19282   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19283   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19284             format("\xef\xbb\xbf#include <iostream>"));
19285   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19286             format("\xef\xbb\xbf\n#include <iostream>"));
19287 }
19288 
19289 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19290 #if !defined(_MSC_VER)
19291 
19292 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19293   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19294                getLLVMStyleWithColumns(35));
19295   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19296                getLLVMStyleWithColumns(31));
19297   verifyFormat("// Однажды в студёную зимнюю пору...",
19298                getLLVMStyleWithColumns(36));
19299   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19300   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19301                getLLVMStyleWithColumns(39));
19302   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19303                getLLVMStyleWithColumns(35));
19304 }
19305 
19306 TEST_F(FormatTest, SplitsUTF8Strings) {
19307   // Non-printable characters' width is currently considered to be the length in
19308   // bytes in UTF8. The characters can be displayed in very different manner
19309   // (zero-width, single width with a substitution glyph, expanded to their code
19310   // (e.g. "<8d>"), so there's no single correct way to handle them.
19311   EXPECT_EQ("\"aaaaÄ\"\n"
19312             "\"\xc2\x8d\";",
19313             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19314   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19315             "\"\xc2\x8d\";",
19316             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19317   EXPECT_EQ("\"Однажды, в \"\n"
19318             "\"студёную \"\n"
19319             "\"зимнюю \"\n"
19320             "\"пору,\"",
19321             format("\"Однажды, в студёную зимнюю пору,\"",
19322                    getLLVMStyleWithColumns(13)));
19323   EXPECT_EQ(
19324       "\"一 二 三 \"\n"
19325       "\"四 五六 \"\n"
19326       "\"七 八 九 \"\n"
19327       "\"十\"",
19328       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19329   EXPECT_EQ("\"一\t\"\n"
19330             "\"二 \t\"\n"
19331             "\"三 四 \"\n"
19332             "\"五\t\"\n"
19333             "\"六 \t\"\n"
19334             "\"七 \"\n"
19335             "\"八九十\tqq\"",
19336             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19337                    getLLVMStyleWithColumns(11)));
19338 
19339   // UTF8 character in an escape sequence.
19340   EXPECT_EQ("\"aaaaaa\"\n"
19341             "\"\\\xC2\x8D\"",
19342             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19343 }
19344 
19345 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19346   EXPECT_EQ("const char *sssss =\n"
19347             "    \"一二三四五六七八\\\n"
19348             " 九 十\";",
19349             format("const char *sssss = \"一二三四五六七八\\\n"
19350                    " 九 十\";",
19351                    getLLVMStyleWithColumns(30)));
19352 }
19353 
19354 TEST_F(FormatTest, SplitsUTF8LineComments) {
19355   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19356             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19357   EXPECT_EQ("// Я из лесу\n"
19358             "// вышел; был\n"
19359             "// сильный\n"
19360             "// мороз.",
19361             format("// Я из лесу вышел; был сильный мороз.",
19362                    getLLVMStyleWithColumns(13)));
19363   EXPECT_EQ("// 一二三\n"
19364             "// 四五六七\n"
19365             "// 八  九\n"
19366             "// 十",
19367             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19368 }
19369 
19370 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19371   EXPECT_EQ("/* Гляжу,\n"
19372             " * поднимается\n"
19373             " * медленно в\n"
19374             " * гору\n"
19375             " * Лошадка,\n"
19376             " * везущая\n"
19377             " * хворосту\n"
19378             " * воз. */",
19379             format("/* Гляжу, поднимается медленно в гору\n"
19380                    " * Лошадка, везущая хворосту воз. */",
19381                    getLLVMStyleWithColumns(13)));
19382   EXPECT_EQ(
19383       "/* 一二三\n"
19384       " * 四五六七\n"
19385       " * 八  九\n"
19386       " * 十  */",
19387       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19388   EXPECT_EQ("/* �������� ��������\n"
19389             " * ��������\n"
19390             " * ������-�� */",
19391             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19392 }
19393 
19394 #endif // _MSC_VER
19395 
19396 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19397   FormatStyle Style = getLLVMStyle();
19398 
19399   Style.ConstructorInitializerIndentWidth = 4;
19400   verifyFormat(
19401       "SomeClass::Constructor()\n"
19402       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19403       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19404       Style);
19405 
19406   Style.ConstructorInitializerIndentWidth = 2;
19407   verifyFormat(
19408       "SomeClass::Constructor()\n"
19409       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19410       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19411       Style);
19412 
19413   Style.ConstructorInitializerIndentWidth = 0;
19414   verifyFormat(
19415       "SomeClass::Constructor()\n"
19416       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19417       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19418       Style);
19419   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19420   verifyFormat(
19421       "SomeLongTemplateVariableName<\n"
19422       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19423       Style);
19424   verifyFormat("bool smaller = 1 < "
19425                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19426                "                       "
19427                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19428                Style);
19429 
19430   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19431   verifyFormat("SomeClass::Constructor() :\n"
19432                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19433                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19434                Style);
19435 }
19436 
19437 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19438   FormatStyle Style = getLLVMStyle();
19439   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19440   Style.ConstructorInitializerIndentWidth = 4;
19441   verifyFormat("SomeClass::Constructor()\n"
19442                "    : a(a)\n"
19443                "    , b(b)\n"
19444                "    , c(c) {}",
19445                Style);
19446   verifyFormat("SomeClass::Constructor()\n"
19447                "    : a(a) {}",
19448                Style);
19449 
19450   Style.ColumnLimit = 0;
19451   verifyFormat("SomeClass::Constructor()\n"
19452                "    : a(a) {}",
19453                Style);
19454   verifyFormat("SomeClass::Constructor() noexcept\n"
19455                "    : a(a) {}",
19456                Style);
19457   verifyFormat("SomeClass::Constructor()\n"
19458                "    : a(a)\n"
19459                "    , b(b)\n"
19460                "    , c(c) {}",
19461                Style);
19462   verifyFormat("SomeClass::Constructor()\n"
19463                "    : a(a) {\n"
19464                "  foo();\n"
19465                "  bar();\n"
19466                "}",
19467                Style);
19468 
19469   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19470   verifyFormat("SomeClass::Constructor()\n"
19471                "    : a(a)\n"
19472                "    , b(b)\n"
19473                "    , c(c) {\n}",
19474                Style);
19475   verifyFormat("SomeClass::Constructor()\n"
19476                "    : a(a) {\n}",
19477                Style);
19478 
19479   Style.ColumnLimit = 80;
19480   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19481   Style.ConstructorInitializerIndentWidth = 2;
19482   verifyFormat("SomeClass::Constructor()\n"
19483                "  : a(a)\n"
19484                "  , b(b)\n"
19485                "  , c(c) {}",
19486                Style);
19487 
19488   Style.ConstructorInitializerIndentWidth = 0;
19489   verifyFormat("SomeClass::Constructor()\n"
19490                ": a(a)\n"
19491                ", b(b)\n"
19492                ", c(c) {}",
19493                Style);
19494 
19495   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19496   Style.ConstructorInitializerIndentWidth = 4;
19497   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19498   verifyFormat(
19499       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19500       Style);
19501   verifyFormat(
19502       "SomeClass::Constructor()\n"
19503       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19504       Style);
19505   Style.ConstructorInitializerIndentWidth = 4;
19506   Style.ColumnLimit = 60;
19507   verifyFormat("SomeClass::Constructor()\n"
19508                "    : aaaaaaaa(aaaaaaaa)\n"
19509                "    , aaaaaaaa(aaaaaaaa)\n"
19510                "    , aaaaaaaa(aaaaaaaa) {}",
19511                Style);
19512 }
19513 
19514 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
19515   FormatStyle Style = getLLVMStyle();
19516   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19517   Style.ConstructorInitializerIndentWidth = 4;
19518   verifyFormat("SomeClass::Constructor()\n"
19519                "    : a{a}\n"
19520                "    , b{b} {}",
19521                Style);
19522   verifyFormat("SomeClass::Constructor()\n"
19523                "    : a{a}\n"
19524                "#if CONDITION\n"
19525                "    , b{b}\n"
19526                "#endif\n"
19527                "{\n}",
19528                Style);
19529   Style.ConstructorInitializerIndentWidth = 2;
19530   verifyFormat("SomeClass::Constructor()\n"
19531                "#if CONDITION\n"
19532                "  : a{a}\n"
19533                "#endif\n"
19534                "  , b{b}\n"
19535                "  , c{c} {\n}",
19536                Style);
19537   Style.ConstructorInitializerIndentWidth = 0;
19538   verifyFormat("SomeClass::Constructor()\n"
19539                ": a{a}\n"
19540                "#ifdef CONDITION\n"
19541                ", b{b}\n"
19542                "#else\n"
19543                ", c{c}\n"
19544                "#endif\n"
19545                ", d{d} {\n}",
19546                Style);
19547   Style.ConstructorInitializerIndentWidth = 4;
19548   verifyFormat("SomeClass::Constructor()\n"
19549                "    : a{a}\n"
19550                "#if WINDOWS\n"
19551                "#if DEBUG\n"
19552                "    , b{0}\n"
19553                "#else\n"
19554                "    , b{1}\n"
19555                "#endif\n"
19556                "#else\n"
19557                "#if DEBUG\n"
19558                "    , b{2}\n"
19559                "#else\n"
19560                "    , b{3}\n"
19561                "#endif\n"
19562                "#endif\n"
19563                "{\n}",
19564                Style);
19565   verifyFormat("SomeClass::Constructor()\n"
19566                "    : a{a}\n"
19567                "#if WINDOWS\n"
19568                "    , b{0}\n"
19569                "#if DEBUG\n"
19570                "    , c{0}\n"
19571                "#else\n"
19572                "    , c{1}\n"
19573                "#endif\n"
19574                "#else\n"
19575                "#if DEBUG\n"
19576                "    , c{2}\n"
19577                "#else\n"
19578                "    , c{3}\n"
19579                "#endif\n"
19580                "    , b{1}\n"
19581                "#endif\n"
19582                "{\n}",
19583                Style);
19584 }
19585 
19586 TEST_F(FormatTest, Destructors) {
19587   verifyFormat("void F(int &i) { i.~int(); }");
19588   verifyFormat("void F(int &i) { i->~int(); }");
19589 }
19590 
19591 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19592   FormatStyle Style = getWebKitStyle();
19593 
19594   // Don't indent in outer namespaces.
19595   verifyFormat("namespace outer {\n"
19596                "int i;\n"
19597                "namespace inner {\n"
19598                "    int i;\n"
19599                "} // namespace inner\n"
19600                "} // namespace outer\n"
19601                "namespace other_outer {\n"
19602                "int i;\n"
19603                "}",
19604                Style);
19605 
19606   // Don't indent case labels.
19607   verifyFormat("switch (variable) {\n"
19608                "case 1:\n"
19609                "case 2:\n"
19610                "    doSomething();\n"
19611                "    break;\n"
19612                "default:\n"
19613                "    ++variable;\n"
19614                "}",
19615                Style);
19616 
19617   // Wrap before binary operators.
19618   EXPECT_EQ("void f()\n"
19619             "{\n"
19620             "    if (aaaaaaaaaaaaaaaa\n"
19621             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19622             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19623             "        return;\n"
19624             "}",
19625             format("void f() {\n"
19626                    "if (aaaaaaaaaaaaaaaa\n"
19627                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19628                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19629                    "return;\n"
19630                    "}",
19631                    Style));
19632 
19633   // Allow functions on a single line.
19634   verifyFormat("void f() { return; }", Style);
19635 
19636   // Allow empty blocks on a single line and insert a space in empty blocks.
19637   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19638   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19639   // However, don't merge non-empty short loops.
19640   EXPECT_EQ("while (true) {\n"
19641             "    continue;\n"
19642             "}",
19643             format("while (true) { continue; }", Style));
19644 
19645   // Constructor initializers are formatted one per line with the "," on the
19646   // new line.
19647   verifyFormat("Constructor()\n"
19648                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19649                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19650                "          aaaaaaaaaaaaaa)\n"
19651                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19652                "{\n"
19653                "}",
19654                Style);
19655   verifyFormat("SomeClass::Constructor()\n"
19656                "    : a(a)\n"
19657                "{\n"
19658                "}",
19659                Style);
19660   EXPECT_EQ("SomeClass::Constructor()\n"
19661             "    : a(a)\n"
19662             "{\n"
19663             "}",
19664             format("SomeClass::Constructor():a(a){}", Style));
19665   verifyFormat("SomeClass::Constructor()\n"
19666                "    : a(a)\n"
19667                "    , b(b)\n"
19668                "    , c(c)\n"
19669                "{\n"
19670                "}",
19671                Style);
19672   verifyFormat("SomeClass::Constructor()\n"
19673                "    : a(a)\n"
19674                "{\n"
19675                "    foo();\n"
19676                "    bar();\n"
19677                "}",
19678                Style);
19679 
19680   // Access specifiers should be aligned left.
19681   verifyFormat("class C {\n"
19682                "public:\n"
19683                "    int i;\n"
19684                "};",
19685                Style);
19686 
19687   // Do not align comments.
19688   verifyFormat("int a; // Do not\n"
19689                "double b; // align comments.",
19690                Style);
19691 
19692   // Do not align operands.
19693   EXPECT_EQ("ASSERT(aaaa\n"
19694             "    || bbbb);",
19695             format("ASSERT ( aaaa\n||bbbb);", Style));
19696 
19697   // Accept input's line breaks.
19698   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19699             "    || bbbbbbbbbbbbbbb) {\n"
19700             "    i++;\n"
19701             "}",
19702             format("if (aaaaaaaaaaaaaaa\n"
19703                    "|| bbbbbbbbbbbbbbb) { i++; }",
19704                    Style));
19705   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19706             "    i++;\n"
19707             "}",
19708             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19709 
19710   // Don't automatically break all macro definitions (llvm.org/PR17842).
19711   verifyFormat("#define aNumber 10", Style);
19712   // However, generally keep the line breaks that the user authored.
19713   EXPECT_EQ("#define aNumber \\\n"
19714             "    10",
19715             format("#define aNumber \\\n"
19716                    " 10",
19717                    Style));
19718 
19719   // Keep empty and one-element array literals on a single line.
19720   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19721             "                                  copyItems:YES];",
19722             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19723                    "copyItems:YES];",
19724                    Style));
19725   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19726             "                                  copyItems:YES];",
19727             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19728                    "             copyItems:YES];",
19729                    Style));
19730   // FIXME: This does not seem right, there should be more indentation before
19731   // the array literal's entries. Nested blocks have the same problem.
19732   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19733             "    @\"a\",\n"
19734             "    @\"a\"\n"
19735             "]\n"
19736             "                                  copyItems:YES];",
19737             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19738                    "     @\"a\",\n"
19739                    "     @\"a\"\n"
19740                    "     ]\n"
19741                    "       copyItems:YES];",
19742                    Style));
19743   EXPECT_EQ(
19744       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19745       "                                  copyItems:YES];",
19746       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19747              "   copyItems:YES];",
19748              Style));
19749 
19750   verifyFormat("[self.a b:c c:d];", Style);
19751   EXPECT_EQ("[self.a b:c\n"
19752             "        c:d];",
19753             format("[self.a b:c\n"
19754                    "c:d];",
19755                    Style));
19756 }
19757 
19758 TEST_F(FormatTest, FormatsLambdas) {
19759   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19760   verifyFormat(
19761       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19762   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19763   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19764   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19765   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19766   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19767   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19768   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19769   verifyFormat("int x = f(*+[] {});");
19770   verifyFormat("void f() {\n"
19771                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19772                "}\n");
19773   verifyFormat("void f() {\n"
19774                "  other(x.begin(), //\n"
19775                "        x.end(),   //\n"
19776                "        [&](int, int) { return 1; });\n"
19777                "}\n");
19778   verifyFormat("void f() {\n"
19779                "  other.other.other.other.other(\n"
19780                "      x.begin(), x.end(),\n"
19781                "      [something, rather](int, int, int, int, int, int, int) { "
19782                "return 1; });\n"
19783                "}\n");
19784   verifyFormat(
19785       "void f() {\n"
19786       "  other.other.other.other.other(\n"
19787       "      x.begin(), x.end(),\n"
19788       "      [something, rather](int, int, int, int, int, int, int) {\n"
19789       "        //\n"
19790       "      });\n"
19791       "}\n");
19792   verifyFormat("SomeFunction([]() { // A cool function...\n"
19793                "  return 43;\n"
19794                "});");
19795   EXPECT_EQ("SomeFunction([]() {\n"
19796             "#define A a\n"
19797             "  return 43;\n"
19798             "});",
19799             format("SomeFunction([](){\n"
19800                    "#define A a\n"
19801                    "return 43;\n"
19802                    "});"));
19803   verifyFormat("void f() {\n"
19804                "  SomeFunction([](decltype(x), A *a) {});\n"
19805                "  SomeFunction([](typeof(x), A *a) {});\n"
19806                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19807                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19808                "}");
19809   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19810                "    [](const aaaaaaaaaa &a) { return a; });");
19811   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19812                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19813                "});");
19814   verifyFormat("Constructor()\n"
19815                "    : Field([] { // comment\n"
19816                "        int i;\n"
19817                "      }) {}");
19818   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19819                "  return some_parameter.size();\n"
19820                "};");
19821   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19822                "    [](const string &s) { return s; };");
19823   verifyFormat("int i = aaaaaa ? 1 //\n"
19824                "               : [] {\n"
19825                "                   return 2; //\n"
19826                "                 }();");
19827   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19828                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19829                "                  return x == 2; // force break\n"
19830                "                });");
19831   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19832                "    [=](int iiiiiiiiiiii) {\n"
19833                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19834                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19835                "    });",
19836                getLLVMStyleWithColumns(60));
19837 
19838   verifyFormat("SomeFunction({[&] {\n"
19839                "                // comment\n"
19840                "              },\n"
19841                "              [&] {\n"
19842                "                // comment\n"
19843                "              }});");
19844   verifyFormat("SomeFunction({[&] {\n"
19845                "  // comment\n"
19846                "}});");
19847   verifyFormat(
19848       "virtual aaaaaaaaaaaaaaaa(\n"
19849       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19850       "    aaaaa aaaaaaaaa);");
19851 
19852   // Lambdas with return types.
19853   verifyFormat("int c = []() -> int { return 2; }();\n");
19854   verifyFormat("int c = []() -> int * { return 2; }();\n");
19855   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19856   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19857   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19858   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19859   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19860   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19861   verifyFormat("[a, a]() -> a<1> {};");
19862   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19863   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19864   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19865   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19866   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19867   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19868   verifyFormat("[]() -> foo<!5> { return {}; };");
19869   verifyFormat("[]() -> foo<~5> { return {}; };");
19870   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19871   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19872   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19873   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19874   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19875   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19876   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19877   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19878   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19879   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19880   verifyFormat("namespace bar {\n"
19881                "// broken:\n"
19882                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19883                "} // namespace bar");
19884   verifyFormat("namespace bar {\n"
19885                "// broken:\n"
19886                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19887                "} // namespace bar");
19888   verifyFormat("namespace bar {\n"
19889                "// broken:\n"
19890                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19891                "} // namespace bar");
19892   verifyFormat("namespace bar {\n"
19893                "// broken:\n"
19894                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19895                "} // namespace bar");
19896   verifyFormat("namespace bar {\n"
19897                "// broken:\n"
19898                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19899                "} // namespace bar");
19900   verifyFormat("namespace bar {\n"
19901                "// broken:\n"
19902                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19903                "} // namespace bar");
19904   verifyFormat("namespace bar {\n"
19905                "// broken:\n"
19906                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19907                "} // namespace bar");
19908   verifyFormat("namespace bar {\n"
19909                "// broken:\n"
19910                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19911                "} // namespace bar");
19912   verifyFormat("namespace bar {\n"
19913                "// broken:\n"
19914                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19915                "} // namespace bar");
19916   verifyFormat("namespace bar {\n"
19917                "// broken:\n"
19918                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19919                "} // namespace bar");
19920   verifyFormat("namespace bar {\n"
19921                "// broken:\n"
19922                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19923                "} // namespace bar");
19924   verifyFormat("namespace bar {\n"
19925                "// broken:\n"
19926                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19927                "} // namespace bar");
19928   verifyFormat("namespace bar {\n"
19929                "// broken:\n"
19930                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19931                "} // namespace bar");
19932   verifyFormat("namespace bar {\n"
19933                "// broken:\n"
19934                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19935                "} // namespace bar");
19936   verifyFormat("namespace bar {\n"
19937                "// broken:\n"
19938                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19939                "} // namespace bar");
19940   verifyFormat("namespace bar {\n"
19941                "// broken:\n"
19942                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19943                "} // namespace bar");
19944   verifyFormat("namespace bar {\n"
19945                "// broken:\n"
19946                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19947                "} // namespace bar");
19948   verifyFormat("namespace bar {\n"
19949                "// broken:\n"
19950                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19951                "} // namespace bar");
19952   verifyFormat("[]() -> a<1> {};");
19953   verifyFormat("[]() -> a<1> { ; };");
19954   verifyFormat("[]() -> a<1> { ; }();");
19955   verifyFormat("[a, a]() -> a<true> {};");
19956   verifyFormat("[]() -> a<true> {};");
19957   verifyFormat("[]() -> a<true> { ; };");
19958   verifyFormat("[]() -> a<true> { ; }();");
19959   verifyFormat("[a, a]() -> a<false> {};");
19960   verifyFormat("[]() -> a<false> {};");
19961   verifyFormat("[]() -> a<false> { ; };");
19962   verifyFormat("[]() -> a<false> { ; }();");
19963   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19964   verifyFormat("namespace bar {\n"
19965                "auto foo{[]() -> foo<false> { ; }};\n"
19966                "} // namespace bar");
19967   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19968                "                   int j) -> int {\n"
19969                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19970                "};");
19971   verifyFormat(
19972       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19973       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19974       "      return aaaaaaaaaaaaaaaaa;\n"
19975       "    });",
19976       getLLVMStyleWithColumns(70));
19977   verifyFormat("[]() //\n"
19978                "    -> int {\n"
19979                "  return 1; //\n"
19980                "};");
19981   verifyFormat("[]() -> Void<T...> {};");
19982   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19983 
19984   // Lambdas with explicit template argument lists.
19985   verifyFormat(
19986       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
19987 
19988   // Multiple lambdas in the same parentheses change indentation rules. These
19989   // lambdas are forced to start on new lines.
19990   verifyFormat("SomeFunction(\n"
19991                "    []() {\n"
19992                "      //\n"
19993                "    },\n"
19994                "    []() {\n"
19995                "      //\n"
19996                "    });");
19997 
19998   // A lambda passed as arg0 is always pushed to the next line.
19999   verifyFormat("SomeFunction(\n"
20000                "    [this] {\n"
20001                "      //\n"
20002                "    },\n"
20003                "    1);\n");
20004 
20005   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20006   // the arg0 case above.
20007   auto Style = getGoogleStyle();
20008   Style.BinPackArguments = false;
20009   verifyFormat("SomeFunction(\n"
20010                "    a,\n"
20011                "    [this] {\n"
20012                "      //\n"
20013                "    },\n"
20014                "    b);\n",
20015                Style);
20016   verifyFormat("SomeFunction(\n"
20017                "    a,\n"
20018                "    [this] {\n"
20019                "      //\n"
20020                "    },\n"
20021                "    b);\n");
20022 
20023   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20024   // the BinPackArguments value (as long as the code is wide enough).
20025   verifyFormat(
20026       "something->SomeFunction(\n"
20027       "    a,\n"
20028       "    [this] {\n"
20029       "      "
20030       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20031       "    },\n"
20032       "    b);\n");
20033 
20034   // A multi-line lambda is pulled up as long as the introducer fits on the
20035   // previous line and there are no further args.
20036   verifyFormat("function(1, [this, that] {\n"
20037                "  //\n"
20038                "});\n");
20039   verifyFormat("function([this, that] {\n"
20040                "  //\n"
20041                "});\n");
20042   // FIXME: this format is not ideal and we should consider forcing the first
20043   // arg onto its own line.
20044   verifyFormat("function(a, b, c, //\n"
20045                "         d, [this, that] {\n"
20046                "           //\n"
20047                "         });\n");
20048 
20049   // Multiple lambdas are treated correctly even when there is a short arg0.
20050   verifyFormat("SomeFunction(\n"
20051                "    1,\n"
20052                "    [this] {\n"
20053                "      //\n"
20054                "    },\n"
20055                "    [this] {\n"
20056                "      //\n"
20057                "    },\n"
20058                "    1);\n");
20059 
20060   // More complex introducers.
20061   verifyFormat("return [i, args...] {};");
20062 
20063   // Not lambdas.
20064   verifyFormat("constexpr char hello[]{\"hello\"};");
20065   verifyFormat("double &operator[](int i) { return 0; }\n"
20066                "int i;");
20067   verifyFormat("std::unique_ptr<int[]> foo() {}");
20068   verifyFormat("int i = a[a][a]->f();");
20069   verifyFormat("int i = (*b)[a]->f();");
20070 
20071   // Other corner cases.
20072   verifyFormat("void f() {\n"
20073                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20074                "  );\n"
20075                "}");
20076 
20077   // Lambdas created through weird macros.
20078   verifyFormat("void f() {\n"
20079                "  MACRO((const AA &a) { return 1; });\n"
20080                "  MACRO((AA &a) { return 1; });\n"
20081                "}");
20082 
20083   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20084                "      doo_dah();\n"
20085                "      doo_dah();\n"
20086                "    })) {\n"
20087                "}");
20088   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20089                "                doo_dah();\n"
20090                "                doo_dah();\n"
20091                "              })) {\n"
20092                "}");
20093   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20094                "                doo_dah();\n"
20095                "                doo_dah();\n"
20096                "              })) {\n"
20097                "}");
20098   verifyFormat("auto lambda = []() {\n"
20099                "  int a = 2\n"
20100                "#if A\n"
20101                "          + 2\n"
20102                "#endif\n"
20103                "      ;\n"
20104                "};");
20105 
20106   // Lambdas with complex multiline introducers.
20107   verifyFormat(
20108       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20109       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20110       "        -> ::std::unordered_set<\n"
20111       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20112       "      //\n"
20113       "    });");
20114 
20115   FormatStyle DoNotMerge = getLLVMStyle();
20116   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20117   verifyFormat("auto c = []() {\n"
20118                "  return b;\n"
20119                "};",
20120                "auto c = []() { return b; };", DoNotMerge);
20121   verifyFormat("auto c = []() {\n"
20122                "};",
20123                " auto c = []() {};", DoNotMerge);
20124 
20125   FormatStyle MergeEmptyOnly = getLLVMStyle();
20126   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20127   verifyFormat("auto c = []() {\n"
20128                "  return b;\n"
20129                "};",
20130                "auto c = []() {\n"
20131                "  return b;\n"
20132                " };",
20133                MergeEmptyOnly);
20134   verifyFormat("auto c = []() {};",
20135                "auto c = []() {\n"
20136                "};",
20137                MergeEmptyOnly);
20138 
20139   FormatStyle MergeInline = getLLVMStyle();
20140   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20141   verifyFormat("auto c = []() {\n"
20142                "  return b;\n"
20143                "};",
20144                "auto c = []() { return b; };", MergeInline);
20145   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20146                MergeInline);
20147   verifyFormat("function([]() { return b; }, a)",
20148                "function([]() { return b; }, a)", MergeInline);
20149   verifyFormat("function(a, []() { return b; })",
20150                "function(a, []() { return b; })", MergeInline);
20151 
20152   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20153   // AllowShortLambdasOnASingleLine
20154   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20155   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20156   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20157   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20158       FormatStyle::ShortLambdaStyle::SLS_None;
20159   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20160                "    []()\n"
20161                "    {\n"
20162                "      return 17;\n"
20163                "    });",
20164                LLVMWithBeforeLambdaBody);
20165   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20166                "    []()\n"
20167                "    {\n"
20168                "    });",
20169                LLVMWithBeforeLambdaBody);
20170   verifyFormat("auto fct_SLS_None = []()\n"
20171                "{\n"
20172                "  return 17;\n"
20173                "};",
20174                LLVMWithBeforeLambdaBody);
20175   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20176                "    []()\n"
20177                "    {\n"
20178                "      return Call(\n"
20179                "          []()\n"
20180                "          {\n"
20181                "            return 17;\n"
20182                "          });\n"
20183                "    });",
20184                LLVMWithBeforeLambdaBody);
20185   verifyFormat("void Fct() {\n"
20186                "  return {[]()\n"
20187                "          {\n"
20188                "            return 17;\n"
20189                "          }};\n"
20190                "}",
20191                LLVMWithBeforeLambdaBody);
20192 
20193   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20194       FormatStyle::ShortLambdaStyle::SLS_Empty;
20195   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20196                "    []()\n"
20197                "    {\n"
20198                "      return 17;\n"
20199                "    });",
20200                LLVMWithBeforeLambdaBody);
20201   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20202                LLVMWithBeforeLambdaBody);
20203   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20204                "ongFunctionName_SLS_Empty(\n"
20205                "    []() {});",
20206                LLVMWithBeforeLambdaBody);
20207   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20208                "                                []()\n"
20209                "                                {\n"
20210                "                                  return 17;\n"
20211                "                                });",
20212                LLVMWithBeforeLambdaBody);
20213   verifyFormat("auto fct_SLS_Empty = []()\n"
20214                "{\n"
20215                "  return 17;\n"
20216                "};",
20217                LLVMWithBeforeLambdaBody);
20218   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20219                "    []()\n"
20220                "    {\n"
20221                "      return Call([]() {});\n"
20222                "    });",
20223                LLVMWithBeforeLambdaBody);
20224   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20225                "                           []()\n"
20226                "                           {\n"
20227                "                             return Call([]() {});\n"
20228                "                           });",
20229                LLVMWithBeforeLambdaBody);
20230   verifyFormat(
20231       "FctWithLongLineInLambda_SLS_Empty(\n"
20232       "    []()\n"
20233       "    {\n"
20234       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20235       "                               AndShouldNotBeConsiderAsInline,\n"
20236       "                               LambdaBodyMustBeBreak);\n"
20237       "    });",
20238       LLVMWithBeforeLambdaBody);
20239 
20240   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20241       FormatStyle::ShortLambdaStyle::SLS_Inline;
20242   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20243                LLVMWithBeforeLambdaBody);
20244   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20245                LLVMWithBeforeLambdaBody);
20246   verifyFormat("auto fct_SLS_Inline = []()\n"
20247                "{\n"
20248                "  return 17;\n"
20249                "};",
20250                LLVMWithBeforeLambdaBody);
20251   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20252                "17; }); });",
20253                LLVMWithBeforeLambdaBody);
20254   verifyFormat(
20255       "FctWithLongLineInLambda_SLS_Inline(\n"
20256       "    []()\n"
20257       "    {\n"
20258       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20259       "                               AndShouldNotBeConsiderAsInline,\n"
20260       "                               LambdaBodyMustBeBreak);\n"
20261       "    });",
20262       LLVMWithBeforeLambdaBody);
20263   verifyFormat("FctWithMultipleParams_SLS_Inline("
20264                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20265                "                                 []() { return 17; });",
20266                LLVMWithBeforeLambdaBody);
20267   verifyFormat(
20268       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20269       LLVMWithBeforeLambdaBody);
20270 
20271   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20272       FormatStyle::ShortLambdaStyle::SLS_All;
20273   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20274                LLVMWithBeforeLambdaBody);
20275   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20276                LLVMWithBeforeLambdaBody);
20277   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20278                LLVMWithBeforeLambdaBody);
20279   verifyFormat("FctWithOneParam_SLS_All(\n"
20280                "    []()\n"
20281                "    {\n"
20282                "      // A cool function...\n"
20283                "      return 43;\n"
20284                "    });",
20285                LLVMWithBeforeLambdaBody);
20286   verifyFormat("FctWithMultipleParams_SLS_All("
20287                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20288                "                              []() { return 17; });",
20289                LLVMWithBeforeLambdaBody);
20290   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20291                LLVMWithBeforeLambdaBody);
20292   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20293                LLVMWithBeforeLambdaBody);
20294   verifyFormat(
20295       "FctWithLongLineInLambda_SLS_All(\n"
20296       "    []()\n"
20297       "    {\n"
20298       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20299       "                               AndShouldNotBeConsiderAsInline,\n"
20300       "                               LambdaBodyMustBeBreak);\n"
20301       "    });",
20302       LLVMWithBeforeLambdaBody);
20303   verifyFormat(
20304       "auto fct_SLS_All = []()\n"
20305       "{\n"
20306       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20307       "                           AndShouldNotBeConsiderAsInline,\n"
20308       "                           LambdaBodyMustBeBreak);\n"
20309       "};",
20310       LLVMWithBeforeLambdaBody);
20311   LLVMWithBeforeLambdaBody.BinPackParameters = false;
20312   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
20313                LLVMWithBeforeLambdaBody);
20314   verifyFormat(
20315       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
20316       "                                FirstParam,\n"
20317       "                                SecondParam,\n"
20318       "                                ThirdParam,\n"
20319       "                                FourthParam);",
20320       LLVMWithBeforeLambdaBody);
20321   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20322                "    []() { return "
20323                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
20324                "    FirstParam,\n"
20325                "    SecondParam,\n"
20326                "    ThirdParam,\n"
20327                "    FourthParam);",
20328                LLVMWithBeforeLambdaBody);
20329   verifyFormat(
20330       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
20331       "                                SecondParam,\n"
20332       "                                ThirdParam,\n"
20333       "                                FourthParam,\n"
20334       "                                []() { return SomeValueNotSoLong; });",
20335       LLVMWithBeforeLambdaBody);
20336   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20337                "    []()\n"
20338                "    {\n"
20339                "      return "
20340                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
20341                "eConsiderAsInline;\n"
20342                "    });",
20343                LLVMWithBeforeLambdaBody);
20344   verifyFormat(
20345       "FctWithLongLineInLambda_SLS_All(\n"
20346       "    []()\n"
20347       "    {\n"
20348       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20349       "                               AndShouldNotBeConsiderAsInline,\n"
20350       "                               LambdaBodyMustBeBreak);\n"
20351       "    });",
20352       LLVMWithBeforeLambdaBody);
20353   verifyFormat("FctWithTwoParams_SLS_All(\n"
20354                "    []()\n"
20355                "    {\n"
20356                "      // A cool function...\n"
20357                "      return 43;\n"
20358                "    },\n"
20359                "    87);",
20360                LLVMWithBeforeLambdaBody);
20361   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20362                LLVMWithBeforeLambdaBody);
20363   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20364                LLVMWithBeforeLambdaBody);
20365   verifyFormat(
20366       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20367       LLVMWithBeforeLambdaBody);
20368   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20369                "}); }, x);",
20370                LLVMWithBeforeLambdaBody);
20371   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20372                "    []()\n"
20373                "    {\n"
20374                "      // A cool function...\n"
20375                "      return Call([]() { return 17; });\n"
20376                "    });",
20377                LLVMWithBeforeLambdaBody);
20378   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20379                "    []()\n"
20380                "    {\n"
20381                "      return Call(\n"
20382                "          []()\n"
20383                "          {\n"
20384                "            // A cool function...\n"
20385                "            return 17;\n"
20386                "          });\n"
20387                "    });",
20388                LLVMWithBeforeLambdaBody);
20389 
20390   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20391       FormatStyle::ShortLambdaStyle::SLS_None;
20392 
20393   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20394                "{\n"
20395                "  return MyAssignment::SelectFromList(this);\n"
20396                "};\n",
20397                LLVMWithBeforeLambdaBody);
20398 
20399   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20400                "{\n"
20401                "  return MyAssignment::SelectFromList(this);\n"
20402                "};\n",
20403                LLVMWithBeforeLambdaBody);
20404 
20405   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20406                "{\n"
20407                "  return MyAssignment::SelectFromList(this);\n"
20408                "};\n",
20409                LLVMWithBeforeLambdaBody);
20410 
20411   verifyFormat("namespace test {\n"
20412                "class Test {\n"
20413                "public:\n"
20414                "  Test() = default;\n"
20415                "};\n"
20416                "} // namespace test",
20417                LLVMWithBeforeLambdaBody);
20418 
20419   // Lambdas with different indentation styles.
20420   Style = getLLVMStyleWithColumns(100);
20421   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20422             "  return promise.then(\n"
20423             "      [this, &someVariable, someObject = "
20424             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20425             "        return someObject.startAsyncAction().then(\n"
20426             "            [this, &someVariable](AsyncActionResult result) "
20427             "mutable { result.processMore(); });\n"
20428             "      });\n"
20429             "}\n",
20430             format("SomeResult doSomething(SomeObject promise) {\n"
20431                    "  return promise.then([this, &someVariable, someObject = "
20432                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20433                    "    return someObject.startAsyncAction().then([this, "
20434                    "&someVariable](AsyncActionResult result) mutable {\n"
20435                    "      result.processMore();\n"
20436                    "    });\n"
20437                    "  });\n"
20438                    "}\n",
20439                    Style));
20440   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20441   verifyFormat("test() {\n"
20442                "  ([]() -> {\n"
20443                "    int b = 32;\n"
20444                "    return 3;\n"
20445                "  }).foo();\n"
20446                "}",
20447                Style);
20448   verifyFormat("test() {\n"
20449                "  []() -> {\n"
20450                "    int b = 32;\n"
20451                "    return 3;\n"
20452                "  }\n"
20453                "}",
20454                Style);
20455   verifyFormat("std::sort(v.begin(), v.end(),\n"
20456                "          [](const auto &someLongArgumentName, const auto "
20457                "&someOtherLongArgumentName) {\n"
20458                "  return someLongArgumentName.someMemberVariable < "
20459                "someOtherLongArgumentName.someMemberVariable;\n"
20460                "});",
20461                Style);
20462   verifyFormat("test() {\n"
20463                "  (\n"
20464                "      []() -> {\n"
20465                "        int b = 32;\n"
20466                "        return 3;\n"
20467                "      },\n"
20468                "      foo, bar)\n"
20469                "      .foo();\n"
20470                "}",
20471                Style);
20472   verifyFormat("test() {\n"
20473                "  ([]() -> {\n"
20474                "    int b = 32;\n"
20475                "    return 3;\n"
20476                "  })\n"
20477                "      .foo()\n"
20478                "      .bar();\n"
20479                "}",
20480                Style);
20481   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20482             "  return promise.then(\n"
20483             "      [this, &someVariable, someObject = "
20484             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20485             "    return someObject.startAsyncAction().then(\n"
20486             "        [this, &someVariable](AsyncActionResult result) mutable { "
20487             "result.processMore(); });\n"
20488             "  });\n"
20489             "}\n",
20490             format("SomeResult doSomething(SomeObject promise) {\n"
20491                    "  return promise.then([this, &someVariable, someObject = "
20492                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20493                    "    return someObject.startAsyncAction().then([this, "
20494                    "&someVariable](AsyncActionResult result) mutable {\n"
20495                    "      result.processMore();\n"
20496                    "    });\n"
20497                    "  });\n"
20498                    "}\n",
20499                    Style));
20500   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20501             "  return promise.then([this, &someVariable] {\n"
20502             "    return someObject.startAsyncAction().then(\n"
20503             "        [this, &someVariable](AsyncActionResult result) mutable { "
20504             "result.processMore(); });\n"
20505             "  });\n"
20506             "}\n",
20507             format("SomeResult doSomething(SomeObject promise) {\n"
20508                    "  return promise.then([this, &someVariable] {\n"
20509                    "    return someObject.startAsyncAction().then([this, "
20510                    "&someVariable](AsyncActionResult result) mutable {\n"
20511                    "      result.processMore();\n"
20512                    "    });\n"
20513                    "  });\n"
20514                    "}\n",
20515                    Style));
20516   Style = getGoogleStyle();
20517   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20518   EXPECT_EQ("#define A                                       \\\n"
20519             "  [] {                                          \\\n"
20520             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20521             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20522             "      }",
20523             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20524                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20525                    Style));
20526   // TODO: The current formatting has a minor issue that's not worth fixing
20527   // right now whereby the closing brace is indented relative to the signature
20528   // instead of being aligned. This only happens with macros.
20529 }
20530 
20531 TEST_F(FormatTest, LambdaWithLineComments) {
20532   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20533   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20534   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20535   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20536       FormatStyle::ShortLambdaStyle::SLS_All;
20537 
20538   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20539   verifyFormat("auto k = []() // comment\n"
20540                "{ return; }",
20541                LLVMWithBeforeLambdaBody);
20542   verifyFormat("auto k = []() /* comment */ { return; }",
20543                LLVMWithBeforeLambdaBody);
20544   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20545                LLVMWithBeforeLambdaBody);
20546   verifyFormat("auto k = []() // X\n"
20547                "{ return; }",
20548                LLVMWithBeforeLambdaBody);
20549   verifyFormat(
20550       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20551       "{ return; }",
20552       LLVMWithBeforeLambdaBody);
20553 }
20554 
20555 TEST_F(FormatTest, EmptyLinesInLambdas) {
20556   verifyFormat("auto lambda = []() {\n"
20557                "  x(); //\n"
20558                "};",
20559                "auto lambda = []() {\n"
20560                "\n"
20561                "  x(); //\n"
20562                "\n"
20563                "};");
20564 }
20565 
20566 TEST_F(FormatTest, FormatsBlocks) {
20567   FormatStyle ShortBlocks = getLLVMStyle();
20568   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20569   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20570   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20571   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20572   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20573   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20574   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20575 
20576   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20577   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20578   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20579 
20580   verifyFormat("[operation setCompletionBlock:^{\n"
20581                "  [self onOperationDone];\n"
20582                "}];");
20583   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20584                "  [self onOperationDone];\n"
20585                "}]};");
20586   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20587                "  f();\n"
20588                "}];");
20589   verifyFormat("int a = [operation block:^int(int *i) {\n"
20590                "  return 1;\n"
20591                "}];");
20592   verifyFormat("[myObject doSomethingWith:arg1\n"
20593                "                      aaa:^int(int *a) {\n"
20594                "                        return 1;\n"
20595                "                      }\n"
20596                "                      bbb:f(a * bbbbbbbb)];");
20597 
20598   verifyFormat("[operation setCompletionBlock:^{\n"
20599                "  [self.delegate newDataAvailable];\n"
20600                "}];",
20601                getLLVMStyleWithColumns(60));
20602   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20603                "  NSString *path = [self sessionFilePath];\n"
20604                "  if (path) {\n"
20605                "    // ...\n"
20606                "  }\n"
20607                "});");
20608   verifyFormat("[[SessionService sharedService]\n"
20609                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20610                "      if (window) {\n"
20611                "        [self windowDidLoad:window];\n"
20612                "      } else {\n"
20613                "        [self errorLoadingWindow];\n"
20614                "      }\n"
20615                "    }];");
20616   verifyFormat("void (^largeBlock)(void) = ^{\n"
20617                "  // ...\n"
20618                "};\n",
20619                getLLVMStyleWithColumns(40));
20620   verifyFormat("[[SessionService sharedService]\n"
20621                "    loadWindowWithCompletionBlock: //\n"
20622                "        ^(SessionWindow *window) {\n"
20623                "          if (window) {\n"
20624                "            [self windowDidLoad:window];\n"
20625                "          } else {\n"
20626                "            [self errorLoadingWindow];\n"
20627                "          }\n"
20628                "        }];",
20629                getLLVMStyleWithColumns(60));
20630   verifyFormat("[myObject doSomethingWith:arg1\n"
20631                "    firstBlock:^(Foo *a) {\n"
20632                "      // ...\n"
20633                "      int i;\n"
20634                "    }\n"
20635                "    secondBlock:^(Bar *b) {\n"
20636                "      // ...\n"
20637                "      int i;\n"
20638                "    }\n"
20639                "    thirdBlock:^Foo(Bar *b) {\n"
20640                "      // ...\n"
20641                "      int i;\n"
20642                "    }];");
20643   verifyFormat("[myObject doSomethingWith:arg1\n"
20644                "               firstBlock:-1\n"
20645                "              secondBlock:^(Bar *b) {\n"
20646                "                // ...\n"
20647                "                int i;\n"
20648                "              }];");
20649 
20650   verifyFormat("f(^{\n"
20651                "  @autoreleasepool {\n"
20652                "    if (a) {\n"
20653                "      g();\n"
20654                "    }\n"
20655                "  }\n"
20656                "});");
20657   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20658   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20659                "};");
20660 
20661   FormatStyle FourIndent = getLLVMStyle();
20662   FourIndent.ObjCBlockIndentWidth = 4;
20663   verifyFormat("[operation setCompletionBlock:^{\n"
20664                "    [self onOperationDone];\n"
20665                "}];",
20666                FourIndent);
20667 }
20668 
20669 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20670   FormatStyle ZeroColumn = getLLVMStyle();
20671   ZeroColumn.ColumnLimit = 0;
20672 
20673   verifyFormat("[[SessionService sharedService] "
20674                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20675                "  if (window) {\n"
20676                "    [self windowDidLoad:window];\n"
20677                "  } else {\n"
20678                "    [self errorLoadingWindow];\n"
20679                "  }\n"
20680                "}];",
20681                ZeroColumn);
20682   EXPECT_EQ("[[SessionService sharedService]\n"
20683             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20684             "      if (window) {\n"
20685             "        [self windowDidLoad:window];\n"
20686             "      } else {\n"
20687             "        [self errorLoadingWindow];\n"
20688             "      }\n"
20689             "    }];",
20690             format("[[SessionService sharedService]\n"
20691                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20692                    "                if (window) {\n"
20693                    "    [self windowDidLoad:window];\n"
20694                    "  } else {\n"
20695                    "    [self errorLoadingWindow];\n"
20696                    "  }\n"
20697                    "}];",
20698                    ZeroColumn));
20699   verifyFormat("[myObject doSomethingWith:arg1\n"
20700                "    firstBlock:^(Foo *a) {\n"
20701                "      // ...\n"
20702                "      int i;\n"
20703                "    }\n"
20704                "    secondBlock:^(Bar *b) {\n"
20705                "      // ...\n"
20706                "      int i;\n"
20707                "    }\n"
20708                "    thirdBlock:^Foo(Bar *b) {\n"
20709                "      // ...\n"
20710                "      int i;\n"
20711                "    }];",
20712                ZeroColumn);
20713   verifyFormat("f(^{\n"
20714                "  @autoreleasepool {\n"
20715                "    if (a) {\n"
20716                "      g();\n"
20717                "    }\n"
20718                "  }\n"
20719                "});",
20720                ZeroColumn);
20721   verifyFormat("void (^largeBlock)(void) = ^{\n"
20722                "  // ...\n"
20723                "};",
20724                ZeroColumn);
20725 
20726   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20727   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20728             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20729   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20730   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20731             "  int i;\n"
20732             "};",
20733             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20734 }
20735 
20736 TEST_F(FormatTest, SupportsCRLF) {
20737   EXPECT_EQ("int a;\r\n"
20738             "int b;\r\n"
20739             "int c;\r\n",
20740             format("int a;\r\n"
20741                    "  int b;\r\n"
20742                    "    int c;\r\n",
20743                    getLLVMStyle()));
20744   EXPECT_EQ("int a;\r\n"
20745             "int b;\r\n"
20746             "int c;\r\n",
20747             format("int a;\r\n"
20748                    "  int b;\n"
20749                    "    int c;\r\n",
20750                    getLLVMStyle()));
20751   EXPECT_EQ("int a;\n"
20752             "int b;\n"
20753             "int c;\n",
20754             format("int a;\r\n"
20755                    "  int b;\n"
20756                    "    int c;\n",
20757                    getLLVMStyle()));
20758   EXPECT_EQ("\"aaaaaaa \"\r\n"
20759             "\"bbbbbbb\";\r\n",
20760             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20761   EXPECT_EQ("#define A \\\r\n"
20762             "  b;      \\\r\n"
20763             "  c;      \\\r\n"
20764             "  d;\r\n",
20765             format("#define A \\\r\n"
20766                    "  b; \\\r\n"
20767                    "  c; d; \r\n",
20768                    getGoogleStyle()));
20769 
20770   EXPECT_EQ("/*\r\n"
20771             "multi line block comments\r\n"
20772             "should not introduce\r\n"
20773             "an extra carriage return\r\n"
20774             "*/\r\n",
20775             format("/*\r\n"
20776                    "multi line block comments\r\n"
20777                    "should not introduce\r\n"
20778                    "an extra carriage return\r\n"
20779                    "*/\r\n"));
20780   EXPECT_EQ("/*\r\n"
20781             "\r\n"
20782             "*/",
20783             format("/*\r\n"
20784                    "    \r\r\r\n"
20785                    "*/"));
20786 
20787   FormatStyle style = getLLVMStyle();
20788 
20789   style.DeriveLineEnding = true;
20790   style.UseCRLF = false;
20791   EXPECT_EQ("union FooBarBazQux {\n"
20792             "  int foo;\n"
20793             "  int bar;\n"
20794             "  int baz;\n"
20795             "};",
20796             format("union FooBarBazQux {\r\n"
20797                    "  int foo;\n"
20798                    "  int bar;\r\n"
20799                    "  int baz;\n"
20800                    "};",
20801                    style));
20802   style.UseCRLF = true;
20803   EXPECT_EQ("union FooBarBazQux {\r\n"
20804             "  int foo;\r\n"
20805             "  int bar;\r\n"
20806             "  int baz;\r\n"
20807             "};",
20808             format("union FooBarBazQux {\r\n"
20809                    "  int foo;\n"
20810                    "  int bar;\r\n"
20811                    "  int baz;\n"
20812                    "};",
20813                    style));
20814 
20815   style.DeriveLineEnding = false;
20816   style.UseCRLF = false;
20817   EXPECT_EQ("union FooBarBazQux {\n"
20818             "  int foo;\n"
20819             "  int bar;\n"
20820             "  int baz;\n"
20821             "  int qux;\n"
20822             "};",
20823             format("union FooBarBazQux {\r\n"
20824                    "  int foo;\n"
20825                    "  int bar;\r\n"
20826                    "  int baz;\n"
20827                    "  int qux;\r\n"
20828                    "};",
20829                    style));
20830   style.UseCRLF = true;
20831   EXPECT_EQ("union FooBarBazQux {\r\n"
20832             "  int foo;\r\n"
20833             "  int bar;\r\n"
20834             "  int baz;\r\n"
20835             "  int qux;\r\n"
20836             "};",
20837             format("union FooBarBazQux {\r\n"
20838                    "  int foo;\n"
20839                    "  int bar;\r\n"
20840                    "  int baz;\n"
20841                    "  int qux;\n"
20842                    "};",
20843                    style));
20844 
20845   style.DeriveLineEnding = true;
20846   style.UseCRLF = false;
20847   EXPECT_EQ("union FooBarBazQux {\r\n"
20848             "  int foo;\r\n"
20849             "  int bar;\r\n"
20850             "  int baz;\r\n"
20851             "  int qux;\r\n"
20852             "};",
20853             format("union FooBarBazQux {\r\n"
20854                    "  int foo;\n"
20855                    "  int bar;\r\n"
20856                    "  int baz;\n"
20857                    "  int qux;\r\n"
20858                    "};",
20859                    style));
20860   style.UseCRLF = true;
20861   EXPECT_EQ("union FooBarBazQux {\n"
20862             "  int foo;\n"
20863             "  int bar;\n"
20864             "  int baz;\n"
20865             "  int qux;\n"
20866             "};",
20867             format("union FooBarBazQux {\r\n"
20868                    "  int foo;\n"
20869                    "  int bar;\r\n"
20870                    "  int baz;\n"
20871                    "  int qux;\n"
20872                    "};",
20873                    style));
20874 }
20875 
20876 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20877   verifyFormat("MY_CLASS(C) {\n"
20878                "  int i;\n"
20879                "  int j;\n"
20880                "};");
20881 }
20882 
20883 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20884   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20885   TwoIndent.ContinuationIndentWidth = 2;
20886 
20887   EXPECT_EQ("int i =\n"
20888             "  longFunction(\n"
20889             "    arg);",
20890             format("int i = longFunction(arg);", TwoIndent));
20891 
20892   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20893   SixIndent.ContinuationIndentWidth = 6;
20894 
20895   EXPECT_EQ("int i =\n"
20896             "      longFunction(\n"
20897             "            arg);",
20898             format("int i = longFunction(arg);", SixIndent));
20899 }
20900 
20901 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20902   FormatStyle Style = getLLVMStyle();
20903   verifyFormat("int Foo::getter(\n"
20904                "    //\n"
20905                ") const {\n"
20906                "  return foo;\n"
20907                "}",
20908                Style);
20909   verifyFormat("void Foo::setter(\n"
20910                "    //\n"
20911                ") {\n"
20912                "  foo = 1;\n"
20913                "}",
20914                Style);
20915 }
20916 
20917 TEST_F(FormatTest, SpacesInAngles) {
20918   FormatStyle Spaces = getLLVMStyle();
20919   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20920 
20921   verifyFormat("vector< ::std::string > x1;", Spaces);
20922   verifyFormat("Foo< int, Bar > x2;", Spaces);
20923   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20924 
20925   verifyFormat("static_cast< int >(arg);", Spaces);
20926   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20927   verifyFormat("f< int, float >();", Spaces);
20928   verifyFormat("template <> g() {}", Spaces);
20929   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20930   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20931   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20932                Spaces);
20933 
20934   Spaces.Standard = FormatStyle::LS_Cpp03;
20935   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20936   verifyFormat("A< A< int > >();", Spaces);
20937 
20938   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20939   verifyFormat("A<A<int> >();", Spaces);
20940 
20941   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20942   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20943                Spaces);
20944   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20945                Spaces);
20946 
20947   verifyFormat("A<A<int> >();", Spaces);
20948   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20949   verifyFormat("A< A< int > >();", Spaces);
20950 
20951   Spaces.Standard = FormatStyle::LS_Cpp11;
20952   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20953   verifyFormat("A< A< int > >();", Spaces);
20954 
20955   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20956   verifyFormat("vector<::std::string> x4;", Spaces);
20957   verifyFormat("vector<int> x5;", Spaces);
20958   verifyFormat("Foo<int, Bar> x6;", Spaces);
20959   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20960 
20961   verifyFormat("A<A<int>>();", Spaces);
20962 
20963   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20964   verifyFormat("vector<::std::string> x4;", Spaces);
20965   verifyFormat("vector< ::std::string > x4;", Spaces);
20966   verifyFormat("vector<int> x5;", Spaces);
20967   verifyFormat("vector< int > x5;", Spaces);
20968   verifyFormat("Foo<int, Bar> x6;", Spaces);
20969   verifyFormat("Foo< int, Bar > x6;", Spaces);
20970   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20971   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20972 
20973   verifyFormat("A<A<int>>();", Spaces);
20974   verifyFormat("A< A< int > >();", Spaces);
20975   verifyFormat("A<A<int > >();", Spaces);
20976   verifyFormat("A< A< int>>();", Spaces);
20977 }
20978 
20979 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20980   FormatStyle Style = getLLVMStyle();
20981   Style.SpaceAfterTemplateKeyword = false;
20982   verifyFormat("template<int> void foo();", Style);
20983 }
20984 
20985 TEST_F(FormatTest, TripleAngleBrackets) {
20986   verifyFormat("f<<<1, 1>>>();");
20987   verifyFormat("f<<<1, 1, 1, s>>>();");
20988   verifyFormat("f<<<a, b, c, d>>>();");
20989   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
20990   verifyFormat("f<param><<<1, 1>>>();");
20991   verifyFormat("f<1><<<1, 1>>>();");
20992   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
20993   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20994                "aaaaaaaaaaa<<<\n    1, 1>>>();");
20995   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
20996                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
20997 }
20998 
20999 TEST_F(FormatTest, MergeLessLessAtEnd) {
21000   verifyFormat("<<");
21001   EXPECT_EQ("< < <", format("\\\n<<<"));
21002   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21003                "aaallvm::outs() <<");
21004   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21005                "aaaallvm::outs()\n    <<");
21006 }
21007 
21008 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21009   std::string code = "#if A\n"
21010                      "#if B\n"
21011                      "a.\n"
21012                      "#endif\n"
21013                      "    a = 1;\n"
21014                      "#else\n"
21015                      "#endif\n"
21016                      "#if C\n"
21017                      "#else\n"
21018                      "#endif\n";
21019   EXPECT_EQ(code, format(code));
21020 }
21021 
21022 TEST_F(FormatTest, HandleConflictMarkers) {
21023   // Git/SVN conflict markers.
21024   EXPECT_EQ("int a;\n"
21025             "void f() {\n"
21026             "  callme(some(parameter1,\n"
21027             "<<<<<<< text by the vcs\n"
21028             "              parameter2),\n"
21029             "||||||| text by the vcs\n"
21030             "              parameter2),\n"
21031             "         parameter3,\n"
21032             "======= text by the vcs\n"
21033             "              parameter2, parameter3),\n"
21034             ">>>>>>> text by the vcs\n"
21035             "         otherparameter);\n",
21036             format("int a;\n"
21037                    "void f() {\n"
21038                    "  callme(some(parameter1,\n"
21039                    "<<<<<<< text by the vcs\n"
21040                    "  parameter2),\n"
21041                    "||||||| text by the vcs\n"
21042                    "  parameter2),\n"
21043                    "  parameter3,\n"
21044                    "======= text by the vcs\n"
21045                    "  parameter2,\n"
21046                    "  parameter3),\n"
21047                    ">>>>>>> text by the vcs\n"
21048                    "  otherparameter);\n"));
21049 
21050   // Perforce markers.
21051   EXPECT_EQ("void f() {\n"
21052             "  function(\n"
21053             ">>>> text by the vcs\n"
21054             "      parameter,\n"
21055             "==== text by the vcs\n"
21056             "      parameter,\n"
21057             "==== text by the vcs\n"
21058             "      parameter,\n"
21059             "<<<< text by the vcs\n"
21060             "      parameter);\n",
21061             format("void f() {\n"
21062                    "  function(\n"
21063                    ">>>> text by the vcs\n"
21064                    "  parameter,\n"
21065                    "==== text by the vcs\n"
21066                    "  parameter,\n"
21067                    "==== text by the vcs\n"
21068                    "  parameter,\n"
21069                    "<<<< text by the vcs\n"
21070                    "  parameter);\n"));
21071 
21072   EXPECT_EQ("<<<<<<<\n"
21073             "|||||||\n"
21074             "=======\n"
21075             ">>>>>>>",
21076             format("<<<<<<<\n"
21077                    "|||||||\n"
21078                    "=======\n"
21079                    ">>>>>>>"));
21080 
21081   EXPECT_EQ("<<<<<<<\n"
21082             "|||||||\n"
21083             "int i;\n"
21084             "=======\n"
21085             ">>>>>>>",
21086             format("<<<<<<<\n"
21087                    "|||||||\n"
21088                    "int i;\n"
21089                    "=======\n"
21090                    ">>>>>>>"));
21091 
21092   // FIXME: Handle parsing of macros around conflict markers correctly:
21093   EXPECT_EQ("#define Macro \\\n"
21094             "<<<<<<<\n"
21095             "Something \\\n"
21096             "|||||||\n"
21097             "Else \\\n"
21098             "=======\n"
21099             "Other \\\n"
21100             ">>>>>>>\n"
21101             "    End int i;\n",
21102             format("#define Macro \\\n"
21103                    "<<<<<<<\n"
21104                    "  Something \\\n"
21105                    "|||||||\n"
21106                    "  Else \\\n"
21107                    "=======\n"
21108                    "  Other \\\n"
21109                    ">>>>>>>\n"
21110                    "  End\n"
21111                    "int i;\n"));
21112 }
21113 
21114 TEST_F(FormatTest, DisableRegions) {
21115   EXPECT_EQ("int i;\n"
21116             "// clang-format off\n"
21117             "  int j;\n"
21118             "// clang-format on\n"
21119             "int k;",
21120             format(" int  i;\n"
21121                    "   // clang-format off\n"
21122                    "  int j;\n"
21123                    " // clang-format on\n"
21124                    "   int   k;"));
21125   EXPECT_EQ("int i;\n"
21126             "/* clang-format off */\n"
21127             "  int j;\n"
21128             "/* clang-format on */\n"
21129             "int k;",
21130             format(" int  i;\n"
21131                    "   /* clang-format off */\n"
21132                    "  int j;\n"
21133                    " /* clang-format on */\n"
21134                    "   int   k;"));
21135 
21136   // Don't reflow comments within disabled regions.
21137   EXPECT_EQ("// clang-format off\n"
21138             "// long long long long long long line\n"
21139             "/* clang-format on */\n"
21140             "/* long long long\n"
21141             " * long long long\n"
21142             " * line */\n"
21143             "int i;\n"
21144             "/* clang-format off */\n"
21145             "/* long long long long long long line */\n",
21146             format("// clang-format off\n"
21147                    "// long long long long long long line\n"
21148                    "/* clang-format on */\n"
21149                    "/* long long long long long long line */\n"
21150                    "int i;\n"
21151                    "/* clang-format off */\n"
21152                    "/* long long long long long long line */\n",
21153                    getLLVMStyleWithColumns(20)));
21154 }
21155 
21156 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21157   format("? ) =");
21158   verifyNoCrash("#define a\\\n /**/}");
21159 }
21160 
21161 TEST_F(FormatTest, FormatsTableGenCode) {
21162   FormatStyle Style = getLLVMStyle();
21163   Style.Language = FormatStyle::LK_TableGen;
21164   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21165 }
21166 
21167 TEST_F(FormatTest, ArrayOfTemplates) {
21168   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21169             format("auto a = new unique_ptr<int > [ 10];"));
21170 
21171   FormatStyle Spaces = getLLVMStyle();
21172   Spaces.SpacesInSquareBrackets = true;
21173   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21174             format("auto a = new unique_ptr<int > [10];", Spaces));
21175 }
21176 
21177 TEST_F(FormatTest, ArrayAsTemplateType) {
21178   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21179             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21180 
21181   FormatStyle Spaces = getLLVMStyle();
21182   Spaces.SpacesInSquareBrackets = true;
21183   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21184             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21185 }
21186 
21187 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21188 
21189 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21190   llvm::vfs::InMemoryFileSystem FS;
21191   auto Style1 = getStyle("file", "", "Google", "", &FS);
21192   ASSERT_TRUE((bool)Style1);
21193   ASSERT_EQ(*Style1, getGoogleStyle());
21194 }
21195 
21196 TEST(FormatStyle, GetStyleOfFile) {
21197   llvm::vfs::InMemoryFileSystem FS;
21198   // Test 1: format file in the same directory.
21199   ASSERT_TRUE(
21200       FS.addFile("/a/.clang-format", 0,
21201                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21202   ASSERT_TRUE(
21203       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21204   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21205   ASSERT_TRUE((bool)Style1);
21206   ASSERT_EQ(*Style1, getLLVMStyle());
21207 
21208   // Test 2.1: fallback to default.
21209   ASSERT_TRUE(
21210       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21211   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21212   ASSERT_TRUE((bool)Style2);
21213   ASSERT_EQ(*Style2, getMozillaStyle());
21214 
21215   // Test 2.2: no format on 'none' fallback style.
21216   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21217   ASSERT_TRUE((bool)Style2);
21218   ASSERT_EQ(*Style2, getNoStyle());
21219 
21220   // Test 2.3: format if config is found with no based style while fallback is
21221   // 'none'.
21222   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21223                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21224   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21225   ASSERT_TRUE((bool)Style2);
21226   ASSERT_EQ(*Style2, getLLVMStyle());
21227 
21228   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21229   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21230   ASSERT_TRUE((bool)Style2);
21231   ASSERT_EQ(*Style2, getLLVMStyle());
21232 
21233   // Test 3: format file in parent directory.
21234   ASSERT_TRUE(
21235       FS.addFile("/c/.clang-format", 0,
21236                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21237   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21238                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21239   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21240   ASSERT_TRUE((bool)Style3);
21241   ASSERT_EQ(*Style3, getGoogleStyle());
21242 
21243   // Test 4: error on invalid fallback style
21244   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21245   ASSERT_FALSE((bool)Style4);
21246   llvm::consumeError(Style4.takeError());
21247 
21248   // Test 5: error on invalid yaml on command line
21249   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21250   ASSERT_FALSE((bool)Style5);
21251   llvm::consumeError(Style5.takeError());
21252 
21253   // Test 6: error on invalid style
21254   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21255   ASSERT_FALSE((bool)Style6);
21256   llvm::consumeError(Style6.takeError());
21257 
21258   // Test 7: found config file, error on parsing it
21259   ASSERT_TRUE(
21260       FS.addFile("/d/.clang-format", 0,
21261                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21262                                                   "InvalidKey: InvalidValue")));
21263   ASSERT_TRUE(
21264       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21265   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21266   ASSERT_FALSE((bool)Style7a);
21267   llvm::consumeError(Style7a.takeError());
21268 
21269   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21270   ASSERT_TRUE((bool)Style7b);
21271 
21272   // Test 8: inferred per-language defaults apply.
21273   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21274   ASSERT_TRUE((bool)StyleTd);
21275   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21276 
21277   // Test 9.1: overwriting a file style, when parent no file exists with no
21278   // fallback style
21279   ASSERT_TRUE(FS.addFile(
21280       "/e/sub/.clang-format", 0,
21281       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21282                                        "ColumnLimit: 20")));
21283   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21284                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21285   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21286   ASSERT_TRUE(static_cast<bool>(Style9));
21287   ASSERT_EQ(*Style9, [] {
21288     auto Style = getNoStyle();
21289     Style.ColumnLimit = 20;
21290     return Style;
21291   }());
21292 
21293   // Test 9.2: with LLVM fallback style
21294   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
21295   ASSERT_TRUE(static_cast<bool>(Style9));
21296   ASSERT_EQ(*Style9, [] {
21297     auto Style = getLLVMStyle();
21298     Style.ColumnLimit = 20;
21299     return Style;
21300   }());
21301 
21302   // Test 9.3: with a parent file
21303   ASSERT_TRUE(
21304       FS.addFile("/e/.clang-format", 0,
21305                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
21306                                                   "UseTab: Always")));
21307   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21308   ASSERT_TRUE(static_cast<bool>(Style9));
21309   ASSERT_EQ(*Style9, [] {
21310     auto Style = getGoogleStyle();
21311     Style.ColumnLimit = 20;
21312     Style.UseTab = FormatStyle::UT_Always;
21313     return Style;
21314   }());
21315 
21316   // Test 9.4: propagate more than one level
21317   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
21318                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21319   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
21320                          llvm::MemoryBuffer::getMemBuffer(
21321                              "BasedOnStyle: InheritParentConfig\n"
21322                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
21323   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
21324 
21325   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
21326     auto Style = getGoogleStyle();
21327     Style.ColumnLimit = 20;
21328     Style.UseTab = FormatStyle::UT_Always;
21329     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21330     return Style;
21331   }();
21332 
21333   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21334   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21335   ASSERT_TRUE(static_cast<bool>(Style9));
21336   ASSERT_EQ(*Style9, SubSubStyle);
21337 
21338   // Test 9.5: use InheritParentConfig as style name
21339   Style9 =
21340       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
21341   ASSERT_TRUE(static_cast<bool>(Style9));
21342   ASSERT_EQ(*Style9, SubSubStyle);
21343 
21344   // Test 9.6: use command line style with inheritance
21345   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
21346                     "none", "", &FS);
21347   ASSERT_TRUE(static_cast<bool>(Style9));
21348   ASSERT_EQ(*Style9, SubSubStyle);
21349 
21350   // Test 9.7: use command line style with inheritance and own config
21351   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
21352                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
21353                     "/e/sub/code.cpp", "none", "", &FS);
21354   ASSERT_TRUE(static_cast<bool>(Style9));
21355   ASSERT_EQ(*Style9, SubSubStyle);
21356 
21357   // Test 9.8: use inheritance from a file without BasedOnStyle
21358   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
21359                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
21360   ASSERT_TRUE(
21361       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21362                  llvm::MemoryBuffer::getMemBuffer(
21363                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21364   // Make sure we do not use the fallback style
21365   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21366   ASSERT_TRUE(static_cast<bool>(Style9));
21367   ASSERT_EQ(*Style9, [] {
21368     auto Style = getLLVMStyle();
21369     Style.ColumnLimit = 123;
21370     return Style;
21371   }());
21372 
21373   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21374   ASSERT_TRUE(static_cast<bool>(Style9));
21375   ASSERT_EQ(*Style9, [] {
21376     auto Style = getLLVMStyle();
21377     Style.ColumnLimit = 123;
21378     Style.IndentWidth = 7;
21379     return Style;
21380   }());
21381 }
21382 
21383 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21384   // Column limit is 20.
21385   std::string Code = "Type *a =\n"
21386                      "    new Type();\n"
21387                      "g(iiiii, 0, jjjjj,\n"
21388                      "  0, kkkkk, 0, mm);\n"
21389                      "int  bad     = format   ;";
21390   std::string Expected = "auto a = new Type();\n"
21391                          "g(iiiii, nullptr,\n"
21392                          "  jjjjj, nullptr,\n"
21393                          "  kkkkk, nullptr,\n"
21394                          "  mm);\n"
21395                          "int  bad     = format   ;";
21396   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21397   tooling::Replacements Replaces = toReplacements(
21398       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21399                             "auto "),
21400        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21401                             "nullptr"),
21402        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21403                             "nullptr"),
21404        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21405                             "nullptr")});
21406 
21407   format::FormatStyle Style = format::getLLVMStyle();
21408   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21409   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21410   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21411       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21412   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21413   EXPECT_TRUE(static_cast<bool>(Result));
21414   EXPECT_EQ(Expected, *Result);
21415 }
21416 
21417 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21418   std::string Code = "#include \"a.h\"\n"
21419                      "#include \"c.h\"\n"
21420                      "\n"
21421                      "int main() {\n"
21422                      "  return 0;\n"
21423                      "}";
21424   std::string Expected = "#include \"a.h\"\n"
21425                          "#include \"b.h\"\n"
21426                          "#include \"c.h\"\n"
21427                          "\n"
21428                          "int main() {\n"
21429                          "  return 0;\n"
21430                          "}";
21431   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21432   tooling::Replacements Replaces = toReplacements(
21433       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21434                             "#include \"b.h\"\n")});
21435 
21436   format::FormatStyle Style = format::getLLVMStyle();
21437   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21438   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21439   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21440       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21441   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21442   EXPECT_TRUE(static_cast<bool>(Result));
21443   EXPECT_EQ(Expected, *Result);
21444 }
21445 
21446 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21447   EXPECT_EQ("using std::cin;\n"
21448             "using std::cout;",
21449             format("using std::cout;\n"
21450                    "using std::cin;",
21451                    getGoogleStyle()));
21452 }
21453 
21454 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21455   format::FormatStyle Style = format::getLLVMStyle();
21456   Style.Standard = FormatStyle::LS_Cpp03;
21457   // cpp03 recognize this string as identifier u8 and literal character 'a'
21458   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21459 }
21460 
21461 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21462   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21463   // all modes, including C++11, C++14 and C++17
21464   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21465 }
21466 
21467 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21468   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21469   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21470 }
21471 
21472 TEST_F(FormatTest, StructuredBindings) {
21473   // Structured bindings is a C++17 feature.
21474   // all modes, including C++11, C++14 and C++17
21475   verifyFormat("auto [a, b] = f();");
21476   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21477   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21478   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21479   EXPECT_EQ("auto const volatile [a, b] = f();",
21480             format("auto  const   volatile[a, b] = f();"));
21481   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21482   EXPECT_EQ("auto &[a, b, c] = f();",
21483             format("auto   &[  a  ,  b,c   ] = f();"));
21484   EXPECT_EQ("auto &&[a, b, c] = f();",
21485             format("auto   &&[  a  ,  b,c   ] = f();"));
21486   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21487   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21488             format("auto  const  volatile  &&[a, b] = f();"));
21489   EXPECT_EQ("auto const &&[a, b] = f();",
21490             format("auto  const   &&  [a, b] = f();"));
21491   EXPECT_EQ("const auto &[a, b] = f();",
21492             format("const  auto  &  [a, b] = f();"));
21493   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21494             format("const  auto   volatile  &&[a, b] = f();"));
21495   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21496             format("volatile  const  auto   &&[a, b] = f();"));
21497   EXPECT_EQ("const auto &&[a, b] = f();",
21498             format("const  auto  &&  [a, b] = f();"));
21499 
21500   // Make sure we don't mistake structured bindings for lambdas.
21501   FormatStyle PointerMiddle = getLLVMStyle();
21502   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21503   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21504   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21505   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21506   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21507   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21508   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21509   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21510   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21511   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21512   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21513   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21514   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21515 
21516   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21517             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21518   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21519             format("for (const auto   &   [a, b] : some_range) {\n}"));
21520   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21521             format("for (const auto[a, b] : some_range) {\n}"));
21522   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21523   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21524   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21525   EXPECT_EQ("auto const &[x, y](expr);",
21526             format("auto  const  &  [x,y]  (expr);"));
21527   EXPECT_EQ("auto const &&[x, y](expr);",
21528             format("auto  const  &&  [x,y]  (expr);"));
21529   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21530   EXPECT_EQ("auto const &[x, y]{expr};",
21531             format("auto  const  &  [x,y]  {expr};"));
21532   EXPECT_EQ("auto const &&[x, y]{expr};",
21533             format("auto  const  &&  [x,y]  {expr};"));
21534 
21535   format::FormatStyle Spaces = format::getLLVMStyle();
21536   Spaces.SpacesInSquareBrackets = true;
21537   verifyFormat("auto [ a, b ] = f();", Spaces);
21538   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21539   verifyFormat("auto &[ a, b ] = f();", Spaces);
21540   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21541   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21542 }
21543 
21544 TEST_F(FormatTest, FileAndCode) {
21545   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21546   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21547   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21548   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21549   EXPECT_EQ(FormatStyle::LK_ObjC,
21550             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21551   EXPECT_EQ(
21552       FormatStyle::LK_ObjC,
21553       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21554   EXPECT_EQ(FormatStyle::LK_ObjC,
21555             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21556   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21557   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21558   EXPECT_EQ(FormatStyle::LK_ObjC,
21559             guessLanguage("foo", "@interface Foo\n@end\n"));
21560   EXPECT_EQ(FormatStyle::LK_ObjC,
21561             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21562   EXPECT_EQ(
21563       FormatStyle::LK_ObjC,
21564       guessLanguage("foo.h",
21565                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21566   EXPECT_EQ(
21567       FormatStyle::LK_Cpp,
21568       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21569 }
21570 
21571 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21572   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21573   EXPECT_EQ(FormatStyle::LK_ObjC,
21574             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21575   EXPECT_EQ(FormatStyle::LK_Cpp,
21576             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21577   EXPECT_EQ(
21578       FormatStyle::LK_Cpp,
21579       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21580   EXPECT_EQ(FormatStyle::LK_ObjC,
21581             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21582   EXPECT_EQ(FormatStyle::LK_Cpp,
21583             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21584   EXPECT_EQ(FormatStyle::LK_ObjC,
21585             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21586   EXPECT_EQ(FormatStyle::LK_Cpp,
21587             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21588   EXPECT_EQ(FormatStyle::LK_Cpp,
21589             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21590   EXPECT_EQ(FormatStyle::LK_ObjC,
21591             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21592   EXPECT_EQ(FormatStyle::LK_Cpp,
21593             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21594   EXPECT_EQ(
21595       FormatStyle::LK_Cpp,
21596       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21597   EXPECT_EQ(
21598       FormatStyle::LK_Cpp,
21599       guessLanguage("foo.h",
21600                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21601   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21602 }
21603 
21604 TEST_F(FormatTest, GuessLanguageWithCaret) {
21605   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21606   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21607   EXPECT_EQ(FormatStyle::LK_ObjC,
21608             guessLanguage("foo.h", "int(^)(char, float);"));
21609   EXPECT_EQ(FormatStyle::LK_ObjC,
21610             guessLanguage("foo.h", "int(^foo)(char, float);"));
21611   EXPECT_EQ(FormatStyle::LK_ObjC,
21612             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21613   EXPECT_EQ(FormatStyle::LK_ObjC,
21614             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21615   EXPECT_EQ(
21616       FormatStyle::LK_ObjC,
21617       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21618 }
21619 
21620 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21621   EXPECT_EQ(FormatStyle::LK_Cpp,
21622             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21623   EXPECT_EQ(FormatStyle::LK_Cpp,
21624             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21625   EXPECT_EQ(FormatStyle::LK_Cpp,
21626             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21627 }
21628 
21629 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21630   // ASM symbolic names are identifiers that must be surrounded by [] without
21631   // space in between:
21632   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21633 
21634   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21635   verifyFormat(R"(//
21636 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21637 )");
21638 
21639   // A list of several ASM symbolic names.
21640   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21641 
21642   // ASM symbolic names in inline ASM with inputs and outputs.
21643   verifyFormat(R"(//
21644 asm("cmoveq %1, %2, %[result]"
21645     : [result] "=r"(result)
21646     : "r"(test), "r"(new), "[result]"(old));
21647 )");
21648 
21649   // ASM symbolic names in inline ASM with no outputs.
21650   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21651 }
21652 
21653 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21654   EXPECT_EQ(FormatStyle::LK_Cpp,
21655             guessLanguage("foo.h", "void f() {\n"
21656                                    "  asm (\"mov %[e], %[d]\"\n"
21657                                    "     : [d] \"=rm\" (d)\n"
21658                                    "       [e] \"rm\" (*e));\n"
21659                                    "}"));
21660   EXPECT_EQ(FormatStyle::LK_Cpp,
21661             guessLanguage("foo.h", "void f() {\n"
21662                                    "  _asm (\"mov %[e], %[d]\"\n"
21663                                    "     : [d] \"=rm\" (d)\n"
21664                                    "       [e] \"rm\" (*e));\n"
21665                                    "}"));
21666   EXPECT_EQ(FormatStyle::LK_Cpp,
21667             guessLanguage("foo.h", "void f() {\n"
21668                                    "  __asm (\"mov %[e], %[d]\"\n"
21669                                    "     : [d] \"=rm\" (d)\n"
21670                                    "       [e] \"rm\" (*e));\n"
21671                                    "}"));
21672   EXPECT_EQ(FormatStyle::LK_Cpp,
21673             guessLanguage("foo.h", "void f() {\n"
21674                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21675                                    "     : [d] \"=rm\" (d)\n"
21676                                    "       [e] \"rm\" (*e));\n"
21677                                    "}"));
21678   EXPECT_EQ(FormatStyle::LK_Cpp,
21679             guessLanguage("foo.h", "void f() {\n"
21680                                    "  asm (\"mov %[e], %[d]\"\n"
21681                                    "     : [d] \"=rm\" (d),\n"
21682                                    "       [e] \"rm\" (*e));\n"
21683                                    "}"));
21684   EXPECT_EQ(FormatStyle::LK_Cpp,
21685             guessLanguage("foo.h", "void f() {\n"
21686                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21687                                    "     : [d] \"=rm\" (d)\n"
21688                                    "       [e] \"rm\" (*e));\n"
21689                                    "}"));
21690 }
21691 
21692 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21693   EXPECT_EQ(FormatStyle::LK_Cpp,
21694             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21695   EXPECT_EQ(FormatStyle::LK_ObjC,
21696             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21697   EXPECT_EQ(
21698       FormatStyle::LK_Cpp,
21699       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21700   EXPECT_EQ(
21701       FormatStyle::LK_ObjC,
21702       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21703 }
21704 
21705 TEST_F(FormatTest, TypenameMacros) {
21706   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21707 
21708   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21709   FormatStyle Google = getGoogleStyleWithColumns(0);
21710   Google.TypenameMacros = TypenameMacros;
21711   verifyFormat("struct foo {\n"
21712                "  int bar;\n"
21713                "  TAILQ_ENTRY(a) bleh;\n"
21714                "};",
21715                Google);
21716 
21717   FormatStyle Macros = getLLVMStyle();
21718   Macros.TypenameMacros = TypenameMacros;
21719 
21720   verifyFormat("STACK_OF(int) a;", Macros);
21721   verifyFormat("STACK_OF(int) *a;", Macros);
21722   verifyFormat("STACK_OF(int const *) *a;", Macros);
21723   verifyFormat("STACK_OF(int *const) *a;", Macros);
21724   verifyFormat("STACK_OF(int, string) a;", Macros);
21725   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21726   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21727   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21728   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21729   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21730   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21731 
21732   Macros.PointerAlignment = FormatStyle::PAS_Left;
21733   verifyFormat("STACK_OF(int)* a;", Macros);
21734   verifyFormat("STACK_OF(int*)* a;", Macros);
21735   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21736   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21737   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21738 }
21739 
21740 TEST_F(FormatTest, AtomicQualifier) {
21741   // Check that we treate _Atomic as a type and not a function call
21742   FormatStyle Google = getGoogleStyleWithColumns(0);
21743   verifyFormat("struct foo {\n"
21744                "  int a1;\n"
21745                "  _Atomic(a) a2;\n"
21746                "  _Atomic(_Atomic(int) *const) a3;\n"
21747                "};",
21748                Google);
21749   verifyFormat("_Atomic(uint64_t) a;");
21750   verifyFormat("_Atomic(uint64_t) *a;");
21751   verifyFormat("_Atomic(uint64_t const *) *a;");
21752   verifyFormat("_Atomic(uint64_t *const) *a;");
21753   verifyFormat("_Atomic(const uint64_t *) *a;");
21754   verifyFormat("_Atomic(uint64_t) a;");
21755   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21756   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21757   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21758   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21759 
21760   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21761   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21762   FormatStyle Style = getLLVMStyle();
21763   Style.PointerAlignment = FormatStyle::PAS_Left;
21764   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21765   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21766   verifyFormat("_Atomic(int)* a;", Style);
21767   verifyFormat("_Atomic(int*)* a;", Style);
21768   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21769 
21770   Style.SpacesInCStyleCastParentheses = true;
21771   Style.SpacesInParentheses = false;
21772   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21773   Style.SpacesInCStyleCastParentheses = false;
21774   Style.SpacesInParentheses = true;
21775   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21776   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21777 }
21778 
21779 TEST_F(FormatTest, AmbersandInLamda) {
21780   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21781   FormatStyle AlignStyle = getLLVMStyle();
21782   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21783   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21784   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21785   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21786 }
21787 
21788 TEST_F(FormatTest, SpacesInConditionalStatement) {
21789   FormatStyle Spaces = getLLVMStyle();
21790   Spaces.IfMacros.clear();
21791   Spaces.IfMacros.push_back("MYIF");
21792   Spaces.SpacesInConditionalStatement = true;
21793   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21794   verifyFormat("if ( !a )\n  return;", Spaces);
21795   verifyFormat("if ( a )\n  return;", Spaces);
21796   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21797   verifyFormat("MYIF ( a )\n  return;", Spaces);
21798   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21799   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21800   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21801   verifyFormat("while ( a )\n  return;", Spaces);
21802   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21803   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21804   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21805   // Check that space on the left of "::" is inserted as expected at beginning
21806   // of condition.
21807   verifyFormat("while ( ::func() )\n  return;", Spaces);
21808 
21809   // Check impact of ControlStatementsExceptControlMacros is honored.
21810   Spaces.SpaceBeforeParens =
21811       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21812   verifyFormat("MYIF( a )\n  return;", Spaces);
21813   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21814   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21815 }
21816 
21817 TEST_F(FormatTest, AlternativeOperators) {
21818   // Test case for ensuring alternate operators are not
21819   // combined with their right most neighbour.
21820   verifyFormat("int a and b;");
21821   verifyFormat("int a and_eq b;");
21822   verifyFormat("int a bitand b;");
21823   verifyFormat("int a bitor b;");
21824   verifyFormat("int a compl b;");
21825   verifyFormat("int a not b;");
21826   verifyFormat("int a not_eq b;");
21827   verifyFormat("int a or b;");
21828   verifyFormat("int a xor b;");
21829   verifyFormat("int a xor_eq b;");
21830   verifyFormat("return this not_eq bitand other;");
21831   verifyFormat("bool operator not_eq(const X bitand other)");
21832 
21833   verifyFormat("int a and 5;");
21834   verifyFormat("int a and_eq 5;");
21835   verifyFormat("int a bitand 5;");
21836   verifyFormat("int a bitor 5;");
21837   verifyFormat("int a compl 5;");
21838   verifyFormat("int a not 5;");
21839   verifyFormat("int a not_eq 5;");
21840   verifyFormat("int a or 5;");
21841   verifyFormat("int a xor 5;");
21842   verifyFormat("int a xor_eq 5;");
21843 
21844   verifyFormat("int a compl(5);");
21845   verifyFormat("int a not(5);");
21846 
21847   /* FIXME handle alternate tokens
21848    * https://en.cppreference.com/w/cpp/language/operator_alternative
21849   // alternative tokens
21850   verifyFormat("compl foo();");     //  ~foo();
21851   verifyFormat("foo() <%%>;");      // foo();
21852   verifyFormat("void foo() <%%>;"); // void foo(){}
21853   verifyFormat("int a <:1:>;");     // int a[1];[
21854   verifyFormat("%:define ABC abc"); // #define ABC abc
21855   verifyFormat("%:%:");             // ##
21856   */
21857 }
21858 
21859 TEST_F(FormatTest, STLWhileNotDefineChed) {
21860   verifyFormat("#if defined(while)\n"
21861                "#define while EMIT WARNING C4005\n"
21862                "#endif // while");
21863 }
21864 
21865 TEST_F(FormatTest, OperatorSpacing) {
21866   FormatStyle Style = getLLVMStyle();
21867   Style.PointerAlignment = FormatStyle::PAS_Right;
21868   verifyFormat("Foo::operator*();", Style);
21869   verifyFormat("Foo::operator void *();", Style);
21870   verifyFormat("Foo::operator void **();", Style);
21871   verifyFormat("Foo::operator void *&();", Style);
21872   verifyFormat("Foo::operator void *&&();", Style);
21873   verifyFormat("Foo::operator void const *();", Style);
21874   verifyFormat("Foo::operator void const **();", Style);
21875   verifyFormat("Foo::operator void const *&();", Style);
21876   verifyFormat("Foo::operator void const *&&();", Style);
21877   verifyFormat("Foo::operator()(void *);", Style);
21878   verifyFormat("Foo::operator*(void *);", Style);
21879   verifyFormat("Foo::operator*();", Style);
21880   verifyFormat("Foo::operator**();", Style);
21881   verifyFormat("Foo::operator&();", Style);
21882   verifyFormat("Foo::operator<int> *();", Style);
21883   verifyFormat("Foo::operator<Foo> *();", Style);
21884   verifyFormat("Foo::operator<int> **();", Style);
21885   verifyFormat("Foo::operator<Foo> **();", Style);
21886   verifyFormat("Foo::operator<int> &();", Style);
21887   verifyFormat("Foo::operator<Foo> &();", Style);
21888   verifyFormat("Foo::operator<int> &&();", Style);
21889   verifyFormat("Foo::operator<Foo> &&();", Style);
21890   verifyFormat("Foo::operator<int> *&();", Style);
21891   verifyFormat("Foo::operator<Foo> *&();", Style);
21892   verifyFormat("Foo::operator<int> *&&();", Style);
21893   verifyFormat("Foo::operator<Foo> *&&();", Style);
21894   verifyFormat("operator*(int (*)(), class Foo);", Style);
21895 
21896   verifyFormat("Foo::operator&();", Style);
21897   verifyFormat("Foo::operator void &();", Style);
21898   verifyFormat("Foo::operator void const &();", Style);
21899   verifyFormat("Foo::operator()(void &);", Style);
21900   verifyFormat("Foo::operator&(void &);", Style);
21901   verifyFormat("Foo::operator&();", Style);
21902   verifyFormat("operator&(int (&)(), class Foo);", Style);
21903   verifyFormat("operator&&(int (&)(), class Foo);", Style);
21904 
21905   verifyFormat("Foo::operator&&();", Style);
21906   verifyFormat("Foo::operator**();", Style);
21907   verifyFormat("Foo::operator void &&();", Style);
21908   verifyFormat("Foo::operator void const &&();", Style);
21909   verifyFormat("Foo::operator()(void &&);", Style);
21910   verifyFormat("Foo::operator&&(void &&);", Style);
21911   verifyFormat("Foo::operator&&();", Style);
21912   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21913   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21914   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21915                Style);
21916   verifyFormat("operator void **()", Style);
21917   verifyFormat("operator const FooRight<Object> &()", Style);
21918   verifyFormat("operator const FooRight<Object> *()", Style);
21919   verifyFormat("operator const FooRight<Object> **()", Style);
21920   verifyFormat("operator const FooRight<Object> *&()", Style);
21921   verifyFormat("operator const FooRight<Object> *&&()", Style);
21922 
21923   Style.PointerAlignment = FormatStyle::PAS_Left;
21924   verifyFormat("Foo::operator*();", Style);
21925   verifyFormat("Foo::operator**();", Style);
21926   verifyFormat("Foo::operator void*();", Style);
21927   verifyFormat("Foo::operator void**();", Style);
21928   verifyFormat("Foo::operator void*&();", Style);
21929   verifyFormat("Foo::operator void*&&();", Style);
21930   verifyFormat("Foo::operator void const*();", Style);
21931   verifyFormat("Foo::operator void const**();", Style);
21932   verifyFormat("Foo::operator void const*&();", Style);
21933   verifyFormat("Foo::operator void const*&&();", Style);
21934   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21935   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21936   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21937   verifyFormat("Foo::operator()(void*);", Style);
21938   verifyFormat("Foo::operator*(void*);", Style);
21939   verifyFormat("Foo::operator*();", Style);
21940   verifyFormat("Foo::operator<int>*();", Style);
21941   verifyFormat("Foo::operator<Foo>*();", Style);
21942   verifyFormat("Foo::operator<int>**();", Style);
21943   verifyFormat("Foo::operator<Foo>**();", Style);
21944   verifyFormat("Foo::operator<Foo>*&();", Style);
21945   verifyFormat("Foo::operator<int>&();", Style);
21946   verifyFormat("Foo::operator<Foo>&();", Style);
21947   verifyFormat("Foo::operator<int>&&();", Style);
21948   verifyFormat("Foo::operator<Foo>&&();", Style);
21949   verifyFormat("Foo::operator<int>*&();", Style);
21950   verifyFormat("Foo::operator<Foo>*&();", Style);
21951   verifyFormat("operator*(int (*)(), class Foo);", Style);
21952 
21953   verifyFormat("Foo::operator&();", Style);
21954   verifyFormat("Foo::operator void&();", Style);
21955   verifyFormat("Foo::operator void const&();", Style);
21956   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21957   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21958   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21959   verifyFormat("Foo::operator()(void&);", Style);
21960   verifyFormat("Foo::operator&(void&);", Style);
21961   verifyFormat("Foo::operator&();", Style);
21962   verifyFormat("operator&(int (&)(), class Foo);", Style);
21963   verifyFormat("operator&(int (&&)(), class Foo);", Style);
21964   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21965 
21966   verifyFormat("Foo::operator&&();", Style);
21967   verifyFormat("Foo::operator void&&();", Style);
21968   verifyFormat("Foo::operator void const&&();", Style);
21969   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21970   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21971   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21972   verifyFormat("Foo::operator()(void&&);", Style);
21973   verifyFormat("Foo::operator&&(void&&);", Style);
21974   verifyFormat("Foo::operator&&();", Style);
21975   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21976   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21977   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
21978                Style);
21979   verifyFormat("operator void**()", Style);
21980   verifyFormat("operator const FooLeft<Object>&()", Style);
21981   verifyFormat("operator const FooLeft<Object>*()", Style);
21982   verifyFormat("operator const FooLeft<Object>**()", Style);
21983   verifyFormat("operator const FooLeft<Object>*&()", Style);
21984   verifyFormat("operator const FooLeft<Object>*&&()", Style);
21985 
21986   // PR45107
21987   verifyFormat("operator Vector<String>&();", Style);
21988   verifyFormat("operator const Vector<String>&();", Style);
21989   verifyFormat("operator foo::Bar*();", Style);
21990   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
21991   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
21992                Style);
21993 
21994   Style.PointerAlignment = FormatStyle::PAS_Middle;
21995   verifyFormat("Foo::operator*();", Style);
21996   verifyFormat("Foo::operator void *();", Style);
21997   verifyFormat("Foo::operator()(void *);", Style);
21998   verifyFormat("Foo::operator*(void *);", Style);
21999   verifyFormat("Foo::operator*();", Style);
22000   verifyFormat("operator*(int (*)(), class Foo);", Style);
22001 
22002   verifyFormat("Foo::operator&();", Style);
22003   verifyFormat("Foo::operator void &();", Style);
22004   verifyFormat("Foo::operator void const &();", Style);
22005   verifyFormat("Foo::operator()(void &);", Style);
22006   verifyFormat("Foo::operator&(void &);", Style);
22007   verifyFormat("Foo::operator&();", Style);
22008   verifyFormat("operator&(int (&)(), class Foo);", Style);
22009 
22010   verifyFormat("Foo::operator&&();", Style);
22011   verifyFormat("Foo::operator void &&();", Style);
22012   verifyFormat("Foo::operator void const &&();", Style);
22013   verifyFormat("Foo::operator()(void &&);", Style);
22014   verifyFormat("Foo::operator&&(void &&);", Style);
22015   verifyFormat("Foo::operator&&();", Style);
22016   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22017 }
22018 
22019 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22020   FormatStyle Style = getLLVMStyle();
22021   // PR46157
22022   verifyFormat("foo(operator+, -42);", Style);
22023   verifyFormat("foo(operator++, -42);", Style);
22024   verifyFormat("foo(operator--, -42);", Style);
22025   verifyFormat("foo(-42, operator--);", Style);
22026   verifyFormat("foo(-42, operator, );", Style);
22027   verifyFormat("foo(operator, , -42);", Style);
22028 }
22029 
22030 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22031   FormatStyle Style = getLLVMStyle();
22032   Style.WhitespaceSensitiveMacros.push_back("FOO");
22033 
22034   // Don't use the helpers here, since 'mess up' will change the whitespace
22035   // and these are all whitespace sensitive by definition
22036   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22037             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22038   EXPECT_EQ(
22039       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22040       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22041   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22042             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22043   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22044             "       Still=Intentional);",
22045             format("FOO(String-ized&Messy+But,: :\n"
22046                    "       Still=Intentional);",
22047                    Style));
22048   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22049   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22050             "       Still=Intentional);",
22051             format("FOO(String-ized=&Messy+But,: :\n"
22052                    "       Still=Intentional);",
22053                    Style));
22054 
22055   Style.ColumnLimit = 21;
22056   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22057             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22058 }
22059 
22060 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22061   // These tests are not in NamespaceFixer because that doesn't
22062   // test its interaction with line wrapping
22063   FormatStyle Style = getLLVMStyle();
22064   Style.ColumnLimit = 80;
22065   verifyFormat("namespace {\n"
22066                "int i;\n"
22067                "int j;\n"
22068                "} // namespace",
22069                Style);
22070 
22071   verifyFormat("namespace AAA {\n"
22072                "int i;\n"
22073                "int j;\n"
22074                "} // namespace AAA",
22075                Style);
22076 
22077   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22078             "int i;\n"
22079             "int j;\n"
22080             "} // namespace Averyveryveryverylongnamespace",
22081             format("namespace Averyveryveryverylongnamespace {\n"
22082                    "int i;\n"
22083                    "int j;\n"
22084                    "}",
22085                    Style));
22086 
22087   EXPECT_EQ(
22088       "namespace "
22089       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22090       "    went::mad::now {\n"
22091       "int i;\n"
22092       "int j;\n"
22093       "} // namespace\n"
22094       "  // "
22095       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22096       "went::mad::now",
22097       format("namespace "
22098              "would::it::save::you::a::lot::of::time::if_::i::"
22099              "just::gave::up::and_::went::mad::now {\n"
22100              "int i;\n"
22101              "int j;\n"
22102              "}",
22103              Style));
22104 
22105   // This used to duplicate the comment again and again on subsequent runs
22106   EXPECT_EQ(
22107       "namespace "
22108       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22109       "    went::mad::now {\n"
22110       "int i;\n"
22111       "int j;\n"
22112       "} // namespace\n"
22113       "  // "
22114       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22115       "went::mad::now",
22116       format("namespace "
22117              "would::it::save::you::a::lot::of::time::if_::i::"
22118              "just::gave::up::and_::went::mad::now {\n"
22119              "int i;\n"
22120              "int j;\n"
22121              "} // namespace\n"
22122              "  // "
22123              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22124              "and_::went::mad::now",
22125              Style));
22126 }
22127 
22128 TEST_F(FormatTest, LikelyUnlikely) {
22129   FormatStyle Style = getLLVMStyle();
22130 
22131   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22132                "  return 29;\n"
22133                "}",
22134                Style);
22135 
22136   verifyFormat("if (argc > 5) [[likely]] {\n"
22137                "  return 29;\n"
22138                "}",
22139                Style);
22140 
22141   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22142                "  return 29;\n"
22143                "} else [[likely]] {\n"
22144                "  return 42;\n"
22145                "}\n",
22146                Style);
22147 
22148   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22149                "  return 29;\n"
22150                "} else if (argc > 10) [[likely]] {\n"
22151                "  return 99;\n"
22152                "} else {\n"
22153                "  return 42;\n"
22154                "}\n",
22155                Style);
22156 
22157   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22158                "  return 29;\n"
22159                "}",
22160                Style);
22161 }
22162 
22163 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22164   verifyFormat("Constructor()\n"
22165                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22166                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22167                "aaaaaaaaaaaaaaaaaat))");
22168   verifyFormat("Constructor()\n"
22169                "    : aaaaaaaaaaaaa(aaaaaa), "
22170                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22171 
22172   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22173   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22174   verifyFormat("Constructor()\n"
22175                "    : aaaaaa(aaaaaa),\n"
22176                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22177                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22178                StyleWithWhitespacePenalty);
22179   verifyFormat("Constructor()\n"
22180                "    : aaaaaaaaaaaaa(aaaaaa), "
22181                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22182                StyleWithWhitespacePenalty);
22183 }
22184 
22185 TEST_F(FormatTest, LLVMDefaultStyle) {
22186   FormatStyle Style = getLLVMStyle();
22187   verifyFormat("extern \"C\" {\n"
22188                "int foo();\n"
22189                "}",
22190                Style);
22191 }
22192 TEST_F(FormatTest, GNUDefaultStyle) {
22193   FormatStyle Style = getGNUStyle();
22194   verifyFormat("extern \"C\"\n"
22195                "{\n"
22196                "  int foo ();\n"
22197                "}",
22198                Style);
22199 }
22200 TEST_F(FormatTest, MozillaDefaultStyle) {
22201   FormatStyle Style = getMozillaStyle();
22202   verifyFormat("extern \"C\"\n"
22203                "{\n"
22204                "  int foo();\n"
22205                "}",
22206                Style);
22207 }
22208 TEST_F(FormatTest, GoogleDefaultStyle) {
22209   FormatStyle Style = getGoogleStyle();
22210   verifyFormat("extern \"C\" {\n"
22211                "int foo();\n"
22212                "}",
22213                Style);
22214 }
22215 TEST_F(FormatTest, ChromiumDefaultStyle) {
22216   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
22217   verifyFormat("extern \"C\" {\n"
22218                "int foo();\n"
22219                "}",
22220                Style);
22221 }
22222 TEST_F(FormatTest, MicrosoftDefaultStyle) {
22223   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
22224   verifyFormat("extern \"C\"\n"
22225                "{\n"
22226                "    int foo();\n"
22227                "}",
22228                Style);
22229 }
22230 TEST_F(FormatTest, WebKitDefaultStyle) {
22231   FormatStyle Style = getWebKitStyle();
22232   verifyFormat("extern \"C\" {\n"
22233                "int foo();\n"
22234                "}",
22235                Style);
22236 }
22237 
22238 TEST_F(FormatTest, ConceptsAndRequires) {
22239   FormatStyle Style = getLLVMStyle();
22240   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22241 
22242   verifyFormat("template <typename T>\n"
22243                "concept Hashable = requires(T a) {\n"
22244                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22245                "};",
22246                Style);
22247   verifyFormat("template <typename T>\n"
22248                "concept EqualityComparable = requires(T a, T b) {\n"
22249                "  { a == b } -> bool;\n"
22250                "};",
22251                Style);
22252   verifyFormat("template <typename T>\n"
22253                "concept EqualityComparable = requires(T a, T b) {\n"
22254                "  { a == b } -> bool;\n"
22255                "  { a != b } -> bool;\n"
22256                "};",
22257                Style);
22258   verifyFormat("template <typename T>\n"
22259                "concept EqualityComparable = requires(T a, T b) {\n"
22260                "  { a == b } -> bool;\n"
22261                "  { a != b } -> bool;\n"
22262                "};",
22263                Style);
22264 
22265   verifyFormat("template <typename It>\n"
22266                "requires Iterator<It>\n"
22267                "void sort(It begin, It end) {\n"
22268                "  //....\n"
22269                "}",
22270                Style);
22271 
22272   verifyFormat("template <typename T>\n"
22273                "concept Large = sizeof(T) > 10;",
22274                Style);
22275 
22276   verifyFormat("template <typename T, typename U>\n"
22277                "concept FooableWith = requires(T t, U u) {\n"
22278                "  typename T::foo_type;\n"
22279                "  { t.foo(u) } -> typename T::foo_type;\n"
22280                "  t++;\n"
22281                "};\n"
22282                "void doFoo(FooableWith<int> auto t) {\n"
22283                "  t.foo(3);\n"
22284                "}",
22285                Style);
22286   verifyFormat("template <typename T>\n"
22287                "concept Context = sizeof(T) == 1;",
22288                Style);
22289   verifyFormat("template <typename T>\n"
22290                "concept Context = is_specialization_of_v<context, T>;",
22291                Style);
22292   verifyFormat("template <typename T>\n"
22293                "concept Node = std::is_object_v<T>;",
22294                Style);
22295   verifyFormat("template <typename T>\n"
22296                "concept Tree = true;",
22297                Style);
22298 
22299   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
22300                "  //...\n"
22301                "}",
22302                Style);
22303 
22304   verifyFormat(
22305       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
22306       "  //...\n"
22307       "}",
22308       Style);
22309 
22310   verifyFormat(
22311       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
22312       "  //...\n"
22313       "}",
22314       Style);
22315 
22316   verifyFormat("template <typename T>\n"
22317                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
22318                "Concept2<I> {\n"
22319                "  //...\n"
22320                "}",
22321                Style);
22322 
22323   verifyFormat("template <typename T>\n"
22324                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
22325                "Concept2<I> {\n"
22326                "  //...\n"
22327                "}",
22328                Style);
22329 
22330   verifyFormat(
22331       "template <typename T>\n"
22332       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
22333       "  //...\n"
22334       "}",
22335       Style);
22336 
22337   verifyFormat(
22338       "template <typename T>\n"
22339       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
22340       "  //...\n"
22341       "}",
22342       Style);
22343 
22344   verifyFormat("template <typename It>\n"
22345                "requires Foo<It>() && Bar<It> {\n"
22346                "  //....\n"
22347                "}",
22348                Style);
22349 
22350   verifyFormat("template <typename It>\n"
22351                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
22352                "  //....\n"
22353                "}",
22354                Style);
22355 
22356   verifyFormat("template <typename It>\n"
22357                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
22358                "  //....\n"
22359                "}",
22360                Style);
22361 
22362   verifyFormat(
22363       "template <typename It>\n"
22364       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22365       "  //....\n"
22366       "}",
22367       Style);
22368 
22369   Style.IndentRequires = true;
22370   verifyFormat("template <typename It>\n"
22371                "  requires Iterator<It>\n"
22372                "void sort(It begin, It end) {\n"
22373                "  //....\n"
22374                "}",
22375                Style);
22376   verifyFormat("template <std::size index_>\n"
22377                "  requires(index_ < sizeof...(Children_))\n"
22378                "Tree auto &child() {\n"
22379                "  // ...\n"
22380                "}",
22381                Style);
22382 
22383   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22384   verifyFormat("template <typename T>\n"
22385                "concept Hashable = requires (T a) {\n"
22386                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22387                "};",
22388                Style);
22389 
22390   verifyFormat("template <class T = void>\n"
22391                "  requires EqualityComparable<T> || Same<T, void>\n"
22392                "struct equal_to;",
22393                Style);
22394 
22395   verifyFormat("template <class T>\n"
22396                "  requires requires {\n"
22397                "    T{};\n"
22398                "    T (int);\n"
22399                "  }\n",
22400                Style);
22401 
22402   Style.ColumnLimit = 78;
22403   verifyFormat("template <typename T>\n"
22404                "concept Context = Traits<typename T::traits_type> and\n"
22405                "    Interface<typename T::interface_type> and\n"
22406                "    Request<typename T::request_type> and\n"
22407                "    Response<typename T::response_type> and\n"
22408                "    ContextExtension<typename T::extension_type> and\n"
22409                "    ::std::is_copy_constructable<T> and "
22410                "::std::is_move_constructable<T> and\n"
22411                "    requires (T c) {\n"
22412                "  { c.response; } -> Response;\n"
22413                "} and requires (T c) {\n"
22414                "  { c.request; } -> Request;\n"
22415                "}\n",
22416                Style);
22417 
22418   verifyFormat("template <typename T>\n"
22419                "concept Context = Traits<typename T::traits_type> or\n"
22420                "    Interface<typename T::interface_type> or\n"
22421                "    Request<typename T::request_type> or\n"
22422                "    Response<typename T::response_type> or\n"
22423                "    ContextExtension<typename T::extension_type> or\n"
22424                "    ::std::is_copy_constructable<T> or "
22425                "::std::is_move_constructable<T> or\n"
22426                "    requires (T c) {\n"
22427                "  { c.response; } -> Response;\n"
22428                "} or requires (T c) {\n"
22429                "  { c.request; } -> Request;\n"
22430                "}\n",
22431                Style);
22432 
22433   verifyFormat("template <typename T>\n"
22434                "concept Context = Traits<typename T::traits_type> &&\n"
22435                "    Interface<typename T::interface_type> &&\n"
22436                "    Request<typename T::request_type> &&\n"
22437                "    Response<typename T::response_type> &&\n"
22438                "    ContextExtension<typename T::extension_type> &&\n"
22439                "    ::std::is_copy_constructable<T> && "
22440                "::std::is_move_constructable<T> &&\n"
22441                "    requires (T c) {\n"
22442                "  { c.response; } -> Response;\n"
22443                "} && requires (T c) {\n"
22444                "  { c.request; } -> Request;\n"
22445                "}\n",
22446                Style);
22447 
22448   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22449                "Constraint2<T>;");
22450 
22451   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22452   Style.BraceWrapping.AfterFunction = true;
22453   Style.BraceWrapping.AfterClass = true;
22454   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22455   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22456   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22457                "{\n"
22458                "  return\n"
22459                "}\n",
22460                Style);
22461 
22462   verifyFormat("void Foo () requires std::copyable<T>\n"
22463                "{\n"
22464                "  return\n"
22465                "}\n",
22466                Style);
22467 
22468   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22469                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22470                "struct constant;",
22471                Style);
22472 
22473   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22474                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22475                "struct constant;",
22476                Style);
22477 
22478   verifyFormat("template <class T>\n"
22479                "class plane_with_very_very_very_long_name\n"
22480                "{\n"
22481                "  constexpr plane_with_very_very_very_long_name () requires "
22482                "std::copyable<T>\n"
22483                "      : plane_with_very_very_very_long_name (1)\n"
22484                "  {\n"
22485                "  }\n"
22486                "}\n",
22487                Style);
22488 
22489   verifyFormat("template <class T>\n"
22490                "class plane_with_long_name\n"
22491                "{\n"
22492                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22493                "      : plane_with_long_name (1)\n"
22494                "  {\n"
22495                "  }\n"
22496                "}\n",
22497                Style);
22498 
22499   Style.BreakBeforeConceptDeclarations = false;
22500   verifyFormat("template <typename T> concept Tree = true;", Style);
22501 
22502   Style.IndentRequires = false;
22503   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22504                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22505                "struct constant;",
22506                Style);
22507 }
22508 
22509 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22510   FormatStyle Style = getLLVMStyle();
22511   StringRef Source = "void Foo::slot() {\n"
22512                      "  unsigned char MyChar = 'x';\n"
22513                      "  emit signal(MyChar);\n"
22514                      "  Q_EMIT signal(MyChar);\n"
22515                      "}";
22516 
22517   EXPECT_EQ(Source, format(Source, Style));
22518 
22519   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22520   EXPECT_EQ("void Foo::slot() {\n"
22521             "  unsigned char MyChar = 'x';\n"
22522             "  emit          signal(MyChar);\n"
22523             "  Q_EMIT signal(MyChar);\n"
22524             "}",
22525             format(Source, Style));
22526 
22527   Style.StatementAttributeLikeMacros.push_back("emit");
22528   EXPECT_EQ(Source, format(Source, Style));
22529 
22530   Style.StatementAttributeLikeMacros = {};
22531   EXPECT_EQ("void Foo::slot() {\n"
22532             "  unsigned char MyChar = 'x';\n"
22533             "  emit          signal(MyChar);\n"
22534             "  Q_EMIT        signal(MyChar);\n"
22535             "}",
22536             format(Source, Style));
22537 }
22538 
22539 TEST_F(FormatTest, IndentAccessModifiers) {
22540   FormatStyle Style = getLLVMStyle();
22541   Style.IndentAccessModifiers = true;
22542   // Members are *two* levels below the record;
22543   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22544   verifyFormat("class C {\n"
22545                "    int i;\n"
22546                "};\n",
22547                Style);
22548   verifyFormat("union C {\n"
22549                "    int i;\n"
22550                "    unsigned u;\n"
22551                "};\n",
22552                Style);
22553   // Access modifiers should be indented one level below the record.
22554   verifyFormat("class C {\n"
22555                "  public:\n"
22556                "    int i;\n"
22557                "};\n",
22558                Style);
22559   verifyFormat("struct S {\n"
22560                "  private:\n"
22561                "    class C {\n"
22562                "        int j;\n"
22563                "\n"
22564                "      public:\n"
22565                "        C();\n"
22566                "    };\n"
22567                "\n"
22568                "  public:\n"
22569                "    int i;\n"
22570                "};\n",
22571                Style);
22572   // Enumerations are not records and should be unaffected.
22573   Style.AllowShortEnumsOnASingleLine = false;
22574   verifyFormat("enum class E {\n"
22575                "  A,\n"
22576                "  B\n"
22577                "};\n",
22578                Style);
22579   // Test with a different indentation width;
22580   // also proves that the result is Style.AccessModifierOffset agnostic.
22581   Style.IndentWidth = 3;
22582   verifyFormat("class C {\n"
22583                "   public:\n"
22584                "      int i;\n"
22585                "};\n",
22586                Style);
22587 }
22588 
22589 TEST_F(FormatTest, LimitlessStringsAndComments) {
22590   auto Style = getLLVMStyleWithColumns(0);
22591   constexpr StringRef Code =
22592       "/**\n"
22593       " * This is a multiline comment with quite some long lines, at least for "
22594       "the LLVM Style.\n"
22595       " * We will redo this with strings and line comments. Just to  check if "
22596       "everything is working.\n"
22597       " */\n"
22598       "bool foo() {\n"
22599       "  /* Single line multi line comment. */\n"
22600       "  const std::string String = \"This is a multiline string with quite "
22601       "some long lines, at least for the LLVM Style.\"\n"
22602       "                             \"We already did it with multi line "
22603       "comments, and we will do it with line comments. Just to check if "
22604       "everything is working.\";\n"
22605       "  // This is a line comment (block) with quite some long lines, at "
22606       "least for the LLVM Style.\n"
22607       "  // We already did this with multi line comments and strings. Just to "
22608       "check if everything is working.\n"
22609       "  const std::string SmallString = \"Hello World\";\n"
22610       "  // Small line comment\n"
22611       "  return String.size() > SmallString.size();\n"
22612       "}";
22613   EXPECT_EQ(Code, format(Code, Style));
22614 }
22615 
22616 TEST_F(FormatTest, FormatDecayCopy) {
22617   // error cases from unit tests
22618   verifyFormat("foo(auto())");
22619   verifyFormat("foo(auto{})");
22620   verifyFormat("foo(auto({}))");
22621   verifyFormat("foo(auto{{}})");
22622 
22623   verifyFormat("foo(auto(1))");
22624   verifyFormat("foo(auto{1})");
22625   verifyFormat("foo(new auto(1))");
22626   verifyFormat("foo(new auto{1})");
22627   verifyFormat("decltype(auto(1)) x;");
22628   verifyFormat("decltype(auto{1}) x;");
22629   verifyFormat("auto(x);");
22630   verifyFormat("auto{x};");
22631   verifyFormat("new auto{x};");
22632   verifyFormat("auto{x} = y;");
22633   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
22634                                 // the user's own fault
22635   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
22636                                          // clearly the user's own fault
22637   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
22638 }
22639 
22640 TEST_F(FormatTest, Cpp20ModulesSupport) {
22641   FormatStyle Style = getLLVMStyle();
22642   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
22643   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22644 
22645   verifyFormat("export import foo;", Style);
22646   verifyFormat("export import foo:bar;", Style);
22647   verifyFormat("export import foo.bar;", Style);
22648   verifyFormat("export import foo.bar:baz;", Style);
22649   verifyFormat("export import :bar;", Style);
22650   verifyFormat("export module foo:bar;", Style);
22651   verifyFormat("export module foo;", Style);
22652   verifyFormat("export module foo.bar;", Style);
22653   verifyFormat("export module foo.bar:baz;", Style);
22654   verifyFormat("export import <string_view>;", Style);
22655 
22656   verifyFormat("export type_name var;", Style);
22657   verifyFormat("template <class T> export using A = B<T>;", Style);
22658   verifyFormat("export using A = B;", Style);
22659   verifyFormat("export int func() {\n"
22660                "  foo();\n"
22661                "}",
22662                Style);
22663   verifyFormat("export struct {\n"
22664                "  int foo;\n"
22665                "};",
22666                Style);
22667   verifyFormat("export {\n"
22668                "  int foo;\n"
22669                "};",
22670                Style);
22671   verifyFormat("export export char const *hello() { return \"hello\"; }");
22672 
22673   verifyFormat("import bar;", Style);
22674   verifyFormat("import foo.bar;", Style);
22675   verifyFormat("import foo:bar;", Style);
22676   verifyFormat("import :bar;", Style);
22677   verifyFormat("import <ctime>;", Style);
22678   verifyFormat("import \"header\";", Style);
22679 
22680   verifyFormat("module foo;", Style);
22681   verifyFormat("module foo:bar;", Style);
22682   verifyFormat("module foo.bar;", Style);
22683   verifyFormat("module;", Style);
22684 
22685   verifyFormat("export namespace hi {\n"
22686                "const char *sayhi();\n"
22687                "}",
22688                Style);
22689 
22690   verifyFormat("module :private;", Style);
22691   verifyFormat("import <foo/bar.h>;", Style);
22692   verifyFormat("import foo...bar;", Style);
22693   verifyFormat("import ..........;", Style);
22694   verifyFormat("module foo:private;", Style);
22695   verifyFormat("import a", Style);
22696   verifyFormat("module a", Style);
22697   verifyFormat("export import a", Style);
22698   verifyFormat("export module a", Style);
22699 
22700   verifyFormat("import", Style);
22701   verifyFormat("module", Style);
22702   verifyFormat("export", Style);
22703 }
22704 
22705 } // namespace
22706 } // namespace format
22707 } // namespace clang
22708